sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v2/scaffolds/api.go (about) 1 /* 2 Copyright 2019 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 "github.com/spf13/afero" 24 25 "sigs.k8s.io/kubebuilder/v3/pkg/config" 26 cfgv2 "sigs.k8s.io/kubebuilder/v3/pkg/config/v2" 27 "sigs.k8s.io/kubebuilder/v3/pkg/machinery" 28 "sigs.k8s.io/kubebuilder/v3/pkg/model/resource" 29 "sigs.k8s.io/kubebuilder/v3/pkg/plugins" 30 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates" 31 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates/api" 32 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates/config/crd" 33 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates/config/crd/patches" 34 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates/config/rbac" 35 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates/config/samples" 36 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates/controllers" 37 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates/hack" 38 ) 39 40 var _ plugins.Scaffolder = &apiScaffolder{} 41 42 // apiScaffolder contains configuration for generating scaffolding for Go type 43 // representing the API and controller that implements the behavior for the API. 44 type apiScaffolder struct { 45 config config.Config 46 resource resource.Resource 47 48 // fs is the filesystem that will be used by the scaffolder 49 fs machinery.Filesystem 50 51 // force indicates whether to scaffold controller files even if it exists or not 52 force bool 53 } 54 55 // NewAPIScaffolder returns a new Scaffolder for API/controller creation operations 56 func NewAPIScaffolder(config config.Config, res resource.Resource, force bool) plugins.Scaffolder { 57 return &apiScaffolder{ 58 config: config, 59 resource: res, 60 force: force, 61 } 62 } 63 64 // InjectFS implements cmdutil.Scaffolder 65 func (s *apiScaffolder) InjectFS(fs machinery.Filesystem) { 66 s.fs = fs 67 } 68 69 // Scaffold implements cmdutil.Scaffolder 70 func (s *apiScaffolder) Scaffold() error { 71 log.Println("Writing scaffold for you to edit...") 72 73 // Load the boilerplate 74 boilerplate, err := afero.ReadFile(s.fs.FS, hack.DefaultBoilerplatePath) 75 if err != nil { 76 return fmt.Errorf("error scaffolding API/controller: unable to load boilerplate: %w", err) 77 } 78 79 // Initialize the machinery.Scaffold that will write the files to disk 80 scaffold := machinery.NewScaffold(s.fs, 81 machinery.WithConfig(s.config), 82 machinery.WithBoilerplate(string(boilerplate)), 83 machinery.WithResource(&s.resource), 84 ) 85 86 // Keep track of these values before the update 87 doAPI := s.resource.HasAPI() 88 doController := s.resource.HasController() 89 90 // Project version v2 only tracked GVK triplets of each resource. 91 // As they were only tracked when the API was scaffolded, the presence of a 92 // resource in the config file was used in webhook creation to verify that 93 // the API had been scaffolded previously. From project version v3 onwards 94 // this information is stored in the API field of the resource, so we can 95 // update the resources except for project version 2 when no API was scaffolded. 96 if doAPI || s.config.GetVersion().Compare(cfgv2.Version) == 1 { 97 if err := s.config.UpdateResource(s.resource); err != nil { 98 return fmt.Errorf("error updating resource: %w", err) 99 } 100 } 101 102 if doAPI { 103 if err := scaffold.Execute( 104 &api.Types{Force: s.force}, 105 &api.Group{}, 106 &samples.CRDSample{Force: s.force}, 107 &rbac.CRDEditorRole{}, 108 &rbac.CRDViewerRole{}, 109 &patches.EnableWebhookPatch{}, 110 &patches.EnableCAInjectionPatch{}, 111 ); err != nil { 112 return fmt.Errorf("error scaffolding APIs: %w", err) 113 } 114 115 if err := scaffold.Execute( 116 &crd.Kustomization{}, 117 &crd.KustomizeConfig{}, 118 ); err != nil { 119 return fmt.Errorf("error scaffolding kustomization: %v", err) 120 } 121 } 122 123 if doController { 124 if err := scaffold.Execute( 125 &controllers.SuiteTest{Force: s.force}, 126 &controllers.Controller{ControllerRuntimeVersion: ControllerRuntimeVersion, Force: s.force}, 127 ); err != nil { 128 return fmt.Errorf("error scaffolding controller: %v", err) 129 } 130 } 131 132 if err := scaffold.Execute( 133 &templates.MainUpdater{WireResource: doAPI, WireController: doController}, 134 ); err != nil { 135 return fmt.Errorf("error updating main.go: %v", err) 136 } 137 138 return nil 139 }