Skip to content

Commit

Permalink
Add a retain flag to retain the broken nodes for debugging.
Browse files Browse the repository at this point in the history
Signed-off-by: Jintao Zhang <zhangjintao9020@gmail.com>
  • Loading branch information
tao12345666333 committed Nov 21, 2018
1 parent 9a6f910 commit 00c8131
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 20 deletions.
4 changes: 3 additions & 1 deletion cmd/kind/create/cluster/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type flags struct {
Name string
Config string
ImageName string
Retain bool
}

// NewCommand returns a new cobra.Command for cluster creation
Expand All @@ -47,6 +48,7 @@ func NewCommand() *cobra.Command {
cmd.Flags().StringVar(&flags.Name, "name", "1", "the 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, "whether retain the broken nodes for debugging")
return cmd
}

Expand All @@ -68,7 +70,7 @@ func run(flags *flags, cmd *cobra.Command, args []string) {
log.Fatal("Aborting due to invalid configuration.")
}
// create a cluster context and create the cluster
ctx, err := cluster.NewContext(flags.Name)
ctx, err := cluster.NewContext(flags.Name, flags.Retain)
if err != nil {
log.Fatalf("Failed to create cluster context! %v", err)
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/kind/delete/cluster/deletecluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
)

type flags struct {
Name string
Name string
Retain bool
}

// NewCommand returns a new cobra.Command for cluster creation
Expand All @@ -41,12 +42,13 @@ func NewCommand() *cobra.Command {
},
}
cmd.Flags().StringVar(&flags.Name, "name", "1", "the cluster name")
cmd.Flags().BoolVar(&flags.Retain, "retain", false, "whether retain the broken nodes for debugging")
return cmd
}

func run(flags *flags, cmd *cobra.Command, args []string) {
// TODO(bentheelder): make this more configurable
ctx, err := cluster.NewContext(flags.Name)
ctx, err := cluster.NewContext(flags.Name, flags.Retain)
if err != nil {
log.Fatalf("Failed to create cluster context! %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/kind/get/kubeconfigpath/kubeconfigpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewCommand() *cobra.Command {
}

func runE(flags *flags, cmd *cobra.Command, args []string) error {
ctx, err := cluster.NewContext(flags.Name)
ctx, err := cluster.NewContext(flags.Name, false)
if err != nil {
return fmt.Errorf("failed to create cluster context! %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func List() ([]Context, error) {
}
clusters := []Context{}
for name := range n {
clusters = append(clusters, *newContextNoValidation(name))
clusters = append(clusters, *newContextNoValidation(name, false))
}
return clusters, nil
}
39 changes: 24 additions & 15 deletions pkg/cluster/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
type Context struct {
name string
status *logutil.Status
retain bool
}

// similar to valid docker container names, but since we will prefix
Expand All @@ -52,7 +53,7 @@ var validNameRE = regexp.MustCompile(`^[a-zA-Z0-9_.-]+$`)

// NewContext returns a new cluster management context
// if name is "" the default ("1") will be used
func NewContext(name string) (ctx *Context, err error) {
func NewContext(name string, retain bool) (ctx *Context, err error) {
// TODO(bentheelder): move validation out of NewContext and into create type
// calls, so that EG delete still works on previously valid, now invalid
// names if kind updates
Expand All @@ -66,16 +67,17 @@ func NewContext(name string) (ctx *Context, err error) {
name, validNameRE.String(),
)
}
return newContextNoValidation(name), nil
return newContextNoValidation(name, retain), nil
}

// internal helper that does the actual allocation consitently, but does not
// validate the cluster name
// we need this so that if we tighten the validation, other internal code
// can still create contexts to existing clusters by name (see List())
func newContextNoValidation(name string) *Context {
func newContextNoValidation(name string, retain bool) *Context {
return &Context{
name: name,
name: name,
retain: retain,
}
}

Expand Down Expand Up @@ -180,8 +182,9 @@ func (c *Context) provisionControlPlane(
// that don't seem to be configurable, and we need that flag
if err := node.FixMounts(); err != nil {
// TODO(bentheelder): logging here
// TODO(bentheelder): add a flag to retain the broken nodes for debugging
nodes.Delete(node)
if !c.retain {
nodes.Delete(node)
}
return "", err
}

Expand All @@ -198,17 +201,19 @@ func (c *Context) provisionControlPlane(
// signal the node entrypoint to continue booting into systemd
if err := node.SignalStart(); err != nil {
// TODO(bentheelder): logging here
// TODO(bentheelder): add a flag to retain the broken nodes for debugging
nodes.Delete(node)
if !c.retain {
nodes.Delete(node)
}
return "", err
}

c.status.Start(fmt.Sprintf("[%s] Waiting for docker to be ready 🐋", nodeName))
// wait for docker to be ready
if !node.WaitForDocker(time.Now().Add(time.Second * 30)) {
// TODO(bentheelder): logging here
// TODO(bentheelder): add a flag to retain the broken nodes for debugging
nodes.Delete(node)
if !c.retain {
nodes.Delete(node)
}
return "", fmt.Errorf("timed out waiting for docker to be ready on node")
}

Expand All @@ -219,8 +224,9 @@ func (c *Context) provisionControlPlane(
kubeVersion, err := node.KubeVersion()
if err != nil {
// TODO(bentheelder): logging here
// TODO(bentheelder): add a flag to retain the broken nodes for debugging
nodes.Delete(node)
if !c.retain {
nodes.Delete(node)
}
return "", fmt.Errorf("failed to get kubernetes version from node: %v", err)
}

Expand All @@ -234,15 +240,18 @@ func (c *Context) provisionControlPlane(
},
)
if err != nil {
nodes.Delete(node)
if !c.retain {
nodes.Delete(node)
}
return "", fmt.Errorf("failed to create kubeadm config: %v", err)
}

// copy the config to the node
if err := node.CopyTo(kubeadmConfig, "/kind/kubeadm.conf"); err != nil {
// TODO(bentheelder): logging here
// TODO(bentheelder): add a flag to retain the broken nodes for debugging
nodes.Delete(node)
if !c.retain {
nodes.Delete(node)
}
return kubeadmConfig, errors.Wrap(err, "failed to copy kubeadm config to node")
}

Expand Down

0 comments on commit 00c8131

Please sign in to comment.