首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Kubernetes CRD OpenAPI模式中正确使用OpenAPI

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

Stack Overflow用户
提问于 2022-06-20 16:48:21
回答 1查看 327关注 0票数 3

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

我有一个自定义资源,可以使用属性widgetName ( Widget资源的名称)或widgetDefinition (内联Widget定义)。也就是说,这是有效的:

代码语言:javascript
复制
widget:
  widgetName: Foo

这是有效的:

代码语言:javascript
复制
widget:
  widgetDefinition:
    name: Foo
    size: large
    color: red

但是您不能同时拥有widget.widgetNamewidget.widgetDefinition (允许两者都拥有)。我试过这个:

代码语言:javascript
复制
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

但这行不通,因为:

代码语言:javascript
复制
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可能包含这两种属性中的一种,而不是同时包含这两种属性”?

EN

回答 1

Stack Overflow用户

发布于 2022-07-06 22:01:56

在过去,我使用如下语法定义了类似的CRD模式:

代码语言:javascript
复制
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" ]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72690407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档