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

     1  /*
     2  Copyright 2020 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 crd
    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 file that defines the kustomization scheme for the crd folder
    32  type Kustomization struct {
    33  	machinery.TemplateMixin
    34  	machinery.ResourceMixin
    35  }
    36  
    37  // SetTemplateDefaults implements file.Template
    38  func (f *Kustomization) SetTemplateDefaults() error {
    39  	if f.Path == "" {
    40  		f.Path = filepath.Join("config", "crd", "kustomization.yaml")
    41  	}
    42  	f.Path = f.Resource.Replacer().Replace(f.Path)
    43  
    44  	f.TemplateBody = fmt.Sprintf(kustomizationTemplate,
    45  		machinery.NewMarkerFor(f.Path, resourceMarker),
    46  		machinery.NewMarkerFor(f.Path, webhookPatchMarker),
    47  		machinery.NewMarkerFor(f.Path, caInjectionPatchMarker),
    48  	)
    49  
    50  	return nil
    51  }
    52  
    53  //nolint:gosec to ignore false complain G101: Potential hardcoded credentials (gosec)
    54  const (
    55  	resourceMarker         = "crdkustomizeresource"
    56  	webhookPatchMarker     = "crdkustomizewebhookpatch"
    57  	caInjectionPatchMarker = "crdkustomizecainjectionpatch"
    58  )
    59  
    60  // GetMarkers implements file.Inserter
    61  func (f *Kustomization) GetMarkers() []machinery.Marker {
    62  	return []machinery.Marker{
    63  		machinery.NewMarkerFor(f.Path, resourceMarker),
    64  		machinery.NewMarkerFor(f.Path, webhookPatchMarker),
    65  		machinery.NewMarkerFor(f.Path, caInjectionPatchMarker),
    66  	}
    67  }
    68  
    69  const (
    70  	resourceCodeFragment = `- bases/%s_%s.yaml
    71  `
    72  	webhookPatchCodeFragment = `#- patches/webhook_in_%s.yaml
    73  `
    74  	caInjectionPatchCodeFragment = `#- patches/cainjection_in_%s.yaml
    75  `
    76  )
    77  
    78  // GetCodeFragments implements file.Inserter
    79  func (f *Kustomization) GetCodeFragments() machinery.CodeFragmentsMap {
    80  	fragments := make(machinery.CodeFragmentsMap, 3)
    81  
    82  	// Generate resource code fragments
    83  	res := make([]string, 0)
    84  	res = append(res, fmt.Sprintf(resourceCodeFragment, f.Resource.QualifiedGroup(), f.Resource.Plural))
    85  
    86  	// Generate resource code fragments
    87  	webhookPatch := make([]string, 0)
    88  	webhookPatch = append(webhookPatch, fmt.Sprintf(webhookPatchCodeFragment, f.Resource.Plural))
    89  
    90  	// Generate resource code fragments
    91  	caInjectionPatch := make([]string, 0)
    92  	caInjectionPatch = append(caInjectionPatch, fmt.Sprintf(caInjectionPatchCodeFragment, f.Resource.Plural))
    93  
    94  	// Only store code fragments in the map if the slices are non-empty
    95  	if len(res) != 0 {
    96  		fragments[machinery.NewMarkerFor(f.Path, resourceMarker)] = res
    97  	}
    98  	if len(webhookPatch) != 0 {
    99  		fragments[machinery.NewMarkerFor(f.Path, webhookPatchMarker)] = webhookPatch
   100  	}
   101  	if len(caInjectionPatch) != 0 {
   102  		fragments[machinery.NewMarkerFor(f.Path, caInjectionPatchMarker)] = caInjectionPatch
   103  	}
   104  
   105  	return fragments
   106  }
   107  
   108  var kustomizationTemplate = `# This kustomization.yaml is not intended to be run by itself,
   109  # since it depends on service name and namespace that are out of this kustomize package.
   110  # It should be run by config/default
   111  resources:
   112  %s
   113  
   114  patchesStrategicMerge:
   115  # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix.
   116  # patches here are for enabling the conversion webhook for each CRD
   117  %s
   118  
   119  # [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix.
   120  # patches here are for enabling the CA injection for each CRD
   121  %s
   122  
   123  # the following config is for teaching kustomize how to do kustomization for CRDs.
   124  configurations:
   125  - kustomizeconfig.yaml
   126  `