From 3049ce0b63c0af931661c00f96f50f6feceb6d10 Mon Sep 17 00:00:00 2001 From: Sean McGinnis Date: Mon, 25 Oct 2021 14:40:55 -0500 Subject: [PATCH] Address local cluster lint issues (#2346) Cleans up a few issues that we pushed in. Signed-off-by: Sean McGinnis --- .../standalone-cluster/cluster/cluster.go | 5 +++++ .../plugin/standalone-cluster/config/config.go | 16 ++++++++++------ cli/cmd/plugin/standalone-cluster/configure.go | 3 +-- cli/cmd/plugin/standalone-cluster/delete.go | 2 +- .../plugin/standalone-cluster/tanzu/tanzu.go | 18 ++++++++---------- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/cli/cmd/plugin/standalone-cluster/cluster/cluster.go b/cli/cmd/plugin/standalone-cluster/cluster/cluster.go index 154d8c1dcd..c3c7cdfb53 100644 --- a/cli/cmd/plugin/standalone-cluster/cluster/cluster.go +++ b/cli/cmd/plugin/standalone-cluster/cluster/cluster.go @@ -9,6 +9,11 @@ import ( "github.com/vmware-tanzu/community-edition/cli/cmd/plugin/standalone-cluster/config" ) +const ( + NoneClusterManagerProvider = "none" + KindClusterManagerProvider = "kind" +) + // KubernetesCluster represents a defines k8s cluster. type KubernetesCluster struct { // Name is the name of the cluster. diff --git a/cli/cmd/plugin/standalone-cluster/config/config.go b/cli/cmd/plugin/standalone-cluster/config/config.go index 4e077ab454..5d5a13a2e8 100644 --- a/cli/cmd/plugin/standalone-cluster/config/config.go +++ b/cli/cmd/plugin/standalone-cluster/config/config.go @@ -26,6 +26,7 @@ const ( ServiceCIDR = "ServiceCidr" configDir = ".config" tanzuConfigDir = "tanzu" + yamlIndent = 2 ) var defaultConfigValues = map[string]string{ @@ -167,35 +168,38 @@ func RenderConfigToFile(filePath string, config interface{}) error { // if it does not exist, which is expected, create it if !os.IsNotExist(err) { - return fmt.Errorf("Failed to create config file at %s. Does it already exist?", filePath) + return fmt.Errorf("failed to create config file at %q, does it already exist", filePath) } var rawConfig bytes.Buffer yamlEncoder := yaml.NewEncoder(&rawConfig) - yamlEncoder.SetIndent(2) + yamlEncoder.SetIndent(yamlIndent) err = yamlEncoder.Encode(config) if err != nil { - return fmt.Errorf("Failed to render configuration file. Error: %s", err.Error()) + return fmt.Errorf("failed to render configuration file. Error: %s", err.Error()) } err = os.WriteFile(filePath, rawConfig.Bytes(), 0644) if err != nil { - return fmt.Errorf("Failed to write rawConfig file. Error: %s", err.Error()) + return fmt.Errorf("failed to write rawConfig file. Error: %s", err.Error()) } // if it does, return an error // otherwise, write config to file return nil } +// RenderFileToConfig reads in configuration from a file and returns the +// LocalClusterConfig structure based on it. If the file does not exist or there +// is a problem reading the configuration from it an error is returned. func RenderFileToConfig(filePath string) (*LocalClusterConfig, error) { d, err := os.ReadFile(filePath) if err != nil { - return nil, fmt.Errorf("Failed reading config file. Error: %s", err.Error()) + return nil, fmt.Errorf("failed reading config file. Error: %s", err.Error()) } lcc := &LocalClusterConfig{} err = yaml.Unmarshal(d, lcc) if err != nil { - return nil, fmt.Errorf("Configuration at %s was invalid. Error: %s", filePath, err.Error()) + return nil, fmt.Errorf("configuration at %s was invalid. Error: %s", filePath, err.Error()) } return lcc, nil diff --git a/cli/cmd/plugin/standalone-cluster/configure.go b/cli/cmd/plugin/standalone-cluster/configure.go index 7a09ae123b..cf5808f878 100644 --- a/cli/cmd/plugin/standalone-cluster/configure.go +++ b/cli/cmd/plugin/standalone-cluster/configure.go @@ -7,12 +7,11 @@ import ( "fmt" "github.com/spf13/cobra" + "github.com/vmware-tanzu/community-edition/cli/cmd/plugin/standalone-cluster/config" logger "github.com/vmware-tanzu/community-edition/cli/cmd/plugin/standalone-cluster/log" ) -const yamlIndent = 2 - // ConfigureCmd creates a standalone workload cluster. var ConfigureCmd = &cobra.Command{ Use: "configure ", diff --git a/cli/cmd/plugin/standalone-cluster/delete.go b/cli/cmd/plugin/standalone-cluster/delete.go index c40c7f1512..2891401ea9 100644 --- a/cli/cmd/plugin/standalone-cluster/delete.go +++ b/cli/cmd/plugin/standalone-cluster/delete.go @@ -7,9 +7,9 @@ import ( "fmt" "github.com/spf13/cobra" - "github.com/vmware-tanzu/community-edition/cli/cmd/plugin/standalone-cluster/tanzu" logger "github.com/vmware-tanzu/community-edition/cli/cmd/plugin/standalone-cluster/log" + "github.com/vmware-tanzu/community-edition/cli/cmd/plugin/standalone-cluster/tanzu" ) // DeleteCmd deletes a standalone workload cluster. diff --git a/cli/cmd/plugin/standalone-cluster/tanzu/tanzu.go b/cli/cmd/plugin/standalone-cluster/tanzu/tanzu.go index 91602527d5..2f3f6cd8cc 100644 --- a/cli/cmd/plugin/standalone-cluster/tanzu/tanzu.go +++ b/cli/cmd/plugin/standalone-cluster/tanzu/tanzu.go @@ -104,14 +104,14 @@ func validateConfiguration(lcConfig *config.LocalClusterConfig) error { if lcConfig.Provider == "" { // Should have been validated earlier, but not an error. We can just // default it to kind. - lcConfig.Provider = "kind" + lcConfig.Provider = cluster.KindClusterManagerProvider } return nil } // Deploy deploys a new cluster. -//nolint:funlen +//nolint:funlen,gocyclo func (t *TanzuLocal) Deploy(lcConfig *config.LocalClusterConfig) error { var err error // 1. Validate the configuration @@ -261,9 +261,9 @@ func (t *TanzuLocal) Delete(name string) error { // TODO(joshrosso): fill out with different providers switch t.config.Provider { - case "kind": + case cluster.KindClusterManagerProvider: cm = cluster.NewKindClusterManager() - case "none": + case cluster.NoneClusterManagerProvider: // do nothing, cluster is provisioned elsewhere return nil } @@ -347,10 +347,9 @@ func resolveClusterDir(clusterName string) (string, error) { _, err = os.ReadDir(fp) if os.IsNotExist(err) { - return "", fmt.Errorf("Failed to locate the cluster's config file at %s", fp) + return "", fmt.Errorf("failed to locate the cluster's config file at %s", fp) } return fp, nil - } func resolveClusterConfig(clusterName string) (string, error) { @@ -364,7 +363,7 @@ func resolveClusterConfig(clusterName string) (string, error) { files, err := os.ReadDir(fp) if os.IsNotExist(err) { - return "", fmt.Errorf("Failed to locate the cluster's config file at %s", fp) + return "", fmt.Errorf("failed to locate the cluster's config file at %s", fp) } var resolvedConfigFile string @@ -376,11 +375,10 @@ func resolveClusterConfig(clusterName string) (string, error) { expectFp := filepath.Join(fp, configFileName) if resolvedConfigFile == "" { - return "", fmt.Errorf("Failed to locate a config file at %s", expectFp) + return "", fmt.Errorf("failed to locate a config file at %s", expectFp) } return filepath.Join(fp, resolvedConfigFile), nil - } func createClusterDirectory(clusterName string) (string, error) { @@ -394,7 +392,7 @@ func createClusterDirectory(clusterName string) (string, error) { // if it does not exist, which is expected, create it if !os.IsNotExist(err) { - return "", fmt.Errorf("Directory %s already exists. This cluster must be deleted before proceeding.", fp) + return "", fmt.Errorf("directory %s already exists, this cluster must be deleted before proceeding", fp) } err = os.MkdirAll(fp, 0755)