Skip to content

Commit

Permalink
Merge pull request #260 from neolit123/name-flag
Browse files Browse the repository at this point in the history
CLI improvements to create, delete and get
  • Loading branch information
BenTheElder authored Feb 4, 2019
2 parents 9307ec0 + 0d21fce commit d1773a7
Show file tree
Hide file tree
Showing 16 changed files with 170 additions and 84 deletions.
3 changes: 0 additions & 3 deletions cmd/kind/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ func NewCommand() *cobra.Command {
Use: "build",
Short: "Build one of [base-image, node-image]",
Long: "Build the base node image (base-image) or the node image (node-image)",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}
// add subcommands
cmd.AddCommand(baseimage.NewCommand())
Expand Down
13 changes: 12 additions & 1 deletion cmd/kind/create/cluster/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package cluster

import (
"fmt"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -49,7 +50,7 @@ func NewCommand() *cobra.Command {
return runE(flags, cmd, args)
},
}
cmd.Flags().StringVar(&flags.Name, "name", "1", "cluster context name")
cmd.Flags().StringVar(&flags.Name, "name", cluster.DefaultName, "cluster context name")
cmd.Flags().StringVar(&flags.Config, "config", "", "path to a kind config file")
cmd.Flags().StringVar(&flags.ImageName, "image", "", "node docker image to use for booting the cluster")
cmd.Flags().BoolVar(&flags.Retain, "retain", false, "retain nodes for debugging when cluster creation fails")
Expand All @@ -75,6 +76,15 @@ func runE(flags *flagpole, cmd *cobra.Command, args []string) error {
return errors.New("aborting due to invalid configuration")
}

// Check if the cluster name already exists
known, err := cluster.IsKnown(flags.Name)
if err != nil {
return err
}
if known {
return errors.Errorf("a cluster with the name %q already exists", flags.Name)
}

// create a cluster context and create the cluster
ctx := cluster.NewContext(flags.Name)
if flags.ImageName != "" {
Expand All @@ -91,6 +101,7 @@ func runE(flags *flagpole, cmd *cobra.Command, args []string) error {
return errors.New("aborting due to invalid configuration")
}
}
fmt.Printf("Creating cluster %q ...\n", flags.Name)
if err = ctx.Create(cfg, flags.Retain, flags.Wait); err != nil {
return errors.Wrap(err, "failed to create cluster")
}
Expand Down
10 changes: 0 additions & 10 deletions cmd/kind/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ limitations under the License.
package create

import (
"fmt"

"github.com/spf13/cobra"

createcluster "sigs.k8s.io/kind/cmd/kind/create/cluster"
Expand All @@ -32,15 +30,7 @@ func NewCommand() *cobra.Command {
Use: "create",
Short: "Creates one of [cluster]",
Long: "Creates one of local Kubernetes cluster (cluster)",
RunE: run,
}
cmd.AddCommand(createcluster.NewCommand())
return cmd
}

func run(cmd *cobra.Command, args []string) error {
fmt.Println("You likely want `kind create cluster`, please migrate!")
fmt.Println()
cmd.Usage()
return nil
}
14 changes: 13 additions & 1 deletion cmd/kind/delete/cluster/deletecluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ limitations under the License.
package cluster

import (
"fmt"

"github.com/pkg/errors"
"github.com/spf13/cobra"

Expand All @@ -42,11 +44,21 @@ func NewCommand() *cobra.Command {
return runE(flags, cmd, args)
},
}
cmd.Flags().StringVar(&flags.Name, "name", "1", "the cluster name")
cmd.Flags().StringVar(&flags.Name, "name", cluster.DefaultName, "the cluster name")
return cmd
}

func runE(flags *flagpole, cmd *cobra.Command, args []string) error {
// Check if the cluster name exists
known, err := cluster.IsKnown(flags.Name)
if err != nil {
return err
}
if !known {
return errors.Errorf("unknown cluster %q", flags.Name)
}
// Delete the cluster
fmt.Printf("Deleting cluster %q ...\n", flags.Name)
ctx := cluster.NewContext(flags.Name)
if err := ctx.Delete(); err != nil {
return errors.Wrap(err, "failed to delete cluster")
Expand Down
10 changes: 0 additions & 10 deletions cmd/kind/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ limitations under the License.
package delete

import (
"fmt"

"github.com/spf13/cobra"

deletecluster "sigs.k8s.io/kind/cmd/kind/delete/cluster"
Expand All @@ -33,15 +31,7 @@ func NewCommand() *cobra.Command {
Use: "delete",
Short: "Deletes one of [cluster]",
Long: "Deletes one of [cluster]",
RunE: run,
}
cmd.AddCommand(deletecluster.NewCommand())
return cmd
}

func run(cmd *cobra.Command, args []string) error {
fmt.Println("You likely want `kind delete cluster`, please migrate!")
fmt.Println()
cmd.Usage()
return nil
}
3 changes: 0 additions & 3 deletions cmd/kind/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ func NewCommand() *cobra.Command {
Use: "export",
Short: "exports one of [logs]",
Long: "exports one of [logs]",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}
// add subcommands
cmd.AddCommand(logs.NewCommand())
Expand Down
12 changes: 10 additions & 2 deletions cmd/kind/export/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package logs
import (
"fmt"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"sigs.k8s.io/kind/pkg/cluster"
Expand All @@ -43,12 +44,19 @@ func NewCommand() *cobra.Command {
return runE(flags, cmd, args)
},
}
// TODO(bentheelder): this default should be a constant somewhere
cmd.Flags().StringVar(&flags.Name, "name", "1", "the cluster context name")
cmd.Flags().StringVar(&flags.Name, "name", cluster.DefaultName, "the cluster context name")
return cmd
}

func runE(flags *flagpole, cmd *cobra.Command, args []string) error {
// Check if the cluster name exists
known, err := cluster.IsKnown(flags.Name)
if err != nil {
return err
}
if !known {
return errors.Errorf("unknown cluster %q", flags.Name)
}
// get the optional directory argument, or create a tempdir
var dir string
if len(args) == 0 {
Expand Down
3 changes: 1 addition & 2 deletions cmd/kind/get/clusters/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package clusters
import (
"fmt"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"sigs.k8s.io/kind/pkg/cluster"
Expand All @@ -44,7 +43,7 @@ func NewCommand() *cobra.Command {
func runE(cmd *cobra.Command, args []string) error {
clusters, err := cluster.List()
if err != nil {
return errors.Wrap(err, "error listing clusters")
return err
}
for _, cluster := range clusters {
fmt.Println(cluster.Name())
Expand Down
7 changes: 3 additions & 4 deletions cmd/kind/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"sigs.k8s.io/kind/cmd/kind/get/clusters"
"sigs.k8s.io/kind/cmd/kind/get/kubeconfigpath"
"sigs.k8s.io/kind/cmd/kind/get/nodes"
)

// NewCommand returns a new cobra.Command for get
Expand All @@ -31,13 +32,11 @@ func NewCommand() *cobra.Command {
// TODO(bentheelder): more detailed usage
Use: "get",
Short: "Gets one of [clusters, kubeconfig-path]",
Long: "Gets one of [clusters, kubeconfig-path]",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
Long: "Gets one of [clusters, nodes, kubeconfig-path]",
}
// add subcommands
cmd.AddCommand(clusters.NewCommand())
cmd.AddCommand(nodes.NewCommand())
cmd.AddCommand(kubeconfigpath.NewCommand())
return cmd
}
12 changes: 11 additions & 1 deletion cmd/kind/get/kubeconfigpath/kubeconfigpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package kubeconfigpath
import (
"fmt"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"sigs.k8s.io/kind/pkg/cluster"
Expand All @@ -45,13 +46,22 @@ func NewCommand() *cobra.Command {
cmd.Flags().StringVar(
&flags.Name,
"name",
"1",
cluster.DefaultName,
"the cluster context name",
)
return cmd
}

func runE(flags *flagpole, cmd *cobra.Command, args []string) error {
// Check if a cluster with this name exists
known, err := cluster.IsKnown(flags.Name)
if err != nil {
return err
}
if !known {
return errors.Errorf("unknown cluster %q", flags.Name)
}
// Obtain the kubeconfig path for this cluster
ctx := cluster.NewContext(flags.Name)
fmt.Println(ctx.KubeConfigPath())
return nil
Expand Down
69 changes: 69 additions & 0 deletions cmd/kind/get/nodes/nodes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package nodes implements the `nodes` command
package nodes

import (
"fmt"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"sigs.k8s.io/kind/pkg/cluster"
clusternodes "sigs.k8s.io/kind/pkg/cluster/nodes"
)

type flagpole struct {
Name string
}

// NewCommand returns a new cobra.Command for getting the list of nodes for a given cluster
func NewCommand() *cobra.Command {
flags := &flagpole{}
cmd := &cobra.Command{
Args: cobra.NoArgs,
Use: "nodes",
Short: "lists existing kind nodes by their name",
Long: "lists existing kind nodes by their name",
RunE: func(cmd *cobra.Command, args []string) error {
return runE(flags, cmd, args)
},
}
cmd.Flags().StringVar(
&flags.Name,
"name",
cluster.DefaultName,
"the cluster context name",
)
return cmd
}

func runE(flags *flagpole, cmd *cobra.Command, args []string) error {
// List nodes by cluster context name
n, err := clusternodes.ListByCluster()
if err != nil {
return err
}
nodes, known := n[flags.Name]
if !known {
return errors.Errorf("unknown cluster %q", flags.Name)
}
for _, node := range nodes {
fmt.Println(node.String())
}
return nil
}
11 changes: 4 additions & 7 deletions cmd/kind/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ func NewCommand() *cobra.Command {
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return runE(flags, cmd, args)
},
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
Version: version.Version,
SilenceUsage: true,
Version: version.Version,
}
cmd.PersistentFlags().StringVar(
&flags.LogLevel,
Expand Down Expand Up @@ -90,7 +88,7 @@ func Run() error {
return NewCommand().Execute()
}

// Main wraps Run, adding a log.Fatal(err) on error, and setting the log formatter
// Main wraps Run and sets the log formatter
func Main() {
// let's explicitly set stdout
log.SetOutput(os.Stdout)
Expand All @@ -105,7 +103,6 @@ func Main() {
ForceColors: logutil.IsTerminal(log.StandardLogger().Out),
})
if err := Run(); err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(-1)
os.Exit(1)
}
}
Loading

0 comments on commit d1773a7

Please sign in to comment.