Skip to content

Commit b540030

Browse files
committed
wip
1 parent 58f2219 commit b540030

File tree

17 files changed

+492
-169
lines changed

17 files changed

+492
-169
lines changed

README.md

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
Package Name Here
2-
===================================
1+
Laravel PHP K8s
2+
===============
33

4-
![CI](https://github.com/renoki-co/:package_name/workflows/CI/badge.svg?branch=master)
5-
[![codecov](https://codecov.io/gh/renoki-co/:package_name/branch/master/graph/badge.svg)](https://codecov.io/gh/renoki-co/:package_name/branch/master)
4+
![CI](https://github.com/renoki-co/laravel-php-k8s/workflows/CI/badge.svg?branch=master)
5+
[![codecov](https://codecov.io/gh/renoki-co/laravel-php-k8s/branch/master/graph/badge.svg)](https://codecov.io/gh/renoki-co/laravel-php-k8s/branch/master)
66
[![StyleCI](https://github.styleci.io/repos/:styleci_code/shield?branch=master)](https://github.styleci.io/repos/:styleci_code)
7-
[![Latest Stable Version](https://poser.pugx.org/renoki-co/:package_name/v/stable)](https://packagist.org/packages/renoki-co/:package_name)
8-
[![Total Downloads](https://poser.pugx.org/renoki-co/:package_name/downloads)](https://packagist.org/packages/renoki-co/:package_name)
9-
[![Monthly Downloads](https://poser.pugx.org/renoki-co/:package_name/d/monthly)](https://packagist.org/packages/renoki-co/:package_name)
10-
[![License](https://poser.pugx.org/renoki-co/:package_name/license)](https://packagist.org/packages/renoki-co/:package_name)
7+
[![Latest Stable Version](https://poser.pugx.org/renoki-co/laravel-php-k8s/v/stable)](https://packagist.org/packages/renoki-co/laravel-php-k8s)
8+
[![Total Downloads](https://poser.pugx.org/renoki-co/laravel-php-k8s/downloads)](https://packagist.org/packages/renoki-co/laravel-php-k8s)
9+
[![Monthly Downloads](https://poser.pugx.org/renoki-co/laravel-php-k8s/d/monthly)](https://packagist.org/packages/renoki-co/laravel-php-k8s)
10+
[![License](https://poser.pugx.org/renoki-co/laravel-php-k8s/license)](https://packagist.org/packages/renoki-co/laravel-php-k8s)
1111

12-
**Note:** Replace ```:package_name``` ```:package_description``` ```:package_namespace``` ```:package_service_provider``` ```:author_name``` ```:author_link``` ```:styleci_code``` with their correct values in [README.md](README.md), [CONTRIBUTING.md](CONTRIBUTING.md), [LICENSE](LICENSE) and [composer.json](composer.json) files, then delete this line.
13-
14-
This is where your description should go. Try and limit it to a paragraph or two. Consider adding a small example.
12+
Just a simple port of [renoki-co/php-k8s](https://github.com/renoki-co/php-k8s) for easier access in Laravel.
1513

1614
## 🤝 Supporting
1715

@@ -24,19 +22,13 @@ If you are using your application in your day-to-day job, on presentation demos,
2422
You can install the package via composer:
2523

2624
```bash
27-
composer require renoki-co/:package_name
25+
composer require renoki-co/laravel-php-k8s
2826
```
2927

3028
Publish the config:
3129

3230
```bash
33-
$ php artisan vendor:publish --provider="RenokiCo\:package_namespace\:package_service_provider" --tag="config"
34-
```
35-
36-
Publish the migrations:
37-
38-
```bash
39-
$ php artisan vendor:publish --provider="RenokiCo\:package_namespace\:package_service_provider" --tag="migrations"
31+
$ php artisan vendor:publish --provider="RenokiCo\LaravelK8s\LaravelK8sServiceProvider" --tag="config"
4032
```
4133

4234
## 🙌 Usage

composer.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"name": "renoki-co/:package_name",
3-
"description": ":package_description",
4-
"keywords": ["laravel", "php"],
2+
"name": "renoki-co/laravel-php-k8s",
3+
"description": "Just a simple port of renoki-co/php-k8s for easier access in Laravel.",
4+
"keywords": ["laravel", "php", "laravel", "kubernetes", "k8s", "k3s", "kube"],
55
"license": "Apache-2.0",
6-
"homepage": "https://github.com/renoki-co/:package_name",
6+
"homepage": "https://github.com/renoki-co/laravel-php-k8s",
77
"authors": [
88
{
99
"name": "Alex Renoki",
@@ -12,16 +12,17 @@
1212
}
1313
],
1414
"require": {
15-
"php": "^7.3"
15+
"php": "^7.2",
16+
"renoki-co/php-k8s": "^1.0"
1617
},
1718
"autoload": {
1819
"psr-4": {
19-
"RenokiCo\\:package_namespace\\": "src/"
20+
"RenokiCo\\LaravelK8s\\": "src/"
2021
}
2122
},
2223
"autoload-dev": {
2324
"psr-4": {
24-
"RenokiCo\\:package_namespace\\Test\\": "tests"
25+
"RenokiCo\\LaravelK8s\\Test\\": "tests"
2526
}
2627
},
2728
"scripts": {
@@ -39,7 +40,7 @@
3940
"extra": {
4041
"laravel": {
4142
"providers": [
42-
"RenokiCo\\:package_namespace\\:package_service_provider"
43+
"RenokiCo\\LaravelK8s\\LaravelK8sServiceProvider"
4344
]
4445
}
4546
}

config/.gitkeep

Whitespace-only changes.

config/k8s.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Default Connection
8+
|--------------------------------------------------------------------------
9+
|
10+
| The default connection that PHP K8s will connect to.
11+
|
12+
*/
13+
14+
'default' => env('KUBE_CONNECTION', null),
15+
16+
'connections' => [
17+
18+
/*
19+
|--------------------------------------------------------------------------
20+
| Kubeconfig Driver
21+
|--------------------------------------------------------------------------
22+
|
23+
| Kubeconfig Driver works by reading from the KubeConfig file
24+
| and then selecting a specific context to work with.
25+
| Please see the documentation on contexts:
26+
| https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/
27+
|
28+
*/
29+
30+
'kubeconfig' => [
31+
'driver' => 'kubeconfig',
32+
'path' => env('KUBECONFIG_PATH', '/.kube/config'),
33+
'context' => env('KUBECONFIG_CONTEXT', 'minikube'),
34+
],
35+
36+
/*
37+
|--------------------------------------------------------------------------
38+
| HTTP Driver
39+
|--------------------------------------------------------------------------
40+
|
41+
| Proxy Driver works by directly making a request to the API URL using
42+
| the HTTP authentication through username and password.
43+
|
44+
*/
45+
46+
'http' => [
47+
'driver' => 'http',
48+
'host' => env('KUBE_HOST', '127.0.0.1'),
49+
'port' => env('KUBE_PORT', 8080),
50+
'ssl' => [
51+
'certificate' => env('KUBE_CERTIFICATE_PATH', null),
52+
'key' => env('KUBE_KEY_PATH', null),
53+
'ca' => env('KUBE_CA_PATH', null),
54+
'verify' => env('VERIFY_SSL', true),
55+
],
56+
'auth' => [
57+
'username' => env('KUBE_HTTP_USER', null),
58+
'password' => env('KUBE_HTTP_PASSWORD', null),
59+
],
60+
],
61+
62+
/*
63+
|--------------------------------------------------------------------------
64+
| Bearer Token Driver
65+
|--------------------------------------------------------------------------
66+
|
67+
| Proxy Driver works by directly making a request to the API URL using
68+
| the bearer token authentication.
69+
|
70+
*/
71+
72+
'token' => [
73+
'driver' => 'token',
74+
'host' => env('KUBE_HOST', '127.0.0.1'),
75+
'port' => env('KUBE_PORT', 8080),
76+
'ssl' => [
77+
'certificate' => env('KUBE_CERTIFICATE_PATH', null),
78+
'key' => env('KUBE_KEY_PATH', null),
79+
'ca' => env('KUBE_CA_PATH', null),
80+
'verify' => env('VERIFY_SSL', true),
81+
],
82+
'token' => env('KUBE_BEARER_TOKEN', null),
83+
],
84+
85+
],
86+
87+
];

database/migrations/.gitkeep

Whitespace-only changes.

src/.gitkeep

Whitespace-only changes.

src/KubernetesCluster.php

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php
2+
3+
namespace RenokiCo\LaravelK8s;
4+
5+
use RenokiCo\PhpK8s\KubernetesCluster as PhpK8sCluster;
6+
7+
class KubernetesCluster
8+
{
9+
/**
10+
* The Kubernetes cluster instance.
11+
*
12+
* @var \RenokiCo\PhpK8s\KubernetesCluster
13+
*/
14+
protected $cluster;
15+
16+
/**
17+
* Create a new Kubernetes Cluster.
18+
*
19+
* @param array $config
20+
* @return void
21+
*/
22+
public function __construct(array $config)
23+
{
24+
$this->loadFromConfig($config);
25+
}
26+
27+
/**
28+
* Switch the connection.
29+
*
30+
* @param string $connection
31+
* @return $this
32+
*/
33+
public function connection(string $connection)
34+
{
35+
$this->loadFromConfig(
36+
config('k8s.connections')[$connection] ?? config('k8s.default')
37+
);
38+
39+
return $this;
40+
}
41+
42+
/**
43+
* Load the Cluster instance from the given config.
44+
*
45+
* @param array $config
46+
* @return void
47+
*/
48+
protected function loadFromConfig(array $config)
49+
{
50+
$this->cluster = new PhpK8sCluster('http://127.0.0.1', 8080);
51+
52+
switch ($config['driver'] ?? null) {
53+
case 'kubeconfig': $this->configureWithKubeConfigFile($config); break;
54+
case 'http': $this->configureWithHttpAuth($config); break;
55+
case 'token': $this->configureWithToken($config); break;
56+
default: break;
57+
}
58+
}
59+
60+
/**
61+
* Configure the cluster using a Kube Config file.
62+
*
63+
* @param array $config
64+
* @return void
65+
*/
66+
protected function configureWithKubeConfigFile(array $config)
67+
{
68+
$this->cluster->fromKubeConfigYamlFile(
69+
$config['path'], $config['context']
70+
);
71+
}
72+
73+
/**
74+
* Configure the cluster with HTTP authentication.
75+
*
76+
* @param array $config
77+
* @return void
78+
*/
79+
protected function configureWithHttpAuth(array $config)
80+
{
81+
$this->cluster = new PhpK8sCluster(
82+
$config['host'], $config['port']
83+
);
84+
85+
if ($config['ssl']['verify'] ?? true) {
86+
$this->cluster->withCertificate(
87+
$config['ssl']['certificate'] ?? null
88+
);
89+
90+
$this->cluster->withPrivateKey(
91+
$config['ssl']['key'] ?? null
92+
);
93+
94+
$this->cluster->withCaCertificate(
95+
$config['ssl']['ca'] ?? null
96+
);
97+
} else {
98+
$this->cluster->withoutSslChecks();
99+
}
100+
101+
$this->cluster->httpAuthentication(
102+
$config['auth']['username'] ?? null,
103+
$config['auth']['password'] ?? null
104+
);
105+
}
106+
107+
/**
108+
* Configure the cluster with a Bearer Token.
109+
*
110+
* @param array $config
111+
* @return void
112+
*/
113+
protected function configureWithToken(array $config)
114+
{
115+
$this->cluster = new PhpK8sCluster(
116+
$config['host'], $config['port']
117+
);
118+
119+
if ($config['ssl']['verify'] ?? true) {
120+
$this->cluster->withCertificate(
121+
$config['ssl']['certificate'] ?? null
122+
);
123+
124+
$this->cluster->withPrivateKey(
125+
$config['ssl']['key'] ?? null
126+
);
127+
128+
$this->cluster->withCaCertificate(
129+
$config['ssl']['ca'] ?? null
130+
);
131+
} else {
132+
$this->cluster->withoutSslChecks();
133+
}
134+
135+
$this->cluster->withToken($config['token']);
136+
}
137+
138+
/**
139+
* Get the initialized cluster.
140+
*
141+
* @return \RenokiCo\PhpK8s\KubernetesCluster
142+
*/
143+
public function getCluster()
144+
{
145+
return $this->cluster;
146+
}
147+
148+
/**
149+
* Proxy the calls onto the cluster.
150+
*
151+
* @param string $method
152+
* @param array $parameters
153+
* @return mixed
154+
*/
155+
public function __call($method, $parameters)
156+
{
157+
return $this->getCluster()->{$method}(...$parameters);
158+
}
159+
160+
/**
161+
* Proxy the static calls onto the cluster.
162+
*
163+
* @param string $method
164+
* @param array $parameters
165+
* @return mixed
166+
*/
167+
public static function __callStatic($method, $parameters)
168+
{
169+
return PhpK8sCluster::{$method}(...$parameters);
170+
}
171+
}

src/LaravelK8sFacade.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace RenokiCo\LaravelK8s;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class LaravelK8sFacade extends Facade
8+
{
9+
/**
10+
* Get the registered name of the component.
11+
*
12+
* @return string
13+
*/
14+
protected static function getFacadeAccessor()
15+
{
16+
return 'laravel.k8s';
17+
}
18+
}

0 commit comments

Comments
 (0)