forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespaces.go
191 lines (177 loc) · 5.37 KB
/
namespaces.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
183
184
185
186
187
188
189
190
191
package kube
import (
"fmt"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/client/clientset/versioned"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
func EnsureEnvironmentNamespaceSetup(kubeClient kubernetes.Interface, jxClient versioned.Interface, env *v1.Environment, ns string) error {
// lets create the namespace if we are on the same cluster
spec := &env.Spec
if spec.Cluster == "" && spec.Namespace != "" {
labels := map[string]string{
LabelTeam: ns,
LabelEnvironment: env.Name,
}
annotations := map[string]string{}
err := EnsureNamespaceCreated(kubeClient, spec.Namespace, labels, annotations)
if err != nil {
return err
}
}
// lets annotate the team namespace as being the developer environment
labels := map[string]string{
LabelTeam: ns,
LabelEnvironment: LabelValueDevEnvironment,
}
annotations := map[string]string{}
// lets check that the current namespace is marked as the dev environment
err := EnsureNamespaceCreated(kubeClient, ns, labels, annotations)
if err != nil {
return err
}
_, err = EnsureDevEnvironmentSetup(jxClient, ns)
return err
}
// EnsureDevEnvironmentSetup ensures that the Environment is created in the given namespace
func EnsureDevEnvironmentSetup(jxClient versioned.Interface, ns string) (*v1.Environment, error) {
// lets ensure there is a dev Environment setup so that we can easily switch between all the environments
env, err := jxClient.JenkinsV1().Environments(ns).Get(LabelValueDevEnvironment, metav1.GetOptions{})
if err != nil {
// lets create a dev environment
env = &v1.Environment{
ObjectMeta: metav1.ObjectMeta{
Name: LabelValueDevEnvironment,
},
Spec: v1.EnvironmentSpec{
Namespace: ns,
Label: "Development",
PromotionStrategy: v1.PromotionStrategyTypeNever,
Kind: v1.EnvironmentKindTypeDevelopment,
TeamSettings: v1.TeamSettings{
UseGitOPs: true,
AskOnCreate: false,
QuickstartLocations: DefaultQuickstartLocations,
},
},
}
_, err = jxClient.JenkinsV1().Environments(ns).Create(env)
if err != nil {
return nil, err
}
}
return env, nil
}
// EnsureEditEnvironmentSetup ensures that the Environment is created in the given namespace
func EnsureEditEnvironmentSetup(kubeClient kubernetes.Interface, jxClient versioned.Interface, ns string, username string) (*v1.Environment, error) {
// lets ensure there is a dev Environment setup so that we can easily switch between all the environments
envList, err := jxClient.JenkinsV1().Environments(ns).List(metav1.ListOptions{})
if err != nil {
return nil, err
}
if envList != nil {
for _, e := range envList.Items {
if e.Spec.Kind == v1.EnvironmentKindTypeEdit && e.Spec.PreviewGitSpec.User.Username == username {
return &e, nil
}
}
}
editNS := ToValidName(ns + "-edit-" + username)
labels := map[string]string{
LabelTeam: ns,
LabelEnvironment: username,
LabelKind: ValueKindEditNamespace,
LabelUsername: username,
}
annotations := map[string]string{}
err = EnsureNamespaceCreated(kubeClient, editNS, labels, annotations)
if err != nil {
return nil, err
}
cm, err := kubeClient.CoreV1().ConfigMaps(ns).Get(ConfigMapExposecontroller, metav1.GetOptions{})
if err != nil {
return nil, err
}
oldCm, err := kubeClient.CoreV1().ConfigMaps(editNS).Get(ConfigMapExposecontroller, metav1.GetOptions{})
if oldCm == nil || err != nil {
cm.Namespace = editNS
cm.ResourceVersion = ""
_, err := kubeClient.CoreV1().ConfigMaps(editNS).Create(cm)
if err != nil {
return nil, err
}
}
env := &v1.Environment{
ObjectMeta: metav1.ObjectMeta{
Name: username,
},
Spec: v1.EnvironmentSpec{
Namespace: editNS,
Label: username,
PromotionStrategy: v1.PromotionStrategyTypeNever,
Kind: v1.EnvironmentKindTypeEdit,
PreviewGitSpec: v1.PreviewGitSpec{
User: v1.UserSpec{
Username: username,
},
},
Order: 1,
},
}
_, err = jxClient.JenkinsV1().Environments(ns).Create(env)
if err != nil {
return nil, err
}
return env, nil
}
// Ensure that the namespace exists for the given name
func EnsureNamespaceCreated(kubeClient kubernetes.Interface, name string, labels map[string]string, annotations map[string]string) error {
n, err := kubeClient.CoreV1().Namespaces().Get(name, metav1.GetOptions{})
if err == nil {
// lets check if we have the labels setup
if n.Annotations == nil {
n.Annotations = map[string]string{}
}
if n.Labels == nil {
n.Labels = map[string]string{}
}
changed := false
if labels != nil {
for k, v := range labels {
if n.Labels[k] != v {
n.Labels[k] = v
changed = true
}
}
}
if annotations != nil {
for k, v := range annotations {
if n.Annotations[k] != v {
n.Annotations[k] = v
changed = true
}
}
}
if changed {
_, err = kubeClient.CoreV1().Namespaces().Update(n)
if err != nil {
return fmt.Errorf("Failed to label Namespace %s %s", name, err)
}
}
return nil
}
namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: labels,
Annotations: annotations,
},
}
_, err = kubeClient.CoreV1().Namespaces().Create(namespace)
if err != nil {
return fmt.Errorf("Failed to create Namespace %s %s", name, err)
}
return err
}