docker之kubernetes创建代码服务之创建项目(projects)

最近手撸了一下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];
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值