Merge pull request #107834 from SataQiu/fix-applyconfiguration

code-generator: fix the bug that ApplyConfiguration constructor missing WithKind/WithAPIVersion methods
This commit is contained in:
Kubernetes Prow Robot 2022-02-08 09:59:14 -08:00 committed by GitHub
commit 20a2a4c1af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 5 deletions

View File

@ -37,7 +37,10 @@ type ScaleApplyConfiguration struct {
// ScaleApplyConfiguration constructs an declarative configuration of the Scale type for use with
// apply.
func Scale() *ScaleApplyConfiguration {
return &ScaleApplyConfiguration{}
b := &ScaleApplyConfiguration{}
b.WithKind("Scale")
b.WithAPIVersion("apps/v1beta2")
return b
}
// WithKind sets the Kind field in the declarative configuration to the given value

View File

@ -36,7 +36,10 @@ type ScaleApplyConfiguration struct {
// ScaleApplyConfiguration constructs an declarative configuration of the Scale type for use with
// apply.
func Scale() *ScaleApplyConfiguration {
return &ScaleApplyConfiguration{}
b := &ScaleApplyConfiguration{}
b.WithKind("Scale")
b.WithAPIVersion("autoscaling/v1")
return b
}
// WithKind sets the Kind field in the declarative configuration to the given value

View File

@ -37,7 +37,10 @@ type ScaleApplyConfiguration struct {
// ScaleApplyConfiguration constructs an declarative configuration of the Scale type for use with
// apply.
func Scale() *ScaleApplyConfiguration {
return &ScaleApplyConfiguration{}
b := &ScaleApplyConfiguration{}
b.WithKind("Scale")
b.WithAPIVersion("extensions/v1beta1")
return b
}
// WithKind sets the Kind field in the declarative configuration to the given value

View File

@ -36,7 +36,10 @@ type DeleteOptionsApplyConfiguration struct {
// DeleteOptionsApplyConfiguration constructs an declarative configuration of the DeleteOptions type for use with
// apply.
func DeleteOptions() *DeleteOptionsApplyConfiguration {
return &DeleteOptionsApplyConfiguration{}
b := &DeleteOptionsApplyConfiguration{}
b.WithKind("DeleteOptions")
b.WithAPIVersion("meta.k8s.io/v1")
return b
}
// WithKind sets the Kind field in the declarative configuration to the given value

View File

@ -105,12 +105,25 @@ func (g *applyConfigurationGenerator) GenerateType(c *generator.Context, t *type
g.generateClientgenExtract(sw, typeParams, !typeParams.Tags.NoStatus)
}
} else {
sw.Do(constructor, typeParams)
if hasTypeMetaField(t) {
sw.Do(constructorWithTypeMeta, typeParams)
} else {
sw.Do(constructor, typeParams)
}
}
g.generateWithFuncs(t, typeParams, sw, nil)
return sw.Error()
}
func hasTypeMetaField(t *types.Type) bool {
for _, member := range t.Members {
if typeMeta.Name == member.Type.Name {
return true
}
}
return false
}
func blocklisted(t *types.Type, member types.Member) bool {
if objectMeta.Name == t.Name && member.Name == "ManagedFields" {
return true
@ -309,6 +322,17 @@ func $.ApplyConfig.Type|public$(name string) *$.ApplyConfig.ApplyConfiguration|p
}
`
var constructorWithTypeMeta = `
// $.ApplyConfig.ApplyConfiguration|public$ constructs an declarative configuration of the $.ApplyConfig.Type|public$ type for use with
// apply.
func $.ApplyConfig.Type|public$() *$.ApplyConfig.ApplyConfiguration|public$ {
b := &$.ApplyConfig.ApplyConfiguration|public${}
b.WithKind("$.ApplyConfig.Type|singularKind$")
b.WithAPIVersion("$.APIVersion$")
return b
}
`
var constructor = `
// $.ApplyConfig.ApplyConfiguration|public$ constructs an declarative configuration of the $.ApplyConfig.Type|public$ type for use with
// apply.

View File

@ -21,6 +21,7 @@ import "k8s.io/gengo/types"
var (
applyConfiguration = types.Ref("k8s.io/apimachinery/pkg/runtime", "ApplyConfiguration")
groupVersionKind = types.Ref("k8s.io/apimachinery/pkg/runtime/schema", "GroupVersionKind")
typeMeta = types.Ref("k8s.io/apimachinery/pkg/apis/meta/v1", "TypeMeta")
objectMeta = types.Ref("k8s.io/apimachinery/pkg/apis/meta/v1", "ObjectMeta")
rawExtension = types.Ref("k8s.io/apimachinery/pkg/runtime", "RawExtension")
unknown = types.Ref("k8s.io/apimachinery/pkg/runtime", "Unknown")