【问题标题】:kubernetes: validating a yaml file against a custom resourcekubernetes:针对自定义资源验证 yaml 文件
【发布时间】:2019-09-02 21:38:55
【问题描述】:

假设我的 k8s 集群上有一个自定义资源暴露在专有 api 端点上,例如somecompany/v1

有没有办法验证描述此资源的 .yaml 清单?

这是自定义资源提供者应该公开的功能,还是 k8s 原生支持 CRD?

【问题讨论】:

    标签: kubernetes kubernetes-apiserver kubernetes-custom-resources


    【解决方案1】:

    我们来看一个简单的例子:

    apiVersion: apiextensions.k8s.io/v1beta1
    kind: CustomResourceDefinition
    metadata:
      name: myresources.stable.example.com
    spec:
      group: stable.example.com
      versions:
        - name: v1
          served: true
          storage: true
      scope: Namespaced
      names:
        plural: myresources
        singular: myresource
        kind: MyResource
        shortNames:
        - mr
      validation:
        openAPIV3Schema:
          required: ["spec"]
          properties:
            spec:
              required: ["cert","key","domain"]
              properties:
                cert:
                  type: "string"
                  minimum: 1
                key:
                  type: "string"
                  minimum: 1
                domain:
                  type: "string"
                  minimum: 1 
    

    spec.validation 字段描述了自定义资源的自定义验证方法。如果某些字段为空,您可以使用验证来阻止资源的创建。在此示例中,OpenAPIV3Schema 验证约定用于检查我们自定义资源中某些字段的类型。我们确保自定义资源的 specspec.certspec.keyspec.domain 字段确实存在并且它们是字符串类型。用户还可以使用validatingadmissionwebhook 作为验证模式。您可以在official documentation 中找到有关使用此字段的限制的更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 2018-01-17
      • 2020-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-04
      相关资源
      最近更新 更多