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