Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat : add kustomize file for crd sample #3084

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ layout:
`go mod tidy` to ensure that you get the latest dependencies and your Golang code has no breaking changes.
- 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
default scaffolds to be applied on your project
- Create `config/samples/kustomization.yaml` with all CR samples specified. (see for example `testdata/project-v4/config/samples/kustomization.yaml`)
- Replace the import `admissionv1beta1 "k8s.io/api/admission/v1beta1"` with `admissionv1 "k8s.io/api/admission/v1"` in the webhook test files

<aside class="warning">
Expand Down
13 changes: 8 additions & 5 deletions docs/book/src/migration/v3vsv4.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# go/v3 vs go/v4-alpha
# go/v3 vs go/v4-alpha

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`.

The details of all changes (breaking or otherwise) can be found in:

- [controller-runtime][controller-runtime]
- [controller-tools][controller-tools]
- [kustomize][kustomize-release]
Expand All @@ -12,11 +13,12 @@ The details of all changes (breaking or otherwise) can be found in:

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

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

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

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

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

## Migrating to Kubebuilder go/v4-alpha

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

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

**Note**: The `go/v4-alpha` plugin is an unstable version and can have breaking changes in future releases.

</aside>

- [Migration Guide go/v3 to go/v4][migration-guide-gov3-to-gov4] **(Recommended)**
Expand Down
19 changes: 19 additions & 0 deletions pkg/plugins/common/kustomize/v2-alpha/scaffolds/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package scaffolds

Copy link
Member

@camilamacedo86 camilamacedo86 Nov 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Kavinjsir great work, just a few nits:

a) We cannot use 🌱 on this one because this change affects the end-user so that need to be sparkles
b) We need also add this to the migration guide from v3 to v4

However, all is great. Just address this both needs it has my lgtm

c/c @everettraven @varshaprasad96 this one helps SDK because their has a plugin manifests that will create this file so that is possible build the bundle with the owned CRDs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @camilamacedo86 thx for reminding the documentation. Just added some in the place you suggested.

import (
"fmt"
"strings"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
Expand Down Expand Up @@ -68,10 +69,23 @@ func (s *apiScaffolder) Scaffold() error {
machinery.WithResource(&s.resource),
)

rs, err := s.config.GetResources()
if err != nil {
return err
}

crdManifests := []string{}
for _, r := range rs {
crdManifests = append(crdManifests, s.generateManifestsPath(r))
}

crdManifests = append(crdManifests, s.generateManifestsPath(s.resource))

// Keep track of these values before the update
if s.resource.HasAPI() {
if err := scaffold.Execute(
&samples.CRDSample{Force: s.force},
&samples.Kustomization{CRDManifests: crdManifests},
&rbac.CRDEditorRole{},
&rbac.CRDViewerRole{},
&patches.EnableWebhookPatch{},
Expand All @@ -85,3 +99,8 @@ func (s *apiScaffolder) Scaffold() error {

return nil
}

func (s *apiScaffolder) generateManifestsPath(r resource.Resource) string {
// nolint: lll
return strings.ToLower(r.GVK.Group) + "_" + strings.ToLower(r.GVK.Version) + "_" + strings.ToLower(r.GVK.Kind) + ".yaml"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package samples

import (
"bytes"
"fmt"
"path/filepath"
"text/template"

"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
)

var _ machinery.Template = &Kustomization{}

// Kustomization scaffolds a file that defines the kustomization scheme for the prometheus folder
type Kustomization struct {
machinery.TemplateMixin

CRDManifests []string
}

// SetTemplateDefaults implements file.Template
func (f *Kustomization) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("config", "samples", "kustomization.yaml")
}

defaultTemplate, err := f.createTemplate()
if err != nil {
return err
}

f.TemplateBody = defaultTemplate

f.IfExistsAction = machinery.OverwriteFile

return nil
}

func (f *Kustomization) createTemplate() (string, error) {
t := template.Must(template.New("customResourcesConfig").Parse(kustomizationTemplate))

outputTmpl := &bytes.Buffer{}
if err := t.Execute(outputTmpl, f.CRDManifests); err != nil {
return "", fmt.Errorf("error when generating sample kustomization manifest: %w", err)
}

return outputTmpl.String(), nil

}

const kustomizationTemplate = `---
resources:{{ range $i ,$e := . }}
- {{ . }}{{end}}
`
5 changes: 5 additions & 0 deletions testdata/project-v4-config/config/samples/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
resources:
- crew_v1_captain.yaml
- crew_v1_firstmate.yaml
- crew_v1_admiral.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
resources:
- crew_v1_captain.yaml
- crew_v1_firstmate.yaml
- crew_v1_admiral.yaml
13 changes: 13 additions & 0 deletions testdata/project-v4-multigroup/config/samples/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
resources:
- crew_v1_captain.yaml
- ship_v1beta1_frigate.yaml
- ship_v1_destroyer.yaml
- ship_v2alpha1_cruiser.yaml
- sea-creatures_v1beta1_kraken.yaml
- sea-creatures_v1beta2_leviathan.yaml
- foo.policy_v1_healthcheckpolicy.yaml
- apps_v1_deployment.yaml
- foo_v1_bar.yaml
- fiz_v1_bar.yaml
- _v1_lakers.yaml
5 changes: 5 additions & 0 deletions testdata/project-v4/config/samples/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
resources:
- crew_v1_captain.yaml
- crew_v1_firstmate.yaml
- crew_v1_admiral.yaml