1

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

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
spec:
  scope: Namespaced
  group: thismatters.stackoverflow
  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.stackoverflow/v1alpha
kind: BadDefault
metadata:
  name: blank-demo
spec: 
  regularThing: 7

资源为d.shouldDefault.nestedThing时不显示默认值:describe

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

如果我使用此清单更新资源:

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

describe然后在d时填充嵌套字段默认值:

apiVersion: thismatters.stackoverflow/v1alpha
kind: BadDefault
metadata:
  name: blank-demo
spec: 
  regularThing: 7
  shouldDefault:
    nestedThing: 12

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

我的集群在 Kubernetes 版本 1.19 上。

4

1 回答 1

1

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

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
spec:
  scope: Namespaced
  group: thismatters.stackoverflow
  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
于 2021-12-31T02:54:46.323 回答