sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/common/kustomize/v2/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  	pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
    24  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/crd/patches"
    25  
    26  	"sigs.k8s.io/kubebuilder/v3/pkg/config"
    27  	"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
    28  	"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
    29  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins"
    30  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/certmanager"
    31  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/kdefault"
    32  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/webhook"
    33  )
    34  
    35  var _ plugins.Scaffolder = &webhookScaffolder{}
    36  
    37  type webhookScaffolder struct {
    38  	config   config.Config
    39  	resource resource.Resource
    40  
    41  	// fs is the filesystem that will be used by the scaffolder
    42  	fs machinery.Filesystem
    43  
    44  	// force indicates whether to scaffold files even if they exist.
    45  	force bool
    46  }
    47  
    48  // NewWebhookScaffolder returns a new Scaffolder for v2 webhook creation operations
    49  func NewWebhookScaffolder(config config.Config, resource resource.Resource, force bool) plugins.Scaffolder {
    50  	return &webhookScaffolder{
    51  		config:   config,
    52  		resource: resource,
    53  		force:    force,
    54  	}
    55  }
    56  
    57  // InjectFS implements cmdutil.Scaffolder
    58  func (s *webhookScaffolder) InjectFS(fs machinery.Filesystem) { s.fs = fs }
    59  
    60  // Scaffold implements cmdutil.Scaffolder
    61  func (s *webhookScaffolder) Scaffold() error {
    62  	log.Println("Writing kustomize manifests for you to edit...")
    63  
    64  	// Initialize the machinery.Scaffold that will write the files to disk
    65  	scaffold := machinery.NewScaffold(s.fs,
    66  		machinery.WithConfig(s.config),
    67  		machinery.WithResource(&s.resource),
    68  	)
    69  
    70  	if err := s.config.UpdateResource(s.resource); err != nil {
    71  		return fmt.Errorf("error updating resource: %w", err)
    72  	}
    73  
    74  	if err := scaffold.Execute(
    75  		&kdefault.WebhookCAInjectionPatch{},
    76  		&kdefault.ManagerWebhookPatch{},
    77  		&webhook.Kustomization{Force: s.force},
    78  		&webhook.KustomizeConfig{},
    79  		&webhook.Service{},
    80  		&certmanager.Certificate{},
    81  		&certmanager.Kustomization{},
    82  		&certmanager.KustomizeConfig{},
    83  		&patches.EnableWebhookPatch{},
    84  		&patches.EnableCAInjectionPatch{},
    85  	); err != nil {
    86  		return fmt.Errorf("error scaffolding kustomize webhook manifests: %v", err)
    87  	}
    88  
    89  	kustomizeFilePath := "config/default/kustomization.yaml"
    90  	err := pluginutil.UncommentCode(kustomizeFilePath, "#- ../webhook", `#`)
    91  	if err != nil {
    92  		hasWebHookUncommented, err := pluginutil.HasFragment(kustomizeFilePath, "- ../webhook")
    93  		if !hasWebHookUncommented || err != nil {
    94  			log.Errorf("Unable to find the target #- ../webhook to uncomment in the file "+
    95  				"%s.", kustomizeFilePath)
    96  		}
    97  	}
    98  
    99  	err = pluginutil.UncommentCode(kustomizeFilePath, "#- path: manager_webhook_patch.yaml", `#`)
   100  	if err != nil {
   101  		hasWebHookUncommented, err := pluginutil.HasFragment(kustomizeFilePath, "- path: manager_webhook_patch.yaml")
   102  		if !hasWebHookUncommented || err != nil {
   103  			log.Errorf("Unable to find the target #- path: manager_webhook_patch.yaml to uncomment in the file "+
   104  				"%s.", kustomizeFilePath)
   105  		}
   106  	}
   107  
   108  	crdKustomizationsFilePath := "config/crd/kustomization.yaml"
   109  	err = pluginutil.UncommentCode(crdKustomizationsFilePath, "#- path: patches/webhook", `#`)
   110  	if err != nil {
   111  		hasWebHookUncommented, err := pluginutil.HasFragment(crdKustomizationsFilePath, "- path: patches/webhook")
   112  		if !hasWebHookUncommented || err != nil {
   113  			log.Errorf("Unable to find the target(s) #- path: patches/webhook/* to uncomment in the file "+
   114  				"%s.", crdKustomizationsFilePath)
   115  		}
   116  	}
   117  
   118  	err = pluginutil.UncommentCode(crdKustomizationsFilePath, "#configurations:\n#- kustomizeconfig.yaml", `#`)
   119  	if err != nil {
   120  		hasWebHookUncommented, err := pluginutil.HasFragment(crdKustomizationsFilePath, "- kustomizeconfig.yaml")
   121  		if !hasWebHookUncommented || err != nil {
   122  			log.Errorf("Unable to find the target(s) #configurations:\n#- kustomizeconfig.yaml to uncomment in the file "+
   123  				"%s.", crdKustomizationsFilePath)
   124  		}
   125  	}
   126  
   127  	return nil
   128  }