sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/common/kustomize/v1/scaffolds/api.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 scaffolds 18 19 import ( 20 "fmt" 21 22 log "github.com/sirupsen/logrus" 23 24 "sigs.k8s.io/kubebuilder/v3/pkg/config" 25 "sigs.k8s.io/kubebuilder/v3/pkg/machinery" 26 "sigs.k8s.io/kubebuilder/v3/pkg/model/resource" 27 "sigs.k8s.io/kubebuilder/v3/pkg/plugins" 28 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/crd" 29 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/crd/patches" 30 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/rbac" 31 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/samples" 32 ) 33 34 var _ plugins.Scaffolder = &apiScaffolder{} 35 36 // apiScaffolder contains configuration for generating scaffolding for Go type 37 // representing the API and controller that implements the behavior for the API. 38 type apiScaffolder struct { 39 config config.Config 40 resource resource.Resource 41 42 // fs is the filesystem that will be used by the scaffolder 43 fs machinery.Filesystem 44 45 // force indicates whether to scaffold files even if they exist. 46 force bool 47 } 48 49 // NewAPIScaffolder returns a new Scaffolder for API/controller creation operations 50 func NewAPIScaffolder(config config.Config, res resource.Resource, force bool) plugins.Scaffolder { 51 return &apiScaffolder{ 52 config: config, 53 resource: res, 54 force: force, 55 } 56 } 57 58 // InjectFS implements cmdutil.Scaffolder 59 func (s *apiScaffolder) InjectFS(fs machinery.Filesystem) { 60 s.fs = fs 61 } 62 63 // Scaffold implements cmdutil.Scaffolder 64 func (s *apiScaffolder) Scaffold() error { 65 log.Println("Writing kustomize manifests for you to edit...") 66 67 // Initialize the machinery.Scaffold that will write the files to disk 68 scaffold := machinery.NewScaffold(s.fs, 69 machinery.WithConfig(s.config), 70 machinery.WithResource(&s.resource), 71 ) 72 73 // Keep track of these values before the update 74 if s.resource.HasAPI() { 75 if err := scaffold.Execute( 76 &samples.CRDSample{Force: s.force}, 77 &rbac.CRDEditorRole{}, 78 &rbac.CRDViewerRole{}, 79 &patches.EnableWebhookPatch{}, 80 &patches.EnableCAInjectionPatch{}, 81 &crd.Kustomization{}, 82 &crd.KustomizeConfig{}, 83 ); err != nil { 84 return fmt.Errorf("error scaffolding kustomize API manifests: %v", err) 85 } 86 } 87 88 return nil 89 }