Skip to content

Commit

Permalink
feat: add kustomize file for crd sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Kavinjsir committed Nov 18, 2022
1 parent 89f58d3 commit 9896d8d
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
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

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

0 comments on commit 9896d8d

Please sign in to comment.