是否可以在 json 或 yaml 中获取一个 kubernetes 对象,并将所有可选字段显式设置为 null?
Is it possible to get a kubernetes object in json or yaml with all optional fields explicitly set to null?
我正在尝试将 helm/kubernetes 代码库迁移到 dhall-kubernetes. Dhall is typed, so I need to provide full records with optional fields set to null if there were not set. So I'm looking for something like kubectl get objname id -o yaml
, but I need it to output all optional fields like fieldName: null
. Is there a way to accomplish this? I don't know how to do it, so as a plan B I wrote dhall-default,我尝试以不同的方式处理它。
我会将@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
... 如果您向 dhall-to-yaml
提供 --preserve-null
选项,它将保留所有 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
我正在尝试将 helm/kubernetes 代码库迁移到 dhall-kubernetes. Dhall is typed, so I need to provide full records with optional fields set to null if there were not set. So I'm looking for something like kubectl get objname id -o yaml
, but I need it to output all optional fields like fieldName: null
. Is there a way to accomplish this? I don't know how to do it, so as a plan B I wrote dhall-default,我尝试以不同的方式处理它。
我会将@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
... 如果您向 dhall-to-yaml
提供 --preserve-null
选项,它将保留所有 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