Skip to content

Commit

Permalink
adds kubeconfig command, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
xetys committed Feb 14, 2018
1 parent a13670a commit b07d16b
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 38 deletions.
4 changes: 1 addition & 3 deletions cmd/clusterAddWorker.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ You can specify the worker server type as in cluster create.`,

nodes, err := cluster.CreateWorkerNodes(sshKeyName, workerServerType, nodeCount, maxNo)

if err != nil {
log.Fatal(err)
}
FatalOnError(err)

saveCluster(cluster)

Expand Down
4 changes: 1 addition & 3 deletions cmd/clusterCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ to quickly create a Cobra application.`,
if workerCount > 0 {
var err error
nodes, err = cluster.CreateWorkerNodes(sshKeyName, workerServerType, workerCount, 0)
if err != nil {
log.Fatal(err)
}
FatalOnError(err)
}

if cluster.wait {
Expand Down
8 changes: 2 additions & 6 deletions cmd/clusterDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,12 @@ var clusterDeleteCmd = &cobra.Command{
for _, node := range cluster.Nodes {
server, _, err := AppConf.Client.Server.Get(AppConf.Context, node.Name)

if err != nil {
log.Fatal(err)
}
FatalOnError(err)

if server != nil {
_, err = AppConf.Client.Server.Delete(AppConf.Context, server)

if err != nil {
log.Fatal(err)
}
FatalOnError(err)

log.Printf("server '%s' deleted", node.Name)
} else {
Expand Down
71 changes: 64 additions & 7 deletions cmd/clusterKubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,84 @@ package cmd

import (
"github.com/spf13/cobra"
"log"
"fmt"
"errors"
"os/user"
"os"
"io/ioutil"
)

// clusterKubeconfigCmd represents the clusterKubeconfig command
var clusterKubeconfigCmd = &cobra.Command{
Use: "kubeconfig",
Short: "setups the kubeconfig for the local machine",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Long: `fetches the kubeconfig (e.g. for usage with kubectl) and saves it to ~/.kube/config, or prints it.
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Example 1: hetzner-kube cluster kubeconfig -n my-cluster # installs the kubeconfig of the cluster "my-cluster"
Example 2: hetzner-kube cluster kubeconfig -n my-cluster -b # saves the existing before installing
Example 3: hetzner-kube cluster kubeconfig -n my-cluster -p # prints the contents of kubeonfig to console
Example 4: hetzner-kube cluster kubeconfig -n my-cluster -p > my-conf.yaml # prints the contents of kubeonfig into a custom file
`,
PreRunE: validateKubeconfigCmd,
Run: func(cmd *cobra.Command, args []string) {
log.Fatalln("not implemented!")
name, _ := cmd.Flags().GetString("name")
_, cluster := AppConf.Config.FindClusterByName(name)
masterNode, err := cluster.GetMasterNode()

FatalOnError(err)

kubeConfigContent, err := runCmd(*masterNode, "cat /etc/kubernetes/admin.conf")

FatalOnError(err)


printContent, _ := cmd.Flags().GetBool("print")

if printContent {
fmt.Println(kubeConfigContent)
} else {
fmt.Println("create file")

usr, _ := user.Current()
dir := usr.HomeDir
path := fmt.Sprintf("%s/.kube", dir)

if _, err := os.Stat(path); os.IsNotExist(err) {
os.MkdirAll(path, 0755)
}

ioutil.WriteFile(fmt.Sprintf("%s/config", path), []byte(kubeConfigContent), 0755)

fmt.Println("kubeconfig configured")
}
},
}
func validateKubeconfigCmd(cmd *cobra.Command, args []string) error {

name, err := cmd.Flags().GetString("name")
if err != nil {
return nil
}

if name == "" {
return errors.New("flag --name is required")
}

idx, _ := AppConf.Config.FindClusterByName(name)

if idx == -1 {
return errors.New(fmt.Sprintf("cluster '%s' not found", name))
}
return nil
}

func init() {
clusterCmd.AddCommand(clusterKubeconfigCmd)

clusterKubeconfigCmd.Flags().StringP("name", "n", "", "name of the cluster")
clusterKubeconfigCmd.Flags().BoolP("print", "p", false, "prints output to stdout")
clusterKubeconfigCmd.Flags().BoolP("backup", "b", false, "saves existing config")

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
Expand Down
8 changes: 2 additions & 6 deletions cmd/contextAdd.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ var addCmd = &cobra.Command{
for {
fmt.Printf("Token: ")
t, err := r.ReadString('\n')
if err != nil {
log.Fatal(err)
}
FatalOnError(err)
t = strings.TrimSpace(t)
if t == "" {
continue
Expand All @@ -60,9 +58,7 @@ var addCmd = &cobra.Command{
AppConf.Client = hcloud.NewClient(opts...)
_, err := AppConf.Client.Server.All(AppConf.Context)

if err != nil {
log.Fatal(err)
}
FatalOnError(err)

context := &HetznerContext{Name: name, Token: token}
AppConf.Config.AddContext(*context)
Expand Down
5 changes: 1 addition & 4 deletions cmd/contextUse.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ var useCmd = &cobra.Command{

err := AppConf.SwitchContextByName(contextName)

if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
FatalOnError(err)

AppConf.Config.WriteCurrentConfig()
fmt.Printf("switched to context '%s'", contextName)
Expand Down
12 changes: 12 additions & 0 deletions cmd/nodeUtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"log"
"sync"
"errors"
)

func (cluster *Cluster) InstallWorkers(nodes []Node) error {
Expand Down Expand Up @@ -145,6 +146,17 @@ func (cluster *Cluster) runCreateServer(opts *hcloud.ServerCreateOpts) (*hcloud.
return &result, nil
}

func (cluster *Cluster) GetMasterNode() (node *Node, err error) {

for _, node := range cluster.Nodes {
if node.IsMaster {
return &node, nil
}
}

return nil, errors.New("no master node found")
}

func (cluster *Cluster) CreateMasterNodes(sshKeyName string, masterServerType string, count int) error {
template := Node{SSHKeyName: sshKeyName, IsMaster: true, Type: masterServerType}
log.Println("creating master nodes...")
Expand Down
10 changes: 1 addition & 9 deletions cmd/sshKeyDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ import (
var sshKeyDeleteCmd = &cobra.Command{
Use: "delete",
Short: "removes a saved SSH key from local configuration and Hetzner Cloud account",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
PreRunE: validateSHHKeyDeleteFlags,
Run: func(cmd *cobra.Command, args []string) {
name, _ := cmd.Flags().GetString("name")
Expand All @@ -45,9 +39,7 @@ to quickly create a Cobra application.`,
log.Printf("SSH key not found: %s", name)
} else {
_, err = AppConf.Client.SSHKey.Delete(AppConf.Context, sshKey)
if err != nil {
log.Fatal(err)
}
FatalOnError(err)
}

if err = AppConf.Config.DeleteSSHKey(name); err != nil {
Expand Down
6 changes: 6 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,9 @@ func Index(vs []string, t string) int {
func Include(vs []string, t string) bool {
return Index(vs, t) >= 0
}

func FatalOnError(err error) {
if err != nil {
log.Fatal(err)
}
}

0 comments on commit b07d16b

Please sign in to comment.