sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v2/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  //go:deprecated This package has been deprecated
    18  package v2
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"github.com/spf13/pflag"
    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/plugin"
    30  	goPlugin "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang"
    31  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds"
    32  )
    33  
    34  var _ plugin.CreateWebhookSubcommand = &createWebhookSubcommand{}
    35  
    36  type createWebhookSubcommand struct {
    37  	config config.Config
    38  	// For help text.
    39  	commandName string
    40  
    41  	options *goPlugin.Options
    42  
    43  	resource *resource.Resource
    44  }
    45  
    46  func (p *createWebhookSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
    47  	p.commandName = cliMeta.CommandName
    48  
    49  	subcmdMeta.Description = `Scaffold a webhook for an API resource. You can choose to scaffold defaulting,
    50  validating and/or conversion webhooks.
    51  `
    52  	subcmdMeta.Examples = fmt.Sprintf(`  # Create defaulting and validating webhooks for Group: ship, Version: v1beta1
    53    # and Kind: Frigate
    54    %[1]s create webhook --group ship --version v1beta1 --kind Frigate --defaulting --programmatic-validation
    55  
    56    # Create conversion webhook for Group: ship, Version: v1beta1
    57    # and Kind: Frigate
    58    %[1]s create webhook --group ship --version v1beta1 --kind Frigate --conversion
    59  `, cliMeta.CommandName)
    60  }
    61  
    62  func (p *createWebhookSubcommand) BindFlags(fs *pflag.FlagSet) {
    63  	p.options = &goPlugin.Options{WebhookVersion: "v1beta1"}
    64  
    65  	fs.StringVar(&p.options.Plural, "resource", "", "resource irregular plural form")
    66  
    67  	fs.BoolVar(&p.options.DoDefaulting, "defaulting", false,
    68  		"if set, scaffold the defaulting webhook")
    69  	fs.BoolVar(&p.options.DoValidation, "programmatic-validation", false,
    70  		"if set, scaffold the validating webhook")
    71  	fs.BoolVar(&p.options.DoConversion, "conversion", false,
    72  		"if set, scaffold the conversion webhook")
    73  }
    74  
    75  func (p *createWebhookSubcommand) InjectConfig(c config.Config) error {
    76  	p.config = c
    77  
    78  	return nil
    79  }
    80  
    81  func (p *createWebhookSubcommand) InjectResource(res *resource.Resource) error {
    82  	p.resource = res
    83  
    84  	if p.resource.Group == "" {
    85  		return fmt.Errorf("group cannot be empty")
    86  	}
    87  
    88  	p.options.UpdateResource(p.resource, p.config)
    89  
    90  	if err := p.resource.Validate(); err != nil {
    91  		return err
    92  	}
    93  
    94  	if !p.resource.HasDefaultingWebhook() && !p.resource.HasValidationWebhook() && !p.resource.HasConversionWebhook() {
    95  		return fmt.Errorf("%s create webhook requires at least one of --defaulting,"+
    96  			" --programmatic-validation and --conversion to be true", p.commandName)
    97  	}
    98  
    99  	// check if resource exist to create webhook
   100  	if p.config.GetVersion().Compare(cfgv2.Version) == 0 {
   101  		if !p.config.HasResource(p.resource.GVK) {
   102  			return fmt.Errorf("%s create webhook requires a previously created API ", p.commandName)
   103  		}
   104  	} else {
   105  		if r, err := p.config.GetResource(p.resource.GVK); err != nil {
   106  			return fmt.Errorf("%s create webhook requires a previously created API ", p.commandName)
   107  		} else if r.Webhooks != nil && !r.Webhooks.IsEmpty() {
   108  			return fmt.Errorf("webhook resource already exists")
   109  		}
   110  	}
   111  
   112  	return nil
   113  }
   114  
   115  func (p *createWebhookSubcommand) Scaffold(fs machinery.Filesystem) error {
   116  	scaffolder := scaffolds.NewWebhookScaffolder(p.config, *p.resource)
   117  	scaffolder.InjectFS(fs)
   118  	return scaffolder.Scaffold()
   119  }