Kubernetes pod自定义监测

wtzytmuj  于 2023-03-17  发布在  Kubernetes
关注(0)|答案(1)|浏览(156)

我正在尝试实现python脚本,该脚本收集并解析kubernetes pod清单的映像版本和2个不同kubernetes集群中每个pod的secretName,然后如果2个集群之间存在任何差异,则应发送警报。然后,应通过维多利亚Metrics示例解析2个集群的这些指标。如果我选中kubectl describe pod_name,则会在中观察到问题-在其输出中存在字段secretName:

Volumes:
  cacert:
    Type:           Glusterfs (a Glusterfs mount on the host that shares a pod's lifetime)
    EndpointsName:  glusterfs-cluster
    Path:           test/jvm/cert
    ReadOnly:       false
  service-conf-secrets:
    Type:                Projected (a volume that contains injected data from multiple sources)
    SecretName:          example-app-1.25.01-57409t3
    SecretOptionalName:  <nil>

但是如果我使用kubernetes.client.CoreV1Api和它的函数list_pod_for_all_namespaces-在它的输出中根本找不到secretName。
我在哪里可以找到和解析这个字段,并从这些字段中生成prometheus格式度量?

afdcj2ne

afdcj2ne1#

这里有一个例子。
我包含了对Python SDK实现Kubernetes类型的注解引用以及这些类型的类型提示,以帮助解释属性的使用。
为了完整起见,我包含了V1VolumeProjection名称的完整枚举,包括secretV1SecretProjection)。

from kubernetes import client,config

config.load_kube_config()

v1 = client.CoreV1Api()

# https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1PodList.md
pod_list: client.V1PodList = v1.list_pod_for_all_namespaces(watch=False)
# Iterator over returned items (if any)
pods: list[client.V1Pod] = pod_list.items
for pod in pods:
    metadata: client.V1ObjectMeta = pod.metadata
    # https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1PodSpec.md
    spec: client.V1PodSpec = pod.spec
    print(f"{metadata.namespace}/{metadata.name} [{pod.status.pod_ip}]")
    # if pod.metadata.namespace=="shell-operator" and pod.metadata.name=="pods":
    # Iterative over volume (f any)
    # https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Volume.md
    volumes: list[client.V1Volume] = spec.volumes
    for volume in volumes:
        if volume.projected:
            projected: client.V1ProjectedVolumeSource = volume.projected
            # https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1VolumeProjection.md
            sources: list[client.V1VolumeProjection] = projected.sources
            for source in sources:
                # if source.config_map:
                # if source.downward_api:
                if source.secret:
                    # https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1SecretProjection.md
                    secret: client.V1SecretProjection = source.secret
                    print(secret.name)
                    items: list[client.V1KeyToPath] = secret.items
                    for i in items:
                        path: str = i.path
                        print(path)
                # if source.service_account_token:

相关问题