最近手撸了一下K8S对自己带入了一个全新的东西里面一样,废话不多说,结合K8文档应该能看懂的
首先来个项目数据拼装,没有使用yaml,因为那个有点
/**
* 创建项目数据组装
* @param string $projectNameKey 项目唯一key
* @param string $colonyKey 集群唯一key
* @param string $projectRelName 项目昵称
* @param string $desc 项目描叙
* @return array
* @author leishaofa
* @date 2020/10/27 16:11
*/
public static function createProjectData($projectNameKey, $colonyKey, $projectRelName, $desc = "详情描叙")
{
return $data = [
'apiVersion' => "auth.alauda.io/v1",
"kind" => "Project",
"metadata" => [
"annotations" => [
"cpaas.io/display-name" => $projectRelName,
"cpaas.io/description" => $desc,
"cpaas.io/unite-quota-fed-clusters" => ""
],
"labels" => [
"cpaas.io/project.level" => "1",
"cpaas.io/project.parent" => ""
],
"name" => $projectNameKey,
"namespace" => $projectNameKey
],
"spec" => [
"clusters" => [
[
"name" => $colonyKey,//集群名称
"quota" => (object)[]
/*[ "requests.cpu" => "8",//集群设置cpu数量
"limits.cpu" => "8",//集群剩余cpu数量
"requests.memory" => "16Gi",//集群设置cpu数量
"limits.memory" => "16Gi"//集群剩余内存数量]
*/
]
] //集群
]
];
}
例如调用
$data = DockerDataServer::createProjectData("ide", "ide01", "测试项目服");
$resultdata =sendUrl($url, json_encode($data, 320), "post");
顺带贴出sendUrl方法:
/**
* curl请求方法
* @param $url
* @param $data
* @param string $method_type
* @param array $header
* @return array|string
* @author leishaofa
* @date 2020/10/27 16:11
*/
public static function sendUrl($url, $data, $method_type = 'get', $header = [])
{
$ch = curl_init();
$header[] = 'Accept:application/json;';
$header[] = 'charset=utf-8';
$token = getenv('DOCKER_OPS_TOKEN');
if (!$token || empty($token)) {
return "options token is not defind";
}
$header[] = 'Authorization: Bearer ' . $token;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
switch ($method_type) {
case 'get':
if (!empty($data)) {
$data = http_build_query($data);
$url = $url . '?' . $data;
}
$header[] = 'Content-Type:application/json';
break;
case 'post':
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
} else {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, false);
}
$header[] = 'Content-Type:application/json';
break;
case 'delete':
if (!empty($data)) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
} else {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
}
$header[] = 'Content-Type:application/json';
break;
case 'patch':
if (!empty($data)) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
} else {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_POSTFIELDS, false);
}
$header[] = 'Content-Type:application/merge-patch+json';
break;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
$res = curl_exec($ch);
// $error = curl_error($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$resdata = json_decode($res, true);
$http_code = is_array($resdata) && isset($resdata['code']) ? $resdata['code'] : $http_code;
return ['code' => $http_code, 'httpdata' => $res];
}
}