forked from travisghansen/kubernetes-client-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.php
114 lines (95 loc) · 2.8 KB
/
sample.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
// be sure to run composer install first
require_once 'vendor/autoload.php';
declare(ticks = 1);
pcntl_signal(SIGINT, function () {
exit(0);
});
$config = KubernetesClient\Config::BuildConfigFromFile();
$client = new KubernetesClient\Client($config);
$configMapName = 'kubernetes-php-client-test';
$configMapNamespace = 'kube-system';
//POST
$data = [
'kind' => 'ConfigMap',
'metadata' => [
'name' => $configMapName
],
'data' => [
'foo' => 'bar',
],
];
$response = $client->request("/api/v1/namespaces/${configMapNamespace}/configmaps", 'POST', [], $data);
var_dump($response);
//PATCH
$data = [
'kind' => 'ConfigMap',
'metadata' => [
'name' => $configMapName
],
'data' => [
'bar' => 'baz',
],
];
$response = $client->request("/api/v1/namespaces/${configMapNamespace}/configmaps/${configMapName}", 'PATCH', [], $data);
var_dump($response);
//GET
$response = $client->request("/api/v1/namespaces/${configMapNamespace}/configmaps/${configMapName}");
var_dump($response);
//DELETE
$response = $client->request("/api/v1/namespaces/${configMapNamespace}/configmaps/${configMapName}", 'DELETE');
var_dump($response);
//LIST (retrieve large responses)
$params = [
'limit' => 1
];
$list = $client->createList('/api/v1/nodes', $params);
// get all
$items = $list->get();
var_dump($items);
// get 1 page
$pages = 1;
$items = $list->get($pages);
var_dump($items);
// iterate
foreach ($list->stream() as $item) {
var_dump($item);
}
// shared state for closures
$state = [];
$response = $client->request('/api/v1/nodes');
$state['nodes']['list'] = $response;
$callback = function ($event, $watch) use (&$state) {
echo date("c") . ': ' . $event['object']['kind'] . ' ' . $event['object']['metadata']['name'] . ' ' . $event['type'] . ' - ' . $event['object']['metadata']['resourceVersion'] . "\n";
};
$params = [
'watch' => '1',
//'timeoutSeconds' => 10,//if set, the loop will break after the server has severed the connection
'resourceVersion' => $state['nodes']['list']['metadata']['resourceVersion'],
];
$watch = $client->createWatch('/api/v1/nodes?', $params, $callback);
//$watch->setStreamReadLength(55);
// blocking (unless timeoutSeconds has been supplied)
//$watch->start();
// non blocking
$i = 0;
while (true) {
$watch->start(1);
usleep(100 * 1000);
$i++;
if ($i > 100) {
echo date("c").": breaking while loop\n";
break;
}
}
// generator style, blocking
$i = 0;
foreach ($watch->stream() as $event) {
echo date("c") . ': ' . $event['object']['kind'] . ' ' . $event['object']['metadata']['name'] . ' ' . $event['type'] . ' - ' . $event['object']['metadata']['resourceVersion'] . "\n";
//$watch->stop();
$i++;
if ($i > 10) {
echo date("c").": breaking foreach loop\n";
break;
}
}