python - 使用 Python、Kubernetes api 调用 YAML 到 JSON

标签 python json docker kubernetes yaml

我想把这个文件转成json格式,有谁知道怎么做吗?

这是 yaml 文件:

apiVersion: v1
kind: Namespace
metadata:
  name: theiaide
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  #name: rewrite
  name: theiaide
  namespace: theiaide
  annotations:
    #nginx.ingress.kubernetes.io/rewrite-target: /$2
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - host: ide.quantum.com
    http:
      paths:
      - path: /
        backend:
          serviceName: theiaide
          servicePort: 80
---
apiVersion: v1
kind: Service
metadata:
 name: theiaide
 namespace: theiaide
spec:
 ports:
 - port: 80
   targetPort: 3000
 selector:
   app: theiaide
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: theiaide
  name: theiaide
  namespace: theiaide
spec:
  selector:
    matchLabels:
      app: theiaide
  replicas: 1
  template:
    metadata:
      labels:
        app: theiaide
    spec:
      containers:
      - image: theiaide/theia-python
        imagePullPolicy: IfNotPresent
        name: theiaide
        ports:
        - containerPort: 3000

代码.py

import json,yaml
txt=""
with open(r"C:\Users\77922\PycharmProjects\ide-ingress.yaml",'r') as f:
    for a in f.readlines():
        txt=txt+a
print(yaml.dump(yaml.load_all(txt),default_flow_style=False))
print(json.dumps(yaml.load_all(txt),indent=2,sort_keys=True))

当我运行 python code.py 时,出现错误:

TypeError: can't pickle generator objects

不知道是不是这个---分隔符的原因,因为我的yaml文件里面有多个---分隔符

然后我尝试了以下功能:

def main():

    # config.load_kube_config()

    f = open(r"C:\Users\77922\PycharmProjects\ide-ingress.yaml","r")
    generate_dict  = yaml.load_all(f,Loader=yaml.FullLoader)
    generate_json = json.dumps(generate_dict)
    print(generate_json)
    # dep = yaml.load_all(f)
    # k8s_apps_v1 = client.AppsV1Api()
    # resp = k8s_apps_v1.create_namespaced_deployment(
    #     body=dep, namespace="default")
    # print("Deployment created. status='%s'" % resp.metadata.name)
if __name__ == '__main__':
    main()

 raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type generator is not JSON serializable

我只是想用这个yaml文件调用kubernetes api生成命名空间

最佳答案

您的文件包含多个文档。您应该使用 safe_load_all 函数而不是 yaml.loadlist 而不是 json.dumps

import json,yaml
txt=""
with open(r"C:\Users\77922\PycharmProjects\ide-ingress.yaml",'r') as f:
    for a in f.readlines():
        txt=txt+a
print(yaml.dump_all(yaml.safe_load_all(txt),default_flow_style=False))
print(list(yaml.safe_load_all(txt)))

关于python - 使用 Python、Kubernetes api 调用 YAML 到 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63045794/

相关文章:

python - 为同一 matplotlib 图例中的每一行设置不同的 `numpoints` 参数

python - 如何使用 GET 请求发送参数?

python - docker 内的GAE。不能暴露8080

php - Docker:PHP、Apache 和 MySQL 在同一个容器/同一个 Dockerfile 中

json - 我如何需要一个字段或另一个字段或(其他两个字段之一)但不是全部?

docker - 在 docker 文件中添加到 pgadmin 的 postgres 连接

python - 从 3.4 升级后如何在 python3.5 中使用 pip?

python - 在字典列表中添加元素

javascript - MYSQL Count As, Group By, JSON 等效

c# - 我如何在 C# 中解析 JSON 字符串获取错误无法反序列化当前 JSON 对象(例如 {“name” :“value” })