kubernetes:针对自定义资源验证 yaml 文件

kubernetes: validating a yaml file against a custom resource

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

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

它是自定义资源提供程序应该公开的功能,还是 k8s 本机支持 CRD?

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

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 字段确实存在并且它们是 String 类型。用户也可以使用 validatingadmissionwebhook as a validation schema. You can find more about restrictions for using this field in the official documentation.