華為云計(jì)算 云知識(shí) 獲取集群證書CreateKubernetesClusterCert
獲取集群證書CreateKubernetesClusterCert

 

功能介紹

該API用于獲取指定集群的證書信息。

接口約束

該接口適用于1.13及以上集群版本。

調(diào)用方法

請(qǐng)參見如何調(diào)用API。

URI

POST /api/v3/projects/{project_id}/clusters/{cluster_id}/clustercert

表1 路徑參數(shù)

參數(shù)

是否必選

參數(shù)類型

描述

project_id

String

項(xiàng)目ID,獲取方式請(qǐng)參見如何獲取接口URI中參數(shù)

cluster_id

String

集群ID,獲取方式請(qǐng)參見如何獲取接口URI中參數(shù)。

請(qǐng)求參數(shù)

表2 請(qǐng)求Header參數(shù)

參數(shù)

是否必選

參數(shù)類型

描述

Content-Type

String

消息體的類型(格式)

X-Auth-Token

String

調(diào)用接口的認(rèn)證方式分為Token和AK/SK兩種,如果您使用的Token方式,此參數(shù)為必填,請(qǐng)?zhí)顚慣oken的值,獲取方式請(qǐng)參見獲取token

表3 請(qǐng)求Body參數(shù)

參數(shù)

是否必選

參數(shù)類型

描述

duration

Integer

集群證書有效時(shí)間,單位為天,最小值為1,最大值為1825(5*365,1年固定計(jì)365天,忽略閏年影響);若填-1則為最大值5年。

響應(yīng)參數(shù)

狀態(tài)碼: 200

表4 響應(yīng)Header參數(shù)

參數(shù)

參數(shù)類型

描述

Port-ID

String

集群控制節(jié)點(diǎn)端口ID

表5 響應(yīng)Body參數(shù)

參數(shù)

參數(shù)類型

描述

kind

String

API類型,固定值“Config”,該值不可修改。

apiVersion

String

API版本,固定值“v1”。

preferences

Object

當(dāng)前未使用該字段,當(dāng)前默認(rèn)為空。

clusters

Array of Clusters objects

集群列表。

users

Array of Users objects

存放了指定用戶的一些證書信息和ClientKey信息。

contexts

Array of Contexts objects

上下文列表。

current-context

String

當(dāng)前上下文,若存在publicIp(虛擬機(jī)彈性IP)時(shí)為 external; 若不存在publicIp為 internal。

表6 Clusters

參數(shù)

參數(shù)類型

描述

name

String

集群名字。

  • 若不存在publicIp(虛擬機(jī)彈性IP),則集群列表的集群數(shù)量為1,該字段值為“internalCluster”。

  • 若存在publicIp,則集群列表的集群數(shù)量大于1,所有擴(kuò)展的cluster的name的值為“externalCluster”。

cluster

ClusterCert object

集群信息。

表7 ClusterCert

參數(shù)

參數(shù)類型

描述

server

String

服務(wù)器地址。

certificate-authority-data

String

證書授權(quán)數(shù)據(jù)。

insecure-skip-tls-verify

Boolean

不校驗(yàn)服務(wù)端證書,在 cluster 類型為 externalCluster 時(shí),該值為 true。

表8 Users

參數(shù)

參數(shù)類型

描述

name

String

當(dāng)前為固定值“user“。

user

User object

存放了指定用戶的一些證書信息和ClientKey信息。

表9 User

參數(shù)

參數(shù)類型

描述

client-certificate-data

String

客戶端證書。

client-key-data

String

包含來自TLS客戶端密鑰文件的PEM編碼數(shù)據(jù)。

表10 Contexts

參數(shù)

參數(shù)類型

描述

name

String

上下文的名稱。

  • 若不存在publicIp(虛擬機(jī)彈性IP),則集群列表的集群數(shù)量為1,該字段值為“internal”。

  • 若存在publicIp,則集群列表的集群數(shù)量大于1,所有擴(kuò)展的context的name的值為“external”。

context

Context object

上下文信息。

表11 Context

參數(shù)

參數(shù)類型

描述

cluster

String

上下文cluster信息。

user

String

上下文user信息。

請(qǐng)求示例

申請(qǐng)30天有效的集群訪問證書

{
  "duration" : 30
}

響應(yīng)示例

狀態(tài)碼: 200

表示成功獲取指定集群的證書。證書文件格式參見kubernetes v1.Config結(jié)構(gòu)

{
  "kind" : "Config",
  "apiVersion" : "v1",
  "preferences" : { },
  "clusters" : [ {
    "name" : "internalCluster",
    "cluster" : {
      "server" : "https://192.168.1.7:5443",
      "certificate-authority-data" : "Q2VydGlmaWNhdGU6******FTkQgQ0VSVElGSUNBVEUtLS0tLQo="
    }
  } ],
  "users" : [ {
    "name" : "user",
    "user" : {
      "client-certificate-data" : "LS0tLS1CRUdJTiBDR******QVRFLS0tLS0K",
      "client-key-data" : "LS0tLS1CRUdJTi******BLRVktLS0tLQo="
    }
  } ],
  "contexts" : [ {
    "name" : "internal",
    "context" : {
      "cluster" : "internalCluster",
      "user" : "user"
    }
  } ],
  "current-context" : "internal"
}

SDK代碼示例

SDK代碼示例如下。

  • Java
  • Python
  • Go
  • 更多

申請(qǐng)30天有效的集群訪問證書

package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.cce.v3.region.CceRegion;
import com.huaweicloud.sdk.cce.v3.*;
import com.huaweicloud.sdk.cce.v3.model.*;


public class CreateKubernetesClusterCertSolution {

    public static void main(String[] args) {
        String ak = "<YOUR AK>";
        String sk = "<YOUR SK>";
        String projectId = "<project_id>";

        ICredential auth = new BasicCredentials()
                .withProjectId(projectId)
                .withAk(ak)
                .withSk(sk);

        CceClient client = CceClient.newBuilder()
                .withCredential(auth)
                .withRegion(CceRegion.valueOf("cn-north-4"))
                .build();
        CreateKubernetesClusterCertRequest request = new CreateKubernetesClusterCertRequest();
        request.withClusterId("<cluster_id>");
        CertDuration body = new CertDuration();
        body.withDuration(30);
        request.withBody(body);
        try {
            CreateKubernetesClusterCertResponse response = client.createKubernetesClusterCert(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}

申請(qǐng)30天有效的集群訪問證書

# coding: utf-8

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcce.v3.region.cce_region import CceRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkcce.v3 import *

if __name__ == "__main__":
    ak = "<YOUR AK>"
    sk = "<YOUR SK>"
    projectId = "<project_id>"

    credentials = BasicCredentials(ak, sk, projectId) \

    client = CceClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(CceRegion.value_of("cn-north-4")) \
        .build()

    try:
        request = CreateKubernetesClusterCertRequest()
        request.cluster_id = "<cluster_id>"
        request.body = CertDuration(
            duration=30
        )
        response = client.create_kubernetes_cluster_cert(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

申請(qǐng)30天有效的集群訪問證書

package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    cce "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3/region"
)

func main() {
    ak := "<YOUR AK>"
    sk := "<YOUR SK>"
    projectId := "<project_id>"

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        WithProjectId(projectId).
        Build()

    client := cce.NewCceClient(
        cce.CceClientBuilder().
            WithRegion(region.ValueOf("cn-north-4")).
            WithCredential(auth).
            Build())

    request := &model.CreateKubernetesClusterCertRequest{}
	request.ClusterId = "<cluster_id>"
	request.Body = &model.CertDuration{
		Duration: int32(30),
	}
	response, err := client.CreateKubernetesClusterCert(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多編程語言的SDK代碼示例,請(qǐng)參見API Explorer的代碼示例頁簽,可生成自動(dòng)對(duì)應(yīng)的SDK代碼示例。

狀態(tài)碼

狀態(tài)碼

描述

200

表示成功獲取指定集群的證書。證書文件格式參見kubernetes v1.Config結(jié)構(gòu)

錯(cuò)誤碼

請(qǐng)參見錯(cuò)誤碼。