sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v3/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/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/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 // force indicates whether to scaffold controller files even if it exists or not 44 force bool 45 } 46 47 // NewWebhookScaffolder returns a new Scaffolder for v2 webhook creation operations 48 func NewWebhookScaffolder(config config.Config, resource resource.Resource, force bool) plugins.Scaffolder { 49 return &webhookScaffolder{ 50 config: config, 51 resource: resource, 52 force: force, 53 } 54 } 55 56 // InjectFS implements cmdutil.Scaffolder 57 func (s *webhookScaffolder) InjectFS(fs machinery.Filesystem) { 58 s.fs = fs 59 } 60 61 // Scaffold implements cmdutil.Scaffolder 62 func (s *webhookScaffolder) Scaffold() error { 63 log.Println("Writing scaffold for you to edit...") 64 65 // Load the boilerplate 66 boilerplate, err := afero.ReadFile(s.fs.FS, hack.DefaultBoilerplatePath) 67 if err != nil { 68 return fmt.Errorf("error scaffolding webhook: unable to load boilerplate: %w", err) 69 } 70 71 // Initialize the machinery.Scaffold that will write the files to disk 72 scaffold := machinery.NewScaffold(s.fs, 73 machinery.WithConfig(s.config), 74 machinery.WithBoilerplate(string(boilerplate)), 75 machinery.WithResource(&s.resource), 76 ) 77 78 // Keep track of these values before the update 79 doDefaulting := s.resource.HasDefaultingWebhook() 80 doValidation := s.resource.HasValidationWebhook() 81 doConversion := s.resource.HasConversionWebhook() 82 83 if err := s.config.UpdateResource(s.resource); err != nil { 84 return fmt.Errorf("error updating resource: %w", err) 85 } 86 87 if err := scaffold.Execute( 88 &api.Webhook{Force: s.force}, 89 &templates.MainUpdater{WireWebhook: true}, 90 ); err != nil { 91 return err 92 } 93 94 if doConversion { 95 fmt.Println(`Webhook server has been set up for you. 96 You need to implement the conversion.Hub and conversion.Convertible interfaces for your CRD types.`) 97 } 98 99 // TODO: Add test suite for conversion webhook after #1664 has been merged & conversion tests supported in envtest. 100 if doDefaulting || doValidation { 101 if err := scaffold.Execute( 102 &api.WebhookSuite{}, 103 ); err != nil { 104 return err 105 } 106 } 107 108 return nil 109 }