sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/common/kustomize/v1/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  
    22  	"sigs.k8s.io/kubebuilder/v3/pkg/config"
    23  	"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
    24  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins"
    25  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/kdefault"
    26  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/manager"
    27  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/prometheus"
    28  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/rbac"
    29  )
    30  
    31  const (
    32  	imageName = "controller:latest"
    33  )
    34  
    35  var _ plugins.Scaffolder = &initScaffolder{}
    36  
    37  type initScaffolder struct {
    38  	config config.Config
    39  
    40  	// fs is the filesystem that will be used by the scaffolder
    41  	fs machinery.Filesystem
    42  }
    43  
    44  // NewInitScaffolder returns a new Scaffolder for project initialization operations
    45  func NewInitScaffolder(config config.Config) plugins.Scaffolder {
    46  	return &initScaffolder{
    47  		config: config,
    48  	}
    49  }
    50  
    51  // InjectFS implements cmdutil.Scaffolder
    52  func (s *initScaffolder) InjectFS(fs machinery.Filesystem) {
    53  	s.fs = fs
    54  }
    55  
    56  // Scaffold implements cmdutil.Scaffolder
    57  func (s *initScaffolder) Scaffold() error {
    58  	log.Println("Writing kustomize manifests for you to edit...")
    59  
    60  	// Initialize the machinery.Scaffold that will write the files to disk
    61  	scaffold := machinery.NewScaffold(s.fs,
    62  		machinery.WithConfig(s.config),
    63  	)
    64  
    65  	templates := []machinery.Builder{
    66  		&rbac.Kustomization{},
    67  		&rbac.AuthProxyRole{},
    68  		&rbac.AuthProxyRoleBinding{},
    69  		&rbac.AuthProxyService{},
    70  		&rbac.AuthProxyClientRole{},
    71  		&rbac.RoleBinding{},
    72  		&rbac.LeaderElectionRole{},
    73  		&rbac.LeaderElectionRoleBinding{},
    74  		&rbac.ServiceAccount{},
    75  		&manager.Kustomization{},
    76  		&manager.Config{Image: imageName},
    77  		&kdefault.Kustomization{},
    78  		&kdefault.ManagerAuthProxyPatch{},
    79  		&kdefault.ManagerConfigPatch{},
    80  		&prometheus.Kustomization{},
    81  		&prometheus.Monitor{},
    82  	}
    83  
    84  	if s.config.IsComponentConfig() {
    85  		templates = append(templates, &manager.ControllerManagerConfig{})
    86  	}
    87  
    88  	return scaffold.Execute(templates...)
    89  }