是否可以在所有可选字段显式设置为null的json或yaml中获得kubernetes对象?

时间:2020-04-03 11:02:29

标签: kubernetes dhall

我正在尝试将头盔/ kubernetes代码库迁移到dhall-kubernetes。 Dhall是键入的,因此我需要提供完整记录,如果未设置,则将可选字段设置为null。所以我正在寻找类似kubectl get objname id -o yaml的东西,但是我需要它来输出所有可选字段,例如fieldName: null。有没有办法做到这一点?我不知道该怎么做,所以作为计划B,我写了dhall-default,然后尝试以其他方式进行处理。

1 个答案:

答案 0 :(得分:3)

我将把@sjakobi的解决方案变成类似@dredozubov建议的答案

您可以将所需的类型从dhall-kubernetes传递到yaml-to-dhall,即使没有任何简化,它也会生成等效的Dhall代码。

作为示例,假设您具有以下Kubernetes资源:

# ./nginx.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      name: nginx
  template:
    metadata:
      name: nginx
    spec:
      containers:
        - image: nginx:1.15.3
          name: nginx
          ports:
            - containerPort: 80

...以及Kubernetes部署的以下Dhall类型:

-- ./Deployment.dhall

let kubernetes = https://raw.githubusercontent.com/dhall-lang/dhall-kubernetes/506d633e382872346927b8cb9884d8b7382e6cab/package.dhall

in  kubernetes.Deployment.Type

然后您可以通过运行以下命令将YAML转换为Dhall:

$ yaml-to-dhall --file ./nginx.yaml ./Deployment.dhall

输出有点大(约1300行),因为yaml-to-dhall尚未利用对默认值的支持,因此在此不包括输出。

如果将结果通过管道传送回dhall-to-yaml,则将获得原始资源(尽管字段已排序):

$ yaml-to-dhall --file ./nginx.yaml ./Deployment.dhall | dhall-to-yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      name: nginx
  template:
    metadata:
      name: nginx
    spec:
      containers:
        - image: nginx:1.15.3
          name: nginx
          ports:
            - containerPort: 80

...,如果您向--preserve-null提供dhall-to-yaml选项,它将保留所有null字段作为问题请求:

$ yaml-to-dhall --file ./nginx.yaml ./Deployment.dhall | dhall-to-yaml --preserve-null 
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations: null
  clusterName: null
  creationTimestamp: null
  deletionGracePeriodSeconds: null
  deletionTimestamp: null
  finalizers: null
  generateName: null
  generation: null
  labels: null
  managedFields: null
  name: nginx
  namespace: null
  ownerReferences: null
  resourceVersion: null
  selfLink: null
  uid: null
spec:
  minReadySeconds: null
  paused: null
  progressDeadlineSeconds: null
  replicas: 2
  revisionHistoryLimit: null
  selector:
    matchExpressions: null
    matchLabels:
      name: nginx
  strategy: null
  template:
    metadata:
      annotations: null
      clusterName: null
      creationTimestamp: null
      deletionGracePeriodSeconds: null
      deletionTimestamp: null
      finalizers: null
      generateName: null
      generation: null
      labels: null
      managedFields: null
      name: nginx
      namespace: null
      ownerReferences: null
      resourceVersion: null
      selfLink: null
      uid: null
    spec:
      activeDeadlineSeconds: null
      affinity: null
      automountServiceAccountToken: null
      containers:
        - args: null
          command: null
          env: null
          envFrom: null
          image: nginx:1.15.3
          imagePullPolicy: null
          lifecycle: null
          livenessProbe: null
          name: nginx
          ports:
            - containerPort: 80
              hostIP: null
              hostPort: null
              name: null
              protocol: null
          readinessProbe: null
          resources: null
          securityContext: null
          startupProbe: null
          stdin: null
          stdinOnce: null
          terminationMessagePath: null
          terminationMessagePolicy: null
          tty: null
          volumeDevices: null
          volumeMounts: null
          workingDir: null
      dnsConfig: null
      dnsPolicy: null
      enableServiceLinks: null
      ephemeralContainers: null
      hostAliases: null
      hostIPC: null
      hostNetwork: null
      hostPID: null
      hostname: null
      imagePullSecrets: null
      initContainers: null
      nodeName: null
      nodeSelector: null
      overhead: null
      preemptionPolicy: null
      priority: null
      priorityClassName: null
      readinessGates: null
      restartPolicy: null
      runtimeClassName: null
      schedulerName: null
      securityContext: null
      serviceAccount: null
      serviceAccountName: null
      shareProcessNamespace: null
      subdomain: null
      terminationGracePeriodSeconds: null
      tolerations: null
      topologySpreadConstraints: null
      volumes: null
status: null