Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce the --use flag when creating a cluster #517

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/kind/create/cluster/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type flagpole struct {
Config string
ImageName string
Retain bool
Use bool
Wait time.Duration
}

Expand All @@ -55,6 +56,7 @@ func NewCommand() *cobra.Command {
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")
cmd.Flags().BoolVar(&flags.Use, "use", false, "set KUBECONFIG to the configuration of this cluster, once it's created")
cmd.Flags().DurationVar(&flags.Wait, "wait", time.Duration(0), "Wait for control plane node to be ready (default 0s)")
return cmd
}
Expand Down Expand Up @@ -106,6 +108,7 @@ func runE(flags *flagpole, cmd *cobra.Command, args []string) error {
if err = ctx.Create(cfg,
create.Retain(flags.Retain),
create.WaitForReady(flags.Wait),
create.Use(flags.Use),
); err != nil {
return errors.Wrap(err, "failed to create cluster")
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/cluster/create/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ func WaitForReady(interval time.Duration) ClusterOption {
}
}

// Use configures the cluster created to be active cluster after its creation
func Use(use bool) ClusterOption {
return func(o *internalcreate.Options) *internalcreate.Options {
o.Use = use
return o
}
}

// SetupKubernetes configures create command to setup kubernetes after creating nodes containers
// TODO: Refactor this. It is a temporary solution for a phased breakdown of different
// operations, specifically create. see https://github.com/kubernetes-sigs/kind/issues/324
Expand Down
51 changes: 49 additions & 2 deletions pkg/cluster/internal/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ import (
"sigs.k8s.io/kind/pkg/cluster/internal/create/actions/kubeadmjoin"
"sigs.k8s.io/kind/pkg/cluster/internal/create/actions/loadbalancer"
"sigs.k8s.io/kind/pkg/cluster/internal/create/actions/waitforready"
"sigs.k8s.io/kind/pkg/util"
)

// Options holds cluster creation options
// NOTE: this is only exported for usage by ./../create
type Options struct {
Retain bool
WaitForReady time.Duration
Use bool
//TODO: Refactor this. It is a temporary solution for a phased breakdown of different
// operations, specifically create. see https://github.com/kubernetes-sigs/kind/issues/324
SetupKubernetes bool // if kind should setup kubernetes after creating nodes
Expand Down Expand Up @@ -119,8 +121,49 @@ func Cluster(ctx *context.Context, cfg *config.Cluster, opts *Options) error {
return nil
}

// print how to set KUBECONFIG to point to the cluster etc.
printUsage(ctx.Name())
if opts.Use {
if err := setupKubeconfig(ctx, cfg); err != nil {
return err
}
printFootnote()
} else {
// print how to set KUBECONFIG to point to the cluster etc.
printUsage(ctx.Name())
}

return nil
}

func setupKubeconfig(ctx *context.Context, cfg *config.Cluster) error {

pki := util.NewKubeConfig()
certData, err := pki.CertData(ctx.KubeConfigPath())
if err != nil {
return err
}

pkiPaths, err := pki.CertDataPath(certData)
if err != nil {
return err
}

var server string
for _, v := range certData.Clusters {
server = v.Server
}

kcs := &util.KubeConfigSetup{
ClusterName: ctx.Name(),
ClusterServerAddress: server,
ClientCertificate: pkiPaths.ClientCertPath,
ClientKey: pkiPaths.ClientKeyPath,
CertificateAuthority: pkiPaths.CertificateAuthorityPath,
}

kcs.SetKubeConfigFile(util.DefaultKubeconfigFile)
if err := util.SetupKubeConfig(kcs); err != nil {
return err
}

return nil
}
Expand Down Expand Up @@ -162,3 +205,7 @@ func printSetupInstruction(name string) {
name,
)
}

func printFootnote() {
fmt.Print("\nYou can now use the cluster:\nkubectl cluster-info\n")
}
Loading