sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/common/kustomize/v2/scaffolds/api.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  	pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
    23  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/crd"
    24  
    25  	log "github.com/sirupsen/logrus"
    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/rbac"
    31  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/samples"
    32  )
    33  
    34  var _ plugins.Scaffolder = &apiScaffolder{}
    35  
    36  // apiScaffolder contains configuration for generating scaffolding for Go type
    37  // representing the API and controller that implements the behavior for the API.
    38  type apiScaffolder struct {
    39  	config   config.Config
    40  	resource resource.Resource
    41  
    42  	// fs is the filesystem that will be used by the scaffolder
    43  	fs machinery.Filesystem
    44  
    45  	// force indicates whether to scaffold files even if they exist.
    46  	force bool
    47  }
    48  
    49  // NewAPIScaffolder returns a new Scaffolder for API/controller creation operations
    50  func NewAPIScaffolder(config config.Config, res resource.Resource, force bool) plugins.Scaffolder {
    51  	return &apiScaffolder{
    52  		config:   config,
    53  		resource: res,
    54  		force:    force,
    55  	}
    56  }
    57  
    58  // InjectFS implements cmdutil.Scaffolder
    59  func (s *apiScaffolder) InjectFS(fs machinery.Filesystem) {
    60  	s.fs = fs
    61  }
    62  
    63  // Scaffold implements cmdutil.Scaffolder
    64  func (s *apiScaffolder) Scaffold() error {
    65  	log.Println("Writing kustomize manifests for you to edit...")
    66  
    67  	// Initialize the machinery.Scaffold that will write the files to disk
    68  	scaffold := machinery.NewScaffold(s.fs,
    69  		machinery.WithConfig(s.config),
    70  		machinery.WithResource(&s.resource),
    71  	)
    72  
    73  	// Keep track of these values before the update
    74  	if s.resource.HasAPI() {
    75  		if err := scaffold.Execute(
    76  			&samples.CRDSample{Force: s.force},
    77  			&rbac.CRDEditorRole{},
    78  			&rbac.CRDViewerRole{},
    79  			&crd.Kustomization{},
    80  			&crd.KustomizeConfig{},
    81  		); err != nil {
    82  			return fmt.Errorf("error scaffolding kustomize API manifests: %v", err)
    83  		}
    84  
    85  		// If the gvk is non-empty
    86  		if s.resource.Group != "" || s.resource.Version != "" || s.resource.Kind != "" {
    87  			if err := scaffold.Execute(&samples.Kustomization{}); err != nil {
    88  				return fmt.Errorf("error scaffolding manifests: %v", err)
    89  			}
    90  		}
    91  
    92  		kustomizeFilePath := "config/default/kustomization.yaml"
    93  		err := pluginutil.UncommentCode(kustomizeFilePath, "#- ../crd", `#`)
    94  		if err != nil {
    95  			hasCRUncommented, err := pluginutil.HasFragment(kustomizeFilePath, "- ../crd")
    96  			if !hasCRUncommented || err != nil {
    97  				log.Errorf("Unable to find the target #- ../crd to uncomment in the file "+
    98  					"%s.", kustomizeFilePath)
    99  			}
   100  		}
   101  	}
   102  
   103  	return nil
   104  }