【问题标题】:YAML to JSON with Python, Kubernetes api call使用 Python、Kubernetes api 调用的 YAML 到 JSON
【发布时间】:2020-07-23 02:09:56
【问题描述】:

我想把这个文件转成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

code.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生成命名空间

【问题讨论】:

  • 为什么需要转储到 JSON?如果您已经拥有该 YAML,则使用 subprocess.call(['kubectl', 'apply', '-f', 'ide-ingress.yaml']) 可以加载它,或者如您在上一个示例中所示,Kubernetes Python client 具有用于 Kubernetes 对象的本机数据结构和 API。
  • @Layne Wu Ewerton 发布的解决方案是否解决了您的问题?
  • @DavidMaze 其实我只是想用API来调用,但是目前createdeployment失败。 igress命名空间和服务创建成功。
  • @DawidKruk 是的,Ewerton 帮我解决了这个文件转换问题,但我似乎在和 David Maze 讨论其他事情。我需要打开一个新问题来讨论它吗?

标签: python json docker kubernetes yaml


【解决方案1】:

您的文件包含多个文档。你应该使用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)))

【讨论】:

  • 我试过了,好像还有其他错误。我更新了。
猜你喜欢
  • 2018-12-26
  • 1970-01-01
  • 2019-01-25
  • 1970-01-01
  • 2019-11-26
  • 2016-09-03
  • 2017-09-28
  • 2021-10-18
  • 2015-02-07
相关资源
最近更新 更多