Skip to content

Commit

Permalink
optional salutation
Browse files Browse the repository at this point in the history
  • Loading branch information
BenTheElder committed Nov 11, 2019
1 parent 3173f18 commit 9b8c661
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
17 changes: 17 additions & 0 deletions pkg/cluster/createoption.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,20 @@ func CreateWithStopBeforeSettingUpKubernetes(stopBeforeSettingUpKubernetes bool)
return nil
})
}

// CreateWithDisplayUsage enables displaying usage if displayUsage is true
func CreateWithDisplayUsage(displayUsage bool) CreateOption {
return createOptionAdapter(func(o *internalcreate.ClusterOptions) error {
o.DisplayUsage = displayUsage
return nil
})
}

// CreateWithDisplaySalutation enables display a salutation t the end of create
// cluster if displaySalutation is true
func CreateWithDisplaySalutation(displaySalutation bool) CreateOption {
return createOptionAdapter(func(o *internalcreate.ClusterOptions) error {
o.DisplaySalutation = displaySalutation
return nil
})
}
2 changes: 2 additions & 0 deletions pkg/cmd/kind/create/cluster/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ func runE(logger log.Logger, streams cmd.IOStreams, flags *flagpole) error {
cluster.CreateWithRetain(flags.Retain),
cluster.CreateWithWaitForReady(flags.Wait),
cluster.CreateWithKubeconfigPath(flags.Kubeconfig),
cluster.CreateWithDisplayUsage(true),
cluster.CreateWithDisplaySalutation(true),
); err != nil {
if errs := errors.Errors(err); errs != nil {
for _, problem := range errs {
Expand Down
44 changes: 33 additions & 11 deletions pkg/internal/cluster/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package create

import (
"fmt"
"math/rand"
"regexp"
"time"

Expand Down Expand Up @@ -64,6 +65,9 @@ type ClusterOptions struct {
KubeconfigPath string
// see https://github.com/kubernetes-sigs/kind/issues/324
StopBeforeSettingUpKubernetes bool // if false kind should setup kubernetes after creating nodes
// Options to control output
DisplayUsage bool
DisplaySalutation bool
}

// Cluster creates a cluster
Expand Down Expand Up @@ -137,31 +141,49 @@ func Cluster(logger log.Logger, ctx *context.Context, opts *ClusterOptions) erro
}
}

// skip the rest if we're not setting up kubernetes
if opts.StopBeforeSettingUpKubernetes {
return nil
}

return exportKubeconfig(logger, ctx, opts.KubeconfigPath)
}

// exportKubeconfig exports the cluster's kubeconfig and prints usage
func exportKubeconfig(logger log.Logger, ctx *context.Context, kubeconfigPath string) error {
// actually export KUBECONFIG
if err := kubeconfig.Export(ctx, kubeconfigPath); err != nil {
if err := kubeconfig.Export(ctx, opts.KubeconfigPath); err != nil {
return err
}

// optionally display usage
if opts.DisplayUsage {
logUsage(logger, ctx, opts.KubeconfigPath)
}
// optionally give the user a friendly salutation
if opts.DisplaySalutation {
logger.V(0).Info("")
logSalutation(logger)
}
return nil
}

func logUsage(logger log.Logger, ctx *context.Context, explicitKubeconfigPath string) {
// construct a sample command for interacting with the cluster
kctx := kubeconfig.ContextForCluster(ctx.Name())
sampleCommand := fmt.Sprintf("kubectl cluster-info --context %s", kctx)
if kubeconfigPath != "" {
if explicitKubeconfigPath != "" {
// explicit path, include this
sampleCommand += " --kubeconfig " + shellescape.Quote(kubeconfigPath)
sampleCommand += " --kubeconfig " + shellescape.Quote(explicitKubeconfigPath)
}

logger.V(0).Infof(`Set kubectl context to "%s"`, kctx)
logger.V(0).Infof("You can now use your cluster with:\n\n" + sampleCommand)
return nil
}

func logSalutation(logger log.Logger) {
salutations := []string{
"Have a nice day! 👋",
"Thanks for using kind! 😊",
"Not sure what to do next? 😅 Check out https://kind.sigs.k8s.io/docs/user/quick-start/",
"Have a question, bug, or feature request? Let us know! https://kind.sigs.k8s.io/#community 🙂",
}
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
s := salutations[r.Intn(len(salutations))]
logger.V(0).Info(s)
}

func fixupOptions(opts *ClusterOptions) error {
Expand Down

0 comments on commit 9b8c661

Please sign in to comment.