Skip to content

Commit 94211ef

Browse files
authored
Merge branch 'kubernetes-sigs:master' into master
2 parents e2c0c50 + afce6a0 commit 94211ef

File tree

311 files changed

+841
-559
lines changed

Some content is hidden

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

311 files changed

+841
-559
lines changed

docs/book/src/migration/manually_migration_guide_gov3_to_gov4.md

Lines changed: 138 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@ The recommended upgrade approach is to follow the [Migration Guide go/v3 to go/v
1616

1717
## Migration from project config version "go/v3" to "go/v4"
1818

19-
Update `PROJECT` file layout which stores the information about the resources are use to enable plugins to make useful decisions when scaffolding.
19+
Update the `PROJECT` file layout which stores information about the resources that are used to enable plugins make
20+
useful decisions while scaffolding. The `layout` field indicates the scaffolding and the primary plugin version in use.
2021

21-
Furthermore, the `PROJECT` file itself is now versioned. The `version` field corresponds to the version of the `PROJECT` file itself, while the `layout` field indicates the scaffolding and the primary plugin version in use.
22+
### Steps to migrate
23+
24+
#### Migrate the layout version into the PROJECT file
25+
26+
The following steps describe the manual changes required to bring the project configuration file (`PROJECT`).
27+
These change will add the information that Kubebuilder would add when generating the file. This file can be found in the root directory.
2228

23-
Update:
29+
Update the PROJECT file by replacing:
2430

2531
```yaml
2632
layout:
@@ -35,34 +41,150 @@ layout:
3541

3642
```
3743
38-
### Steps to migrate
44+
#### Changes to the layout
3945
40-
- Update the `main.go` with the changes which can be found in the samples under testdata for the release tag used. (see for example `testdata/project-v4/main.go`).
41-
- Update the Makefile with the changes which can be found in the samples under testdata for the release tag used. (see for example `testdata/project-v4/Makefile`)
42-
- Update the `go.mod` with the changes which can be found in the samples under `testdata` for the release tag used. (see for example `testdata/project-v4/go.mod`). Then, run
43-
`go mod tidy` to ensure that you get the latest dependencies and your Golang code has no breaking changes.
44-
- Update the manifest under `config/` directory with all changes performed in the default scaffold done with `go/v4-alpha` plugin. (see for example `testdata/project-v4/config/`) to get all changes in the
45-
default scaffolds to be applied on your project
46-
- Create `config/samples/kustomization.yaml` with all CR samples specified. (see for example `testdata/project-v4/config/samples/kustomization.yaml`)
47-
- Replace the import `admissionv1beta1 "k8s.io/api/admission/v1beta1"` with `admissionv1 "k8s.io/api/admission/v1"` in the webhook test files
46+
##### New layout:
47+
48+
- The directory `apis` was renamed to `api` to follow the standard
49+
- The `controller(s)` directory has been moved under a new directory called `internal` and renamed to singular as well `controller`
50+
- The `main.go` previously scaffolded in the root directory has been moved under a new directory called `cmd`
51+
52+
Therefore, you can check the changes in the layout results into:
53+
54+
```sh
55+
...
56+
├── cmd
57+
│ └── main.go
58+
├── internal
59+
│ └── controller
60+
└── api
61+
```
62+
63+
##### Migrating to the new layout:
64+
65+
- Create a new directory `cmd` and move the `main.go` under it.
66+
- If your project support multi-group the APIs are scaffold under a directory called `apis`. Rename this directory to `api`
67+
- Move the `controllers` directory under the `internal` and rename it for `controller`
68+
- Now ensure that the imports will be updated accordingly by:
69+
- Update the `main.go` imports to look for the new path of your controllers under the `pkg` directory
70+
71+
**Then, let's update the scaffolds paths**
72+
73+
- Update the Dockerfile to ensure that you will have:
74+
75+
```
76+
COPY cmd/main.go cmd/main.go
77+
COPY api/ api/
78+
COPY internal/controller/ internal/controller/
79+
```
80+
81+
Then, replace:
82+
83+
```
84+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager main.go
85+
86+
```
87+
88+
With:
89+
90+
```
91+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
92+
```
93+
94+
- Update the Makefile targets to build and run the manager by replacing:
95+
96+
```
97+
.PHONY: build
98+
build: manifests generate fmt vet ## Build manager binary.
99+
go build -o bin/manager main.go
100+
101+
.PHONY: run
102+
run: manifests generate fmt vet ## Run a controller from your host.
103+
go run ./main.go
104+
```
105+
106+
With:
107+
108+
```
109+
.PHONY: build
110+
build: manifests generate fmt vet ## Build manager binary.
111+
go build -o bin/manager cmd/main.go
112+
113+
.PHONY: run
114+
run: manifests generate fmt vet ## Run a controller from your host.
115+
go run ./cmd/main.go
116+
```
117+
118+
- Update the `internal/controller/suite_test.go` to set the path for the `CRDDirectoryPaths`:
119+
120+
Replace:
121+
122+
```
123+
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
124+
```
125+
126+
With:
127+
128+
```
129+
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
130+
```
131+
132+
Note that if your project has multiple groups (`multigroup:true`) then the above update should result into `"..", "..", "..",` instead of `"..",".."`
133+
134+
#### Now, let's update the PATHs in the PROJECT file accordingly
135+
136+
The PROJECT tracks the paths of all APIs used in your project. Ensure that they now point to `api/...` as the following example:
137+
138+
Before update:
139+
group: crew
140+
kind: Captain
141+
path: sigs.k8s.io/kubebuilder/testdata/project-v4/apis/crew/v1
142+
```
143+
144+
After Update:
145+
146+
```
147+
group: crew
148+
kind: Captain
149+
path: sigs.k8s.io/kubebuilder/testdata/project-v4/api/crew/v1
150+
```
151+
152+
### Update kustomize manifests with the changes made so far
153+
154+
- Update the manifest under `config/` directory with all changes performed in the default scaffold done with `go/v4-alpha` plugin. (see for example `testdata/project-v4/config/`) to get all changes in the
155+
default scaffolds to be applied on your project
156+
- Create `config/samples/kustomization.yaml` with all Custom Resources samples specified into `config/samples`. _(see for example `testdata/project-v4/config/samples/kustomization.yaml`)_
48157

49158
<aside class="warning">
50159
<h1>`config/` directory with changes into the scaffold files</h1>
51160

52161
Note that under the `config/` directory you will find scaffolding changes since using
53162
`go/v4-alpha` you will ensure that you are no longer using Kustomize v3x.
54163

55-
You can mainly compare the `config/` directory from the samples scaffolded under the `testdata`directory by
164+
You can mainly compare the `config/` directory from the samples scaffolded under the `testdata`directory by
56165
checking the differences between the `testdata/project-v3/config/` with `testdata/project-v4/config/` which
57166
are samples created with the same commands with the only difference being versions.
58167

59168
However, note that if you create your project with Kubebuilder CLI 3.0.0, its scaffolds
60-
might change to accommodate changes up to the latest releases using `go/v3` which are not considered
61-
breaking for users and/or are forced by the changes introduced in the dependencies
62-
used by the project such as [controller-runtime][controller-runtime] and [controller-tools][controller-tools].
169+
might change to accommodate changes up to the latest releases using `go/v3` which are not considered
170+
breaking for users and/or are forced by the changes introduced in the dependencies
171+
used by the project such as [controller-runtime][controller-runtime] and [controller-tools][controller-tools].
63172

64173
</aside>
65174

175+
### If you have webhooks:
176+
177+
Replace the import `admissionv1beta1 "k8s.io/api/admission/v1beta1"` with `admissionv1 "k8s.io/api/admission/v1"` in the webhook test files
178+
179+
### Makefile updates
180+
181+
Update the Makefile with the changes which can be found in the samples under testdata for the release tag used. (see for example `testdata/project-v4/Makefile`)
182+
183+
### Update the dependencies
184+
185+
Update the `go.mod` with the changes which can be found in the samples under `testdata` for the release tag used. (see for example `testdata/project-v4/go.mod`). Then, run
186+
`go mod tidy` to ensure that you get the latest dependencies and your Golang code has no breaking changes.
187+
66188
### Verification
67189

68190
In the steps above, you updated your project manually with the goal of ensuring that it follows

docs/book/src/migration/v3vsv4.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ The details of all changes (breaking or otherwise) can be found in:
2020
- no longer scaffold webhook test files with `"k8s.io/api/admission/v1beta1"` the k8s API which is no longer served since k8s `1.25`. By default
2121
webhooks test files are scaffolding using `"k8s.io/api/admission/v1"` which is support from k8s `1.20`
2222
- no longer provide backwards compatible support with k8s versions < `1.16`
23+
- change the layout to accommodate the community request to follow the [Standard Go Project Layout][standard-go-project]
24+
by moving the api(s) under a new directory called `api`, controller(s) under a new directory called `internal` and the `main.go` under a new directory named `cmd`
2325

2426
<aside class="note">
2527
<H1> TL;DR of the New `go/v4-alpha` Plugin </H1>
@@ -86,3 +88,4 @@ This way is more complex, susceptible to errors, and success cannot be assured.
8688
[go/v4-doc]: ./../plugins/go-v4-plugin.md
8789
[migration-guide-gov3-to-gov4]: migration_guide_gov3_to_gov4.md
8890
[manually-upgrade]: manually_migration_guide_gov3_to_gov4.md
91+
[standard-go-project]: https://github.com/golang-standards/project-layout

docs/book/src/plugins/go-v4-plugin.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# go/v4-alpha (go.kubebuilder.io/v4-alpha)
22

33
Kubebuilder will scaffold using the `go/v4-alpha` plugin only if specified when initializing the project.
4-
This plugin is a composition of the plugins ` kustomize.common.kubebuilder.io/v2-alpha` and `base.go.kubebuilder.io/v4`.
4+
This plugin is a composition of the plugins ` kustomize.common.kubebuilder.io/v2-alpha` and `base.go.kubebuilder.io/v4-alpha`.
55
It scaffolds a project template that helps in constructing sets of [controllers][controller-runtime].
66

77
It scaffolds boilerplate code to create and design controllers.
@@ -24,6 +24,8 @@ under the [testdata][testdata] directory on the root directory of the Kubebuilde
2424
- If you are looking to have your project update with the latest version available
2525
- if you are not targeting k8s versions < `1.16` and `1.20` if you are using webhooks
2626
- If you are looking to work on with scaffolds which are compatible with k8s `1.25+`
27+
- If you are looking for the new layout following the [Standard Go Project Layout][standard-go-project] where
28+
the "api(s)" are scaffold under the `api` directory, "controller(s)" under `internal`, and the `main.go` under `cmd`
2729

2830
<aside class="note">
2931

@@ -61,4 +63,5 @@ kubebuilder init --domain tutorial.kubebuilder.io --repo tutorial.kubebuilder.io
6163
[testdata]: https://github.com/kubernetes-sigs/kubebuilder/tree/master/testdata
6264
[plugins-main]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/cmd/main.go
6365
[kustomize-plugin]: ../plugins/kustomize-v2-alpha.md
64-
[kustomize]: https://github.com/kubernetes-sigs/kustomize
66+
[kustomize]: https://github.com/kubernetes-sigs/kustomize
67+
[standard-go-project]: https://github.com/golang-standards/project-layout

pkg/model/resource/utils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ func safeImport(unsafe string) string {
5050

5151
// APIPackagePath returns the default path
5252
func APIPackagePath(repo, group, version string, multiGroup bool) string {
53+
if multiGroup && group != "" {
54+
return path.Join(repo, "api", group, version)
55+
}
56+
return path.Join(repo, "api", version)
57+
}
58+
59+
// APIPackagePathLegacy returns the default path
60+
func APIPackagePathLegacy(repo, group, version string, multiGroup bool) string {
5361
if multiGroup {
5462
if group != "" {
5563
return path.Join(repo, "apis", group, version)

pkg/model/resource/utils_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ var _ = Describe("APIPackagePath", func() {
4545
Expect(APIPackagePath(repo, group, version, multiGroup)).To(Equal(p))
4646
},
4747
Entry("single group setup", repo, group, version, false, path.Join(repo, "api", version)),
48+
Entry("multiple group setup", repo, group, version, true, path.Join(repo, "api", group, version)),
49+
Entry("multiple group setup with empty group", repo, "", version, true, path.Join(repo, "api", version)),
50+
)
51+
})
52+
53+
var _ = Describe("APIPackagePathLegacy", func() {
54+
const (
55+
repo = "github.com/kubernetes-sigs/kubebuilder"
56+
group = "group"
57+
version = "v1"
58+
)
59+
60+
DescribeTable("should work",
61+
func(repo, group, version string, multiGroup bool, p string) {
62+
Expect(APIPackagePathLegacy(repo, group, version, multiGroup)).To(Equal(p))
63+
},
64+
Entry("single group setup", repo, group, version, false, path.Join(repo, "api", version)),
4865
Entry("multiple group setup", repo, group, version, true, path.Join(repo, "apis", group, version)),
4966
Entry("multiple group setup with empty group", repo, "", version, true, path.Join(repo, "apis", version)),
5067
)

pkg/plugin/helpers.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ func GetShortName(name string) string {
4747
return strings.SplitN(name, ".", 2)[0]
4848
}
4949

50+
// Deprecated: it was added to ensure backwards compatibility and should
51+
// be removed when we remove the go/v3 plugin
52+
// IsLegacyLayout returns true when is possible to identify that the project
53+
// was scaffolded with the previous layout
54+
func IsLegacyLayout(config config.Config) bool {
55+
for _, pluginKey := range config.GetPluginChain() {
56+
if strings.Contains(pluginKey, "go.kubebuilder.io/v3") || strings.Contains(pluginKey, "go.kubebuilder.io/v2") {
57+
return true
58+
}
59+
}
60+
return false
61+
}
62+
5063
// Validate ensures a Plugin is valid.
5164
func Validate(p Plugin) error {
5265
if err := validateName(p.Name()); err != nil {

pkg/plugin/util/helpers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@ import (
2020
"sigs.k8s.io/kubebuilder/v3/pkg/config"
2121
)
2222

23+
// Deprecated: go/v4 no longer supports v1beta1 option
2324
// HasDifferentCRDVersion returns true if any other CRD version is tracked in the project configuration.
2425
func HasDifferentCRDVersion(config config.Config, crdVersion string) bool {
2526
return hasDifferentAPIVersion(config.ListCRDVersions(), crdVersion)
2627
}
2728

29+
// Deprecated: go/v4 no longer supports v1beta1 option
2830
// HasDifferentWebhookVersion returns true if any other webhook version is tracked in the project configuration.
2931
func HasDifferentWebhookVersion(config config.Config, webhookVersion string) bool {
3032
return hasDifferentAPIVersion(config.ListWebhookVersions(), webhookVersion)
3133
}
3234

35+
// Deprecated: go/v4 no longer supports v1beta1 option
3336
func hasDifferentAPIVersion(versions []string, version string) bool {
3437
return !(len(versions) == 0 || (len(versions) == 1 && versions[0] == version))
3538
}

pkg/plugin/util/util.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,25 @@ func EnsureExistAndReplace(input, match, replace string) (string, error) {
191191
return strings.Replace(input, match, replace, -1), nil
192192
}
193193

194+
func HasFragment(path, target string) (bool, error) {
195+
_, err := os.Stat(path)
196+
if err != nil {
197+
return false, err
198+
}
199+
200+
// false positive
201+
// nolint:gosec
202+
b, err := os.ReadFile(path)
203+
if err != nil {
204+
return false, err
205+
}
206+
207+
if !strings.Contains(string(b), target) {
208+
return false, nil
209+
}
210+
return true, nil
211+
}
212+
194213
// ReplaceInFile replaces all instances of old with new in the file at path.
195214
func ReplaceInFile(path, old, new string) error {
196215
info, err := os.Stat(path)

pkg/plugins/golang/declarative/v1/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error {
100100
}
101101

102102
// Update Dockerfile
103-
err = updateDockerfile()
103+
// nolint:staticcheck
104+
err = updateDockerfile(plugin.IsLegacyLayout(p.config))
104105
if err != nil {
105106
return err
106107
}

pkg/plugins/golang/declarative/v1/init.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,32 @@ func (p *initSubcommand) InjectConfig(c config.Config) error {
4141
}
4242

4343
func (p *initSubcommand) Scaffold(_ machinery.Filesystem) error {
44-
err := updateDockerfile()
44+
//nolint:staticcheck
45+
err := updateDockerfile(plugin.IsLegacyLayout(p.config))
4546
if err != nil {
4647
return err
4748
}
4849
return nil
4950
}
5051

5152
// updateDockerfile will add channels staging required for declarative plugin
52-
func updateDockerfile() error {
53+
func updateDockerfile(isLegacyLayout bool) error {
5354
fmt.Println("updating Dockerfile to add channels/ directory in the image")
54-
managerFile := filepath.Join("Dockerfile")
55+
dockerfile := filepath.Join("Dockerfile")
5556

57+
controllerPath := "internal/controller/"
58+
if isLegacyLayout {
59+
controllerPath = "controllers/"
60+
}
5661
// nolint:lll
57-
err := insertCodeIfDoesNotExist(managerFile,
58-
"COPY controllers/ controllers/",
62+
err := insertCodeIfDoesNotExist(dockerfile,
63+
fmt.Sprintf("COPY %s %s", controllerPath, controllerPath),
5964
"\n# https://github.com/kubernetes-sigs/kubebuilder-declarative-pattern/blob/master/docs/addon/walkthrough/README.md#adding-a-manifest\n# Stage channels and make readable\nCOPY channels/ /channels/\nRUN chmod -R a+rx /channels/")
6065
if err != nil {
6166
return err
6267
}
6368

64-
err = insertCodeIfDoesNotExist(managerFile,
69+
err = insertCodeIfDoesNotExist(dockerfile,
6570
"COPY --from=builder /workspace/manager .",
6671
"\n# copy channels\nCOPY --from=builder /channels /channels\n")
6772
if err != nil {

0 commit comments

Comments
 (0)