sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v3/scaffolds/api.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 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 "sigs.k8s.io/kubebuilder/v3/pkg/machinery" 27 "sigs.k8s.io/kubebuilder/v3/pkg/model/resource" 28 "sigs.k8s.io/kubebuilder/v3/pkg/plugins" 29 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds/internal/templates" 30 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds/internal/templates/api" 31 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds/internal/templates/controllers" 32 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds/internal/templates/hack" 33 ) 34 35 var _ plugins.Scaffolder = &apiScaffolder{} 36 37 // apiScaffolder contains configuration for generating scaffolding for Go type 38 // representing the API and controller that implements the behavior for the API. 39 type apiScaffolder struct { 40 config config.Config 41 resource resource.Resource 42 43 // fs is the filesystem that will be used by the scaffolder 44 fs machinery.Filesystem 45 46 // force indicates whether to scaffold controller files even if it exists or not 47 force bool 48 } 49 50 // NewAPIScaffolder returns a new Scaffolder for API/controller creation operations 51 func NewAPIScaffolder(config config.Config, res resource.Resource, force bool) plugins.Scaffolder { 52 return &apiScaffolder{ 53 config: config, 54 resource: res, 55 force: force, 56 } 57 } 58 59 // InjectFS implements cmdutil.Scaffolder 60 func (s *apiScaffolder) InjectFS(fs machinery.Filesystem) { 61 s.fs = fs 62 } 63 64 // Scaffold implements cmdutil.Scaffolder 65 func (s *apiScaffolder) Scaffold() error { 66 log.Println("Writing scaffold for you to edit...") 67 68 // Load the boilerplate 69 boilerplate, err := afero.ReadFile(s.fs.FS, hack.DefaultBoilerplatePath) 70 if err != nil { 71 return fmt.Errorf("error scaffolding API/controller: unable to load boilerplate: %w", err) 72 } 73 74 // Initialize the machinery.Scaffold that will write the files to disk 75 scaffold := machinery.NewScaffold(s.fs, 76 machinery.WithConfig(s.config), 77 machinery.WithBoilerplate(string(boilerplate)), 78 machinery.WithResource(&s.resource), 79 ) 80 81 // Keep track of these values before the update 82 doAPI := s.resource.HasAPI() 83 doController := s.resource.HasController() 84 85 if err := s.config.UpdateResource(s.resource); err != nil { 86 return fmt.Errorf("error updating resource: %w", err) 87 } 88 89 if doAPI { 90 if err := scaffold.Execute( 91 &api.Types{Force: s.force}, 92 &api.Group{}, 93 ); err != nil { 94 return fmt.Errorf("error scaffolding APIs: %v", err) 95 } 96 } 97 98 if doController { 99 if err := scaffold.Execute( 100 &controllers.SuiteTest{Force: s.force}, 101 &controllers.Controller{ControllerRuntimeVersion: ControllerRuntimeVersion, Force: s.force}, 102 ); err != nil { 103 return fmt.Errorf("error scaffolding controller: %v", err) 104 } 105 } 106 107 if err := scaffold.Execute( 108 &templates.MainUpdater{WireResource: doAPI, WireController: doController}, 109 ); err != nil { 110 return fmt.Errorf("error updating main.go: %v", err) 111 } 112 113 return nil 114 }