/
namespace.go
122 lines (106 loc) · 3.5 KB
/
namespace.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
package deployment_service_checker
import (
"context"
"time"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
func createDeploymentNamespace(ctx context.Context, client *kubernetes.Clientset) error {
// check namespace, delete it if exist
_, err := client.CoreV1().Namespaces().Get(ctx, cfg.CheckNamespace, metav1.GetOptions{})
if err != nil {
if k8sErrors.IsNotFound(err) {
logrus.Infof("namespace [%s] not found, create it", cfg.CheckNamespace)
} else {
logrus.Errorf("get namespace failed, namespace: %s, error: %v", cfg.CheckNamespace, err)
return err
}
} else {
err := deleteNamespace(ctx, client)
if err != nil {
log.Errorf("delete previous namespaces failed, namespace: %s, error: %v", cfg.CheckNamespace, err)
return err
}
}
// create namespace
err = createNamespace(ctx, client)
if err != nil {
log.Errorf("create namespace failed, namespace: %s, error: %v", cfg.CheckNamespace, err)
return err
}
log.Infof("create namespace successfully, namespace: %s", cfg.CheckNamespace)
return nil
}
func createNamespace(ctx context.Context, client *kubernetes.Clientset) error {
// create namespace
namespace := corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: cfg.CheckNamespace,
},
}
_, err := client.CoreV1().Namespaces().Create(ctx, &namespace, metav1.CreateOptions{})
if err != nil {
logrus.Errorf("create namespace failed, namespace: %s, error: %v", cfg.CheckNamespace, err)
return err
}
for {
logrus.Infoln("Watching for namespace ready.")
// Watch that it is up.
watch, err := client.CoreV1().Namespaces().Watch(ctx, metav1.ListOptions{
Watch: true,
FieldSelector: "metadata.name=" + cfg.CheckNamespace,
})
if err != nil {
return err
}
// If the watch is nil, skip to the next loop and make a new watch object.
if watch == nil {
time.Sleep(3 * time.Second)
continue
}
for event := range watch.ResultChan() { // Watch for deployment events.
n, ok := event.Object.(*corev1.Namespace)
if !ok {
logrus.Infoln("Got a watch event for a non-namespace object -- ignoring.")
continue
}
logrus.Debugln("Received an event watching for namespace changes:", n.Name, "got event", event.Type)
// Look at the status conditions for the deployment object;
// we want it to be reporting Available = True.
if namespaceAvailable(n) {
return nil
}
}
// Stop the watch on each loop because we will create a new one.
watch.Stop()
}
}
func deleteNamespace(ctx context.Context, client *kubernetes.Clientset) error {
period := int64(0)
_, err := client.CoreV1().Namespaces().Get(ctx, cfg.CheckNamespace, metav1.GetOptions{})
if err != nil && k8sErrors.IsNotFound(err) {
logrus.Infof("namespace deleted")
return nil
}
err = client.CoreV1().Namespaces().Delete(ctx, cfg.CheckNamespace, metav1.DeleteOptions{GracePeriodSeconds: &period})
if err != nil {
logrus.Errorf("delete namespace failed, namespace: %s, error: %v", cfg.CheckNamespace, err)
return err
}
for {
logrus.Infoln("Watching for namespace deleted.")
time.Sleep(3 * time.Second)
_, err := client.CoreV1().Namespaces().Get(ctx, cfg.CheckNamespace, metav1.GetOptions{})
if err != nil && k8sErrors.IsNotFound(err) {
logrus.Infof("namespace deleted")
return nil
}
}
}
func namespaceAvailable(n *corev1.Namespace) bool {
return n.Status.Phase == corev1.NamespaceActive
}