首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >有没有可能在json或yaml中获得一个所有可选字段都显式设置为null的kubernetes对象?

有没有可能在json或yaml中获得一个所有可选字段都显式设置为null的kubernetes对象?
EN

Stack Overflow用户
提问于 2020-04-03 19:02:29
回答 1查看 447关注 0票数 1

我正在尝试将一个helm/kubernetes代码库迁移到dhall-kubernetes。Dhall是类型的,所以我需要提供完整的记录,如果没有设置,我需要将可选字段设置为null。所以我正在寻找像kubectl get objname id -o yaml这样的东西,但我需要它来输出所有可选字段,比如fieldName: null。有没有办法做到这一点?我不知道怎么做,所以作为一个B计划,我编写了dhall-default,并尝试用一种不同的方式来实现它。

EN

Stack Overflow用户

回答已采纳

发布于 2020-04-03 23:15:54

我将把@sjakobi的解决方案变成一个像@dredozubov建议的答案

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

例如,假设您有以下Kubernetes资源:

代码语言:javascript
运行
复制
# ./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类型:

代码语言:javascript
运行
复制
-- ./Deployment.dhall

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

in  kubernetes.Deployment.Type

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

代码语言:javascript
运行
复制
$ yaml-to-dhall --file ./nginx.yaml ./Deployment.dhall

输出有点大(大约1300行),因为yaml-to-dhall还没有利用对默认值的支持,所以我不在这里介绍输出。

如果您通过管道将结果返回到dhall-to-yaml,那么您将获得原始资源(尽管字段已排序):

代码语言:javascript
运行
复制
$ 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字段:

代码语言:javascript
运行
复制
$ 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
票数 4
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61010406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档