make applyconfiguration-gen skip generation for types that have generated clients and lack objectmeta

This commit is contained in:
David Eads 2022-08-25 16:29:08 -04:00
parent 06497aa13c
commit 45c4311b4e
2 changed files with 25 additions and 42 deletions

View File

@ -97,12 +97,9 @@ func (g *applyConfigurationGenerator) GenerateType(c *generator.Context, t *type
g.generateStruct(sw, typeParams) g.generateStruct(sw, typeParams)
if typeParams.Tags.GenerateClient { if typeParams.Tags.GenerateClient {
switch { if typeParams.Tags.NonNamespaced {
case !hasObjectMetaField(t):
sw.Do(clientgenTypeConstructorWithoutObjectMeta, typeParams)
case typeParams.Tags.NonNamespaced:
sw.Do(clientgenTypeConstructorNonNamespaced, typeParams) sw.Do(clientgenTypeConstructorNonNamespaced, typeParams)
default: } else {
sw.Do(clientgenTypeConstructorNamespaced, typeParams) sw.Do(clientgenTypeConstructorNamespaced, typeParams)
} }
if typeParams.OpenAPIType != nil { if typeParams.OpenAPIType != nil {
@ -128,15 +125,6 @@ func hasTypeMetaField(t *types.Type) bool {
return false return false
} }
func hasObjectMetaField(t *types.Type) bool {
for _, member := range t.Members {
if objectMeta.Name == member.Type.Name && member.Embedded {
return true
}
}
return false
}
func blocklisted(t *types.Type, member types.Member) bool { func blocklisted(t *types.Type, member types.Member) bool {
if objectMeta.Name == t.Name && member.Name == "ManagedFields" { if objectMeta.Name == t.Name && member.Name == "ManagedFields" {
return true return true
@ -369,17 +357,6 @@ func $.ApplyConfig.Type|public$(name string) *$.ApplyConfig.ApplyConfiguration|p
} }
` `
var clientgenTypeConstructorWithoutObjectMeta = `
// $.ApplyConfig.Type|public$ constructs an declarative configuration of the $.ApplyConfig.Type|public$ type for use with
// apply.
func $.ApplyConfig.Type|public$(name string) *$.ApplyConfig.ApplyConfiguration|public$ {
b := &$.ApplyConfig.ApplyConfiguration|public${}
b.WithKind("$.ApplyConfig.Type|singularKind$")
b.WithAPIVersion("$.APIVersion$")
return b
}
`
var constructorWithTypeMeta = ` var constructorWithTypeMeta = `
// $.ApplyConfig.ApplyConfiguration|public$ constructs an declarative configuration of the $.ApplyConfig.Type|public$ type for use with // $.ApplyConfig.ApplyConfiguration|public$ constructs an declarative configuration of the $.ApplyConfig.Type|public$ type for use with
// apply. // apply.
@ -425,18 +402,7 @@ func Extract$.ApplyConfig.Type|public$Status($.Struct|private$ *$.Struct|raw$, f
} }
`, typeParams) `, typeParams)
} }
if !hasObjectMetaField(typeParams.Struct) { sw.Do(`
sw.Do(`
func extract$.ApplyConfig.Type|public$($.Struct|private$ *$.Struct|raw$, fieldManager string, subresource string) (*$.ApplyConfig.ApplyConfiguration|public$, error) {
b := &$.ApplyConfig.ApplyConfiguration|public${}
err := $.ExtractInto|raw$($.Struct|private$, $.ParserFunc|raw$().Type("$.OpenAPIType$"), fieldManager, b, subresource)
if err != nil {
return nil, err
}
`, typeParams)
} else { // it has objectMeta
sw.Do(`
func extract$.ApplyConfig.Type|public$($.Struct|private$ *$.Struct|raw$, fieldManager string, subresource string) (*$.ApplyConfig.ApplyConfiguration|public$, error) { func extract$.ApplyConfig.Type|public$($.Struct|private$ *$.Struct|raw$, fieldManager string, subresource string) (*$.ApplyConfig.ApplyConfiguration|public$, error) {
b := &$.ApplyConfig.ApplyConfiguration|public${} b := &$.ApplyConfig.ApplyConfiguration|public${}
err := $.ExtractInto|raw$($.Struct|private$, $.ParserFunc|raw$().Type("$.OpenAPIType$"), fieldManager, b, subresource) err := $.ExtractInto|raw$($.Struct|private$, $.ParserFunc|raw$().Type("$.OpenAPIType$"), fieldManager, b, subresource)
@ -445,9 +411,8 @@ func extract$.ApplyConfig.Type|public$($.Struct|private$ *$.Struct|raw$, fieldMa
} }
b.WithName($.Struct|private$.Name) b.WithName($.Struct|private$.Name)
`, typeParams) `, typeParams)
if !typeParams.Tags.NonNamespaced { if !typeParams.Tags.NonNamespaced {
sw.Do("b.WithNamespace($.Struct|private$.Namespace)\n", typeParams) sw.Do("b.WithNamespace($.Struct|private$.Namespace)\n", typeParams)
}
} }
sw.Do(` sw.Do(`
b.WithKind("$.ApplyConfig.Type|singularKind$") b.WithKind("$.ApplyConfig.Type|singularKind$")

View File

@ -82,6 +82,13 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
var toGenerate []applyConfig var toGenerate []applyConfig
for _, t := range p.Types { for _, t := range p.Types {
// If we don't have an ObjectMeta field, we lack the information required to make the Apply or ApplyStatus call
// to the kube-apiserver, so we don't need to generate the type at all
clientTags := genclientTags(t)
if clientTags.GenerateClient && !hasObjectMetaField(t) {
klog.V(5).Infof("skipping type %v because does not have ObjectMeta", t)
continue
}
if typePkg, ok := refs[t.Name]; ok { if typePkg, ok := refs[t.Name]; ok {
toGenerate = append(toGenerate, applyConfig{ toGenerate = append(toGenerate, applyConfig{
Type: t, Type: t,
@ -237,9 +244,11 @@ func packageTypesForInputDirs(context *generator.Context, inputDirs []string, ou
klog.Warningf("Skipping internal package: %s", p.Path) klog.Warningf("Skipping internal package: %s", p.Path)
continue continue
} }
// this is how the client generator finds the package we are creating. It uses the API package name, not the group name. // This is how the client generator finds the package we are creating. It uses the API package name, not the group name.
// This matches the approach of the client-gen, so the two generator can work together.
// For example, if openshift/api/cloudnetwork/v1 contains an apigroup cloud.network.openshift.io, the client-gen
// builds a package called cloudnetwork/v1 to contain it. This change makes the applyconfiguration-gen use the same.
_, gvPackageString := util.ParsePathGroupVersion(p.Path) _, gvPackageString := util.ParsePathGroupVersion(p.Path)
pkg := filepath.Join(outputPath, strings.ToLower(gvPackageString)) pkg := filepath.Join(outputPath, strings.ToLower(gvPackageString))
pkgTypes[pkg] = p pkgTypes[pkg] = p
} }
@ -277,3 +286,12 @@ func isInternal(m types.Member) bool {
_, ok := lookupJSONTags(m) _, ok := lookupJSONTags(m)
return !ok return !ok
} }
func hasObjectMetaField(t *types.Type) bool {
for _, member := range t.Members {
if objectMeta.Name == member.Type.Name && member.Embedded {
return true
}
}
return false
}