Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix --force option to recreate the files by kubebuilder create api #1847

Merged
merged 1 commit into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions generate_testdata.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ scaffold_test_project() {
if [ $project == "project-v2" ] || [ $project == "project-v3" ] || [ $project == "project-v3-config" ]; then
header_text 'Creating APIs ...'
$kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --make=false
$kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --make=false --force
$kb create webhook --group crew --version v1 --kind Captain --defaulting --programmatic-validation
$kb create api --group crew --version v1 --kind FirstMate --controller=true --resource=true --make=false
$kb create webhook --group crew --version v1 --kind FirstMate --conversion
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugins/golang/v2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (p *createAPISubcommand) GetScaffolder() (cmdutil.Scaffolder, error) {

// Create the actual resource from the resource options
res := p.resource.NewResource(p.config, p.doResource)
return scaffolds.NewAPIScaffolder(p.config, string(bp), res, p.doResource, p.doController, plugins), nil
return scaffolds.NewAPIScaffolder(p.config, string(bp), res, p.doResource, p.doController, p.force, plugins), nil
}

func (p *createAPISubcommand) PostScaffold() error {
Expand Down
13 changes: 8 additions & 5 deletions pkg/plugins/golang/v2/scaffolds/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ type apiScaffolder struct {
doResource bool
// doController indicates whether to scaffold controller files or not
doController bool

force bool
}

// NewAPIScaffolder returns a new Scaffolder for API/controller creation operations
func NewAPIScaffolder(
config *config.Config,
boilerplate string,
res *resource.Resource,
doResource, doController bool,
doResource, doController, force bool,
plugins []model.Plugin,
) cmdutil.Scaffolder {
return &apiScaffolder{
Expand All @@ -68,6 +70,7 @@ func NewAPIScaffolder(
plugins: plugins,
doResource: doResource,
doController: doController,
force: force,
}
}

Expand Down Expand Up @@ -97,9 +100,9 @@ func (s *apiScaffolder) scaffold() error {

if err := machinery.NewScaffold(s.plugins...).Execute(
s.newUniverse(),
&api.Types{},
&api.Types{Force: s.force},
&api.Group{},
&samples.CRDSample{},
&samples.CRDSample{Force: s.force},
&rbac.CRDEditorRole{},
&rbac.CRDViewerRole{},
&patches.EnableWebhookPatch{},
Expand All @@ -121,8 +124,8 @@ func (s *apiScaffolder) scaffold() error {
if s.doController {
if err := machinery.NewScaffold(s.plugins...).Execute(
s.newUniverse(),
&controllers.SuiteTest{WireResource: s.doResource},
&controllers.Controller{WireResource: s.doResource},
&controllers.SuiteTest{WireResource: s.doResource, Force: s.force},
&controllers.Controller{WireResource: s.doResource, Force: s.force},
); err != nil {
return fmt.Errorf("error scaffolding controller: %v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ import (
var _ file.Template = &Types{}

// Types scaffolds the file that defines the schema for a CRD
// nolint:maligned
type Types struct {
file.TemplateMixin
file.MultiGroupMixin
file.BoilerplateMixin
file.ResourceMixin

Force bool
}

// SetTemplateDefaults implements file.Template
Expand All @@ -47,7 +50,11 @@ func (f *Types) SetTemplateDefaults() error {

f.TemplateBody = typesTemplate

f.IfExistsAction = file.Error
if f.Force {
f.IfExistsAction = file.Overwrite
} else {
f.IfExistsAction = file.Error
}

return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var _ file.Template = &CRDSample{}
type CRDSample struct {
file.TemplateMixin
file.ResourceMixin

Force bool
}

// SetTemplateDefaults implements file.Template
Expand All @@ -37,7 +39,11 @@ func (f *CRDSample) SetTemplateDefaults() error {
}
f.Path = f.Resource.Replacer().Replace(f.Path)

f.IfExistsAction = file.Error
if f.Force {
f.IfExistsAction = file.Overwrite
} else {
f.IfExistsAction = file.Error
}

f.TemplateBody = crdSampleTemplate

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type Controller struct {

// WireResource defines the api resources are generated or not.
WireResource bool

Force bool
}

// SetTemplateDefaults implements file.Template
Expand All @@ -51,7 +53,11 @@ func (f *Controller) SetTemplateDefaults() error {

f.TemplateBody = controllerTemplate

f.IfExistsAction = file.Error
if f.Force {
f.IfExistsAction = file.Overwrite
} else {
f.IfExistsAction = file.Error
}

return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type SuiteTest struct {

// WireResource defines the api resources are generated or not.
WireResource bool

Force bool
}

// SetTemplateDefaults implements file.Template
Expand All @@ -64,6 +66,10 @@ func (f *SuiteTest) SetTemplateDefaults() error {
f.CRDDirectoryRelativePath = `"..", ".."`
}

if f.Force {
f.IfExistsAction = file.Overwrite
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/plugins/golang/v3/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (p *createAPISubcommand) GetScaffolder() (cmdutil.Scaffolder, error) {

// Create the actual resource from the resource options
res := p.resource.NewResource(p.config, p.doResource)
return scaffolds.NewAPIScaffolder(p.config, string(bp), res, p.doResource, p.doController, plugins), nil
return scaffolds.NewAPIScaffolder(p.config, string(bp), res, p.doResource, p.doController, p.force, plugins), nil
}

func (p *createAPISubcommand) PostScaffold() error {
Expand Down
14 changes: 9 additions & 5 deletions pkg/plugins/golang/v3/scaffolds/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ type apiScaffolder struct {
doResource bool
// doController indicates whether to scaffold controller files or not
doController bool
// force indicates whether to scaffold controller files even if it exists or not
force bool
}

// NewAPIScaffolder returns a new Scaffolder for API/controller creation operations
func NewAPIScaffolder(
config *config.Config,
boilerplate string,
res *resource.Resource,
doResource, doController bool,
doResource, doController, force bool,
plugins []model.Plugin,
) cmdutil.Scaffolder {
return &apiScaffolder{
Expand All @@ -64,6 +66,7 @@ func NewAPIScaffolder(
plugins: plugins,
doResource: doResource,
doController: doController,
force: force,
}
}

Expand All @@ -89,9 +92,9 @@ func (s *apiScaffolder) scaffold() error {

if err := machinery.NewScaffold(s.plugins...).Execute(
s.newUniverse(),
&api.Types{},
&api.Types{Force: s.force},
&api.Group{},
&samples.CRDSample{},
&samples.CRDSample{Force: s.force},
&rbac.CRDEditorRole{},
&rbac.CRDViewerRole{},
&patches.EnableWebhookPatch{CRDVersion: s.resource.API.CRDVersion},
Expand All @@ -113,8 +116,9 @@ func (s *apiScaffolder) scaffold() error {
if s.doController {
if err := machinery.NewScaffold(s.plugins...).Execute(
s.newUniverse(),
&controllers.SuiteTest{WireResource: s.doResource},
&controllers.Controller{ControllerRuntimeVersion: ControllerRuntimeVersion, WireResource: s.doResource},
&controllers.SuiteTest{WireResource: s.doResource, Force: s.force},
&controllers.Controller{ControllerRuntimeVersion: ControllerRuntimeVersion, WireResource: s.doResource,
Force: s.force},
); err != nil {
return fmt.Errorf("error scaffolding controller: %v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ import (
var _ file.Template = &Types{}

// Types scaffolds the file that defines the schema for a CRD
// nolint:maligned
type Types struct {
file.TemplateMixin
file.MultiGroupMixin
file.BoilerplateMixin
file.ResourceMixin

Force bool
}

// SetTemplateDefaults implements file.Template
Expand All @@ -51,7 +54,11 @@ func (f *Types) SetTemplateDefaults() error {

f.TemplateBody = typesTemplate

f.IfExistsAction = file.Error
if f.Force {
f.IfExistsAction = file.Overwrite
} else {
f.IfExistsAction = file.Error
}

return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var _ file.Template = &CRDSample{}
type CRDSample struct {
file.TemplateMixin
file.ResourceMixin

Force bool
}

// SetTemplateDefaults implements file.Template
Expand All @@ -37,7 +39,11 @@ func (f *CRDSample) SetTemplateDefaults() error {
}
f.Path = f.Resource.Replacer().Replace(f.Path)

f.IfExistsAction = file.Error
if f.Force {
f.IfExistsAction = file.Overwrite
} else {
f.IfExistsAction = file.Error
}

f.TemplateBody = crdSampleTemplate

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type Controller struct {

// WireResource defines the api resources are generated or not.
WireResource bool

Force bool
}

// SetTemplateDefaults implements file.Template
Expand All @@ -53,7 +55,11 @@ func (f *Controller) SetTemplateDefaults() error {

f.TemplateBody = controllerTemplate

f.IfExistsAction = file.Error
if f.Force {
f.IfExistsAction = file.Overwrite
} else {
f.IfExistsAction = file.Error
}

return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type SuiteTest struct {

// WireResource defines the api resources are generated or not.
WireResource bool

Force bool
}

// SetTemplateDefaults implements file.Template
Expand All @@ -64,6 +66,10 @@ func (f *SuiteTest) SetTemplateDefaults() error {
f.CRDDirectoryRelativePath = `"..", ".."`
}

if f.Force {
f.IfExistsAction = file.Overwrite
}

return nil
}

Expand Down
8 changes: 8 additions & 0 deletions testdata/project-v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ func main() {
os.Exit(1)
}

if err = (&controllers.CaptainReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Captain"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Captain")
os.Exit(1)
}
if err = (&controllers.CaptainReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Captain"),
Expand Down
8 changes: 8 additions & 0 deletions testdata/project-v3-config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ func main() {
os.Exit(1)
}

if err = (&controllers.CaptainReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Captain"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Captain")
os.Exit(1)
}
camilamacedo86 marked this conversation as resolved.
Show resolved Hide resolved
if err = (&controllers.CaptainReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Captain"),
Expand Down
8 changes: 8 additions & 0 deletions testdata/project-v3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ func main() {
os.Exit(1)
}

if err = (&controllers.CaptainReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Captain"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Captain")
os.Exit(1)
}
if err = (&controllers.CaptainReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Captain"),
Expand Down