Skip to content

Commit

Permalink
make applyconfiguration-gen skip generation for types that have gener…
Browse files Browse the repository at this point in the history
…ated clients and lack objectmeta
  • Loading branch information
deads2k committed Aug 26, 2022
1 parent 06497aa commit 45c4311
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 42 deletions.
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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$")
Expand Down
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

0 comments on commit 45c4311

Please sign in to comment.