Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
98.21% |
55 / 56 |
|
94.44% |
17 / 18 |
CRAP | |
0.00% |
0 / 1 |
KubernetesService | |
98.21% |
55 / 56 |
|
94.44% |
17 / 18 |
32 | |
0.00% |
0 / 1 |
getCluster | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getClusters | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createCluster | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
4.02 | |||
updateCluster | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
deleteCluster | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
deleteClusterAndRelatedResources | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getResources | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
6 | |||
getAvailableClusterUpgrades | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
startClusterUpgrade | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
createNodePool | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getNodePools | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getNodePool | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
updateNodePool | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
deleteNodePool | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
deleteNodePoolInstance | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
recycleNodePoolInstance | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getClusterKubeconfig | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getAvailableVersions | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Vultr\VultrPhp\Services\Kubernetes; |
6 | |
7 | use Vultr\VultrPhp\Services\Kubernetes\Resources\BlockResource; |
8 | use Vultr\VultrPhp\Services\Kubernetes\Resources\LoadBalancerResource; |
9 | use Vultr\VultrPhp\Services\VultrService; |
10 | use Vultr\VultrPhp\Util\ListOptions; |
11 | use Vultr\VultrPhp\Util\VultrUtil; |
12 | use Vultr\VultrPhp\VultrClientException; |
13 | |
14 | /** |
15 | * Kubernetes service handler, for all kubernetes endpoints. |
16 | * |
17 | * @see https://www.vultr.com/api/#tag/kubernetes |
18 | */ |
19 | class KubernetesService extends VultrService |
20 | { |
21 | /** |
22 | * Retrieve a kubernetes cluster on the account. |
23 | * |
24 | * @see https://www.vultr.com/api/#operation/get-kubernetes-clusters |
25 | * @param $id - string - VKE UUID, Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
26 | * @throws KubernetesException |
27 | * @return VKECluster |
28 | */ |
29 | public function getCluster(string $id) : VKECluster |
30 | { |
31 | return $this->getObject('kubernetes/clusters/'.$id, new VKECluster()); |
32 | } |
33 | |
34 | /** |
35 | * Get a list of kubernetes clusters on the account. |
36 | * |
37 | * @see https://www.vultr.com/api/#operation/list-kubernetes-clusters |
38 | * @param $options - ListOptions|null - Interact via reference. |
39 | * @throws KubernetesException |
40 | * @return VKECluster[] |
41 | */ |
42 | public function getClusters(?ListOptions &$options = null) |
43 | { |
44 | return $this->getListObjects('kubernetes/clusters', new VKECluster(), $options); |
45 | } |
46 | |
47 | /** |
48 | * Create a kubernetes cluster. |
49 | * |
50 | * @see https://www.vultr.com/api/#operation/create-kubernetes-cluster |
51 | * @param $region - string |
52 | * @param $version - string |
53 | * @param $node_pools - NodePool[] |
54 | * @throws KubernetesException |
55 | * @return VKECluster |
56 | */ |
57 | public function createCluster(string $region, string $version, array $node_pools, ?string $label = null) : VKECluster |
58 | { |
59 | $pools = []; |
60 | foreach ($node_pools as $pool) |
61 | { |
62 | if (!($pool instanceof NodePool)) |
63 | { |
64 | throw new KubernetesException('$node_pools must be an array of NodePool objects.'); |
65 | } |
66 | |
67 | $pools[] = $pool->getInitializedProps(); |
68 | } |
69 | |
70 | $params = [ |
71 | 'region' => $region, |
72 | 'version' => $version, |
73 | 'node_pools' => $pools, |
74 | ]; |
75 | |
76 | if ($label !== null) |
77 | { |
78 | $params['label'] = $label; |
79 | } |
80 | |
81 | return $this->createObject('kubernetes/clusters', new VKECluster(), $params); |
82 | } |
83 | |
84 | /** |
85 | * Update a kubernetes cluster. |
86 | * |
87 | * @param $id - string - VKE UUID, Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
88 | * @param $label - string |
89 | * @throws KubernetesException |
90 | * @return void |
91 | */ |
92 | public function updateCluster(string $id, string $label) : void |
93 | { |
94 | $this->patchObject('kubernetes/clusters/'.$id, new VKECluster(), ['label' => $label]); |
95 | } |
96 | |
97 | /** |
98 | * Delete a kubernetes cluster. |
99 | * |
100 | * @see https://www.vultr.com/api/#operation/delete-kubernetes-cluster |
101 | * @param $id - string - VKE UUID, Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
102 | * @throws KubernetesException |
103 | * @return void |
104 | */ |
105 | public function deleteCluster(string $id) : void |
106 | { |
107 | $this->deleteObject('kubernetes/clusters/'.$id, new VKECluster()); |
108 | } |
109 | |
110 | /** |
111 | * Delete a kubernetes cluster and its resources. |
112 | * |
113 | * This means it will delete blockstorage, load balancers, and any other related resources. |
114 | * |
115 | * @see https://www.vultr.com/api/#operation/delete-kubernetes-cluster-vke-id-delete-with-linked-resources |
116 | * @param $id - string - VKE UUID, Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
117 | * @throws KubernetesException |
118 | * @return void |
119 | */ |
120 | public function deleteClusterAndRelatedResources(string $id) |
121 | { |
122 | $this->deleteObject('kubernetes/clusters/'.$id.'/delete-with-linked-resources', new VKECluster()); |
123 | } |
124 | |
125 | /** |
126 | * Get resources belonging to a kubernetes cluster. |
127 | * |
128 | * @see https://www.vultr.com/api/#operation/get-kubernetes-resources |
129 | * @param $id - string - VKE UUID, Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
130 | * @throws KubernetesException |
131 | * @throws VultrException |
132 | * @return array |
133 | */ |
134 | public function getResources(string $id) : array |
135 | { |
136 | try |
137 | { |
138 | $response = $this->getClientHandler()->get('kubernetes/clusters/'.$id.'/resources'); |
139 | } |
140 | catch (VultrClientException $e) |
141 | { |
142 | throw new KubernetesException('Failed to get resources:'.$e->getMessage(), $e->getHTTPCode(), $e); |
143 | } |
144 | |
145 | $objects = []; |
146 | $model = new NodeResource(); |
147 | $stdclass = VultrUtil::decodeJSON((string)$response->getBody()); |
148 | $response_name = $model->getResponseListName(); |
149 | foreach ($stdclass->$response_name as $type => $resources) |
150 | { |
151 | $object = $model; |
152 | if ($type === 'block_storage') $object = new BlockResource(); |
153 | else if ($type === 'load_balancer') $object = new LoadBalancerResource(); |
154 | |
155 | foreach ($resources as $resource) |
156 | { |
157 | $objects[] = VultrUtil::mapObject($resource, $object); |
158 | } |
159 | } |
160 | |
161 | return $objects; |
162 | } |
163 | |
164 | /** |
165 | * Get available cluster upgrades. |
166 | * |
167 | * @see https://www.vultr.com/api/#operation/get-kubernetes-available-upgrades |
168 | * @param $id - string - VKE UUID, Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
169 | * @throws KubernetesException |
170 | * @throws VultrException |
171 | * @return array |
172 | */ |
173 | public function getAvailableClusterUpgrades(string $id) : array |
174 | { |
175 | try |
176 | { |
177 | $response = $this->getClientHandler()->get('kubernetes/clusters/'.$id.'/available-upgrades'); |
178 | } |
179 | catch (VultrClientException $e) |
180 | { |
181 | throw new KubernetesException('Failed to get cluster upgrades: '.$e->getMessage(), $e->getHTTPCode(), $e); |
182 | } |
183 | |
184 | return VultrUtil::decodeJSON((string)$response->getBody(), true)['available_upgrades']; |
185 | } |
186 | |
187 | /** |
188 | * Start a kubernetes cluster version upgrade. |
189 | * |
190 | * @see https://www.vultr.com/api/#operation/start-kubernetes-cluster-upgrade |
191 | * @param $id - string - VKE UUID, Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
192 | * @param $upgrade_version - string - Example: v1.22.8+3 |
193 | * @throws KubernetesException |
194 | * @return void |
195 | */ |
196 | public function startClusterUpgrade(string $id, string $upgrade_version) : void |
197 | { |
198 | try |
199 | { |
200 | $this->getClientHandler()->post('kubernetes/clusters/'.$id.'/upgrades', ['upgrade_version' => $upgrade_version]); |
201 | } |
202 | catch (VultrClientException $e) |
203 | { |
204 | throw new KubernetesException('Failed to start the upgrade for the cluster: '.$e->getMessage(), $e->getHTTPCode(), $e); |
205 | } |
206 | } |
207 | |
208 | /** |
209 | * Create a node pool in the kubernetes cluster. |
210 | * |
211 | * @see https://www.vultr.com/api/#operation/create-nodepools |
212 | * @param $id - string - VKE UUID, Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
213 | * @param $pool - NodePool |
214 | * @throws KubernetesException |
215 | * @return NodePool |
216 | */ |
217 | public function createNodePool(string $id, NodePool $pool) : NodePool |
218 | { |
219 | return $this->createObject('kubernetes/clusters/'.$id.'/node-pools', new NodePool(), $pool->getInitializedProps()); |
220 | } |
221 | |
222 | /** |
223 | * Get available node pools in the kubernetes cluster. |
224 | * |
225 | * @see https://www.vultr.com/api/#operation/get-nodepools |
226 | * @param $id - string - VKE UUID, Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
227 | * @throws KubernetesException |
228 | * @return NodePool[] |
229 | */ |
230 | public function getNodePools(string $id, ?ListOptions &$options = null) : array |
231 | { |
232 | return $this->getListObjects('kubernetes/clusters/'.$id.'/node-pools', new NodePool(), $options); |
233 | } |
234 | |
235 | /** |
236 | * Get a specific node pool in the kubernetes cluster. |
237 | * |
238 | * @see https://www.vultr.com/api/#operation/get-nodepool |
239 | * @param $id - string - VKE UUID, Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
240 | * @param $nodepool_id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
241 | * @throws KubernetesException |
242 | * @return NodePool |
243 | */ |
244 | public function getNodePool(string $id, string $nodepool_id) : NodePool |
245 | { |
246 | return $this->getObject('kubernetes/clusters/'.$id.'/node-pools/'.$nodepool_id, new NodePool()); |
247 | } |
248 | |
249 | /** |
250 | * Update a node pool in the kubernetes cluster with attributes from an initialized object. |
251 | * |
252 | * @see https://www.vultr.com/api/#operation/update-nodepool |
253 | * @param $id - string - VKE UUID, Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
254 | * @param $nodepool - NodePool |
255 | * @throws KubernetesException |
256 | * @throws VultrException |
257 | * @return NodePool |
258 | */ |
259 | public function updateNodePool(string $id, NodePool $nodepool) : NodePool |
260 | { |
261 | try |
262 | { |
263 | $response = $this->getClientHandler()->patch('kubernetes/clusters/'.$id.'/node-pools/'.$nodepool->getId(), $nodepool->getInitializedProps()); |
264 | } |
265 | catch (VultrClientException $e) |
266 | { |
267 | throw new KubernetesException('Failed to update node pool: '.$e->getMessage(), $e->getHTTPCode(), $e); |
268 | } |
269 | |
270 | $model = new NodePool(); |
271 | return VultrUtil::convertJSONToObject((string)$response->getBody(), $model, $model->getResponseName()); |
272 | } |
273 | |
274 | /** |
275 | * Delete a node pool from a kubernetes cluster. |
276 | * |
277 | * @see https://www.vultr.com/api/#operation/delete-nodepool |
278 | * @param $id - string - VKE UUID, Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
279 | * @param $nodepool_id - string |
280 | * @throws KubernetesException |
281 | * @return void |
282 | */ |
283 | public function deleteNodePool(string $id, string $nodepool_id) : void |
284 | { |
285 | $this->deleteObject('kubernetes/clusters/'.$id.'/node-pools/'.$nodepool_id, new NodePool()); |
286 | } |
287 | |
288 | /** |
289 | * Delete an instance from a node pool. |
290 | * |
291 | * @see https://www.vultr.com/api/#operation/delete-nodepool-instance |
292 | * @param $id - string - VKE UUID, Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
293 | * @param $nodepool_id - string |
294 | * @param $node_id - string |
295 | * @throws KubernetesException |
296 | * @return void |
297 | */ |
298 | public function deleteNodePoolInstance(string $id, string $nodepool_id, string $node_id) : void |
299 | { |
300 | $this->deleteObject('kubernetes/clusters/'.$id.'/node-pools/'.$nodepool_id.'/nodes/'.$node_id, new Node()); |
301 | } |
302 | |
303 | /** |
304 | * Reinstall a specific instance from a node pool. |
305 | * |
306 | * @see https://www.vultr.com/api/#operation/recycle-nodepool-instance |
307 | * @param $id - string - VKE UUID, Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
308 | * @param $nodepool_id - string |
309 | * @param $node_id - string |
310 | * @throws KubernetesException |
311 | * @return void |
312 | */ |
313 | public function recycleNodePoolInstance(string $id, string $nodepool_id, string $node_id) : void |
314 | { |
315 | try |
316 | { |
317 | $this->getClientHandler()->post('kubernetes/clusters/'.$id.'/node-pools/'.$nodepool_id.'/nodes/'.$node_id); |
318 | } |
319 | catch (VultrClientException $e) |
320 | { |
321 | throw new KubernetesException('Failed to recycle node pool instance: '.$e->getMessage(), $e->getHTTPCode(), $e); |
322 | } |
323 | } |
324 | |
325 | /** |
326 | * Get the kubeconfig for the kubernetes cluster. |
327 | * |
328 | * @see https://www.vultr.com/api/#operation/get-kubernetes-clusters-config |
329 | * @param $id - string - VKE UUID, Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
330 | * @throws KubernetesException |
331 | * @throws VultrException |
332 | * @return string |
333 | */ |
334 | public function getClusterKubeconfig(string $id) : string |
335 | { |
336 | try |
337 | { |
338 | $response = $this->getClientHandler()->get('kubernetes/clusters/'.$id.'/config'); |
339 | } |
340 | catch (VultrClientException $e) |
341 | { |
342 | throw new KubernetesException('Failed to get kubeconfig: '.$e->getMessage(), $e->getHTTPCode(), $e); |
343 | } |
344 | |
345 | return base64_decode(VultrUtil::decodeJSON((string)$response->getBody(), true)['kube_config']); |
346 | } |
347 | |
348 | /** |
349 | * Get available kubernetes versions that vultr supports. |
350 | * |
351 | * @see https://www.vultr.com/api/#operation/get-kubernetes-versions |
352 | * @throws KubernetesException |
353 | * @throws VultrException |
354 | * @return array |
355 | */ |
356 | public function getAvailableVersions() : array |
357 | { |
358 | try |
359 | { |
360 | $response = $this->getClientHandler()->get('kubernetes/versions'); |
361 | } |
362 | catch (VultrClientException $e) |
363 | { |
364 | throw new KubernetesException('Failed to get kubernetes versions: '.$e->getMessage(), $e->getHTTPCode(), $e); |
365 | } |
366 | |
367 | return VultrUtil::decodeJSON((string)$response->getBody(), true)['versions']; |
368 | } |
369 | } |