sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/common/kustomize/v1/scaffolds/webhook.go (about)

     1  /*
     2  Copyright 2022 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  
    24  	"sigs.k8s.io/kubebuilder/v3/pkg/config"
    25  	"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
    26  	"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
    27  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins"
    28  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/certmanager"
    29  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/kdefault"
    30  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/webhook"
    31  )
    32  
    33  var _ plugins.Scaffolder = &webhookScaffolder{}
    34  
    35  type webhookScaffolder struct {
    36  	config   config.Config
    37  	resource resource.Resource
    38  
    39  	// fs is the filesystem that will be used by the scaffolder
    40  	fs machinery.Filesystem
    41  
    42  	// force indicates whether to scaffold files even if they exist.
    43  	force bool
    44  }
    45  
    46  // NewWebhookScaffolder returns a new Scaffolder for v2 webhook creation operations
    47  func NewWebhookScaffolder(config config.Config, resource resource.Resource, force bool) plugins.Scaffolder {
    48  	return &webhookScaffolder{
    49  		config:   config,
    50  		resource: resource,
    51  		force:    force,
    52  	}
    53  }
    54  
    55  // InjectFS implements cmdutil.Scaffolder
    56  func (s *webhookScaffolder) InjectFS(fs machinery.Filesystem) { s.fs = fs }
    57  
    58  // Scaffold implements cmdutil.Scaffolder
    59  func (s *webhookScaffolder) Scaffold() error {
    60  	log.Println("Writing kustomize manifests for you to edit...")
    61  
    62  	// Initialize the machinery.Scaffold that will write the files to disk
    63  	scaffold := machinery.NewScaffold(s.fs,
    64  		machinery.WithConfig(s.config),
    65  		machinery.WithResource(&s.resource),
    66  	)
    67  
    68  	if err := s.config.UpdateResource(s.resource); err != nil {
    69  		return fmt.Errorf("error updating resource: %w", err)
    70  	}
    71  
    72  	if err := scaffold.Execute(
    73  		&kdefault.WebhookCAInjectionPatch{},
    74  		&kdefault.ManagerWebhookPatch{},
    75  		&webhook.Kustomization{Force: s.force},
    76  		&webhook.KustomizeConfig{},
    77  		&webhook.Service{},
    78  		&certmanager.Certificate{},
    79  		&certmanager.Kustomization{},
    80  		&certmanager.KustomizeConfig{},
    81  	); err != nil {
    82  		return fmt.Errorf("error scaffolding kustomize webhook manifests: %v", err)
    83  	}
    84  
    85  	return nil
    86  }