sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/external/webhook.go (about)

     1  /*
     2  Copyright 2021 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 external
    18  
    19  import (
    20  	"github.com/spf13/pflag"
    21  
    22  	"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
    23  	"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
    24  	"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
    25  	"sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
    26  )
    27  
    28  var _ plugin.CreateWebhookSubcommand = &createWebhookSubcommand{}
    29  
    30  type createWebhookSubcommand struct {
    31  	Path string
    32  	Args []string
    33  }
    34  
    35  func (p *createWebhookSubcommand) InjectResource(*resource.Resource) error {
    36  	// Do nothing since resource flags are passed to the external plugin directly.
    37  	return nil
    38  }
    39  
    40  func (p *createWebhookSubcommand) UpdateMetadata(_ plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
    41  	setExternalPluginMetadata("webhook", p.Path, subcmdMeta)
    42  }
    43  
    44  func (p *createWebhookSubcommand) BindFlags(fs *pflag.FlagSet) {
    45  	bindExternalPluginFlags(fs, "webhook", p.Path, p.Args)
    46  }
    47  
    48  func (p *createWebhookSubcommand) Scaffold(fs machinery.Filesystem) error {
    49  	req := external.PluginRequest{
    50  		APIVersion: defaultAPIVersion,
    51  		Command:    "create webhook",
    52  		Args:       p.Args,
    53  	}
    54  
    55  	err := handlePluginResponse(fs, req, p.Path)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	return nil
    61  }