sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v2/scaffolds/init.go (about)

     1  /*
     2  Copyright 2019 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  
    23  	"sigs.k8s.io/kubebuilder/v3/pkg/config"
    24  	"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
    25  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins"
    26  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates"
    27  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates/config/certmanager"
    28  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates/config/kdefault"
    29  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates/config/manager"
    30  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates/config/prometheus"
    31  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates/config/rbac"
    32  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates/config/webhook"
    33  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates/hack"
    34  )
    35  
    36  const (
    37  	// ControllerRuntimeVersion is the kubernetes-sigs/controller-runtime version to be used in the project
    38  	ControllerRuntimeVersion = "v0.6.4"
    39  	// ControllerToolsVersion is the kubernetes-sigs/controller-tools version to be used in the project
    40  	ControllerToolsVersion = "v0.3.0"
    41  	// KustomizeVersion is the kubernetes-sigs/kustomize version to be used in the project
    42  	// @Deprecate: KustomizeVersion came from the Kustomize plugin since go/v3
    43  	// Go/v2 plugin exist only to ensure the backwards compatibility with the old scaffold
    44  	// produced with kubebuilder >= 2.0.0 < 3.0.0. It does not take advantage of the plugin system
    45  	// (see that the PROJECT file does not have the layout of the plugin and uses the old/legacy
    46  	// version/schema.) This plugin will be deprecated with go/v4 and can be removed
    47  	// when go/v4 stable is published.
    48  	KustomizeVersion = "v3.5.4"
    49  
    50  	imageName = "controller:latest"
    51  )
    52  
    53  var _ plugins.Scaffolder = &initScaffolder{}
    54  
    55  type initScaffolder struct {
    56  	config          config.Config
    57  	boilerplatePath string
    58  	license         string
    59  	owner           string
    60  
    61  	// fs is the filesystem that will be used by the scaffolder
    62  	fs machinery.Filesystem
    63  }
    64  
    65  // NewInitScaffolder returns a new Scaffolder for project initialization operations
    66  func NewInitScaffolder(config config.Config, license, owner string) plugins.Scaffolder {
    67  	return &initScaffolder{
    68  		config:          config,
    69  		boilerplatePath: hack.DefaultBoilerplatePath,
    70  		license:         license,
    71  		owner:           owner,
    72  	}
    73  }
    74  
    75  // InjectFS implements cmdutil.Scaffolder
    76  func (s *initScaffolder) InjectFS(fs machinery.Filesystem) {
    77  	s.fs = fs
    78  }
    79  
    80  // Scaffold implements cmdutil.Scaffolder
    81  func (s *initScaffolder) Scaffold() error {
    82  	log.Println("Writing scaffold for you to edit...")
    83  
    84  	// Initialize the machinery.Scaffold that will write the boilerplate file to disk
    85  	// The boilerplate file needs to be scaffolded as a separate step as it is going to
    86  	// be used by the rest of the files, even those scaffolded in this command call.
    87  	scaffold := machinery.NewScaffold(s.fs,
    88  		machinery.WithConfig(s.config),
    89  	)
    90  
    91  	bpFile := &hack.Boilerplate{
    92  		License: s.license,
    93  		Owner:   s.owner,
    94  	}
    95  	bpFile.Path = s.boilerplatePath
    96  	if err := scaffold.Execute(bpFile); err != nil {
    97  		return err
    98  	}
    99  
   100  	boilerplate, err := afero.ReadFile(s.fs.FS, s.boilerplatePath)
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	// Initialize the machinery.Scaffold that will write the files to disk
   106  	scaffold = machinery.NewScaffold(s.fs,
   107  		machinery.WithConfig(s.config),
   108  		machinery.WithBoilerplate(string(boilerplate)),
   109  	)
   110  
   111  	return scaffold.Execute(
   112  		&rbac.Kustomization{},
   113  		&rbac.AuthProxyRole{},
   114  		&rbac.AuthProxyRoleBinding{},
   115  		&rbac.AuthProxyService{},
   116  		&rbac.AuthProxyClientRole{},
   117  		&rbac.RoleBinding{},
   118  		&rbac.LeaderElectionRole{},
   119  		&rbac.LeaderElectionRoleBinding{},
   120  		&manager.Kustomization{},
   121  		&manager.Config{Image: imageName},
   122  		&templates.Main{},
   123  		&templates.GoMod{ControllerRuntimeVersion: ControllerRuntimeVersion},
   124  		&templates.GitIgnore{},
   125  		&templates.Makefile{
   126  			Image:                  imageName,
   127  			BoilerplatePath:        s.boilerplatePath,
   128  			ControllerToolsVersion: ControllerToolsVersion,
   129  			KustomizeVersion:       KustomizeVersion,
   130  		},
   131  		&templates.Dockerfile{},
   132  		&kdefault.Kustomization{},
   133  		&kdefault.ManagerAuthProxyPatch{},
   134  		&kdefault.ManagerWebhookPatch{},
   135  		&kdefault.WebhookCAInjectionPatch{},
   136  		&webhook.Kustomization{},
   137  		&webhook.KustomizeConfig{},
   138  		&webhook.Service{},
   139  		&prometheus.Kustomization{},
   140  		&prometheus.Monitor{},
   141  		&certmanager.Certificate{},
   142  		&certmanager.Kustomization{},
   143  		&certmanager.KustomizeConfig{},
   144  	)
   145  }