forked from kluctl/kluctl
/
kind_cluster.go
163 lines (143 loc) · 4.07 KB
/
kind_cluster.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package e2e
import (
"fmt"
"github.com/codablock/kluctl/pkg/utils"
"github.com/codablock/kluctl/pkg/utils/uo"
"github.com/codablock/kluctl/pkg/yaml"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"os"
"os/exec"
"path/filepath"
"sigs.k8s.io/kind/pkg/cluster"
kindcmd "sigs.k8s.io/kind/pkg/cmd"
"testing"
"time"
)
type KindCluster struct {
Name string
kubeconfig string
config *rest.Config
}
func CreateKindCluster(name, kubeconfigPath string) (*KindCluster, error) {
provider := cluster.NewProvider(cluster.ProviderWithLogger(kindcmd.NewLogger()))
c := &KindCluster{
Name: name,
kubeconfig: kubeconfigPath,
}
n, err := provider.ListNodes(name)
if err != nil {
return nil, err
}
if len(n) == 0 {
if err := kindCreate(name, kubeconfigPath); err != nil {
return nil, err
}
}
return c, nil
}
// Delete removes the cluster from kind. The cluster may not be deleted straight away - this only issues a delete command
func (c *KindCluster) Delete() error {
provider := cluster.NewProvider(cluster.ProviderWithLogger(kindcmd.NewLogger()))
return provider.Delete(c.Name, c.kubeconfig)
}
// Kubeconfig returns the path to the cluster kubeconfig
func (c *KindCluster) Kubeconfig() string {
return c.kubeconfig
}
// RESTConfig returns K8s client config to pass to clientset objects
func (c *KindCluster) RESTConfig() *rest.Config {
if c.config == nil {
var err error
c.config, err = clientcmd.BuildConfigFromFlags("", c.Kubeconfig())
if err != nil {
panic(err)
}
}
return c.config
}
func (c *KindCluster) Kubectl(args ...string) (string, error) {
cmd := exec.Command("kubectl", args...)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintf("KUBECONFIG=%s", c.kubeconfig))
stdout, err := cmd.Output()
return string(stdout), err
}
func (c *KindCluster) KubectlMust(t *testing.T, args ...string) string {
stdout, err := c.Kubectl(args...)
if err != nil {
if e, ok := err.(*exec.ExitError); ok {
t.Fatalf("%v, stderr=%s\n", err, string(e.Stderr))
} else {
t.Fatal(err)
}
}
return stdout
}
func (c *KindCluster) KubectlYaml(args ...string) (*uo.UnstructuredObject, error) {
args = append(args, "-oyaml")
stdout, err := c.Kubectl(args...)
if err != nil {
return nil, err
}
ret := uo.New()
err = yaml.ReadYamlString(stdout, ret)
return ret, err
}
func (c *KindCluster) KubectlYamlMust(t *testing.T, args ...string) *uo.UnstructuredObject {
o, err := c.KubectlYaml(args...)
if err != nil {
if e, ok := err.(*exec.ExitError); ok {
t.Fatalf("%v, stderr=%s\n", err, string(e.Stderr))
} else {
t.Fatal(err)
}
}
return o
}
// kindCreate creates the kind cluster. It will retry up to 10 times if cluster creation fails.
func kindCreate(name, kubeconfig string) error {
fmt.Printf("🌧️ Creating kind cluster %s...\n", name)
provider := cluster.NewProvider(cluster.ProviderWithLogger(kindcmd.NewLogger()))
attempts := 0
maxAttempts := 10
for {
err := provider.Create(
name,
cluster.CreateWithNodeImage(""),
cluster.CreateWithRetain(false),
cluster.CreateWithWaitForReady(time.Duration(0)),
cluster.CreateWithKubeconfigPath(kubeconfig),
cluster.CreateWithDisplayUsage(false),
)
if err == nil {
return nil
}
fmt.Printf("Error bringing up cluster, will retry (attempt %d): %v", attempts, err)
attempts++
if attempts >= maxAttempts {
return errors.Wrapf(err, "Error bringing up cluster, exceeded max attempts (%d)", attempts)
}
}
}
func createKindCluster(name string, kubeconfig string) *KindCluster {
k, err := CreateKindCluster(name, kubeconfig)
if err != nil {
log.Fatal(err)
}
return k
}
func createDefaultKindCluster() *KindCluster {
kindClusterName := os.Getenv("KIND_CLUSTER_NAME")
kindKubeconfig := os.Getenv("KIND_KUBECONFIG")
if kindClusterName == "" {
kindClusterName = "kluctl-e2e"
}
if kindKubeconfig == "" {
kindKubeconfig = filepath.Join(utils.GetTmpBaseDir(), "kluctl-e2e-kubeconfig.yml")
}
return createKindCluster(kindClusterName, kindKubeconfig)
}
var defaultKindCluster = createDefaultKindCluster()