forked from keel-hq/keel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.go
134 lines (108 loc) · 2.77 KB
/
helpers.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
package tests
import (
"context"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
v1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
// load the gcp plugin (required to authenticate against GKE clusters).
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
log "github.com/sirupsen/logrus"
)
var kubeConfig = flag.String("kubeconfig", "", "Path to Kubernetes config file")
func getKubeConfig() string {
if *kubeConfig != "" {
return *kubeConfig
}
return filepath.Join(os.Getenv("HOME"), ".kube", "config")
}
func getKubernetesClient() (*rest.Config, *kubernetes.Clientset) {
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", getKubeConfig())
if err != nil {
panic(err.Error())
}
// create the clientset
clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
return config, clientSet
}
func createNamespaceForTest() string {
_, clientset := getKubernetesClient()
ns := &v1.Namespace{
ObjectMeta: meta_v1.ObjectMeta{
GenerateName: "bow-e2e-test-",
},
}
cns, err := clientset.CoreV1().Namespaces().Create(ns)
if err != nil {
panic(err)
}
log.Infof("test namespace '%s' created", cns.Name)
return cns.Name
}
func deleteTestNamespace(namespace string) error {
defer log.Infof("test namespace '%s' deleted", namespace)
_, clientset := getKubernetesClient()
deleteOptions := meta_v1.DeleteOptions{}
return clientset.CoreV1().Namespaces().Delete(namespace, &deleteOptions)
}
type bowCmd struct {
cmd *exec.Cmd
env []string
}
func (kc *bowCmd) Start(ctx context.Context) error {
log.Info("bow started")
cmd := "bow"
args := []string{"--no-incluster", "--kubeconfig", getKubeConfig()}
c := exec.CommandContext(ctx, cmd, args...)
c.Env = []string{
"DEBUG=true",
}
c.Env = append(c.Env, kc.env...)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
kc.cmd = c
err := c.Run()
if err != nil {
if strings.Contains(err.Error(), "killed") {
return nil
}
}
return err
}
func (kc *bowCmd) Stop() error {
defer log.Info("bow stopped")
return kc.cmd.Process.Kill()
}
func waitFor(ctx context.Context, kcs *kubernetes.Clientset, namespace, name string, desired string) error {
last := ""
for {
select {
case <-ctx.Done():
return fmt.Errorf("expected '%s', got: '%s'", desired, last)
default:
updated, err := kcs.AppsV1().Deployments(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
time.Sleep(500 * time.Millisecond)
continue
}
if updated.Spec.Template.Spec.Containers[0].Image != desired {
time.Sleep(500 * time.Millisecond)
last = updated.Spec.Template.Spec.Containers[0].Image
continue
}
return nil
}
}
}