Skip to content

Commit

Permalink
Merge pull request #3102 from camilamacedo86/fix-kust
Browse files Browse the repository at this point in the history
馃悰  (kustomize/v2-alpha): use marker to track samples and do not create tracks for core types
  • Loading branch information
camilamacedo86 committed Nov 28, 2022
2 parents cc703b2 + e615d6a commit ce42706
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 71 deletions.
27 changes: 7 additions & 20 deletions pkg/plugins/common/kustomize/v2-alpha/scaffolds/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package scaffolds

import (
"fmt"
"strings"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
Expand Down Expand Up @@ -69,23 +67,10 @@ 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 @@ -95,12 +80,14 @@ func (s *apiScaffolder) Scaffold() error {
); err != nil {
return fmt.Errorf("error scaffolding kustomize API manifests: %v", err)
}

// If the gvk is non-empty
if s.resource.Group != "" || s.resource.Version != "" || s.resource.Kind != "" {
if err := scaffold.Execute(&samples.Kustomization{}); err != nil {
return fmt.Errorf("error scaffolding manifests: %v", err)
}
}
}

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
Expand Up @@ -17,54 +17,58 @@ limitations under the License.
package samples

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

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

var _ machinery.Template = &Kustomization{}
var (
_ machinery.Template = &Kustomization{}
_ machinery.Inserter = &Kustomization{}
)

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

CRDManifests []string
machinery.ResourceMixin
}

// SetTemplateDefaults implements file.Template
// SetTemplateDefaults implements machinery.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
f.TemplateBody = fmt.Sprintf(kustomizationTemplate, machinery.NewMarkerFor(f.Path, samplesMarker))

return nil
}

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

outputTmpl := &bytes.Buffer{}
if err := t.Execute(outputTmpl, f.CRDManifests); err != nil {
return "", fmt.Errorf("error when generating sample kustomization manifest: %w", err)
}
// GetMarkers implements file.Inserter
func (f *Kustomization) GetMarkers() []machinery.Marker {
return []machinery.Marker{machinery.NewMarkerFor(f.Path, samplesMarker)}
}

return outputTmpl.String(), nil
const samplesCodeFragment = `- %s
`

// makeCRFileName returns a Custom Resource example file name in the same format
// as kubebuilder's CreateAPI plugin for a gvk.
func (f Kustomization) makeCRFileName() string {
return f.Resource.Replacer().Replace("%[group]_%[version]_%[kind].yaml")
}

// GetCodeFragments implements file.Inserter
func (f *Kustomization) GetCodeFragments() machinery.CodeFragmentsMap {
return machinery.CodeFragmentsMap{
machinery.NewMarkerFor(f.Path, samplesMarker): []string{fmt.Sprintf(samplesCodeFragment, f.makeCRFileName())},
}
}

const kustomizationTemplate = `---
resources:{{ range $i ,$e := . }}
- {{ . }}{{end}}
const kustomizationTemplate = `## Append samples of your project ##
resources:
%s
`
9 changes: 5 additions & 4 deletions testdata/project-v4-config/config/samples/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
## Append samples of your project ##
resources:
- crew_v1_captain.yaml
- crew_v1_firstmate.yaml
- crew_v1_admiral.yaml
- crew_v1_captain.yaml
- crew_v1_firstmate.yaml
- crew_v1_admiral.yaml
#+kubebuilder:scaffold:manifestskustomizesamples
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
## Append samples of your project ##
resources:
- crew_v1_captain.yaml
- crew_v1_firstmate.yaml
- crew_v1_admiral.yaml
- crew_v1_captain.yaml
- crew_v1_firstmate.yaml
- crew_v1_admiral.yaml
#+kubebuilder:scaffold:manifestskustomizesamples
24 changes: 12 additions & 12 deletions testdata/project-v4-multigroup/config/samples/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
## Append samples of your project ##
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
- 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
- foo_v1_bar.yaml
- fiz_v1_bar.yaml
- _v1_lakers.yaml
#+kubebuilder:scaffold:manifestskustomizesamples
9 changes: 5 additions & 4 deletions testdata/project-v4/config/samples/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
## Append samples of your project ##
resources:
- crew_v1_captain.yaml
- crew_v1_firstmate.yaml
- crew_v1_admiral.yaml
- crew_v1_captain.yaml
- crew_v1_firstmate.yaml
- crew_v1_admiral.yaml
#+kubebuilder:scaffold:manifestskustomizesamples

0 comments on commit ce42706

Please sign in to comment.