Skip to content

Commit

Permalink
move cluster options to cluster package
Browse files Browse the repository at this point in the history
  • Loading branch information
BenTheElder committed Nov 1, 2019
1 parent b3bd1ff commit b4edaa7
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 165 deletions.
89 changes: 0 additions & 89 deletions pkg/cluster/create/cluster.go

This file was deleted.

100 changes: 100 additions & 0 deletions pkg/cluster/createoption.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
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 cluster

import (
"time"

"sigs.k8s.io/kind/pkg/apis/config/v1alpha3"
internalencoding "sigs.k8s.io/kind/pkg/internal/apis/config/encoding"
internalcreate "sigs.k8s.io/kind/pkg/internal/cluster/create"
)

// CreateOption is a Provider.Create option
type CreateOption interface {
apply(*internalcreate.ClusterOptions) error
}

type createOptionAdapter func(*internalcreate.ClusterOptions) error

func (c createOptionAdapter) apply(o *internalcreate.ClusterOptions) error {
return c(o)
}

// CreateWithConfigFile configures the config file path to use
func CreateWithConfigFile(path string) CreateOption {
return createOptionAdapter(func(o *internalcreate.ClusterOptions) error {
var err error
o.Config, err = internalencoding.Load(path)
return err
})
}

// CreateWithV1Alpha3Config configures the cluster with a v1alpha3 config
func CreateWithV1Alpha3Config(config *v1alpha3.Cluster) CreateOption {
return createOptionAdapter(func(o *internalcreate.ClusterOptions) error {
o.Config = internalencoding.V1Alpha3ToInternal(config)
return nil
})
}

// CreateWithNodeImage overrides the image on all nodes in config
// as an easy way to change the Kubernetes version
func CreateWithNodeImage(nodeImage string) CreateOption {
return createOptionAdapter(func(o *internalcreate.ClusterOptions) error {
o.NodeImage = nodeImage
return nil
})
}

// CreateWithRetain disables deletion of nodes and any other cleanup
// that would normally occur after a failure to create
// This is mainly used for debugging purposes
func CreateWithRetain(retain bool) CreateOption {
return createOptionAdapter(func(o *internalcreate.ClusterOptions) error {
o.Retain = retain
return nil
})
}

// CreateWithWaitForReady configures a maximum wait time for the control plane
// node(s) to be ready. By defeault no waiting is performed
func CreateWithWaitForReady(waitTime time.Duration) CreateOption {
return createOptionAdapter(func(o *internalcreate.ClusterOptions) error {
o.WaitForReady = waitTime
return nil
})
}

// CreateWithKubeconfigPath sets the explicit --kubeconfig path
func CreateWithKubeconfigPath(explicitPath string) CreateOption {
return createOptionAdapter(func(o *internalcreate.ClusterOptions) error {
o.KubeconfigPath = explicitPath
return nil
})
}

// CreateWithStopBeforeSettingUpKubernetes enables skipping setting up
// kubernetes (kubeadm init etc.) after creating node containers
// This generally shouldn't be used and is only lightly supported, but allows
// provisioning node containers for experimentation
func CreateWithStopBeforeSettingUpKubernetes(stopBeforeSettingUpKubernetes bool) CreateOption {
return createOptionAdapter(func(o *internalcreate.ClusterOptions) error {
o.StopBeforeSettingUpKubernetes = stopBeforeSettingUpKubernetes
return nil
})
}
18 changes: 18 additions & 0 deletions pkg/cluster/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Copyright 2018 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 cluster implements kind kubernetes-in-docker cluster management
package cluster
13 changes: 9 additions & 4 deletions pkg/cluster/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// Package cluster implements kind kubernetes-in-docker cluster management
package cluster

import (
"sigs.k8s.io/kind/pkg/cluster/constants"
"sigs.k8s.io/kind/pkg/cluster/create"
"sigs.k8s.io/kind/pkg/cluster/nodes"

internalcontext "sigs.k8s.io/kind/pkg/internal/cluster/context"
Expand Down Expand Up @@ -60,8 +58,15 @@ func (p *Provider) ic(name string) *internalcontext.Context {
}

// Create provisions and starts a kubernetes-in-docker cluster
func (p *Provider) Create(name string, options ...create.ClusterOption) error {
return internalcreate.Cluster(p.ic(name), options...)
func (p *Provider) Create(name string, options ...CreateOption) error {
// apply options
opts := &internalcreate.ClusterOptions{}
for _, o := range options {
if err := o.apply(opts); err != nil {
return err
}
}
return internalcreate.Cluster(p.ic(name), opts)
}

// Delete tears down a kubernetes-in-docker cluster
Expand Down
11 changes: 5 additions & 6 deletions pkg/cmd/kind/create/cluster/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/spf13/cobra"

"sigs.k8s.io/kind/pkg/cluster"
"sigs.k8s.io/kind/pkg/cluster/create"
"sigs.k8s.io/kind/pkg/errors"
"sigs.k8s.io/kind/pkg/globals"
)
Expand Down Expand Up @@ -75,11 +74,11 @@ func runE(flags *flagpole) error {
fmt.Printf("Creating cluster %q ...\n", flags.Name)
if err = provider.Create(
flags.Name,
create.WithConfigFile(flags.Config),
create.WithNodeImage(flags.ImageName),
create.Retain(flags.Retain),
create.WaitForReady(flags.Wait),
create.WithKubeconfigPath(flags.Kubeconfig),
cluster.CreateWithConfigFile(flags.Config),
cluster.CreateWithNodeImage(flags.ImageName),
cluster.CreateWithRetain(flags.Retain),
cluster.CreateWithWaitForReady(flags.Wait),
cluster.CreateWithKubeconfigPath(flags.Kubeconfig),
); err != nil {
if errs := errors.Errors(err); errs != nil {
for _, problem := range errs {
Expand Down
44 changes: 21 additions & 23 deletions pkg/internal/cluster/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ package create
import (
"fmt"
"regexp"
"time"

"github.com/alessio/shellescape"

"sigs.k8s.io/kind/pkg/internal/cluster/create/actions"

"sigs.k8s.io/kind/pkg/cluster/create"
"sigs.k8s.io/kind/pkg/errors"
"sigs.k8s.io/kind/pkg/globals"
"sigs.k8s.io/kind/pkg/internal/apis/config"
"sigs.k8s.io/kind/pkg/internal/apis/config/encoding"
"sigs.k8s.io/kind/pkg/internal/cluster/context"
createtypes "sigs.k8s.io/kind/pkg/internal/cluster/create/types"
"sigs.k8s.io/kind/pkg/internal/cluster/delete"
"sigs.k8s.io/kind/pkg/internal/util/cli"

Expand All @@ -56,11 +55,22 @@ const (
// https://godoc.org/github.com/docker/docker/daemon/names#pkg-constants
var validNameRE = regexp.MustCompile(`^[a-zA-Z0-9_.-]+$`)

// ClusterOptions holds cluster creation options
type ClusterOptions struct {
Config *config.Cluster
// NodeImage overrides the nodes' images in Config if non-zero
NodeImage string
Retain bool
WaitForReady time.Duration
KubeconfigPath string
// see https://github.com/kubernetes-sigs/kind/issues/324
StopBeforeSettingUpKubernetes bool // if false kind should setup kubernetes after creating nodes
}

// Cluster creates a cluster
func Cluster(ctx *context.Context, options ...create.ClusterOption) error {
// apply options, do defaulting etc.
opts, err := collectOptions(options...)
if err != nil {
func Cluster(ctx *context.Context, opts *ClusterOptions) error {
// default / process options (namely config)
if err := fixupOptions(opts); err != nil {
return err
}

Expand Down Expand Up @@ -99,7 +109,7 @@ func Cluster(ctx *context.Context, options ...create.ClusterOption) error {
loadbalancer.NewAction(), // setup external loadbalancer
configaction.NewAction(), // setup kubeadm config
}
if opts.SetupKubernetes {
if !opts.StopBeforeSettingUpKubernetes {
actionsToRun = append(actionsToRun,
kubeadminit.NewAction(), // run kubeadm init
)
Expand Down Expand Up @@ -128,7 +138,7 @@ func Cluster(ctx *context.Context, options ...create.ClusterOption) error {
}
}

if !opts.SetupKubernetes {
if opts.StopBeforeSettingUpKubernetes {
return nil
}

Expand All @@ -155,25 +165,13 @@ func exportKubeconfig(ctx *context.Context, kubeconfigPath string) error {
return nil
}

func collectOptions(options ...create.ClusterOption) (*createtypes.ClusterOptions, error) {
// apply options
opts := &createtypes.ClusterOptions{
SetupKubernetes: true,
}
for _, option := range options {
newOpts, err := option(opts)
if err != nil {
return nil, err
}
opts = newOpts
}

func fixupOptions(opts *ClusterOptions) error {
// do post processing for options
// first ensure we at least have a default cluster config
if opts.Config == nil {
cfg, err := encoding.Load("")
if err != nil {
return nil, err
return err
}
opts.Config = cfg
}
Expand All @@ -192,5 +190,5 @@ func collectOptions(options ...create.ClusterOption) (*createtypes.ClusterOption
// may be constructed in memory rather than from disk)
config.SetDefaultsCluster(opts.Config)

return opts, nil
return nil
}
Loading

0 comments on commit b4edaa7

Please sign in to comment.