我正在尝试将 helm/kubernetes 代码库迁移到dhall-kubernetes。Dhall 是键入的,所以如果没有设置,我需要提供完整的记录,其中可选字段设置为 null。所以我正在寻找类似的东西kubectl get objname id -o yaml
,但我需要它来输出所有可选字段,如fieldName: null
. 有没有办法做到这一点?我不知道该怎么做,所以作为一个计划 BI 写了dhall-default我尝试以不同的方式处理它。
问问题
534 次
1 回答
4
我会将@sjakobi 的解决方案变成@dredozubov 建议的答案
您可以将所需的类型从dhall-kubernetes
to传递给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
于 2020-04-03T15:15:54.050 回答