Skip to content

Commit 407b9fe

Browse files
committed
✨ (kustomize/v2-alpha): add kustomize file to track crd sample info
1 parent 051b05a commit 407b9fe

File tree

8 files changed

+126
-5
lines changed

8 files changed

+126
-5
lines changed

docs/book/src/migration/manually_migration_guide_gov3_to_gov4.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ layout:
4343
`go mod tidy` to ensure that you get the latest dependencies and your Golang code has no breaking changes.
4444
- 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
4545
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`)
4647
- Replace the import `admissionv1beta1 "k8s.io/api/admission/v1beta1"` with `admissionv1 "k8s.io/api/admission/v1"` in the webhook test files
4748

4849
<aside class="warning">

docs/book/src/migration/v3vsv4.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
# go/v3 vs go/v4-alpha
1+
# go/v3 vs go/v4-alpha
22

33
This document covers all breaking changes when migrating from projects built using the plugin go/v3 (default for any scaffold done since `28 Apr 2021`) to the next alpha version of the Golang plugin `go/v4-alpha`.
44

55
The details of all changes (breaking or otherwise) can be found in:
6+
67
- [controller-runtime][controller-runtime]
78
- [controller-tools][controller-tools]
89
- [kustomize][kustomize-release]
@@ -12,11 +13,12 @@ The details of all changes (breaking or otherwise) can be found in:
1213

1314
- `go/v4-alpha` projects use Kustomize v4x (instead of v3x)
1415
- note that some manifests under `config/` directory have been changed in order to no longer use the deprecated Kustomize features
15-
such as env vars.
16+
such as env vars.
17+
- A `kustomization.yaml` is scaffolded under `config/samples`. This helps simply and flexibly generate sample manifests: `kustomize build config/samples`.
1618
- adds support for Apple Silicon M1 (darwin/arm64)
1719
- remove support to CRD/WebHooks Kubernetes API v1beta1 version which are no longer supported since k8s 1.22
1820
- 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
19-
webhooks test files are scaffolding using `"k8s.io/api/admission/v1"` which is support from k8s `1.20`
21+
webhooks test files are scaffolding using `"k8s.io/api/admission/v1"` which is support from k8s `1.20`
2022
- no longer provide backwards compatible support with k8s versions < `1.16`
2123

2224
<aside class="note">
@@ -28,7 +30,7 @@ Further details can be found in the [go/v4-alpha plugin section][go/v4-doc]
2830

2931
## TL;DR of the New `go/v4-alpha` Plugin
3032

31-
***More details on this can be found at [here][kb-releases], but for the highlights, check below***
33+
**_More details on this can be found at [here][kb-releases], but for the highlights, check below_**
3234

3335
<aside class="note">
3436
<h1>Default plugin</h1>
@@ -47,7 +49,7 @@ For example, you should refrain from moving the scaffolded files, doing so will
4749

4850
## Migrating to Kubebuilder go/v4-alpha
4951

50-
If you want to upgrade your scaffolding to use the latest and greatest features then, follow the guide
52+
If you want to upgrade your scaffolding to use the latest and greatest features then, follow the guide
5153
which will cover the steps in the most straightforward way to allow you to upgrade your project to get all
5254
latest changes and improvements.
5355

@@ -63,6 +65,7 @@ kubebuilder init --domain my.domain --repo my.domain/guestbook --plugins=go/v4-a
6365
```
6466

6567
**Note**: The `go/v4-alpha` plugin is an unstable version and can have breaking changes in future releases.
68+
6669
</aside>
6770

6871
- [Migration Guide go/v3 to go/v4][migration-guide-gov3-to-gov4] **(Recommended)**

pkg/plugins/common/kustomize/v2-alpha/scaffolds/api.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package scaffolds
1818

1919
import (
2020
"fmt"
21+
"strings"
2122

2223
"sigs.k8s.io/kubebuilder/v3/pkg/config"
2324
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
@@ -68,10 +69,23 @@ func (s *apiScaffolder) Scaffold() error {
6869
machinery.WithResource(&s.resource),
6970
)
7071

72+
rs, err := s.config.GetResources()
73+
if err != nil {
74+
return err
75+
}
76+
77+
crdManifests := []string{}
78+
for _, r := range rs {
79+
crdManifests = append(crdManifests, s.generateManifestsPath(r))
80+
}
81+
82+
crdManifests = append(crdManifests, s.generateManifestsPath(s.resource))
83+
7184
// Keep track of these values before the update
7285
if s.resource.HasAPI() {
7386
if err := scaffold.Execute(
7487
&samples.CRDSample{Force: s.force},
88+
&samples.Kustomization{CRDManifests: crdManifests},
7589
&rbac.CRDEditorRole{},
7690
&rbac.CRDViewerRole{},
7791
&patches.EnableWebhookPatch{},
@@ -85,3 +99,8 @@ func (s *apiScaffolder) Scaffold() error {
8599

86100
return nil
87101
}
102+
103+
func (s *apiScaffolder) generateManifestsPath(r resource.Resource) string {
104+
// nolint: lll
105+
return strings.ToLower(r.GVK.Group) + "_" + strings.ToLower(r.GVK.Version) + "_" + strings.ToLower(r.GVK.Kind) + ".yaml"
106+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 samples
18+
19+
import (
20+
"bytes"
21+
"fmt"
22+
"path/filepath"
23+
"text/template"
24+
25+
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
26+
)
27+
28+
var _ machinery.Template = &Kustomization{}
29+
30+
// Kustomization scaffolds a file that defines the kustomization scheme for the prometheus folder
31+
type Kustomization struct {
32+
machinery.TemplateMixin
33+
34+
CRDManifests []string
35+
}
36+
37+
// SetTemplateDefaults implements file.Template
38+
func (f *Kustomization) SetTemplateDefaults() error {
39+
if f.Path == "" {
40+
f.Path = filepath.Join("config", "samples", "kustomization.yaml")
41+
}
42+
43+
defaultTemplate, err := f.createTemplate()
44+
if err != nil {
45+
return err
46+
}
47+
48+
f.TemplateBody = defaultTemplate
49+
50+
f.IfExistsAction = machinery.OverwriteFile
51+
52+
return nil
53+
}
54+
55+
func (f *Kustomization) createTemplate() (string, error) {
56+
t := template.Must(template.New("customResourcesConfig").Parse(kustomizationTemplate))
57+
58+
outputTmpl := &bytes.Buffer{}
59+
if err := t.Execute(outputTmpl, f.CRDManifests); err != nil {
60+
return "", fmt.Errorf("error when generating sample kustomization manifest: %w", err)
61+
}
62+
63+
return outputTmpl.String(), nil
64+
65+
}
66+
67+
const kustomizationTemplate = `---
68+
resources:{{ range $i ,$e := . }}
69+
- {{ . }}{{end}}
70+
`
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
resources:
3+
- crew_v1_captain.yaml
4+
- crew_v1_firstmate.yaml
5+
- crew_v1_admiral.yaml
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
resources:
3+
- crew_v1_captain.yaml
4+
- crew_v1_firstmate.yaml
5+
- crew_v1_admiral.yaml
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
resources:
3+
- crew_v1_captain.yaml
4+
- ship_v1beta1_frigate.yaml
5+
- ship_v1_destroyer.yaml
6+
- ship_v2alpha1_cruiser.yaml
7+
- sea-creatures_v1beta1_kraken.yaml
8+
- sea-creatures_v1beta2_leviathan.yaml
9+
- foo.policy_v1_healthcheckpolicy.yaml
10+
- apps_v1_deployment.yaml
11+
- foo_v1_bar.yaml
12+
- fiz_v1_bar.yaml
13+
- _v1_lakers.yaml
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
resources:
3+
- crew_v1_captain.yaml
4+
- crew_v1_firstmate.yaml
5+
- crew_v1_admiral.yaml

0 commit comments

Comments
 (0)