在Kubernetes CRD OpenAPI模式中正确使用Oneof

发布于 2025-02-08 23:53:04 字数 2155 浏览 1 评论 0原文

我试图描述OpenAPI V3模式中的相互排斥性。

我有一个自定义资源,可以使用属性widgetNamewidget资源的名称或widgetDefinition,inline widget < /代码>定义。也就是说,这是有效的:

widget:
  widgetName: Foo

这是有效的:

widget:
  widgetDefinition:
    name: Foo
    size: large
    color: red

但是您不能同时拥有widget.widgetNamewidget.widgetDefinition(允许它都不具有)。我尝试了一下:

versions:
  - name: v1beta1
    schema:
      openAPIV3Schema:
        type: object
        properties:
        spec:
          type: object
          properties:
            widget:
              type: object
              oneOf:
                - properties:
                    widgetName:
                      type: string
                - properties:
                    widgetDefinition:
                      type: object
                      properties:
                        name:
                          type: string
                        size:
                          type: string
                        color:
                          type: string

但这是行不通的,因为:

The CustomResourceDefinition "widgetcontainers.factory.example.com" is invalid: 
* spec.validation.openAPIV3Schema.properties[spec].properties[widget].oneOf[0].properties[widgetName].type: Forbidden: must be empty to be structural
* spec.validation.openAPIV3Schema.properties[spec].properties[widget].oneOf[1].properties[widgetDefinition].properties[color].type: Forbidden: must be empty to be structural
* spec.validation.openAPIV3Schema.properties[spec].properties[widget].oneOf[1].properties[widgetDefinition].properties[name].type: Forbidden: must be empty to be structural
* spec.validation.openAPIV3Schema.properties[spec].properties[widget].oneOf[1].properties[widgetDefinition].properties[size].type: Forbidden: must be empty to be structural
* spec.validation.openAPIV3Schema.properties[spec].properties[widget].oneOf[1].properties[widgetDefinition].type: Forbidden: must be empty to be structural

说“ CR可能包含这两个的一个”的正确方法是什么 属性,但不是两者”?

I am trying to describe mutually exclusive properties in an OpenAPI v3 schema.

I have a custom resource that can take a property widgetName, a name of a Widget resource, or widgetDefinition, an inline Widget definition. That is, this is valid:

widget:
  widgetName: Foo

And this is valid:

widget:
  widgetDefinition:
    name: Foo
    size: large
    color: red

But you can't have both widget.widgetName and widget.widgetDefinition (it is allowed to have neither). I tried this:

versions:
  - name: v1beta1
    schema:
      openAPIV3Schema:
        type: object
        properties:
        spec:
          type: object
          properties:
            widget:
              type: object
              oneOf:
                - properties:
                    widgetName:
                      type: string
                - properties:
                    widgetDefinition:
                      type: object
                      properties:
                        name:
                          type: string
                        size:
                          type: string
                        color:
                          type: string

But this doesn't work, because:

The CustomResourceDefinition "widgetcontainers.factory.example.com" is invalid: 
* spec.validation.openAPIV3Schema.properties[spec].properties[widget].oneOf[0].properties[widgetName].type: Forbidden: must be empty to be structural
* spec.validation.openAPIV3Schema.properties[spec].properties[widget].oneOf[1].properties[widgetDefinition].properties[color].type: Forbidden: must be empty to be structural
* spec.validation.openAPIV3Schema.properties[spec].properties[widget].oneOf[1].properties[widgetDefinition].properties[name].type: Forbidden: must be empty to be structural
* spec.validation.openAPIV3Schema.properties[spec].properties[widget].oneOf[1].properties[widgetDefinition].properties[size].type: Forbidden: must be empty to be structural
* spec.validation.openAPIV3Schema.properties[spec].properties[widget].oneOf[1].properties[widgetDefinition].type: Forbidden: must be empty to be structural

What's the correct way to say "the cr may contain one of these two
properties but not both"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡水深流 2025-02-15 23:53:04

Larsks,我过去使用这样的语法定义了类似的CRD模式:

versions:
  - name: v1beta1
    schema:
      openAPIV3Schema:
        type: object
        properties:
        spec:
          type: object
          properties:
            widget:
              type: object
              properties:
                 widgetName:
                   type: string
                 widgetDefinition:
                   type: object
                   properties:
                     name:
                       type: string
                     size:
                       type: string
                     color:
                       type: string
              oneOf:
                - properties:
                  required: [ "widgetName" ]
                - properties:
                  required: [ "widgetDefinition" ]

larsks, I've defined similar CRD schema in the past, using a syntax like this:

versions:
  - name: v1beta1
    schema:
      openAPIV3Schema:
        type: object
        properties:
        spec:
          type: object
          properties:
            widget:
              type: object
              properties:
                 widgetName:
                   type: string
                 widgetDefinition:
                   type: object
                   properties:
                     name:
                       type: string
                     size:
                       type: string
                     color:
                       type: string
              oneOf:
                - properties:
                  required: [ "widgetName" ]
                - properties:
                  required: [ "widgetDefinition" ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文