sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/common/kustomize/v1/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  //go:deprecated This package has been deprecated in favor of v2
    18  package v1
    19  
    20  import (
    21  	"fmt"
    22  	"os"
    23  	"path/filepath"
    24  	"runtime"
    25  	"strings"
    26  
    27  	log "github.com/sirupsen/logrus"
    28  	"github.com/spf13/pflag"
    29  
    30  	"sigs.k8s.io/kubebuilder/v3/pkg/config"
    31  	"sigs.k8s.io/kubebuilder/v3/pkg/internal/validation"
    32  	"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
    33  	"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
    34  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds"
    35  )
    36  
    37  var _ plugin.InitSubcommand = &initSubcommand{}
    38  
    39  // Verify if the local environment is supported by this plugin
    40  var supportedArchs = []string{"linux/amd64",
    41  	"linux/arm64",
    42  	"darwin/amd64"}
    43  
    44  type initSubcommand struct {
    45  	config config.Config
    46  
    47  	// config options
    48  	domain          string
    49  	name            string
    50  	componentConfig bool
    51  }
    52  
    53  func (p *initSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
    54  	subcmdMeta.Description = fmt.Sprintf(`Initialize a common project including the following files:
    55    - a "PROJECT" file that stores project configuration
    56    - several YAML files for project deployment under the "config" directory
    57  
    58    NOTE: The kustomize/v1 plugin used to do this scaffold uses the v3 release (%s).
    59  Therefore, darwin/arm64 is not supported since Kustomize does not provide v3
    60  binaries for this architecture. The currently supported architectures are %q. 
    61  More info: https://github.com/kubernetes-sigs/kustomize/issues/4612.
    62  
    63  `, KustomizeVersion, supportedArchs)
    64  
    65  	subcmdMeta.Examples = fmt.Sprintf(`  # Initialize a common project with your domain and name in copyright
    66    %[1]s init --plugins common/v3 --domain example.org
    67  
    68    # Initialize a common project defining a specific project version
    69    %[1]s init --plugins common/v3 --project-version 3
    70  `, cliMeta.CommandName)
    71  }
    72  
    73  func (p *initSubcommand) BindFlags(fs *pflag.FlagSet) {
    74  	fs.StringVar(&p.domain, "domain", "my.domain", "domain for groups")
    75  	fs.StringVar(&p.name, "project-name", "", "name of this project")
    76  	fs.BoolVar(&p.componentConfig, "component-config", false,
    77  		"create a versioned ComponentConfig file, may be 'true' or 'false'")
    78  }
    79  
    80  func (p *initSubcommand) InjectConfig(c config.Config) error {
    81  	p.config = c
    82  
    83  	if err := p.config.SetDomain(p.domain); err != nil {
    84  		return err
    85  	}
    86  
    87  	// Assign a default project name
    88  	if p.name == "" {
    89  		dir, err := os.Getwd()
    90  		if err != nil {
    91  			return fmt.Errorf("error getting current directory: %v", err)
    92  		}
    93  		p.name = strings.ToLower(filepath.Base(dir))
    94  	}
    95  	// Check if the project name is a valid k8s namespace (DNS 1123 label).
    96  	if err := validation.IsDNS1123Label(p.name); err != nil {
    97  		return fmt.Errorf("project name (%s) is invalid: %v", p.name, err)
    98  	}
    99  	if err := p.config.SetProjectName(p.name); err != nil {
   100  		return err
   101  	}
   102  
   103  	if p.componentConfig {
   104  		if err := p.config.SetComponentConfig(); err != nil {
   105  			return err
   106  		}
   107  	}
   108  
   109  	return nil
   110  }
   111  
   112  func (p *initSubcommand) PreScaffold(machinery.Filesystem) error {
   113  	arch := runtime.GOARCH
   114  	// It probably will never return x86_64. However, we are here checking the support for the binaries
   115  	// So that, x86_64 means getting the Linux/amd64 binary. Then, we just keep this line to ensure
   116  	// that it complies with the same code implementation that we have in the targets. In case someone
   117  	// call the command inform the GOARCH=x86_64 then, we will properly handle the scenario
   118  	// since it will work successfully and will instal the Linux/amd64 binary via the Makefile target.
   119  	arch = strings.Replace(arch, "x86_64", "amd64", -1)
   120  	localPlatform := fmt.Sprintf("%s/%s", strings.TrimSpace(runtime.GOOS), strings.TrimSpace(arch))
   121  
   122  	if !hasSupportFor(localPlatform) {
   123  		log.Warnf("the platform of this environment (%s) is not suppported by kustomize v3 (%s) which is "+
   124  			"used in this scaffold. You will be unable to download a binary for the kustomize version supported "+
   125  			"and used by this plugin. The currently supported platforms are: %q",
   126  			localPlatform,
   127  			KustomizeVersion,
   128  			supportedArchs)
   129  	}
   130  
   131  	return nil
   132  }
   133  
   134  func hasSupportFor(localPlatform string) bool {
   135  	for _, value := range supportedArchs {
   136  		if value == localPlatform {
   137  			return true
   138  		}
   139  	}
   140  	return false
   141  }
   142  
   143  func (p *initSubcommand) Scaffold(fs machinery.Filesystem) error {
   144  	scaffolder := scaffolds.NewInitScaffolder(p.config)
   145  	scaffolder.InjectFS(fs)
   146  	return scaffolder.Scaffold()
   147  }