/
createcluster.go
109 lines (94 loc) · 3.49 KB
/
createcluster.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
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 the `create cluster` command
package cluster
import (
"io"
"io/ioutil"
"time"
"github.com/spf13/cobra"
"sigs.k8s.io/kind/pkg/cluster"
"sigs.k8s.io/kind/pkg/cmd"
"sigs.k8s.io/kind/pkg/errors"
"sigs.k8s.io/kind/pkg/log"
"sigs.k8s.io/kind/pkg/internal/cli"
"sigs.k8s.io/kind/pkg/internal/runtime"
)
type flagpole struct {
Name string
Config string
ImageName string
Retain bool
Wait time.Duration
Kubeconfig string
}
// NewCommand returns a new cobra.Command for cluster creation
func NewCommand(logger log.Logger, streams cmd.IOStreams) *cobra.Command {
flags := &flagpole{}
cmd := &cobra.Command{
Args: cobra.NoArgs,
Use: "cluster",
Short: "Creates a local Kubernetes cluster",
Long: "Creates a local Kubernetes cluster using Docker container 'nodes'",
RunE: func(cmd *cobra.Command, args []string) error {
cli.OverrideDefaultName(cmd.Flags())
return runE(logger, streams, flags)
},
}
cmd.Flags().StringVar(&flags.Name, "name", "", "cluster name, overrides KIND_CLUSTER_NAME, config (default kind)")
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().DurationVar(&flags.Wait, "wait", time.Duration(0), "wait for control plane node to be ready (default 0s)")
cmd.Flags().StringVar(&flags.Kubeconfig, "kubeconfig", "", "sets kubeconfig path instead of $KUBECONFIG or $HOME/.kube/config")
return cmd
}
func runE(logger log.Logger, streams cmd.IOStreams, flags *flagpole) error {
provider := cluster.NewProvider(
cluster.ProviderWithLogger(logger),
runtime.GetDefault(logger),
)
// handle config flag, we might need to read from stdin
withConfig, err := configOption(flags.Config, streams.In)
if err != nil {
return err
}
// create the cluster
if err = provider.Create(
flags.Name,
withConfig,
cluster.CreateWithNodeImage(flags.ImageName),
cluster.CreateWithRetain(flags.Retain),
cluster.CreateWithWaitForReady(flags.Wait),
cluster.CreateWithKubeconfigPath(flags.Kubeconfig),
cluster.CreateWithDisplayUsage(true),
cluster.CreateWithDisplaySalutation(true),
); err != nil {
return errors.Wrap(err, "failed to create cluster")
}
return nil
}
// configOption converts the raw --config flag value to a cluster creation
// option matching it. it will read from stdin if the flag value is `-`
func configOption(rawConfigFlag string, stdin io.Reader) (cluster.CreateOption, error) {
// if not - then we are using a real file
if rawConfigFlag != "-" {
return cluster.CreateWithConfigFile(rawConfigFlag), nil
}
// otherwise read from stdin
raw, err := ioutil.ReadAll(stdin)
if err != nil {
return nil, errors.Wrap(err, "error reading config from stdin")
}
return cluster.CreateWithRawConfig(raw), nil
}