gpt4 book ai didi

python - 使用Python API以编程方式克隆Kubernetes对象

转载 作者:行者123 更新时间:2023-12-02 12:29:42 25 4
gpt4 key购买 nike

Python API可用于从集群读取对象。通过克隆,我们可以说:

  • 使用kubectl get来获取现有Kubernetes对象的副本
  • 更改对象
  • 的属性
  • 应用新对象

  • 直到最近, --export api was deprecated in 1.14的选项。我们如何使用Python Kubernetes API执行上述1-3的步骤?

    关于如何提取 code from Python API to YAML,存在多个问题,但尚不清楚如何转换Kubernetes API对象。

    最佳答案

    在查看了需求之后,我花了几个小时研究Kubernetes Python API。 Issue 340和其他人询问如何将Kubernetes API对象转换为dict,但是我发现的唯一解决方法是将retrieve the raw data转换为JSON。

  • 以下代码使用Kubernetes API从命名空间对象中获取deployment及其相关的hpa,但将其原始值检索为JSON。
  • 然后,将数据转换为dict后,您也可以通过removing null references清理数据。
  • 完成后,您可以将dict转换为YAML有效负载,然后转换为save the YAML to the file system
  • 最后,您可以使用kubectl或Kubernetes Python API进行应用。

  • 注意:
  • 确保设置KUBECONFIG=config,以便您可以指向群集
  • 确保使用给定 namespace 中要克隆的相应对象的名称来调整origin_obj_name = "istio-ingressgateway"origin_obj_namespace = "istio-system"的值。

  • import os
    import logging
    import yaml
    import json
    logging.basicConfig(level = logging.INFO)

    import crayons
    from kubernetes import client, config
    from kubernetes.client.rest import ApiException

    LOGGER = logging.getLogger(" IngressGatewayCreator ")

    class IngressGatewayCreator:

    @staticmethod
    def clone_default_ingress(clone_context):
    # Clone the deployment
    IngressGatewayCreator.clone_deployment_object(clone_context)

    # Clone the deployment's HPA
    IngressGatewayCreator.clone_hpa_object(clone_context)

    @staticmethod
    def clone_deployment_object(clone_context):
    kubeconfig = os.getenv('KUBECONFIG')
    config.load_kube_config(kubeconfig)
    v1apps = client.AppsV1beta1Api()

    deployment_name = clone_context.origin_obj_name
    namespace = clone_context.origin_obj_namespace

    try:
    # gets an instance of the api without deserialization to model
    # https://github.com/kubernetes-client/python/issues/574#issuecomment-405400414
    deployment = v1apps.read_namespaced_deployment(deployment_name, namespace, _preload_content=False)

    except ApiException as error:
    if error.status == 404:
    LOGGER.info("Deployment %s not found in namespace %s", deployment_name, namespace)
    return
    raise

    # Clone the object deployment as a dic
    cloned_dict = IngressGatewayCreator.clone_k8s_object(deployment, clone_context)

    # Change additional objects
    cloned_dict["spec"]["selector"]["matchLabels"]["istio"] = clone_context.name
    cloned_dict["spec"]["template"]["metadata"]["labels"]["istio"] = clone_context.name

    # Save the deployment template in the output dir
    context.save_clone_as_yaml(cloned_dict, "deployment")

    @staticmethod
    def clone_hpa_object(clone_context):
    kubeconfig = os.getenv('KUBECONFIG')
    config.load_kube_config(kubeconfig)
    hpas = client.AutoscalingV1Api()

    hpa_name = clone_context.origin_obj_name
    namespace = clone_context.origin_obj_namespace

    try:
    # gets an instance of the api without deserialization to model
    # https://github.com/kubernetes-client/python/issues/574#issuecomment-405400414
    hpa = hpas.read_namespaced_horizontal_pod_autoscaler(hpa_name, namespace, _preload_content=False)

    except ApiException as error:
    if error.status == 404:
    LOGGER.info("HPA %s not found in namespace %s", hpa_name, namespace)
    return
    raise

    # Clone the object deployment as a dic
    cloned_dict = IngressGatewayCreator.clone_k8s_object(hpa, clone_context)

    # Change additional objects
    cloned_dict["spec"]["scaleTargetRef"]["name"] = clone_context.name

    # Save the deployment template in the output dir
    context.save_clone_as_yaml(cloned_dict, "hpa")

    @staticmethod
    def clone_k8s_object(k8s_object, clone_context):
    # Manipilate in the dict level, not k8s api, but from the fetched raw object
    # https://github.com/kubernetes-client/python/issues/574#issuecomment-405400414
    cloned_obj = json.loads(k8s_object.data)

    labels = cloned_obj['metadata']['labels']
    labels['istio'] = clone_context.name

    cloned_obj['status'] = None

    # Scrub by removing the "null" and "None" values
    cloned_obj = IngressGatewayCreator.scrub_dict(cloned_obj)

    # Patch the metadata with the name and labels adjusted
    cloned_obj['metadata'] = {
    "name": clone_context.name,
    "namespace": clone_context.origin_obj_namespace,
    "labels": labels
    }

    return cloned_obj

    # https://stackoverflow.com/questions/12118695/efficient-way-to-remove-keys-with-empty-strings-from-a-dict/59959570#59959570
    @staticmethod
    def scrub_dict(d):
    new_dict = {}
    for k, v in d.items():
    if isinstance(v, dict):
    v = IngressGatewayCreator.scrub_dict(v)
    if isinstance(v, list):
    v = IngressGatewayCreator.scrub_list(v)
    if not v in (u'', None, {}):
    new_dict[k] = v
    return new_dict

    # https://stackoverflow.com/questions/12118695/efficient-way-to-remove-keys-with-empty-strings-from-a-dict/59959570#59959570
    @staticmethod
    def scrub_list(d):
    scrubbed_list = []
    for i in d:
    if isinstance(i, dict):
    i = IngressGatewayCreator.scrub_dict(i)
    scrubbed_list.append(i)
    return scrubbed_list


    class IngressGatewayContext:

    def __init__(self, manifest_dir, name, hostname, nats, type):
    self.manifest_dir = manifest_dir
    self.name = name
    self.hostname = hostname
    self.nats = nats
    self.ingress_type = type

    self.origin_obj_name = "istio-ingressgateway"
    self.origin_obj_namespace = "istio-system"

    def save_clone_as_yaml(self, k8s_object, kind):
    try:
    # Just try to create if it doesn't exist
    os.makedirs(self.manifest_dir)

    except FileExistsError:
    LOGGER.debug("Dir already exists %s", self.manifest_dir)

    full_file_path = os.path.join(self.manifest_dir, self.name + '-' + kind + '.yaml')

    # Store in the file-system with the name provided
    # https://stackoverflow.com/questions/12470665/how-can-i-write-data-in-yaml-format-in-a-file/18210750#18210750
    with open(full_file_path, 'w') as yaml_file:
    yaml.dump(k8s_object, yaml_file, default_flow_style=False)

    LOGGER.info(crayons.yellow("Saved %s '%s' at %s: \n%s"), kind, self.name, full_file_path, k8s_object)

    try:
    k8s_clone_name = "http2-ingressgateway"
    hostname = "my-nlb-awesome.a.company.com"
    nats = ["123.345.678.11", "333.444.222.111", "33.221.444.23"]
    manifest_dir = "out/clones"

    context = IngressGatewayContext(manifest_dir, k8s_clone_name, hostname, nats, "nlb")

    IngressGatewayCreator.clone_default_ingress(context)

    except Exception as err:
    print("ERROR: {}".format(err))

    关于python - 使用Python API以编程方式克隆Kubernetes对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59977058/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com