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

Commit

Permalink
Address local cluster lint issues (#2346)
Browse files Browse the repository at this point in the history
Cleans up a few issues that we pushed in.

Signed-off-by: Sean McGinnis <[email protected]>
  • Loading branch information
stmcginnis authored and joshrosso committed Jan 18, 2022
1 parent a3e93b2 commit 3049ce0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
5 changes: 5 additions & 0 deletions cli/cmd/plugin/standalone-cluster/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 10 additions & 6 deletions cli/cmd/plugin/standalone-cluster/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
ServiceCIDR = "ServiceCidr"
configDir = ".config"
tanzuConfigDir = "tanzu"
yamlIndent = 2
)

var defaultConfigValues = map[string]string{
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions cli/cmd/plugin/standalone-cluster/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cluster name>",
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/plugin/standalone-cluster/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 8 additions & 10 deletions cli/cmd/plugin/standalone-cluster/tanzu/tanzu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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)
Expand Down

0 comments on commit 3049ce0

Please sign in to comment.