Skip to content

Commit d5e70a5

Browse files
authored
Merge pull request #2106 from camilamacedo86/config-base
✨ add the common plugin(s) to allow it to be used by consumers
2 parents 3eb373c + 9e816f5 commit d5e70a5

27 files changed

+273
-73
lines changed

cmd/main.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,33 @@ import (
2222
"sigs.k8s.io/kubebuilder/v3/pkg/cli"
2323
cfgv2 "sigs.k8s.io/kubebuilder/v3/pkg/config/v2"
2424
cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
25+
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
26+
kustomizecommonv1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1"
27+
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang"
2528
declarativev1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/declarative/v1"
26-
pluginv2 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2"
27-
pluginv3 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3"
29+
golangv2 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2"
30+
golangv3 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3"
2831
)
2932

3033
func main() {
34+
35+
// Bundle plugin which built the golang projects scaffold by Kubebuilder go/v3
36+
gov3Bundle, _ := plugin.NewBundle(golang.DefaultNameQualifier, plugin.Version{Number: 3},
37+
kustomizecommonv1.Plugin{},
38+
golangv3.Plugin{},
39+
)
40+
3141
c, err := cli.New(
3242
cli.WithCommandName("kubebuilder"),
3343
cli.WithVersion(versionString()),
3444
cli.WithPlugins(
35-
&pluginv2.Plugin{},
36-
&pluginv3.Plugin{},
45+
golangv2.Plugin{},
46+
gov3Bundle,
47+
&kustomizecommonv1.Plugin{},
3748
&declarativev1.Plugin{},
3849
),
39-
cli.WithDefaultPlugins(cfgv2.Version, &pluginv2.Plugin{}),
40-
cli.WithDefaultPlugins(cfgv3.Version, &pluginv3.Plugin{}),
50+
cli.WithDefaultPlugins(cfgv2.Version, golangv2.Plugin{}),
51+
cli.WithDefaultPlugins(cfgv3.Version, gov3Bundle),
4152
cli.WithDefaultProjectVersion(cfgv3.Version),
4253
cli.WithCompletion(),
4354
)
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
"fmt"
21+
"os"
22+
"path/filepath"
23+
"strings"
24+
25+
"github.com/spf13/pflag"
26+
27+
"sigs.k8s.io/kubebuilder/v3/pkg/config"
28+
"sigs.k8s.io/kubebuilder/v3/pkg/internal/validation"
29+
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
30+
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
31+
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds"
32+
)
33+
34+
var _ plugin.InitSubcommand = &initSubcommand{}
35+
36+
type initSubcommand struct {
37+
config config.Config
38+
39+
// config options
40+
domain string
41+
name string
42+
componentConfig bool
43+
}
44+
45+
func (p *initSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
46+
subcmdMeta.Description = `Initialize a common project including the following files:
47+
- a "PROJECT" file that stores project configuration
48+
- several YAML files for project deployment under the "config" directory
49+
`
50+
subcmdMeta.Examples = fmt.Sprintf(` # Initialize a common project with your domain and name in copyright
51+
%[1]s init --plugins common/v3 --domain example.org
52+
53+
# Initialize a common project defining an specific project version
54+
%[1]s init --plugins common/v3 --project-version 3
55+
`, cliMeta.CommandName)
56+
}
57+
58+
func (p *initSubcommand) BindFlags(fs *pflag.FlagSet) {
59+
fs.StringVar(&p.domain, "domain", "my.domain", "domain for groups")
60+
fs.StringVar(&p.name, "project-name", "", "name of this project")
61+
fs.BoolVar(&p.componentConfig, "component-config", false,
62+
"create a versioned ComponentConfig file, may be 'true' or 'false'")
63+
}
64+
65+
func (p *initSubcommand) InjectConfig(c config.Config) error {
66+
p.config = c
67+
68+
if err := p.config.SetDomain(p.domain); err != nil {
69+
return err
70+
}
71+
72+
// Assign a default project name
73+
if p.name == "" {
74+
dir, err := os.Getwd()
75+
if err != nil {
76+
return fmt.Errorf("error getting current directory: %v", err)
77+
}
78+
p.name = strings.ToLower(filepath.Base(dir))
79+
}
80+
// Check if the project name is a valid k8s namespace (DNS 1123 label).
81+
if err := validation.IsDNS1123Label(p.name); err != nil {
82+
return fmt.Errorf("project name (%s) is invalid: %v", p.name, err)
83+
}
84+
if err := p.config.SetProjectName(p.name); err != nil {
85+
return err
86+
}
87+
88+
if p.componentConfig {
89+
if err := p.config.SetComponentConfig(); err != nil {
90+
return err
91+
}
92+
}
93+
94+
return nil
95+
}
96+
97+
func (p *initSubcommand) Scaffold(fs machinery.Filesystem) error {
98+
scaffolder := scaffolds.NewInitScaffolder(p.config)
99+
scaffolder.InjectFS(fs)
100+
if err := scaffolder.Scaffold(); err != nil {
101+
return err
102+
}
103+
104+
return nil
105+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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/config"
21+
cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
22+
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
23+
"sigs.k8s.io/kubebuilder/v3/pkg/plugins"
24+
)
25+
26+
const pluginName = "kustomize.common." + plugins.DefaultNameQualifier
27+
28+
var (
29+
pluginVersion = plugin.Version{Number: 1}
30+
supportedProjectVersions = []config.Version{cfgv3.Version}
31+
)
32+
33+
var _ plugin.Init = Plugin{}
34+
35+
// Plugin implements the plugin.Full interface
36+
type Plugin struct {
37+
initSubcommand
38+
}
39+
40+
// Name returns the name of the plugin
41+
func (Plugin) Name() string { return pluginName }
42+
43+
// Version returns the version of the plugin
44+
func (Plugin) Version() plugin.Version { return pluginVersion }
45+
46+
// SupportedProjectVersions returns an array with all project versions supported by the plugin
47+
func (Plugin) SupportedProjectVersions() []config.Version { return supportedProjectVersions }
48+
49+
// GetInitSubcommand will return the subcommand which is responsible for scaffolding init project
50+
func (p Plugin) GetInitSubcommand() plugin.InitSubcommand { return &p.initSubcommand }
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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/plugins"
25+
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/kdefault"
26+
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/manager"
27+
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/prometheus"
28+
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/rbac"
29+
)
30+
31+
const (
32+
imageName = "controller:latest"
33+
)
34+
35+
var _ plugins.Scaffolder = &initScaffolder{}
36+
37+
type initScaffolder struct {
38+
config config.Config
39+
40+
// fs is the filesystem that will be used by the scaffolder
41+
fs machinery.Filesystem
42+
}
43+
44+
// NewInitScaffolder returns a new Scaffolder for project initialization operations
45+
func NewInitScaffolder(config config.Config) plugins.Scaffolder {
46+
return &initScaffolder{
47+
config: config,
48+
}
49+
}
50+
51+
// InjectFS implements cmdutil.Scaffolder
52+
func (s *initScaffolder) InjectFS(fs machinery.Filesystem) {
53+
s.fs = fs
54+
}
55+
56+
// Scaffold implements cmdutil.Scaffolder
57+
func (s *initScaffolder) Scaffold() error {
58+
fmt.Println("Writing scaffold for you to edit...")
59+
60+
// Initialize the machinery.Scaffold that will write the files to disk
61+
scaffold := machinery.NewScaffold(s.fs,
62+
machinery.WithConfig(s.config),
63+
)
64+
65+
return scaffold.Execute(
66+
&rbac.Kustomization{},
67+
&rbac.AuthProxyRole{},
68+
&rbac.AuthProxyRoleBinding{},
69+
&rbac.AuthProxyService{},
70+
&rbac.AuthProxyClientRole{},
71+
&rbac.RoleBinding{},
72+
&rbac.LeaderElectionRole{},
73+
&rbac.LeaderElectionRoleBinding{},
74+
&rbac.ServiceAccount{},
75+
&manager.Kustomization{},
76+
&manager.Config{Image: imageName},
77+
&manager.ControllerManagerConfig{},
78+
&kdefault.Kustomization{},
79+
&kdefault.ManagerAuthProxyPatch{},
80+
&kdefault.ManagerConfigPatch{},
81+
&prometheus.Kustomization{},
82+
&prometheus.Monitor{},
83+
)
84+
}
File renamed without changes.
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)