Skip to content

Commit b36ba14

Browse files
committed
pkg/plugins: move all kustomize manifests from golang/v3 to kustomize/v1
Signed-off-by: Eric Stroczynski <[email protected]>
1 parent 7d8d24d commit b36ba14

24 files changed

+321
-31
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1
18+
19+
import (
20+
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
21+
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
22+
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds"
23+
)
24+
25+
var _ plugin.CreateAPISubcommand = &createAPISubcommand{}
26+
27+
type createAPISubcommand struct {
28+
createSubcommand
29+
}
30+
31+
func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error {
32+
if err := p.configure(); err != nil {
33+
return err
34+
}
35+
scaffolder := scaffolds.NewAPIScaffolder(p.config, *p.resource, p.force)
36+
scaffolder.InjectFS(fs)
37+
return scaffolder.Scaffold()
38+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1
18+
19+
import (
20+
"strconv"
21+
22+
"github.com/spf13/pflag"
23+
24+
"sigs.k8s.io/kubebuilder/v3/pkg/config"
25+
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
26+
)
27+
28+
type createSubcommand struct {
29+
config config.Config
30+
resource *resource.Resource
31+
32+
flagSet *pflag.FlagSet
33+
34+
// force indicates whether to scaffold files even if they exist.
35+
force bool
36+
}
37+
38+
func (p *createSubcommand) BindFlags(fs *pflag.FlagSet) { p.flagSet = fs }
39+
40+
func (p *createSubcommand) InjectConfig(c config.Config) error {
41+
p.config = c
42+
return nil
43+
}
44+
45+
func (p *createSubcommand) InjectResource(res *resource.Resource) error {
46+
p.resource = res
47+
return nil
48+
}
49+
50+
func (p *createSubcommand) configure() (err error) {
51+
if forceFlag := p.flagSet.Lookup("force"); forceFlag != nil {
52+
if p.force, err = strconv.ParseBool(forceFlag.Value.String()); err != nil {
53+
return err
54+
}
55+
56+
}
57+
return nil
58+
}

pkg/plugins/common/kustomize/v1/plugin.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@ var (
3030
supportedProjectVersions = []config.Version{cfgv3.Version}
3131
)
3232

33-
var _ plugin.Init = Plugin{}
33+
var (
34+
_ plugin.Init = Plugin{}
35+
_ plugin.CreateAPI = Plugin{}
36+
_ plugin.CreateWebhook = Plugin{}
37+
)
3438

3539
// Plugin implements the plugin.Full interface
3640
type Plugin struct {
3741
initSubcommand
42+
createAPISubcommand
43+
createWebhookSubcommand
3844
}
3945

4046
// Name returns the name of the plugin
@@ -48,3 +54,11 @@ func (Plugin) SupportedProjectVersions() []config.Version { return supportedProj
4854

4955
// GetInitSubcommand will return the subcommand which is responsible for scaffolding init project
5056
func (p Plugin) GetInitSubcommand() plugin.InitSubcommand { return &p.initSubcommand }
57+
58+
// GetCreateAPISubcommand will return the subcommand which is responsible for scaffolding apis
59+
func (p Plugin) GetCreateAPISubcommand() plugin.CreateAPISubcommand { return &p.createAPISubcommand }
60+
61+
// GetCreateWebhookSubcommand will return the subcommand which is responsible for scaffolding webhooks
62+
func (p Plugin) GetCreateWebhookSubcommand() plugin.CreateWebhookSubcommand {
63+
return &p.createWebhookSubcommand
64+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package scaffolds
18+
19+
import (
20+
"fmt"
21+
22+
"sigs.k8s.io/kubebuilder/v3/pkg/config"
23+
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
24+
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
25+
"sigs.k8s.io/kubebuilder/v3/pkg/plugins"
26+
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/crd"
27+
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/crd/patches"
28+
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/rbac"
29+
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/samples"
30+
)
31+
32+
var _ plugins.Scaffolder = &apiScaffolder{}
33+
34+
// apiScaffolder contains configuration for generating scaffolding for Go type
35+
// representing the API and controller that implements the behavior for the API.
36+
type apiScaffolder struct {
37+
config config.Config
38+
resource resource.Resource
39+
40+
// fs is the filesystem that will be used by the scaffolder
41+
fs machinery.Filesystem
42+
43+
// force indicates whether to scaffold files even if they exist.
44+
force bool
45+
}
46+
47+
// NewAPIScaffolder returns a new Scaffolder for API/controller creation operations
48+
func NewAPIScaffolder(config config.Config, res resource.Resource, force bool) plugins.Scaffolder {
49+
return &apiScaffolder{
50+
config: config,
51+
resource: res,
52+
force: force,
53+
}
54+
}
55+
56+
// InjectFS implements cmdutil.Scaffolder
57+
func (s *apiScaffolder) InjectFS(fs machinery.Filesystem) {
58+
s.fs = fs
59+
}
60+
61+
// Scaffold implements cmdutil.Scaffolder
62+
func (s *apiScaffolder) Scaffold() error {
63+
fmt.Println("Writing kustomize manifests for you to edit...")
64+
65+
// Initialize the machinery.Scaffold that will write the files to disk
66+
scaffold := machinery.NewScaffold(s.fs,
67+
machinery.WithConfig(s.config),
68+
machinery.WithResource(&s.resource),
69+
)
70+
71+
// Keep track of these values before the update
72+
if s.resource.HasAPI() {
73+
if err := scaffold.Execute(
74+
&samples.CRDSample{Force: s.force},
75+
&rbac.CRDEditorRole{},
76+
&rbac.CRDViewerRole{},
77+
&patches.EnableWebhookPatch{},
78+
&patches.EnableCAInjectionPatch{},
79+
&crd.Kustomization{},
80+
&crd.KustomizeConfig{},
81+
); err != nil {
82+
return fmt.Errorf("error scaffolding kustomize API manifests: %v", err)
83+
}
84+
}
85+
86+
return nil
87+
}

pkg/plugins/common/kustomize/v1/scaffolds/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (s *initScaffolder) InjectFS(fs machinery.Filesystem) {
5555

5656
// Scaffold implements cmdutil.Scaffolder
5757
func (s *initScaffolder) Scaffold() error {
58-
fmt.Println("Writing scaffold for you to edit...")
58+
fmt.Println("Writing kustomize manifests for you to edit...")
5959

6060
// Initialize the machinery.Scaffold that will write the files to disk
6161
scaffold := machinery.NewScaffold(s.fs,
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)