-
Notifications
You must be signed in to change notification settings - Fork 3
/
cluster.go
182 lines (159 loc) · 5.14 KB
/
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package cluster
import (
"context"
"fmt"
"os"
"os/user"
"path"
"strings"
"testing"
"time"
log "github.com/dnitsch/simplelog"
"github.com/pkg/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/kind/pkg/apis/config/v1alpha4"
"sigs.k8s.io/kind/pkg/cluster"
"sigs.k8s.io/kind/pkg/cmd"
podv1 "k8s.io/api/core/v1"
)
type ClusterTester struct {
Name string
EnsureUp bool
logger log.Loggeriface
}
func (c *ClusterTester) WithLogger(logger log.Loggeriface) *ClusterTester {
c.logger = logger
return c
}
// ====
// BEGIN CUSTOM K8s setup
type KubeConfig struct {
isInCI bool
k8sConfigPath string
kindConfig *v1alpha4.Cluster
masterUrl string
}
// detect either podman or docker
func detectContainerImp() cluster.ProviderOption {
if imp, ok := os.LookupEnv("DOCKER_HOST"); ok {
docker, podman := strings.HasSuffix(imp, "docker.sock"), strings.HasSuffix(imp, "podman.sock")
if docker {
return cluster.ProviderWithDocker()
}
if podman {
return cluster.ProviderWithPodman()
}
}
return nil
}
func (c *ClusterTester) DetermineKubeConfig() KubeConfig {
usr, _ := user.Current()
hd := usr.HomeDir
kubeConfigPath := path.Join(hd, ".kube/config")
resp := KubeConfig{isInCI: false, k8sConfigPath: kubeConfigPath, kindConfig: nil, masterUrl: ""}
// if len(os.Getenv("GITHUB_ACTIONS")) > 0 ||
// len(os.Getenv("TRAVIS")) > 0 ||
// len(os.Getenv("CIRCLECI")) > 0 ||
// len(os.Getenv("GITLAB_CI")) > 0 ||
// len(os.Getenv("CI")) > 0 {
// logger.Info("In CI will be using a service mounted KinD")
// resp.kindConfig = &v1alpha4.Cluster{
// Networking: v1alpha4.Networking{
// APIServerAddress: "127.0.0.1",
// APIServerPort: 6443,
// },
// }
// resp.masterUrl = "https://kind-control-plane:6443"
// return resp
// }
return resp
}
// start kind cluster
func (c *ClusterTester) StartCluster(t *testing.T, kubeStartUpConfig KubeConfig) func() {
impProvider := detectContainerImp()
if impProvider == nil {
c.logger.Info("unable to find suitable containerisation provider")
return func() {}
}
clusterProviderOptions := []cluster.ProviderOption{
cluster.ProviderWithLogger(cmd.NewLogger()),
}
clusterProviderOptions = append(clusterProviderOptions, impProvider)
provider := cluster.NewProvider(clusterProviderOptions...)
clusterCreateOptions := []cluster.CreateOption{
cluster.CreateWithNodeImage(""),
cluster.CreateWithRetain(false),
cluster.CreateWithWaitForReady(time.Second * 60),
cluster.CreateWithKubeconfigPath(kubeStartUpConfig.k8sConfigPath),
cluster.CreateWithDisplayUsage(false),
cluster.CreateWithDisplaySalutation(false),
}
if kubeStartUpConfig.isInCI && kubeStartUpConfig.kindConfig != nil {
clusterCreateOptions = append(clusterCreateOptions, cluster.CreateWithV1Alpha4Config(kubeStartUpConfig.kindConfig))
}
// create the cluster
if err := provider.Create(
c.Name,
clusterCreateOptions...,
); err != nil {
c.logger.Errorf("failed to create cluster: %v", err)
t.Fatal(errors.Wrap(err, "failed to create cluster"))
}
return func() {
// delete cluster
if err := provider.Delete(c.Name, kubeStartUpConfig.k8sConfigPath); err != nil {
t.Errorf("failed to tear down kind cluster: %s", err)
}
}
}
// k8s-client set up
func (c *ClusterTester) KubeClientSetup(t *testing.T, kubeStartUpConfig KubeConfig) (*kubernetes.Clientset, *rest.Config, error) {
cfg, err := clientcmd.BuildConfigFromFlags(kubeStartUpConfig.masterUrl, kubeStartUpConfig.k8sConfigPath)
if err != nil {
return nil, nil, fmt.Errorf("failed to initialise client from config: %s", err.Error())
}
if b, err := os.ReadFile(kubeStartUpConfig.k8sConfigPath); err != nil {
c.logger.Errorf("kubeConfigPath err: %v", err)
} else {
c.logger.Infof("kubeConfigPath file (%s) contents: %v", kubeStartUpConfig.k8sConfigPath, string(b))
}
c.logger.Infof("custom config\n\nHost: %v\nServeName: %s", cfg.Host, cfg.ServerName)
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
return nil, nil, fmt.Errorf("error building kubernetes clientset: %s", err.Error())
}
c.logger.Infof("config kubeClient: %v", kubeClient)
return kubeClient, cfg, nil
}
func (c *ClusterTester) EnsureReachable(kubeClient *kubernetes.Clientset) bool {
if !c.EnsureUp {
return true
}
var keepTrying, attempts = true, 0
for keepTrying {
attempts++
c.logger.Debugf("attemp: %d", attempts)
pod, err := kubeClient.CoreV1().Pods("kube-system").Get(context.Background(), fmt.Sprintf("kube-apiserver-%s-control-plane", c.Name), v1.GetOptions{})
if err != nil {
c.logger.Error(err)
keepTrying = false
}
if attempts < 10 {
c.logger.Debugf("status\nPhase: %s\nMessage: %s\nReason: %s\n", pod.Status.Phase, pod.Status.Message, pod.Status.Reason)
c.logger.Debugf("pod: %v", pod)
keepTrying = pod.Status.Phase != podv1.PodRunning
} else {
c.logger.Infof("status\nPhase: %s\nMessage: %s\nReason: %s\n", pod.Status.Phase, pod.Status.Message, pod.Status.Reason)
c.logger.Infof("attemps depleted")
keepTrying = false
}
time.Sleep(time.Duration(time.Second * 2))
}
return false
}
//
// END CUSTOM K8s setup
// ====