Kubernetes自定义资源定义必需字段

3

我正在尝试编写一个Kubernetes CRD验证模式。我有一个结构的数组(vc),这些结构中的一个字段是必需的(名称字段)。

我尝试查看各种示例,但是当没有名称时它不会生成错误。有什么建议吗?

vc:
  type: array
  items:
    type: object
    properties:
      name:
        type: string
      address:
        type: string
    required:
    - name

其他CRD的验证是否对您起作用?我的意思是,其他必填字段呢?另外,您的Kubernetes版本是什么? - Abdullah Al Maruf - Tuhin
1个回答

5

如果您使用的是v1.8版本,使用验证功能需要启用CustomResourceValidation功能开关。您可以在kube-apiserver上使用以下标志来完成:

--feature-gates=CustomResourceValidation=true

这里是一个工作示例(我在v1.12上测试了这个示例,但此方法也适用于早期版本):
CRD文件:
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: foos.stable.example.com
spec:
  group: stable.example.com
  versions:
    - name: v1
      served: true
      storage: true
  version: v1
  scope: Namespaced
  names:
    plural: foos
    singular: foo
    kind: Foo
  validation:
    openAPIV3Schema:
      properties:
        spec:
          properties:
            vc:
              type: array
              items:
                type: object
                properties:
                  name:
                    type: string
                  address:
                    type: string
                required:
                - name

定制资源:
apiVersion: "stable.example.com/v1"
kind: Foo
metadata:
  name: new-foo
spec:
  vc:
  - address: "bar"
  1. 创建CRD。

kubectl create -f crd.yaml customresourcedefinition.apiextensions.k8s.io/foos.stable.example.com已创建

  1. 获取CRD并检查输出中是否存在validation字段。如果不存在,则可能未启用功能门。

kubectl get crd foos.stable.example.com -oyaml

  1. 尝试创建自定义资源。这应该会失败,并显示以下错误:

kubectl create -f cr-validation.yaml

The Foo "new-foo" is invalid: []: Invalid value: map[string]interface {}{"metadata":map[string]interface {}{"creationTimestamp":"2018-11-18T19:45:23Z", "generation":1, "uid":"7d7f8f0b-eb6a-11e8-b861-54e1ad9de0be", "name":"new-foo", "namespace":"default"}, "spec":map[string]interface {}{"vc":[]interface {}{map[string]interface {}{"address":"bar"}}}, "apiVersion":"stable.example.com/v1", "kind":"Foo"}: 验证失败列表: spec.vc.name在主体中是必需的


如果在自定义资源对象中设置nameaddress,则在vc下应该如何呈现?像这样:vc: - name: "一些名称" address: "bar" - colossal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接