Skip to content

Commit 473f3ca

Browse files
committed
(feat) add an option for applyconfiguration
1 parent 8d51a9f commit 473f3ca

File tree

6 files changed

+27
-2
lines changed

6 files changed

+27
-2
lines changed

pkg/cli/alpha/internal/generate.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,11 @@ func getAPIResourceFlags(res resource.Resource) []string {
521521
} else {
522522
args = append(args, "--namespaced=false")
523523
}
524+
if res.API.GenerateApplyConfiguration {
525+
args = append(args, "--generate-apply-configuration")
526+
} else {
527+
args = append(args, "--generate-apply-configuration=false")
528+
}
524529
}
525530
if res.Controller {
526531
args = append(args, "--controller")

pkg/model/resource/api.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ type API struct {
2727

2828
// Namespaced is true if the API is namespaced.
2929
Namespaced bool `json:"namespaced,omitempty"`
30+
31+
// GenerateApplyConfiguration indicates whether to generate applyconfiguration code for the resource.
32+
GenerateApplyConfiguration bool `json:"generateApplyConfiguration,omitempty"`
3033
}
3134

3235
// Validate checks that the API is valid.
@@ -65,6 +68,9 @@ func (api *API) Update(other *API) error {
6568
// Update the namespace.
6669
api.Namespaced = api.Namespaced || other.Namespaced
6770

71+
// Update the generate apply configuration flag.
72+
api.GenerateApplyConfiguration = api.GenerateApplyConfiguration || other.GenerateApplyConfiguration
73+
6874
return nil
6975
}
7076

pkg/model/resource/resource.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ func (r Resource) IsRegularPlural() bool {
140140
return r.Plural == RegularPlural(r.Kind)
141141
}
142142

143+
// HasApplyConfiguration returns true if the resource has applyconfiguration generation enabled.
144+
func (r Resource) HasApplyConfiguration() bool {
145+
return r.API != nil && r.API.GenerateApplyConfiguration
146+
}
147+
143148
// Copy returns a deep copy of the Resource that can be safely modified without affecting the original.
144149
func (r Resource) Copy() Resource {
145150
// As this function doesn't use a pointer receiver, r is already a shallow copy.

pkg/plugins/golang/options.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ type Options struct {
6868
// Namespaced is true if the resource should be namespaced.
6969
Namespaced bool
7070

71+
// GenerateApplyConfiguration indicates whether to generate applyconfiguration code for the resource.
72+
GenerateApplyConfiguration bool
73+
7174
// Flags that define which parts should be scaffolded
7275
DoAPI bool
7376
DoController bool
@@ -95,8 +98,9 @@ func (opts Options) UpdateResource(res *resource.Resource, c config.Config) {
9598
res.Path = resource.APIPackagePath(c.GetRepository(), res.Group, res.Version, c.IsMultiGroup())
9699

97100
res.API = &resource.API{
98-
CRDVersion: "v1",
99-
Namespaced: opts.Namespaced,
101+
CRDVersion: "v1",
102+
Namespaced: opts.Namespaced,
103+
GenerateApplyConfiguration: opts.GenerateApplyConfiguration,
100104
}
101105
}
102106

pkg/plugins/golang/v4/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func (p *createAPISubcommand) BindFlags(fs *pflag.FlagSet) {
104104
"if set, generate the resource without prompting the user")
105105
p.resourceFlag = fs.Lookup("resource")
106106
fs.BoolVar(&p.options.Namespaced, "namespaced", true, "resource is namespaced")
107+
fs.BoolVar(&p.options.GenerateApplyConfiguration, "generate-apply-configuration", false, "if set, generate applyconfiguration code for the resource")
107108

108109
fs.BoolVar(&p.options.DoController, "controller", true,
109110
"if set, generate the controller without prompting the user")

pkg/plugins/golang/v4/scaffolds/internal/templates/api/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ func (f *Types) SetTemplateDefaults() error {
6464
//nolint:lll
6565
const typesTemplate = `{{ .Boilerplate }}
6666
67+
{{ if .Resource.HasApplyConfiguration }}
6768
// +kubebuilder:ac:generate=true
69+
{{ else }}
70+
// +kubebuilder:ac:generate=false
71+
{{ end }}
6872
package {{ .Resource.Version }}
6973
7074
import (

0 commit comments

Comments
 (0)