sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v2/scaffolds/webhook.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/v2/scaffolds/internal/templates" 30 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates/api" 31 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates/hack" 32 ) 33 34 var _ plugins.Scaffolder = &webhookScaffolder{} 35 36 type webhookScaffolder struct { 37 config config.Config 38 resource resource.Resource 39 40 // fs is the filesystem that will be used by the scaffolder 41 fs machinery.Filesystem 42 } 43 44 // NewWebhookScaffolder returns a new Scaffolder for v2 webhook creation operations 45 func NewWebhookScaffolder(config config.Config, resource resource.Resource) plugins.Scaffolder { 46 return &webhookScaffolder{ 47 config: config, 48 resource: resource, 49 } 50 } 51 52 // InjectFS implements cmdutil.Scaffolder 53 func (s *webhookScaffolder) InjectFS(fs machinery.Filesystem) { 54 s.fs = fs 55 } 56 57 // Scaffold implements cmdutil.Scaffolder 58 func (s *webhookScaffolder) Scaffold() error { 59 log.Println("Writing scaffold for you to edit...") 60 61 // Load the boilerplate 62 boilerplate, err := afero.ReadFile(s.fs.FS, hack.DefaultBoilerplatePath) 63 if err != nil { 64 return fmt.Errorf("error scaffolding webhook: unable to load boilerplate: %w", err) 65 } 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.WithBoilerplate(string(boilerplate)), 71 machinery.WithResource(&s.resource), 72 ) 73 74 if err := s.config.UpdateResource(s.resource); err != nil { 75 return fmt.Errorf("error updating resource: %w", err) 76 } 77 78 if err := scaffold.Execute( 79 &api.Webhook{}, 80 &templates.MainUpdater{WireWebhook: true}, 81 ); err != nil { 82 return err 83 } 84 85 if s.resource.HasConversionWebhook() { 86 fmt.Println(`Webhook server has been set up for you. 87 You need to implement the conversion.Hub and conversion.Convertible interfaces for your CRD types.`) 88 } 89 90 return nil 91 }