kubernetes 自定义资源的嵌套字段中缺少默认值

Missing default value in nested field of kubernetes custom resource

我有一个自定义资源定义,它具有带默认值的嵌套字段(为简洁起见省略了一些样板文件):

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
spec:
  scope: Namespaced
  group: thismatters.Whosebug
  names:
    kind: BadDefault
  versions:
  - name: v1alpha
    schema:
      openAPIV3Schema:
        type: object
        properties:
          spec:
            type: object
            properties:
              regularThing:
                type: integer
                default: 6
              shouldDefault:
                type: object
                properties:
                  nestedThing:
                    type: integer
                    default: 12

每当我创建此类资源而不指定 shouldDefault 对象时:

apiVersion: thismatters.Whosebug/v1alpha
kind: BadDefault
metadata:
  name: blank-demo
spec: 
  regularThing: 7

当资源为 described:

时,.shouldDefault.nestedThing 的默认值不会出现
apiVersion: thismatters.Whosebug/v1alpha
kind: BadDefault
metadata:
  name: blank-demo
spec: 
  regularThing: 7

如果我用这个清单更新资源:

apiVersion: thismatters.Whosebug/v1alpha
kind: BadDefault
metadata:
  name: blank-demo
spec: 
  regularThing: 7
  shouldDefault: {}

然后当described:

时填充嵌套字段默认值
apiVersion: thismatters.Whosebug/v1alpha
kind: BadDefault
metadata:
  name: blank-demo
spec: 
  regularThing: 7
  shouldDefault:
    nestedThing: 12

我可以在 CRD 中做些什么来消除资源清单中对 shouldDefault: {} 行的需求并在嵌套字段中填充默认值吗?

我的集群在 Kubernetes 版本 1.19 上。

shouldDefault 对象添加 default 属性 可以解决此问题:

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
spec:
  scope: Namespaced
  group: thismatters.Whosebug
  names:
    kind: BadDefault
  versions:
  - name: v1alpha
    schema:
      openAPIV3Schema:
        type: object
        properties:
          spec:
            type: object
            properties:
              regularThing:
                type: integer
                default: 6
              shouldDefault:
                type: object
                default: {}   #  <-- this was added
                properties:
                  nestedThing:
                    type: integer
                    default: 12