sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v4/scaffolds/init.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  	log "github.com/sirupsen/logrus"
    21  	"github.com/spf13/afero"
    22  	"sigs.k8s.io/kubebuilder/v3/pkg/config"
    23  	"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
    24  	"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
    25  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins"
    26  	kustomizecommonv1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1"
    27  	kustomizecommonv2alpha "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v2"
    28  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v4/scaffolds/internal/templates"
    29  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v4/scaffolds/internal/templates/hack"
    30  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e"
    31  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v4/scaffolds/internal/templates/test/utils"
    32  )
    33  
    34  const (
    35  	// ControllerRuntimeVersion is the kubernetes-sigs/controller-runtime version to be used in the project
    36  	ControllerRuntimeVersion = "v0.17.0"
    37  	// ControllerToolsVersion is the kubernetes-sigs/controller-tools version to be used in the project
    38  	ControllerToolsVersion = "v0.14.0"
    39  	// EnvtestK8SVersion is the k8s version used to do the scaffold
    40  	EnvtestK8SVersion = "1.29.0"
    41  
    42  	imageName = "controller:latest"
    43  )
    44  
    45  var _ plugins.Scaffolder = &initScaffolder{}
    46  
    47  var kustomizeVersion string
    48  
    49  type initScaffolder struct {
    50  	config          config.Config
    51  	boilerplatePath string
    52  	license         string
    53  	owner           string
    54  
    55  	// fs is the filesystem that will be used by the scaffolder
    56  	fs machinery.Filesystem
    57  }
    58  
    59  // NewInitScaffolder returns a new Scaffolder for project initialization operations
    60  func NewInitScaffolder(config config.Config, license, owner string) plugins.Scaffolder {
    61  	return &initScaffolder{
    62  		config:          config,
    63  		boilerplatePath: hack.DefaultBoilerplatePath,
    64  		license:         license,
    65  		owner:           owner,
    66  	}
    67  }
    68  
    69  // InjectFS implements cmdutil.Scaffolder
    70  func (s *initScaffolder) InjectFS(fs machinery.Filesystem) {
    71  	s.fs = fs
    72  }
    73  
    74  // Scaffold implements cmdutil.Scaffolder
    75  func (s *initScaffolder) Scaffold() error {
    76  	log.Println("Writing scaffold for you to edit...")
    77  
    78  	// Initialize the machinery.Scaffold that will write the boilerplate file to disk
    79  	// The boilerplate file needs to be scaffolded as a separate step as it is going to
    80  	// be used by the rest of the files, even those scaffolded in this command call.
    81  	scaffold := machinery.NewScaffold(s.fs,
    82  		machinery.WithConfig(s.config),
    83  	)
    84  
    85  	if s.license != "none" {
    86  		bpFile := &hack.Boilerplate{
    87  			License: s.license,
    88  			Owner:   s.owner,
    89  		}
    90  		bpFile.Path = s.boilerplatePath
    91  		if err := scaffold.Execute(bpFile); err != nil {
    92  			return err
    93  		}
    94  
    95  		boilerplate, err := afero.ReadFile(s.fs.FS, s.boilerplatePath)
    96  		if err != nil {
    97  			return err
    98  		}
    99  		// Initialize the machinery.Scaffold that will write the files to disk
   100  		scaffold = machinery.NewScaffold(s.fs,
   101  			machinery.WithConfig(s.config),
   102  			machinery.WithBoilerplate(string(boilerplate)),
   103  		)
   104  	} else {
   105  		s.boilerplatePath = ""
   106  		// Initialize the machinery.Scaffold without boilerplate
   107  		scaffold = machinery.NewScaffold(s.fs,
   108  			machinery.WithConfig(s.config),
   109  		)
   110  	}
   111  
   112  	// If the KustomizeV2 was used to do the scaffold then
   113  	// we need to ensure that we use its supported Kustomize Version
   114  	// in order to support it
   115  	kustomizeVersion = kustomizecommonv1.KustomizeVersion
   116  	kustomizev2 := kustomizecommonv2alpha.Plugin{}
   117  	gov4 := "go.kubebuilder.io/v4"
   118  	pluginKeyForKustomizeV2 := plugin.KeyFor(kustomizev2)
   119  
   120  	for _, pluginKey := range s.config.GetPluginChain() {
   121  		if pluginKey == pluginKeyForKustomizeV2 || pluginKey == gov4 {
   122  			kustomizeVersion = kustomizecommonv2alpha.KustomizeVersion
   123  			break
   124  		}
   125  	}
   126  
   127  	return scaffold.Execute(
   128  		&templates.Main{},
   129  		&templates.GoMod{
   130  			ControllerRuntimeVersion: ControllerRuntimeVersion,
   131  		},
   132  		&templates.GitIgnore{},
   133  		&templates.Makefile{
   134  			Image:                    imageName,
   135  			BoilerplatePath:          s.boilerplatePath,
   136  			ControllerToolsVersion:   ControllerToolsVersion,
   137  			KustomizeVersion:         kustomizeVersion,
   138  			ControllerRuntimeVersion: ControllerRuntimeVersion,
   139  		},
   140  		&templates.Dockerfile{},
   141  		&templates.DockerIgnore{},
   142  		&templates.Readme{},
   143  		&templates.Golangci{},
   144  		&e2e.Test{},
   145  		&e2e.SuiteTest{},
   146  		&utils.Utils{},
   147  	)
   148  }