From 45c4311b4e5926ef00ebcccff8e60716bc844b6f Mon Sep 17 00:00:00 2001 From: David Eads Date: Thu, 25 Aug 2022 16:29:08 -0400 Subject: [PATCH] make applyconfiguration-gen skip generation for types that have generated clients and lack objectmeta --- .../generators/applyconfiguration.go | 45 +++---------------- .../generators/packages.go | 22 ++++++++- 2 files changed, 25 insertions(+), 42 deletions(-) diff --git a/staging/src/k8s.io/code-generator/cmd/applyconfiguration-gen/generators/applyconfiguration.go b/staging/src/k8s.io/code-generator/cmd/applyconfiguration-gen/generators/applyconfiguration.go index 5dc3b8f2101d..8e02bb233bc4 100644 --- a/staging/src/k8s.io/code-generator/cmd/applyconfiguration-gen/generators/applyconfiguration.go +++ b/staging/src/k8s.io/code-generator/cmd/applyconfiguration-gen/generators/applyconfiguration.go @@ -97,12 +97,9 @@ func (g *applyConfigurationGenerator) GenerateType(c *generator.Context, t *type g.generateStruct(sw, typeParams) if typeParams.Tags.GenerateClient { - switch { - case !hasObjectMetaField(t): - sw.Do(clientgenTypeConstructorWithoutObjectMeta, typeParams) - case typeParams.Tags.NonNamespaced: + if typeParams.Tags.NonNamespaced { sw.Do(clientgenTypeConstructorNonNamespaced, typeParams) - default: + } else { sw.Do(clientgenTypeConstructorNamespaced, typeParams) } if typeParams.OpenAPIType != nil { @@ -128,15 +125,6 @@ func hasTypeMetaField(t *types.Type) bool { 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 { if objectMeta.Name == t.Name && member.Name == "ManagedFields" { 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 = ` // $.ApplyConfig.ApplyConfiguration|public$ constructs an declarative configuration of the $.ApplyConfig.Type|public$ type for use with // apply. @@ -425,18 +402,7 @@ func Extract$.ApplyConfig.Type|public$Status($.Struct|private$ *$.Struct|raw$, f } `, typeParams) } - if !hasObjectMetaField(typeParams.Struct) { - 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(` + 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) @@ -445,9 +411,8 @@ func extract$.ApplyConfig.Type|public$($.Struct|private$ *$.Struct|raw$, fieldMa } b.WithName($.Struct|private$.Name) `, typeParams) - if !typeParams.Tags.NonNamespaced { - sw.Do("b.WithNamespace($.Struct|private$.Namespace)\n", typeParams) - } + if !typeParams.Tags.NonNamespaced { + sw.Do("b.WithNamespace($.Struct|private$.Namespace)\n", typeParams) } sw.Do(` b.WithKind("$.ApplyConfig.Type|singularKind$") diff --git a/staging/src/k8s.io/code-generator/cmd/applyconfiguration-gen/generators/packages.go b/staging/src/k8s.io/code-generator/cmd/applyconfiguration-gen/generators/packages.go index ec716d529be2..bfeffda593d8 100644 --- a/staging/src/k8s.io/code-generator/cmd/applyconfiguration-gen/generators/packages.go +++ b/staging/src/k8s.io/code-generator/cmd/applyconfiguration-gen/generators/packages.go @@ -82,6 +82,13 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat var toGenerate []applyConfig 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 { toGenerate = append(toGenerate, applyConfig{ Type: t, @@ -237,9 +244,11 @@ func packageTypesForInputDirs(context *generator.Context, inputDirs []string, ou klog.Warningf("Skipping internal package: %s", p.Path) 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) - pkg := filepath.Join(outputPath, strings.ToLower(gvPackageString)) pkgTypes[pkg] = p } @@ -277,3 +286,12 @@ func isInternal(m types.Member) bool { _, ok := lookupJSONTags(m) 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 +}