Skip to content
This repository has been archived by the owner on Feb 14, 2022. It is now read-only.

Commit

Permalink
cleanup code in cmd/
Browse files Browse the repository at this point in the history
  • Loading branch information
Cedric Kienzler committed Mar 18, 2021
1 parent d300ff2 commit 67a8cc2
Show file tree
Hide file tree
Showing 36 changed files with 346 additions and 331 deletions.
42 changes: 18 additions & 24 deletions cmd/add_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package cmd
import (
"fmt"

"github.com/cedi/kkpctl/pkg/model"
"github.com/cedi/kkpctl/pkg/utils"
"github.com/kubermatic/go-kubermatic/models"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand All @@ -18,11 +18,11 @@ var (
usePodNodeSelectorAdmissionPlugin bool
)

// projectCmd represents the project command
var createClusterCmd = &cobra.Command{
Use: "cluster name",
Short: "Lets you create a new cluster",
Args: cobra.ExactArgs(1),
Use: "cluster clusterName",
Short: "Create a new cluster",
Example: "kkpctl add cluster --project 6tmbnhdl7h --datacenter ix2 --provider optimist --version 1.18.13 --labels stage=dev kkpctltest",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clusterName := args[0]

Expand All @@ -31,26 +31,20 @@ var createClusterCmd = &cobra.Command{
return err
}

newCluster := models.CreateClusterSpec{
Cluster: &models.Cluster{
Labels: utils.SplitLabelString(labels),
Name: clusterName,
Type: clusterType,
Spec: &models.ClusterSpec{
UsePodNodeSelectorAdmissionPlugin: usePodNodeSelectorAdmissionPlugin,
UsePodSecurityPolicyAdmissionPlugin: usePodSecurityPolicyAdmissionPlugin,
AuditLogging: &models.AuditLoggingSettings{
Enabled: enableAuditLogging,
},
Cloud: Config.Provider.GetProviderCloudSpec(providerName, datacenter),
Version: k8sVersion,
},
},
}

result, err := kkp.CreateCluster(&newCluster, projectID, datacenter)
newCluster := model.NewCreateClusterSpec(
clusterName,
clusterType,
k8sVersion,
Config.Provider.GetProviderCloudSpec(providerName, datacenter),
utils.SplitLabelString(labels),
usePodNodeSelectorAdmissionPlugin,
usePodSecurityPolicyAdmissionPlugin,
enableAuditLogging,
)

result, err := kkp.CreateCluster(newCluster, projectID, datacenter)
if err != nil {
return errors.Wrap(err, "error creating cluster")
return errors.Wrapf(err, "failed to create %s cluster %s", clusterType, clusterName)
}

fmt.Printf("Successfully created cluster '%s' (%s)\n", clusterName, result.ID)
Expand Down
52 changes: 19 additions & 33 deletions cmd/add_node_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package cmd
import (
"fmt"

"github.com/cedi/kkpctl/pkg/model"
"github.com/cedi/kkpctl/pkg/utils"
"github.com/kubermatic/go-kubermatic/models"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand All @@ -22,55 +22,41 @@ var createNodeDeploymentCmd = &cobra.Command{
Use: "nodedeployment name",
Short: "Lets you create a new node deployment",
Args: cobra.ExactArgs(1),
Example: "kkpctl add nodedeployment --project 6tmbnhdl7h --cluster qvjdddt72t --nodespec flatcar1 --operatingsystem flatcar --provider optimist first_node_deployment",
Example: "kkpctl add nodedeployment --project 6tmbnhdl7h --cluster qvjdddt72t --nodespec flatcar-m1micro --operatingsystem flatcar --provider optimist --labels \"size=micro\" my_node_deployment --replica 3",
RunE: func(cmd *cobra.Command, args []string) error {
clusterName := args[0]
nodeDeploymentName := args[0]

kkp, err := Config.GetKKPClient()
if err != nil {
return err
}

var cluster models.Cluster
if datacenter == "" {
cluster, err = kkp.GetClusterInProject(clusterID, projectID)
} else if datacenter != "" && projectID != "" {
cluster, err = kkp.GetClusterInProjectInDC(clusterID, projectID, datacenter)
}

cluster, err := kkp.GetClusterInProjectInDC(clusterID, projectID, datacenter)
if err != nil {
return errors.Wrap(err, "could not fetch cluster")
return errors.Wrapf(err, "failed to find cluster %s in project %s to add a node deployment", clusterID, projectID)
}

clusterVersion, ok := cluster.Spec.Version.(string)
if !ok {
return fmt.Errorf("cluster version does not appear to be a string")
}

newNodeDp := models.NodeDeployment{
Name: args[0],
Spec: &models.NodeDeploymentSpec{
DynamicConfig: dynamicConfig,
Replicas: &nodeReplica,
Template: &models.NodeSpec{
Labels: utils.SplitLabelString(labels),
SSHUserName: "",
Taints: []*models.TaintSpec{},
Cloud: Config.NodeSpec.GetNodeCloudSpec(nodeSpecName),
OperatingSystem: Config.OSSpec.GetOperatingSystemSpec(),
Versions: &models.NodeVersionInfo{
Kubelet: clusterVersion,
},
},
},
return fmt.Errorf("fatal: cluster version does not appear to be a string")
}

_, err = kkp.CreateNodeDeployment(&newNodeDp, clusterID, projectID, cluster.Spec.Cloud.DatacenterName)
newNodeDp := model.NewNodeDeployment(
nodeDeploymentName,
clusterVersion,
nodeReplica,
dynamicConfig,
Config.NodeSpec.GetNodeCloudSpec(nodeSpecName),
Config.OSSpec.GetOperatingSystemSpec(),
utils.SplitLabelString(labels),
)

nodeDp, err := kkp.CreateNodeDeployment(newNodeDp, clusterID, projectID, cluster.Spec.Cloud.DatacenterName)
if err != nil {
return errors.Wrap(err, "Error fetching projects")
return errors.Wrapf(err, "unable to create node deployment %s for cluster %s in project %s", nodeDeploymentName, clusterID, projectID)
}

fmt.Printf("Successfully created cluster '%s'\n", clusterName)
fmt.Printf("Successfully created node deployment '%s' for cluster %s in project %s\n", nodeDp.ID, clusterID, projectID)
return nil
},
}
Expand Down
19 changes: 6 additions & 13 deletions cmd/add_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package cmd

import (
"fmt"
"strings"

"github.com/cedi/kkpctl/pkg/output"
"github.com/cedi/kkpctl/pkg/utils"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand All @@ -20,28 +20,21 @@ var createProjectCmd = &cobra.Command{
Example: "kkpctl create project test --labels=\"stage=dev,costcentre=123456\"",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
projectName := args[0]

kkp, err := Config.GetKKPClient()
if err != nil {
return err
}

mapLabels := make(map[string]string)
if labels != "" {
slicedLabels := strings.Split(labels, ",")
for _, slicedLabel := range slicedLabels {
splitLabel := strings.Split(slicedLabel, "=")
mapLabels[splitLabel[0]] = splitLabel[1]
}
}

project, err := kkp.CreateProject(args[0], mapLabels)
project, err := kkp.CreateProject(projectName, utils.SplitLabelString(labels))
if err != nil {
return errors.Wrap(err, "Error fetching projects")
return errors.Wrapf(err, "failed to create project %s", projectName)
}

parsed, err := output.ParseOutput(project, outputType, sortBy)
if err != nil {
return errors.Wrap(err, "Error parsing projects")
return errors.Wrap(err, "failed to parse output")
}
fmt.Print(parsed)
return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
// addCmd represents the add command
var configCmd = &cobra.Command{
Use: "config",
Short: "Lets you configure KKPCTL",
Short: "Configure kkpctl",
}

func init() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/config_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
// addCmd represents the add command
var configAddCmd = &cobra.Command{
Use: "add",
Short: "Lets add a specific configuration object",
Short: "Add a specific configuration object",
}

func init() {
Expand Down
11 changes: 6 additions & 5 deletions cmd/config_add_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ var configAddCloudCmd = &cobra.Command{
Example: "kkpctl config add cloud imke --url https://imke.cloud",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]

if Config.Cloud == nil {
Config.Cloud = make(map[string]config.Cloud)
}
Config.Cloud[args[0]] = config.Cloud{
URL: cloudURL,
Config.Cloud = config.NewCloudConfig()
}

Config.Cloud.Set(name, config.Cloud{URL: cloudURL})

return Config.Save()
},
}

func init() {
configAddCmd.AddCommand(configAddCloudCmd)
configAddCloudCmd.Flags().StringVar(&cloudURL, "url", "", "The KKP Cloud URL to connect to")
configAddCloudCmd.Flags().StringVar(&cloudURL, "url", "", "The URL to your KKP installation")
configAddCloudCmd.MarkFlagRequired("url")
}
15 changes: 8 additions & 7 deletions cmd/config_add_node.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/cedi/kkpctl/pkg/errors"
"github.com/kubermatic/go-kubermatic/models"
"github.com/spf13/cobra"
)
Expand All @@ -12,27 +13,27 @@ var (
)

var configAddNodeSpecCmd = &cobra.Command{
Use: "node",
Short: "Lets add a cloud node spec",
Args: cobra.ExactArgs(1),
ValidArgs: []string{"openstack"},
Use: "node",
Short: "Lets add a cloud node spec",
Args: cobra.ExactArgs(1),
}

var configAddOSNodeSpecCmd = &cobra.Command{
Use: "openstack",
Use: "openstack name",
Short: "Lets add a cloud node spec for openstack",
Args: cobra.ExactArgs(1),
Example: "kkpctl config add node openstack --flavor \"m1.micro\" --image \"Flatcar_Production 2020 - Latest\" flatcar-m1micro",
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]

err := Config.NodeSpec.AddCloudNodeSpec(args[0], models.OpenstackNodeSpec{
err := Config.NodeSpec.AddCloudNodeSpec(name, models.OpenstackNodeSpec{
Flavor: &flavor,
Image: &image,
UseFloatingIP: useFloatingIP,
})

if err != nil {
return err
return errors.Wrapf(err, "failed to add openstack cloud %s", name)
}

return Config.Save()
Expand Down
69 changes: 0 additions & 69 deletions cmd/config_add_operatingsystem.go

This file was deleted.

Loading

0 comments on commit 67a8cc2

Please sign in to comment.