Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

Add CLI args for local cluster create and configure #2337

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions cli/cmd/plugin/standalone-cluster/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ import (
)

const (
TKRLocation = "TkrLocation"
Provider = "Provider"
Cni = "Cni"
Tty = "Tty"
PodCIDR = "PodCidr"
ServiceCIDR = "ServiceCidr"
configDir = ".config"
tanzuConfigDir = "tanzu"
ClusterConfigFile = "ClusterConfigFile"
ClusterName = "ClusterName"
Tty = "Tty"
TKRLocation = "TkrLocation"
Provider = "Provider"
Cni = "Cni"
PodCIDR = "PodCidr"
ServiceCIDR = "ServiceCidr"
configDir = ".config"
tanzuConfigDir = "tanzu"
)

var defaultConfigValues = map[string]string{
Expand Down Expand Up @@ -90,8 +92,8 @@ func InitializeConfiguration(commandArgs map[string]string) (*LocalClusterConfig
config := &LocalClusterConfig{}

// First, populate values based on a supplied config file
if commandArgs["clusterconfigfile"] != "" {
configData, err := os.ReadFile(commandArgs["clusterconfigfile"])
if commandArgs[ClusterConfigFile] != "" {
configData, err := os.ReadFile(commandArgs[ClusterConfigFile])
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -119,7 +121,7 @@ func InitializeConfiguration(commandArgs map[string]string) (*LocalClusterConfig
}

// Check if an explicit value was passed in
if value, ok := commandArgs[strings.ToLower(fieldName)]; ok {
if value, ok := commandArgs[fieldName]; ok {
element.FieldByName(field.Name).SetString(value)
} else if value := os.Getenv(fieldNameToEnvName(fieldName)); value != "" {
// See if there is an environment variable set for this field
Expand Down
6 changes: 3 additions & 3 deletions cli/cmd/plugin/standalone-cluster/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestInitializeConfigurationNoName(t *testing.T) {
}

func TestInitializeConfigurationDefaults(t *testing.T) {
args := map[string]string{"clustername": "test"}
args := map[string]string{ClusterName: "test"}
config, err := InitializeConfiguration(args)
if err != nil {
t.Error("initialization should pass")
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestInitializeConfigurationEnvVariables(t *testing.T) {
func TestInitializeConfigurationArgsTakePrecedent(t *testing.T) {
os.Setenv("TANZU_PROVIDER", "test_provider")
os.Setenv("TANZU_CLUSTER_NAME", "test2")
args := map[string]string{"clustername": "test"}
args := map[string]string{ClusterName: "test"}
config, err := InitializeConfiguration(args)
if err != nil {
t.Error("initialization should pass")
Expand Down Expand Up @@ -148,7 +148,7 @@ func TestInitializeConfigurationFromConfigFile(t *testing.T) {
return
}

args := map[string]string{"clusterconfigfile": f.Name()}
args := map[string]string{ClusterConfigFile: f.Name()}
config, err := InitializeConfiguration(args)
if err != nil {
t.Error("initialization should pass")
Expand Down
34 changes: 26 additions & 8 deletions cli/cmd/plugin/standalone-cluster/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,47 @@ var ConfigureCmd = &cobra.Command{
RunE: configure,
}

//nolint:dupl
func init() {
ConfigureCmd.Flags().StringVarP(&co.clusterConfigFile, "config", "f", "", "Configuration file for local cluster creation")
ConfigureCmd.Flags().StringVarP(&co.infrastructureProvider, "provider", "p", "", "The infrastructure provider to use for cluster creation. Default is 'kind'")
ConfigureCmd.Flags().StringVarP(&co.tkrLocation, "tkr", "t", "", "The Tanzu Kubernetes Release location.")
ConfigureCmd.Flags().StringVarP(&co.cni, "cni", "c", "", "The CNI to deploy. Default is 'antrea'")
ConfigureCmd.Flags().StringVar(&co.podcidr, "pod-cidr", "", "The CIDR to use for Pod IP addresses. Default and format is '10.244.0.0/16'")
ConfigureCmd.Flags().StringVar(&co.servicecidr, "service-cidr", "", "The CIDR to use for Service IP addresses. Default and format is '10.96.0.0/16'")
ConfigureCmd.Flags().BoolVar(&co.tty, "tty", true, "Specify whether terminal is tty. Set to false to disable styled output; default: true")
}

func configure(cmd *cobra.Command, args []string) error {
var clusterName string

// validate a cluster name was passed when not using the kickstart UI
if len(args) < 1 && !co.ui {
// validate a cluster name was passed
if len(args) < 1 {
return fmt.Errorf("cluster name not specified")
} else if len(args) == 1 {
clusterName = args[0]
}

log := logger.NewLogger(true, 0)
log := logger.NewLogger(co.tty, 0)

var rawConfig bytes.Buffer
yamlEncoder := yaml.NewEncoder(&rawConfig)
yamlEncoder.SetIndent(yamlIndent)

lcConfig, err := config.InitializeConfiguration(map[string]string{"clustername": clusterName})
// Determine our configuration to use
configArgs := map[string]string{
config.ClusterConfigFile: co.clusterConfigFile,
config.ClusterName: clusterName,
config.Provider: co.infrastructureProvider,
config.TKRLocation: co.tkrLocation,
config.Cni: co.cni,
config.PodCIDR: co.podcidr,
config.ServiceCIDR: co.servicecidr,
}
lcConfig, err := config.InitializeConfiguration(configArgs)
if err != nil {
log.Errorf("Failed to initialize configuration. Error: %s\n", err.Error())
}

var rawConfig bytes.Buffer
yamlEncoder := yaml.NewEncoder(&rawConfig)
yamlEncoder.SetIndent(yamlIndent)
err = yamlEncoder.Encode(*lcConfig)
if err != nil {
log.Errorf("Failed to render rawConfig file. Error: %s\n", err.Error())
Expand Down
33 changes: 19 additions & 14 deletions cli/cmd/plugin/standalone-cluster/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package main

import (
"fmt"
"strconv"

"github.com/spf13/cobra"

Expand All @@ -17,7 +16,10 @@ import (
type createLocalOpts struct {
clusterConfigFile string
infrastructureProvider string
ui bool
tkrLocation string
cni string
podcidr string
servicecidr string
tty bool
}

Expand All @@ -30,41 +32,44 @@ var CreateCmd = &cobra.Command{

var co = createLocalOpts{}

//nolint:dupl
func init() {
CreateCmd.Flags().StringVarP(&co.clusterConfigFile, "config", "f", "", "Configuration file for local cluster creation")
CreateCmd.Flags().StringVarP(&co.infrastructureProvider, "provider", "p", "", "The infrastructure provider to use for cluster creation. Default is 'kind'")
CreateCmd.Flags().StringVarP(&co.tkrLocation, "tkr", "t", "", "The Tanzu Kubernetes Release location.")
CreateCmd.Flags().StringVarP(&co.cni, "cni", "c", "", "The CNI to deploy. Default is 'antrea'")
CreateCmd.Flags().StringVar(&co.podcidr, "pod-cidr", "", "The CIDR to use for Pod IP addresses. Default and format is '10.244.0.0/16'")
CreateCmd.Flags().StringVar(&co.servicecidr, "service-cidr", "", "The CIDR to use for Service IP addresses. Default and format is '10.96.0.0/16'")
CreateCmd.Flags().BoolVar(&co.tty, "tty", true, "Specify whether terminal is tty. Set to false to disable styled output; default: true")
}

func create(cmd *cobra.Command, args []string) error {
var clusterName string

// validate a cluster name was passed when not using the kickstart UI
if len(args) < 1 && !co.ui {
// validate a cluster name was passed
if len(args) < 1 {
return fmt.Errorf("cluster name not specified")
} else if len(args) == 1 {
clusterName = args[0]
}
// initial logger, needed for logging if something goes wrong
log := logger.NewLogger(false, 0)
log := logger.NewLogger(co.tty, 0)

// Determine our configuration to use
configArgs := map[string]string{
"clusterconfigfile": co.clusterConfigFile,
"clustername": clusterName,
config.ClusterConfigFile: co.clusterConfigFile,
config.ClusterName: clusterName,
config.Provider: co.infrastructureProvider,
config.TKRLocation: co.tkrLocation,
config.Cni: co.cni,
config.PodCIDR: co.podcidr,
config.ServiceCIDR: co.servicecidr,
}
clusterConfig, err := config.InitializeConfiguration(configArgs)
if err != nil {
log.Errorf("Failed to initialize configuration. Error %s\n", clusterConfig)
return nil
}
ttySetting, err := strconv.ParseBool(clusterConfig.Tty)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this was not added back because --tty has a default of true, and CLI arguments take precedent over config and env variables. So basically this will always be whatever co.tty is, so the first logger initialization on line 56 above will never change.

if err != nil {
log.Errorf("TTY setting was invalid. Error: %s", err.Error())
return nil
}
// reset logger here based on parsed configuration
log = logger.NewLogger(ttySetting, 0)

tm := tanzu.New(log)
err = tm.Deploy(clusterConfig)
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/plugin/standalone-cluster/tanzu/tanzu.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ func resolveCNI(mgr packages.PackageManager, cniName string) (*CNIPackage, error
return nil, err
}
if cniPkg.fqPkgName == "" {
return nil, fmt.Errorf("No package was resolved for CNI choice %s", cniName)
return nil, fmt.Errorf("no package was resolved for CNI choice %s", cniName)
}

return cniPkg, nil
Expand Down