ThinkPHP3.2 调用kubernetes api接口
来源:原创
时间:2020-09-19
作者:脚本小站
分类:PHP
借助github已近写好的基础代码结合ThinkPHP3.2调用kubernetes api接口来实现一些功能。
github地址:
https://github.com/travisghansen/kubernetes-client-php
下载下来后将代码放入ThinkPHP/Library/Vendor目录,在kubernetes-client-php/src 的同级目录创建文件autoload.php文件。
<?php function classLoader($class) { $path = str_replace('\\', DIRECTORY_SEPARATOR, $class); $file = __DIR__ . DIRECTORY_SEPARATOR .'src'. DIRECTORY_SEPARATOR . $path . '.php'; if (file_exists($file)) { require_once $file; } } spl_autoload_register('classLoader');
在框架中使用,代码如下:
<?php namespace Home\Controller; use Home\Common\ArticleCommonController; use KubernetesClient; # 引入名称空间 class IndexController extends ArticleCommonController{ /* * index * @param */ public function index() { Vendor('kubernetes-client-php.autoload'); $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', ], ]; echo "<pre>"; $response = $client->request("/api/v1/namespaces/${configMapNamespace}/configmaps", 'POST', [], $data); var_dump($response); //GET $response = $client->request("/api/v1/namespaces/${configMapNamespace}/configmaps/${configMapName}"); var_dump($response); } }