Update go dependencies for kubernetes 1.16.0

This commit is contained in:
Manuel Alejandro de Brito Fontes 2019-09-19 11:01:00 -03:00
parent 280920980d
commit d7b530cb0a
No known key found for this signature in database
GPG key ID: 786136016A8BA02A
510 changed files with 107206 additions and 52551 deletions

View file

@ -139,6 +139,7 @@ func newAPILinter() *apiLinter {
rules: []APIRule{
&rules.NamesMatch{},
&rules.OmitEmptyMatchCase{},
&rules.ListTypeMissing{},
},
}
}

View file

@ -242,6 +242,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" {
@ -304,9 +314,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 +339,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)
@ -397,11 +435,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,13 +461,13 @@ 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)
@ -438,6 +483,13 @@ func (g openAPITypeWriter) emitExtensions(extensions []extension) {
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)
}
@ -595,7 +647,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)
}
@ -619,7 +677,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,36 @@
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 {
if m.Type.Kind == types.Slice && types.ExtractCommentTags("+", m.CommentLines)[ListTypeIDLTag] == nil {
fields = append(fields, m.Name)
continue
}
}
}
return fields, nil
}

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()...)
}