sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/samples/kustomization.go (about)

     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  	"fmt"
    21  	"path/filepath"
    22  
    23  	"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
    24  )
    25  
    26  var (
    27  	_ machinery.Template = &Kustomization{}
    28  	_ machinery.Inserter = &Kustomization{}
    29  )
    30  
    31  // Kustomization scaffolds a kustomization.yaml for the manifests overlay folder.
    32  type Kustomization struct {
    33  	machinery.TemplateMixin
    34  	machinery.ResourceMixin
    35  }
    36  
    37  // SetTemplateDefaults implements machinery.Template
    38  func (f *Kustomization) SetTemplateDefaults() error {
    39  	if f.Path == "" {
    40  		f.Path = filepath.Join("config", "samples", "kustomization.yaml")
    41  	}
    42  	f.TemplateBody = fmt.Sprintf(kustomizationTemplate, machinery.NewMarkerFor(f.Path, samplesMarker))
    43  
    44  	return nil
    45  }
    46  
    47  const (
    48  	samplesMarker = "manifestskustomizesamples"
    49  )
    50  
    51  // GetMarkers implements file.Inserter
    52  func (f *Kustomization) GetMarkers() []machinery.Marker {
    53  	return []machinery.Marker{machinery.NewMarkerFor(f.Path, samplesMarker)}
    54  }
    55  
    56  const samplesCodeFragment = `- %s
    57  `
    58  
    59  // makeCRFileName returns a Custom Resource example file name in the same format
    60  // as kubebuilder's CreateAPI plugin for a gvk.
    61  func (f Kustomization) makeCRFileName() string {
    62  	return f.Resource.Replacer().Replace("%[group]_%[version]_%[kind].yaml")
    63  }
    64  
    65  // GetCodeFragments implements file.Inserter
    66  func (f *Kustomization) GetCodeFragments() machinery.CodeFragmentsMap {
    67  	return machinery.CodeFragmentsMap{
    68  		machinery.NewMarkerFor(f.Path, samplesMarker): []string{fmt.Sprintf(samplesCodeFragment, f.makeCRFileName())},
    69  	}
    70  }
    71  
    72  const kustomizationTemplate = `## Append samples of your project ##
    73  resources:
    74  %s
    75  `