go.mod: bump client-go and api machinerie

I had to run `make generate`.
Some API functions got additional parameters `Options` and `Context`.
I used empty options and `context.TODO()` for now.

Signed-off-by: leonnicolas <leonloechner@gmx.de>
This commit is contained in:
leonnicolas
2021-05-15 12:08:31 +02:00
parent f2c37b9de6
commit a3bf13711c
2386 changed files with 419055 additions and 183398 deletions

View File

@@ -24,6 +24,12 @@ import (
"github.com/go-openapi/spec"
)
const (
// TODO: Make this configurable.
ExtensionPrefix = "x-kubernetes-"
ExtensionV2Schema = ExtensionPrefix + "v2-schema"
)
// OpenAPIDefinition describes single type. Normally these definitions are auto-generated using gen-openapi.
type OpenAPIDefinition struct {
Schema spec.Schema
@@ -43,6 +49,10 @@ type OpenAPIDefinitionGetter interface {
OpenAPIDefinition() *OpenAPIDefinition
}
type OpenAPIV3DefinitionGetter interface {
OpenAPIV3Definition() *OpenAPIDefinition
}
type PathHandler interface {
Handle(path string, handler http.Handler)
}
@@ -95,28 +105,34 @@ type Config struct {
DefaultSecurity []map[string][]string
}
var schemaTypeFormatMap = map[string][]string{
"uint": {"integer", "int32"},
"uint8": {"integer", "byte"},
"uint16": {"integer", "int32"},
"uint32": {"integer", "int64"},
"uint64": {"integer", "int64"},
"int": {"integer", "int32"},
"int8": {"integer", "byte"},
"int16": {"integer", "int32"},
"int32": {"integer", "int32"},
"int64": {"integer", "int64"},
"byte": {"integer", "byte"},
"float64": {"number", "double"},
"float32": {"number", "float"},
"bool": {"boolean", ""},
"time.Time": {"string", "date-time"},
"string": {"string", ""},
"integer": {"integer", ""},
"number": {"number", ""},
"boolean": {"boolean", ""},
"[]byte": {"string", "byte"}, // base64 encoded characters
"interface{}": {"object", ""},
type typeInfo struct {
name string
format string
zero interface{}
}
var schemaTypeFormatMap = map[string]typeInfo{
"uint": {"integer", "int32", 0.},
"uint8": {"integer", "byte", 0.},
"uint16": {"integer", "int32", 0.},
"uint32": {"integer", "int64", 0.},
"uint64": {"integer", "int64", 0.},
"int": {"integer", "int32", 0.},
"int8": {"integer", "byte", 0.},
"int16": {"integer", "int32", 0.},
"int32": {"integer", "int32", 0.},
"int64": {"integer", "int64", 0.},
"byte": {"integer", "byte", 0},
"float64": {"number", "double", 0.},
"float32": {"number", "float", 0.},
"bool": {"boolean", "", false},
"time.Time": {"string", "date-time", ""},
"string": {"string", "", ""},
"integer": {"integer", "", 0.},
"number": {"number", "", 0.},
"boolean": {"boolean", "", false},
"[]byte": {"string", "byte", ""}, // base64 encoded characters
"interface{}": {"object", "", interface{}(nil)},
}
// This function is a reference for converting go (or any custom type) to a simple open API type,format pair. There are
@@ -158,12 +174,22 @@ var schemaTypeFormatMap = map[string][]string{
// }
// }
//
func GetOpenAPITypeFormat(typeName string) (string, string) {
func OpenAPITypeFormat(typeName string) (string, string) {
mapped, ok := schemaTypeFormatMap[typeName]
if !ok {
return "", ""
}
return mapped[0], mapped[1]
return mapped.name, mapped.format
}
// Returns the zero-value for the given type along with true if the type
// could be found.
func OpenAPIZeroValue(typeName string) (interface{}, bool) {
mapped, ok := schemaTypeFormatMap[typeName]
if !ok {
return nil, false
}
return mapped.zero, true
}
func EscapeJsonPointer(p string) string {
@@ -172,3 +198,11 @@ func EscapeJsonPointer(p string) string {
p = strings.Replace(p, "/", "~1", -1)
return p
}
func EmbedOpenAPIDefinitionIntoV2Extension(main OpenAPIDefinition, embedded OpenAPIDefinition) OpenAPIDefinition {
if main.Schema.Extensions == nil {
main.Schema.Extensions = make(map[string]interface{})
}
main.Schema.Extensions[ExtensionV2Schema] = embedded.Schema
return main
}

View File

@@ -28,7 +28,7 @@ import (
"k8s.io/gengo/generator"
"k8s.io/gengo/types"
"k8s.io/klog"
"k8s.io/klog/v2"
)
const apiViolationFileType = "api-violation"
@@ -139,6 +139,7 @@ func newAPILinter() *apiLinter {
rules: []APIRule{
&rules.NamesMatch{},
&rules.OmitEmptyMatchCase{},
&rules.ListTypeMissing{},
},
}
}

View File

@@ -24,7 +24,7 @@ import (
"k8s.io/gengo/generator"
"k8s.io/gengo/namer"
"k8s.io/gengo/types"
"k8s.io/klog"
"k8s.io/klog/v2"
generatorargs "k8s.io/kube-openapi/cmd/openapi-gen/args"
)

View File

@@ -32,6 +32,7 @@ type extensionAttributes struct {
xName string
kind types.Kind
allowedValues sets.String
enforceArray bool
}
// Extension tag to openapi extension attributes
@@ -46,14 +47,25 @@ var tagToExtension = map[string]extensionAttributes{
allowedValues: sets.NewString("merge", "retainKeys"),
},
"listMapKey": {
xName: "x-kubernetes-list-map-keys",
kind: types.Slice,
xName: "x-kubernetes-list-map-keys",
kind: types.Slice,
enforceArray: true,
},
"listType": {
xName: "x-kubernetes-list-type",
kind: types.Slice,
allowedValues: sets.NewString("atomic", "set", "map"),
},
"mapType": {
xName: "x-kubernetes-map-type",
kind: types.Map,
allowedValues: sets.NewString("atomic", "granular"),
},
"structType": {
xName: "x-kubernetes-map-type",
kind: types.Struct,
allowedValues: sets.NewString("atomic", "granular"),
},
}
// Extension encapsulates information necessary to generate an OpenAPI extension.
@@ -113,6 +125,10 @@ func (e extension) hasMultipleValues() bool {
return len(e.values) > 1
}
func (e extension) isAlwaysArrayFormat() bool {
return tagToExtension[e.idlTag].enforceArray
}
// Returns sorted list of map keys. Needed for deterministic testing.
func sortedMapKeys(m map[string][]string) []string {
keys := make([]string, len(m))

View File

@@ -18,6 +18,7 @@ package generators
import (
"bytes"
"encoding/json"
"fmt"
"io"
"path/filepath"
@@ -30,12 +31,13 @@ import (
"k8s.io/gengo/types"
openapi "k8s.io/kube-openapi/pkg/common"
"k8s.io/klog"
"k8s.io/klog/v2"
)
// This is the comment tag that carries parameters for open API generation.
const tagName = "k8s:openapi-gen"
const tagOptional = "optional"
const tagDefault = "default"
// Known values for the tag.
const (
@@ -242,6 +244,16 @@ func methodReturnsValue(mt *types.Type, pkg, name string) bool {
return r.Name.Name == name && r.Name.Package == pkg
}
func hasOpenAPIV3DefinitionMethod(t *types.Type) bool {
for mn, mt := range t.Methods {
if mn != "OpenAPIV3Definition" {
continue
}
return methodReturnsValue(mt, openAPICommonPackagePath, "OpenAPIDefinition")
}
return false
}
func hasOpenAPIDefinitionMethod(t *types.Type) bool {
for mn, mt := range t.Methods {
if mn != "OpenAPIDefinition" {
@@ -272,6 +284,9 @@ func typeShortName(t *types.Type) string {
func (g openAPITypeWriter) generateMembers(t *types.Type, required []string) ([]string, error) {
var err error
for t.Kind == types.Pointer { // fast-forward to effective type containing members
t = t.Elem
}
for _, m := range t.Members {
if hasOpenAPITagValue(m.CommentLines, tagValueFalse) {
continue
@@ -304,9 +319,21 @@ func (g openAPITypeWriter) generateCall(t *types.Type) error {
case types.Struct:
args := argsFromType(t)
g.Do("\"$.$\": ", t.Name)
if hasOpenAPIDefinitionMethod(t) {
hasV2Definition := hasOpenAPIDefinitionMethod(t)
hasV2DefinitionTypeAndFormat := hasOpenAPIDefinitionMethods(t)
hasV3Definition := hasOpenAPIV3DefinitionMethod(t)
switch {
case hasV2DefinitionTypeAndFormat:
g.Do(nameTmpl+"(ref),\n", args)
case hasV2Definition && hasV3Definition:
g.Do("common.EmbedOpenAPIDefinitionIntoV2Extension($.type|raw${}.OpenAPIV3Definition(), $.type|raw${}.OpenAPIDefinition()),\n", args)
case hasV2Definition:
g.Do("$.type|raw${}.OpenAPIDefinition(),\n", args)
} else {
case hasV3Definition:
g.Do("$.type|raw${}.OpenAPIV3Definition(),\n", args)
default:
g.Do(nameTmpl+"(ref),\n", args)
}
}
@@ -317,14 +344,30 @@ func (g openAPITypeWriter) generate(t *types.Type) error {
// Only generate for struct type and ignore the rest
switch t.Kind {
case types.Struct:
if hasOpenAPIDefinitionMethod(t) {
hasV2Definition := hasOpenAPIDefinitionMethod(t)
hasV2DefinitionTypeAndFormat := hasOpenAPIDefinitionMethods(t)
hasV3Definition := hasOpenAPIV3DefinitionMethod(t)
if hasV2Definition || (hasV3Definition && !hasV2DefinitionTypeAndFormat) {
// already invoked directly
return nil
}
args := argsFromType(t)
g.Do("func "+nameTmpl+"(ref $.ReferenceCallback|raw$) $.OpenAPIDefinition|raw$ {\n", args)
if hasOpenAPIDefinitionMethods(t) {
switch {
case hasV2DefinitionTypeAndFormat && hasV3Definition:
g.Do("return common.EmbedOpenAPIDefinitionIntoV2Extension($.type|raw${}.OpenAPIV3Definition(), $.OpenAPIDefinition|raw${\n"+
"Schema: spec.Schema{\n"+
"SchemaProps: spec.SchemaProps{\n", args)
g.generateDescription(t.CommentLines)
g.Do("Type:$.type|raw${}.OpenAPISchemaType(),\n"+
"Format:$.type|raw${}.OpenAPISchemaFormat(),\n"+
"},\n"+
"},\n"+
"})\n}\n\n", args)
return nil
case hasV2DefinitionTypeAndFormat:
g.Do("return $.OpenAPIDefinition|raw${\n"+
"Schema: spec.Schema{\n"+
"SchemaProps: spec.SchemaProps{\n", args)
@@ -373,7 +416,7 @@ func (g openAPITypeWriter) generate(t *types.Type) error {
deps := []string{}
for _, k := range keys {
v := g.refTypes[k]
if t, _ := openapi.GetOpenAPITypeFormat(v.String()); t != "" {
if t, _ := openapi.OpenAPITypeFormat(v.String()); t != "" {
// This is a known type, we do not need a reference to it
// Will eliminate special case of time.Time
continue
@@ -397,11 +440,18 @@ func (g openAPITypeWriter) generateStructExtensions(t *types.Type) error {
// Initially, we will only log struct extension errors.
if len(errors) > 0 {
for _, e := range errors {
klog.V(2).Infof("[%s]: %s\n", t.String(), e)
klog.Errorf("[%s]: %s\n", t.String(), e)
}
}
unions, errors := parseUnions(t)
if len(errors) > 0 {
for _, e := range errors {
klog.Errorf("[%s]: %s\n", t.String(), e)
}
}
// TODO(seans3): Validate struct extensions here.
g.emitExtensions(extensions)
g.emitExtensions(extensions, unions)
return nil
}
@@ -416,28 +466,35 @@ func (g openAPITypeWriter) generateMemberExtensions(m *types.Member, parent *typ
klog.V(2).Infof("%s %s\n", errorPrefix, e)
}
}
g.emitExtensions(extensions)
g.emitExtensions(extensions, nil)
return nil
}
func (g openAPITypeWriter) emitExtensions(extensions []extension) {
func (g openAPITypeWriter) emitExtensions(extensions []extension, unions []union) {
// If any extensions exist, then emit code to create them.
if len(extensions) == 0 {
if len(extensions) == 0 && len(unions) == 0 {
return
}
g.Do("VendorExtensible: spec.VendorExtensible{\nExtensions: spec.Extensions{\n", nil)
for _, extension := range extensions {
g.Do("\"$.$\": ", extension.xName)
if extension.hasMultipleValues() {
if extension.hasMultipleValues() || extension.isAlwaysArrayFormat() {
g.Do("[]interface{}{\n", nil)
}
for _, value := range extension.values {
g.Do("\"$.$\",\n", value)
}
if extension.hasMultipleValues() {
if extension.hasMultipleValues() || extension.isAlwaysArrayFormat() {
g.Do("},\n", nil)
}
}
if len(unions) > 0 {
g.Do("\"x-kubernetes-unions\": []interface{}{\n", nil)
for _, u := range unions {
u.emit(g)
}
g.Do("},\n", nil)
}
g.Do("},\n},\n", nil)
}
@@ -458,6 +515,60 @@ func (g openAPITypeWriter) validatePatchTags(m *types.Member, parent *types.Type
return nil
}
func defaultFromComments(comments []string) (interface{}, error) {
tag, err := getSingleTagsValue(comments, tagDefault)
if tag == "" {
return nil, err
}
var i interface{}
if err := json.Unmarshal([]byte(tag), &i); err != nil {
return nil, fmt.Errorf("failed to unmarshal default: %v", err)
}
return i, nil
}
func mustEnforceDefault(t *types.Type, omitEmpty bool) (interface{}, error) {
switch t.Kind {
case types.Pointer, types.Map, types.Slice, types.Array, types.Interface:
return nil, nil
case types.Struct:
return map[string]interface{}{}, nil
case types.Builtin:
if !omitEmpty {
if zero, ok := openapi.OpenAPIZeroValue(t.String()); ok {
return zero, nil
} else {
return nil, fmt.Errorf("please add type %v to getOpenAPITypeFormat function", t)
}
}
return nil, nil
default:
return nil, fmt.Errorf("not sure how to enforce default for %v", t.Kind)
}
}
func (g openAPITypeWriter) generateDefault(comments []string, t *types.Type, omitEmpty bool) error {
t = resolveAliasType(t)
def, err := defaultFromComments(comments)
if err != nil {
return err
}
if enforced, err := mustEnforceDefault(t, omitEmpty); err != nil {
return err
} else if enforced != nil {
if def == nil {
def = enforced
} else if !reflect.DeepEqual(def, enforced) {
enforcedJson, _ := json.Marshal(enforced)
return fmt.Errorf("invalid default value (%#v) for non-pointer/non-omitempty. If specified, must be: %v", def, string(enforcedJson))
}
}
if def != nil {
g.Do("Default: $.$,\n", fmt.Sprintf("%#v", def))
}
return nil
}
func (g openAPITypeWriter) generateDescription(CommentLines []string) {
var buffer bytes.Buffer
delPrevChar := func() {
@@ -482,7 +593,7 @@ func (g openAPITypeWriter) generateDescription(CommentLines []string) {
default:
if strings.HasPrefix(line, " ") || strings.HasPrefix(line, "\t") {
delPrevChar()
line = "\n" + line + "\n" // Replace it with newline. This is useful when we have a line with: "Example:\n\tJSON-someting..."
line = "\n" + line + "\n" // Replace it with newline. This is useful when we have a line with: "Example:\n\tJSON-something..."
} else {
line += " "
}
@@ -521,9 +632,13 @@ func (g openAPITypeWriter) generateProperty(m *types.Member, parent *types.Type)
g.Do("},\n},\n", nil)
return nil
}
omitEmpty := strings.Contains(reflect.StructTag(m.Tags).Get("json"), "omitempty")
if err := g.generateDefault(m.CommentLines, m.Type, omitEmpty); err != nil {
return fmt.Errorf("failed to generate default in %v: %v: %v", parent, m.Name, err)
}
t := resolveAliasAndPtrType(m.Type)
// If we can get a openAPI type and format for this type, we consider it to be simple property
typeString, format := openapi.GetOpenAPITypeFormat(t.String())
typeString, format := openapi.OpenAPITypeFormat(t.String())
if typeString != "" {
g.generateSimpleProperty(typeString, format)
g.Do("},\n},\n", nil)
@@ -534,11 +649,11 @@ func (g openAPITypeWriter) generateProperty(m *types.Member, parent *types.Type)
return fmt.Errorf("please add type %v to getOpenAPITypeFormat function", t)
case types.Map:
if err := g.generateMapProperty(t); err != nil {
return err
return fmt.Errorf("failed to generate map property in %v: %v: %v", parent, m.Name, err)
}
case types.Slice, types.Array:
if err := g.generateSliceProperty(t); err != nil {
return err
return fmt.Errorf("failed to generate slice property in %v: %v: %v", parent, m.Name, err)
}
case types.Struct, types.Interface:
g.generateReferenceProperty(t)
@@ -559,6 +674,17 @@ func (g openAPITypeWriter) generateReferenceProperty(t *types.Type) {
g.Do("Ref: ref(\"$.$\"),\n", t.Name.String())
}
func resolveAliasType(t *types.Type) *types.Type {
var prev *types.Type
for prev != t {
prev = t
if t.Kind == types.Alias {
t = t.Underlying
}
}
return t
}
func resolveAliasAndPtrType(t *types.Type) *types.Type {
var prev *types.Type
for prev != t {
@@ -581,9 +707,13 @@ func (g openAPITypeWriter) generateMapProperty(t *types.Type) error {
if keyType.Name.Name != "string" {
return fmt.Errorf("map with non-string keys are not supported by OpenAPI in %v", t)
}
g.Do("Type: []string{\"object\"},\n", nil)
g.Do("AdditionalProperties: &spec.SchemaOrBool{\nAllows: true,\nSchema: &spec.Schema{\nSchemaProps: spec.SchemaProps{\n", nil)
typeString, format := openapi.GetOpenAPITypeFormat(elemType.String())
if err := g.generateDefault(t.Elem.CommentLines, t.Elem, false); err != nil {
return err
}
typeString, format := openapi.OpenAPITypeFormat(elemType.String())
if typeString != "" {
g.generateSimpleProperty(typeString, format)
g.Do("},\n},\n},\n", nil)
@@ -595,7 +725,13 @@ func (g openAPITypeWriter) generateMapProperty(t *types.Type) error {
case types.Struct:
g.generateReferenceProperty(elemType)
case types.Slice, types.Array:
g.generateSliceProperty(elemType)
if err := g.generateSliceProperty(elemType); err != nil {
return err
}
case types.Map:
if err := g.generateMapProperty(elemType); err != nil {
return err
}
default:
return fmt.Errorf("map Element kind %v is not supported in %v", elemType.Kind, t.Name)
}
@@ -607,7 +743,10 @@ func (g openAPITypeWriter) generateSliceProperty(t *types.Type) error {
elemType := resolveAliasAndPtrType(t.Elem)
g.Do("Type: []string{\"array\"},\n", nil)
g.Do("Items: &spec.SchemaOrArray{\nSchema: &spec.Schema{\nSchemaProps: spec.SchemaProps{\n", nil)
typeString, format := openapi.GetOpenAPITypeFormat(elemType.String())
if err := g.generateDefault(t.Elem.CommentLines, t.Elem, false); err != nil {
return err
}
typeString, format := openapi.OpenAPITypeFormat(elemType.String())
if typeString != "" {
g.generateSimpleProperty(typeString, format)
g.Do("},\n},\n},\n", nil)
@@ -619,7 +758,13 @@ func (g openAPITypeWriter) generateSliceProperty(t *types.Type) error {
case types.Struct:
g.generateReferenceProperty(elemType)
case types.Slice, types.Array:
g.generateSliceProperty(elemType)
if err := g.generateSliceProperty(elemType); err != nil {
return err
}
case types.Map:
if err := g.generateMapProperty(elemType); err != nil {
return err
}
default:
return fmt.Errorf("slice Element kind %v is not supported in %v", elemType.Kind, t)
}

View File

@@ -0,0 +1,53 @@
package rules
import (
"k8s.io/gengo/types"
)
const ListTypeIDLTag = "listType"
// ListTypeMissing implements APIRule interface.
// A list type is required for inlined list.
type ListTypeMissing struct{}
// Name returns the name of APIRule
func (l *ListTypeMissing) Name() string {
return "list_type_missing"
}
// Validate evaluates API rule on type t and returns a list of field names in
// the type that violate the rule. Empty field name [""] implies the entire
// type violates the rule.
func (l *ListTypeMissing) Validate(t *types.Type) ([]string, error) {
fields := make([]string, 0)
switch t.Kind {
case types.Struct:
for _, m := range t.Members {
hasListType := types.ExtractCommentTags("+", m.CommentLines)[ListTypeIDLTag] != nil
if m.Name == "Items" && m.Type.Kind == types.Slice && hasNamedMember(t, "ListMeta") {
if hasListType {
fields = append(fields, m.Name)
}
continue
}
if m.Type.Kind == types.Slice && !hasListType {
fields = append(fields, m.Name)
continue
}
}
}
return fields, nil
}
func hasNamedMember(t *types.Type, name string) bool {
for _, m := range t.Members {
if m.Name == name {
return true
}
}
return false
}

View File

@@ -155,7 +155,7 @@ func namesMatch(goName, jsonName string) bool {
return true
}
// isCaptical returns true if one character is capital
// isCapital returns true if one character is capital
func isCapital(b byte) bool {
return b >= 'A' && b <= 'Z'
}

207
vendor/k8s.io/kube-openapi/pkg/generators/union.go generated vendored Normal file
View File

@@ -0,0 +1,207 @@
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package generators
import (
"fmt"
"sort"
"k8s.io/gengo/types"
)
const tagUnionMember = "union"
const tagUnionDeprecated = "unionDeprecated"
const tagUnionDiscriminator = "unionDiscriminator"
type union struct {
discriminator string
fieldsToDiscriminated map[string]string
}
// emit prints the union, can be called on a nil union (emits nothing)
func (u *union) emit(g openAPITypeWriter) {
if u == nil {
return
}
g.Do("map[string]interface{}{\n", nil)
if u.discriminator != "" {
g.Do("\"discriminator\": \"$.$\",\n", u.discriminator)
}
g.Do("\"fields-to-discriminateBy\": map[string]interface{}{\n", nil)
keys := []string{}
for field := range u.fieldsToDiscriminated {
keys = append(keys, field)
}
sort.Strings(keys)
for _, field := range keys {
g.Do("\"$.$\": ", field)
g.Do("\"$.$\",\n", u.fieldsToDiscriminated[field])
}
g.Do("},\n", nil)
g.Do("},\n", nil)
}
// Sets the discriminator if it's not set yet, otherwise return an error
func (u *union) setDiscriminator(value string) []error {
errors := []error{}
if u.discriminator != "" {
errors = append(errors, fmt.Errorf("at least two discriminators found: %v and %v", value, u.discriminator))
}
u.discriminator = value
return errors
}
// Add a new member to the union
func (u *union) addMember(jsonName, variableName string) {
if _, ok := u.fieldsToDiscriminated[jsonName]; ok {
panic(fmt.Errorf("same field (%v) found multiple times", jsonName))
}
u.fieldsToDiscriminated[jsonName] = variableName
}
// Makes sure that the union is valid, specifically looking for re-used discriminated
func (u *union) isValid() []error {
errors := []error{}
// Case 1: discriminator but no fields
if u.discriminator != "" && len(u.fieldsToDiscriminated) == 0 {
errors = append(errors, fmt.Errorf("discriminator set with no fields in union"))
}
// Case 2: two fields have the same discriminated value
discriminated := map[string]struct{}{}
for _, d := range u.fieldsToDiscriminated {
if _, ok := discriminated[d]; ok {
errors = append(errors, fmt.Errorf("discriminated value is used twice: %v", d))
}
discriminated[d] = struct{}{}
}
// Case 3: a field is both discriminator AND part of the union
if u.discriminator != "" {
if _, ok := u.fieldsToDiscriminated[u.discriminator]; ok {
errors = append(errors, fmt.Errorf("%v can't be both discriminator and part of the union", u.discriminator))
}
}
return errors
}
// Find unions either directly on the members (or inlined members, not
// going across types) or on the type itself, or on embedded types.
func parseUnions(t *types.Type) ([]union, []error) {
errors := []error{}
unions := []union{}
su, err := parseUnionStruct(t)
if su != nil {
unions = append(unions, *su)
}
errors = append(errors, err...)
eu, err := parseEmbeddedUnion(t)
unions = append(unions, eu...)
errors = append(errors, err...)
mu, err := parseUnionMembers(t)
if mu != nil {
unions = append(unions, *mu)
}
errors = append(errors, err...)
return unions, errors
}
// Find unions in embedded types, unions shouldn't go across types.
func parseEmbeddedUnion(t *types.Type) ([]union, []error) {
errors := []error{}
unions := []union{}
for _, m := range t.Members {
if hasOpenAPITagValue(m.CommentLines, tagValueFalse) {
continue
}
if !shouldInlineMembers(&m) {
continue
}
u, err := parseUnions(m.Type)
unions = append(unions, u...)
errors = append(errors, err...)
}
return unions, errors
}
// Look for union tag on a struct, and then include all the fields
// (except the discriminator if there is one). The struct shouldn't have
// embedded types.
func parseUnionStruct(t *types.Type) (*union, []error) {
errors := []error{}
if types.ExtractCommentTags("+", t.CommentLines)[tagUnionMember] == nil {
return nil, nil
}
u := &union{fieldsToDiscriminated: map[string]string{}}
for _, m := range t.Members {
jsonName := getReferableName(&m)
if jsonName == "" {
continue
}
if shouldInlineMembers(&m) {
errors = append(errors, fmt.Errorf("union structures can't have embedded fields: %v.%v", t.Name, m.Name))
continue
}
if types.ExtractCommentTags("+", m.CommentLines)[tagUnionDeprecated] != nil {
errors = append(errors, fmt.Errorf("union struct can't have unionDeprecated members: %v.%v", t.Name, m.Name))
continue
}
if types.ExtractCommentTags("+", m.CommentLines)[tagUnionDiscriminator] != nil {
errors = append(errors, u.setDiscriminator(jsonName)...)
} else {
if !hasOptionalTag(&m) {
errors = append(errors, fmt.Errorf("union members must be optional: %v.%v", t.Name, m.Name))
}
u.addMember(jsonName, m.Name)
}
}
return u, errors
}
// Find unions specifically on members.
func parseUnionMembers(t *types.Type) (*union, []error) {
errors := []error{}
u := &union{fieldsToDiscriminated: map[string]string{}}
for _, m := range t.Members {
jsonName := getReferableName(&m)
if jsonName == "" {
continue
}
if shouldInlineMembers(&m) {
continue
}
if types.ExtractCommentTags("+", m.CommentLines)[tagUnionDiscriminator] != nil {
errors = append(errors, u.setDiscriminator(jsonName)...)
}
if types.ExtractCommentTags("+", m.CommentLines)[tagUnionMember] != nil {
errors = append(errors, fmt.Errorf("union tag is not accepted on struct members: %v.%v", t.Name, m.Name))
continue
}
if types.ExtractCommentTags("+", m.CommentLines)[tagUnionDeprecated] != nil {
if !hasOptionalTag(&m) {
errors = append(errors, fmt.Errorf("union members must be optional: %v.%v", t.Name, m.Name))
}
u.addMember(jsonName, m.Name)
}
}
if len(u.fieldsToDiscriminated) == 0 {
return nil, nil
}
return u, append(errors, u.isValid()...)
}

2
vendor/k8s.io/kube-openapi/pkg/util/proto/OWNERS generated vendored Normal file
View File

@@ -0,0 +1,2 @@
approvers:
- apelisse

View File

@@ -21,7 +21,7 @@ import (
"sort"
"strings"
"github.com/googleapis/gnostic/OpenAPIv2"
openapi_v2 "github.com/googleapis/gnostic/openapiv2"
"gopkg.in/yaml.v2"
)
@@ -92,13 +92,16 @@ func NewOpenAPIData(doc *openapi_v2.Document) (Models, error) {
// We believe the schema is a reference, verify that and returns a new
// Schema
func (d *Definitions) parseReference(s *openapi_v2.Schema, path *Path) (Schema, error) {
// TODO(wrong): a schema with a $ref can have properties. We can ignore them (would be incomplete), but we cannot return an error.
if len(s.GetProperties().GetAdditionalProperties()) > 0 {
return nil, newSchemaError(path, "unallowed embedded type definition")
}
// TODO(wrong): a schema with a $ref can have a type. We can ignore it (would be incomplete), but we cannot return an error.
if len(s.GetType().GetValue()) > 0 {
return nil, newSchemaError(path, "definition reference can't have a type")
}
// TODO(wrong): $refs outside of the definitions are completely valid. We can ignore them (would be incomplete), but we cannot return an error.
if !strings.HasPrefix(s.GetXRef(), "#/definitions/") {
return nil, newSchemaError(path, "unallowed reference to non-definition %q", s.GetXRef())
}
@@ -106,19 +109,39 @@ func (d *Definitions) parseReference(s *openapi_v2.Schema, path *Path) (Schema,
if _, ok := d.models[reference]; !ok {
return nil, newSchemaError(path, "unknown model in reference: %q", reference)
}
base, err := d.parseBaseSchema(s, path)
if err != nil {
return nil, err
}
return &Ref{
BaseSchema: d.parseBaseSchema(s, path),
BaseSchema: base,
reference: reference,
definitions: d,
}, nil
}
func (d *Definitions) parseBaseSchema(s *openapi_v2.Schema, path *Path) BaseSchema {
func parseDefault(def *openapi_v2.Any) (interface{}, error) {
if def == nil {
return nil, nil
}
var i interface{}
if err := yaml.Unmarshal([]byte(def.Yaml), &i); err != nil {
return nil, err
}
return i, nil
}
func (d *Definitions) parseBaseSchema(s *openapi_v2.Schema, path *Path) (BaseSchema, error) {
def, err := parseDefault(s.GetDefault())
if err != nil {
return BaseSchema{}, err
}
return BaseSchema{
Description: s.GetDescription(),
Default: def,
Extensions: VendorExtensionToMap(s.GetVendorExtension()),
Path: *path,
}
}, nil
}
// We believe the schema is a map, verify and return a new schema
@@ -127,9 +150,14 @@ func (d *Definitions) parseMap(s *openapi_v2.Schema, path *Path) (Schema, error)
return nil, newSchemaError(path, "invalid object type")
}
var sub Schema
// TODO(incomplete): this misses the boolean case as AdditionalProperties is a bool+schema sum type.
if s.GetAdditionalProperties().GetSchema() == nil {
base, err := d.parseBaseSchema(s, path)
if err != nil {
return nil, err
}
sub = &Arbitrary{
BaseSchema: d.parseBaseSchema(s, path),
BaseSchema: base,
}
} else {
var err error
@@ -138,8 +166,12 @@ func (d *Definitions) parseMap(s *openapi_v2.Schema, path *Path) (Schema, error)
return nil, err
}
}
base, err := d.parseBaseSchema(s, path)
if err != nil {
return nil, err
}
return &Map{
BaseSchema: d.parseBaseSchema(s, path),
BaseSchema: base,
SubType: sub,
}, nil
}
@@ -157,11 +189,16 @@ func (d *Definitions) parsePrimitive(s *openapi_v2.Schema, path *Path) (Schema,
case Number: // do nothing
case Integer: // do nothing
case Boolean: // do nothing
// TODO(wrong): this misses "null". Would skip the null case (would be incomplete), but we cannot return an error.
default:
return nil, newSchemaError(path, "Unknown primitive type: %q", t)
}
base, err := d.parseBaseSchema(s, path)
if err != nil {
return nil, err
}
return &Primitive{
BaseSchema: d.parseBaseSchema(s, path),
BaseSchema: base,
Type: t,
Format: s.GetFormat(),
}, nil
@@ -175,14 +212,20 @@ func (d *Definitions) parseArray(s *openapi_v2.Schema, path *Path) (Schema, erro
return nil, newSchemaError(path, `array should have type "array"`)
}
if len(s.GetItems().GetSchema()) != 1 {
// TODO(wrong): Items can have multiple elements. We can ignore Items then (would be incomplete), but we cannot return an error.
// TODO(wrong): "type: array" witohut any items at all is completely valid.
return nil, newSchemaError(path, "array should have exactly one sub-item")
}
sub, err := d.ParseSchema(s.GetItems().GetSchema()[0], path)
if err != nil {
return nil, err
}
base, err := d.parseBaseSchema(s, path)
if err != nil {
return nil, err
}
return &Array{
BaseSchema: d.parseBaseSchema(s, path),
BaseSchema: base,
SubType: sub,
}, nil
}
@@ -209,8 +252,12 @@ func (d *Definitions) parseKind(s *openapi_v2.Schema, path *Path) (Schema, error
fieldOrder = append(fieldOrder, name)
}
base, err := d.parseBaseSchema(s, path)
if err != nil {
return nil, err
}
return &Kind{
BaseSchema: d.parseBaseSchema(s, path),
BaseSchema: base,
RequiredFields: s.GetRequired(),
Fields: fields,
FieldOrder: fieldOrder,
@@ -218,8 +265,12 @@ func (d *Definitions) parseKind(s *openapi_v2.Schema, path *Path) (Schema, error
}
func (d *Definitions) parseArbitrary(s *openapi_v2.Schema, path *Path) (Schema, error) {
base, err := d.parseBaseSchema(s, path)
if err != nil {
return nil, err
}
return &Arbitrary{
BaseSchema: d.parseBaseSchema(s, path),
BaseSchema: base,
}, nil
}
@@ -227,6 +278,8 @@ func (d *Definitions) parseArbitrary(s *openapi_v2.Schema, path *Path) (Schema,
// this function is public, it doesn't leak through the interface.
func (d *Definitions) ParseSchema(s *openapi_v2.Schema, path *Path) (Schema, error) {
if s.GetXRef() != "" {
// TODO(incomplete): ignoring the rest of s is wrong. As long as there are no conflict, everything from s must be considered
// Reference: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#path-item-object
return d.parseReference(s, path)
}
objectTypes := s.GetType().GetValue()
@@ -234,11 +287,15 @@ func (d *Definitions) ParseSchema(s *openapi_v2.Schema, path *Path) (Schema, err
case 0:
// in the OpenAPI schema served by older k8s versions, object definitions created from structs did not include
// the type:object property (they only included the "properties" property), so we need to handle this case
// TODO: validate that we ever published empty, non-nil properties. JSON roundtripping nils them.
if s.GetProperties() != nil {
// TODO(wrong): when verifying a non-object later against this, it will be rejected as invalid type.
// TODO(CRD validation schema publishing): we have to filter properties (empty or not) if type=object is not given
return d.parseKind(s, path)
} else {
// Definition has no type and no properties. Treat it as an arbitrary value
// TODO: what if it has additionalProperties or patternProperties?
// TODO(incomplete): what if it has additionalProperties=false or patternProperties?
// ANSWER: parseArbitrary is less strict than it has to be with patternProperties (which is ignored). So this is correct (of course not complete).
return d.parseArbitrary(s, path)
}
case 1:
@@ -256,6 +313,8 @@ func (d *Definitions) ParseSchema(s *openapi_v2.Schema, path *Path) (Schema, err
return d.parsePrimitive(s, path)
default:
// the OpenAPI generator never generates (nor it ever did in the past) OpenAPI type definitions with multiple types
// TODO(wrong): this is rejecting a completely valid OpenAPI spec
// TODO(CRD validation schema publishing): filter these out
return nil, newSchemaError(path, "definitions with multiple types aren't supported")
}
}

View File

@@ -77,6 +77,8 @@ type Schema interface {
GetPath() *Path
// Describes the field.
GetDescription() string
// Default for that schema.
GetDefault() interface{}
// Returns type extensions.
GetExtensions() map[string]interface{}
}
@@ -129,6 +131,7 @@ func (p *Path) FieldPath(field string) Path {
type BaseSchema struct {
Description string
Extensions map[string]interface{}
Default interface{}
Path Path
}
@@ -141,6 +144,10 @@ func (b *BaseSchema) GetExtensions() map[string]interface{} {
return b.Extensions
}
func (b *BaseSchema) GetDefault() interface{} {
return b.Default
}
func (b *BaseSchema) GetPath() *Path {
return &b.Path
}