我正在尝试将一个helm/kubernetes代码库迁移到dhall-kubernetes。Dhall是类型的,所以我需要提供完整的记录,如果没有设置,我需要将可选字段设置为null。所以我正在寻找像kubectl get objname id -o yaml这样的东西,但我需要它来输出所有可选字段,比如fieldName: null。有没有办法做到这一点?我不知道怎么做,所以作为一个B计划,我编写了dhall-default,并尝试用一种不同的方式来实现它。
发布于 2020-04-03 23:15:54
我将把@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: nullhttps://stackoverflow.com/questions/61010406
复制相似问题