Skip to content

Commit c062d27

Browse files
✨ add kustomize/v2-alpha plugin to support kustomize versions >= v4 (pinned version 4.5.3)
1 parent fc59b56 commit c062d27

File tree

121 files changed

+6050
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+6050
-4
lines changed

cmd/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
2828
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
2929
kustomizecommonv1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1"
30+
kustomizecommonv2 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v2"
3031
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang"
3132
declarativev1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/declarative/v1"
3233
golangv2 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2"
@@ -57,6 +58,7 @@ func main() {
5758
golangv3.Plugin{},
5859
gov3Bundle,
5960
&kustomizecommonv1.Plugin{},
61+
&kustomizecommonv2.Plugin{},
6062
&declarativev1.Plugin{},
6163
),
6264
cli.WithPlugins(externalPlugins...),

docs/book/src/plugins/available-plugins.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This section describes the plugins supported and shipped in with the Kubebuilder
88
| [go.kubebuilder.io/v2](go-v2-plugin.md) | `go/v2` | Golang plugin responsible for scaffold the legacy layout provided with Kubebuilder CLI >= `2.0.0` and < `3.0.0`. |
99
| [declarative.go.kubebuilder.io/v1](declarative-v1.md) | `declarative/v1` | Optional plugin used to scaffold APIs/controllers using the [kubebuilder-declarative-pattern][kubebuilder-declarative-pattern] project. |
1010
| [kustomize.common.kubebuilder.io/v1](kustomize-v1.md) | `kustomize/v1` | Responsible for scaffold all manifests to configure the projects with [kustomize(v3)][kustomize]. (create and update the the `config/` directory). This plugin is used in the composition to create the plugin (`go/v3`). |
11+
| [kustomize.common.kubebuilder.io/v1](kustomize-v2-alpha.md) | `kustomize/v2-alpha` | It has the same purpose of `kustomize/v1`. However, it works with [kustomize][kustomize] version `v4` and address the required changes for the future kustomize configurations. It will probably be used with the future `go/v4-alpha` plugin. |
1112
| `base.go.kubebuilder.io/v3` | `base/v3` | Responsible for scaffold all files which specific requires Golang. This plugin is used in the composition to create the plugin (`go/v3`) |
1213

1314
<aside class="note">
@@ -20,4 +21,4 @@ This section describes the plugins supported and shipped in with the Kubebuilder
2021

2122
[create-plugins]: creating-plugins.md
2223
[kubebuilder-declarative-pattern]: https://github.com/kubernetes-sigs/kubebuilder-declarative-pattern
23-
[kustomize]: https://kustomize.io/
24+
[kustomize]: https://kustomize.io/

docs/book/src/plugins/kustomize-v1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ If you are looking to scaffold the kustomize configuration manifests for your ow
2727
## How to use it ?
2828

2929
If you are looking to define that your language plugin should use kustomize use the [Bundle Plugin][bundle]
30-
to specify that your language plugin is a composition with your plugin responsable for scaffold
30+
to specify that your language plugin is a composition with your plugin responsible for scaffold
3131
all that is laguage specific and kustomize for its configuration, see:
3232

3333
```go
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Kustomize v2-alpha
2+
3+
The kustomize plugin allows you to scaffold all kustomize manifests used to work with the language base plugin `base.go.kubebuilder.io/v3`.
4+
5+
Note that projects such as [Operator-sdk][sdk] consume the Kubebuilder project as a lib and provide options to work with other languages
6+
like Ansible and Helm. The kustomize plugin allows them to easily keep a maintained configuration and ensure that all languages have
7+
the same configuration. It is also helpful if you are looking to provide nice plugins which will perform changes on top of
8+
what is scaffolded by default. With this approach we do not need to keep manually updating this configuration in all possible language plugins
9+
which uses the same and we are also
10+
able to create "helper" plugins which can work with many projects and languages.
11+
12+
<aside class="note">
13+
<h1>Examples</h1>
14+
15+
You can check the kustomize content by looking at the `config/` directory provide on the sample `project-v3-with-kustomize-v2` under the [testdata][testdata]
16+
directory of the Kubebuilder project.
17+
18+
</aside>
19+
20+
## When to use it
21+
22+
- If you are looking to scaffold the kustomize configuration manifests for your own language plugin
23+
- If you are looking for support on Apple silicon (`arm64`)
24+
- If you are looking for to begin to try out the new syntax and features provide by kustomize v4
25+
- If you are NOT looking to build projects which will be used on Kubernetes cluster versions < `1.22` (_The new features provides by kustomize v4 are not officially supported and might not work with kubectl < `1.22`_)
26+
- If you are NOT looking to rely on special URLs in resource fields
27+
28+
<aside class="note">
29+
<h1>Supportability</h1>
30+
31+
You can use `kustomize/v1` plugin which is the default configuration adopted by the `go/v3` plugin if you are not prepared to began to experiment kustomize `v4`.
32+
Also, be aware that the `base.go.kubebuilder.io/v3` is prepared to work with this plugin.
33+
34+
</aside>
35+
36+
37+
## How to use it
38+
39+
If you are looking to define that your language plugin should use kustomize use the [Bundle Plugin][bundle]
40+
to specify that your language plugin is a composition with your plugin responsible for scaffold
41+
all that is language specific and kustomize for its configuration, see:
42+
43+
```go
44+
import (
45+
...
46+
kustomizecommonv2 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v2"
47+
golangv3 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3"
48+
...
49+
)
50+
51+
// Bundle plugin which built the golang projects scaffold by Kubebuilder go/v3
52+
// The follow code is creating a new plugin with its name and version via composition
53+
// You can define that one plugin is composite by 1 or Many others plugins
54+
gov3Bundle, _ := plugin.NewBundle(golang.DefaultNameQualifier, plugin.Version{Number: 3},
55+
kustomizecommonv2.Plugin{}, // scaffold the config/ directory and all kustomize files
56+
golangv3.Plugin{}, // Scaffold the Golang files and all that specific for the language e.g. go.mod, apis, controllers
57+
)
58+
```
59+
60+
Also, with Kubebuilder, you can use kustomize/v2-alpha alone via:
61+
62+
```sh
63+
kubebuilder init --plugins=kustomize/v2-alpha
64+
$ ls -la
65+
total 24
66+
drwxr-xr-x 6 camilamacedo86 staff 192 31 Mar 09:56 .
67+
drwxr-xr-x 11 camilamacedo86 staff 352 29 Mar 21:23 ..
68+
-rw------- 1 camilamacedo86 staff 129 26 Mar 12:01 .dockerignore
69+
-rw------- 1 camilamacedo86 staff 367 26 Mar 12:01 .gitignore
70+
-rw------- 1 camilamacedo86 staff 94 31 Mar 09:56 PROJECT
71+
drwx------ 6 camilamacedo86 staff 192 31 Mar 09:56 config
72+
```
73+
74+
Or combined with the base language plugins:
75+
76+
```sh
77+
# Provides the same scaffold of go/v3 plugin which is composition but with kustomize/v2-alpha
78+
kubebuilder init --plugins=kustomize/v2-alpha,base.go.kubebuilder.io/v3 --domain example.org --repo example.org/guestbook-operator
79+
```
80+
81+
## Subcommands
82+
83+
The kustomize plugin implements the following subcommands:
84+
85+
* init (`$ kubebuilder init [OPTIONS]`)
86+
* create api (`$ kubebuilder create api [OPTIONS]`)
87+
* create webhook (`$ kubebuilder create api [OPTIONS]`)
88+
89+
<aside class="note">
90+
<h1>Create API and Webhook</h1>
91+
92+
Its implementation for the subcommand create api will scaffold the kustomize manifests
93+
which are specific for each API, see [here][kustomize-create-api]. The same applies
94+
to its implementation for create webhook.
95+
96+
</aside>
97+
98+
## Affected files
99+
100+
The following scaffolds will be created or updated by this plugin:
101+
102+
* `config/*`
103+
104+
## Further resources
105+
106+
* Check the kustomize [plugin implementation](https://github.com/kubernetes-sigs/kubebuilder/tree/master/pkg/plugins/common/kustomize)
107+
* Check the [kustomize documentation][kustomize-docs]
108+
* Check the [kustomize repository][kustomize-github]
109+
* To know more about the changes between kustomize v3 and v3 see its [release notes][release-notes]
110+
* Also, you can compare the `config/` directory between the samples `project-v3` and `project-v3-with-kustomize-v2` to check the difference in the syntax of the manifests provided by default
111+
112+
[sdk]:https://github.com/operator-framework/operator-sdk
113+
[testdata]: https://github.com/kubernetes-sigs/kubebuilder/tree/master/testdata/
114+
[bundle]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/pkg/plugin/bundle.go
115+
[kustomize-create-api]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/pkg/plugins/common/kustomize/v2/scaffolds/api.go#L72-L84
116+
[kustomize-docs]: https://kustomize.io/
117+
[kustomize-github]: https://github.com/kubernetes-sigs/kustomize
118+
[release-notes]: https://github.com/kubernetes-sigs/kustomize/releases/tag/kustomize%2Fv4.0.0

docs/book/src/quick-start.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ cd ~/projects/guestbook
6161
kubebuilder init --domain my.domain --repo my.domain/guestbook
6262
```
6363

64+
<aside class="note">
65+
<h1>Supportability (Apple Silicon `arm64`)</h1>
66+
67+
If you are using Apple Silicon (`arm64`) you probably want try initialize the project with
68+
`kubebuilder init --plugins=kustomize/v2-alpha,base.go.kubebuilder.io/v3` instead. [More Info][more-info]
69+
70+
</aside>
71+
6472
<aside class="note">
6573
<h1>Developing in $GOPATH</h1>
6674

@@ -238,3 +246,4 @@ Now, see the [architecture concept diagram][architecture-concept-diagram] for a
238246
[go-modules-blogpost]: https://blog.golang.org/using-go-modules
239247
[envtest]: https://book.kubebuilder.io/reference/testing/envtest.html
240248
[architecture-concept-diagram]: architecture.md
249+
[more-info]: plugins/kustomize-v2-alpha.md

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import (
2323
"sigs.k8s.io/kubebuilder/v3/pkg/plugins"
2424
)
2525

26+
// KustomizeVersion is the kubernetes-sigs/kustomize version to be used in the project
27+
const KustomizeVersion = "v3.8.7"
28+
2629
const pluginName = "kustomize.common." + plugins.DefaultNameQualifier
2730

2831
var (
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright 2022 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 v2
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/v2/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 2022 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 v2
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+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
Copyright 2022 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 v2
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/v2/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+
NOTE: This plugin requires kustomize version v4 and kubectl >= 1.22.
51+
`
52+
subcmdMeta.Examples = fmt.Sprintf(` # Initialize a common project with your domain and name in copyright
53+
%[1]s init --plugins common/v3 --domain example.org
54+
55+
# Initialize a common project defining a specific project version
56+
%[1]s init --plugins common/v3 --project-version 3
57+
`, cliMeta.CommandName)
58+
}
59+
60+
func (p *initSubcommand) BindFlags(fs *pflag.FlagSet) {
61+
fs.StringVar(&p.domain, "domain", "my.domain", "domain for groups")
62+
fs.StringVar(&p.name, "project-name", "", "name of this project")
63+
fs.BoolVar(&p.componentConfig, "component-config", false,
64+
"create a versioned ComponentConfig file, may be 'true' or 'false'")
65+
}
66+
67+
func (p *initSubcommand) InjectConfig(c config.Config) error {
68+
p.config = c
69+
70+
if err := p.config.SetDomain(p.domain); err != nil {
71+
return err
72+
}
73+
74+
// Assign a default project name
75+
if p.name == "" {
76+
dir, err := os.Getwd()
77+
if err != nil {
78+
return fmt.Errorf("error getting current directory: %v", err)
79+
}
80+
p.name = strings.ToLower(filepath.Base(dir))
81+
}
82+
// Check if the project name is a valid k8s namespace (DNS 1123 label).
83+
if err := validation.IsDNS1123Label(p.name); err != nil {
84+
return fmt.Errorf("project name (%s) is invalid: %v", p.name, err)
85+
}
86+
if err := p.config.SetProjectName(p.name); err != nil {
87+
return err
88+
}
89+
90+
if p.componentConfig {
91+
if err := p.config.SetComponentConfig(); err != nil {
92+
return err
93+
}
94+
}
95+
96+
return nil
97+
}
98+
99+
func (p *initSubcommand) Scaffold(fs machinery.Filesystem) error {
100+
scaffolder := scaffolds.NewInitScaffolder(p.config)
101+
scaffolder.InjectFS(fs)
102+
return scaffolder.Scaffold()
103+
}

0 commit comments

Comments
 (0)