sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v4/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 "errors" 21 "fmt" 22 23 log "github.com/sirupsen/logrus" 24 "github.com/spf13/afero" 25 26 "sigs.k8s.io/kubebuilder/v3/pkg/config" 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/v4/scaffolds/internal/templates" 31 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v4/scaffolds/internal/templates/api" 32 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v4/scaffolds/internal/templates/controllers" 33 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v4/scaffolds/internal/templates/hack" 34 ) 35 36 var _ plugins.Scaffolder = &apiScaffolder{} 37 38 // apiScaffolder contains configuration for generating scaffolding for Go type 39 // representing the API and controller that implements the behavior for the API. 40 type apiScaffolder struct { 41 config config.Config 42 resource resource.Resource 43 44 // fs is the filesystem that will be used by the scaffolder 45 fs machinery.Filesystem 46 47 // force indicates whether to scaffold controller files even if it exists or not 48 force bool 49 } 50 51 // NewAPIScaffolder returns a new Scaffolder for API/controller creation operations 52 func NewAPIScaffolder(config config.Config, res resource.Resource, force bool) plugins.Scaffolder { 53 return &apiScaffolder{ 54 config: config, 55 resource: res, 56 force: force, 57 } 58 } 59 60 // InjectFS implements cmdutil.Scaffolder 61 func (s *apiScaffolder) InjectFS(fs machinery.Filesystem) { 62 s.fs = fs 63 } 64 65 // Scaffold implements cmdutil.Scaffolder 66 func (s *apiScaffolder) Scaffold() error { 67 log.Println("Writing scaffold for you to edit...") 68 69 // Load the boilerplate 70 boilerplate, err := afero.ReadFile(s.fs.FS, hack.DefaultBoilerplatePath) 71 if err != nil { 72 if errors.Is(err, afero.ErrFileNotFound) { 73 boilerplate = []byte("") 74 } else { 75 return fmt.Errorf("error scaffolding API/controller: unable to load boilerplate: %w", err) 76 } 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 if err := s.config.UpdateResource(s.resource); err != nil { 91 return fmt.Errorf("error updating resource: %w", err) 92 } 93 94 if doAPI { 95 if err := scaffold.Execute( 96 &api.Types{Force: s.force}, 97 &api.Group{}, 98 ); err != nil { 99 return fmt.Errorf("error scaffolding APIs: %v", err) 100 } 101 } 102 103 if doController { 104 if err := scaffold.Execute( 105 &controllers.SuiteTest{Force: s.force, K8SVersion: EnvtestK8SVersion}, 106 &controllers.Controller{ControllerRuntimeVersion: ControllerRuntimeVersion, Force: s.force}, 107 &controllers.ControllerTest{Force: s.force, DoAPI: doAPI}, 108 ); err != nil { 109 return fmt.Errorf("error scaffolding controller: %v", err) 110 } 111 } 112 113 if err := scaffold.Execute( 114 &templates.MainUpdater{WireResource: doAPI, WireController: doController}, 115 ); err != nil { 116 return fmt.Errorf("error updating cmd/main.go: %v", err) 117 } 118 119 return nil 120 }