-
Notifications
You must be signed in to change notification settings - Fork 31
/
namespaces.go
236 lines (220 loc) · 7.22 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package jxenv
import (
"fmt"
v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx-api/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx-helpers/pkg/kube"
"github.com/jenkins-x/jx-helpers/pkg/kube/naming"
"github.com/jenkins-x/jx-logging/pkg/log"
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{
kube.LabelTeam: ns,
kube.LabelEnvironment: env.Name,
}
annotations := map[string]string{}
err := EnsureNamespaceCreated(kubeClient, spec.Namespace, labels, annotations)
if err != nil {
return err
}
}
err := EnsureDevNamespaceCreatedWithoutEnvironment(kubeClient, ns)
if err != nil {
return err
}
_, err = EnsureDevEnvironmentSetup(jxClient, ns)
return err
}
// EnsureDevNamespaceCreatedWithoutEnvironment ensures that there is a development namespace created
func EnsureDevNamespaceCreatedWithoutEnvironment(kubeClient kubernetes.Interface, ns string) error {
// lets annotate the team namespace as being the developer environment
labels := map[string]string{
kube.LabelTeam: ns,
kube.LabelEnvironment: kube.LabelValueDevEnvironment,
}
annotations := map[string]string{}
// lets check that the current namespace is marked as the dev environment
err := EnsureNamespaceCreated(kubeClient, ns, labels, annotations)
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(kube.LabelValueDevEnvironment, metav1.GetOptions{})
if err != nil {
// lets create a dev environment
env = CreateDefaultDevEnvironment(ns)
env, err = jxClient.JenkinsV1().Environments(ns).Create(env)
if err != nil {
return nil, err
}
}
return env, nil
}
// CreateDefaultDevEnvironment creates a default development environment
func CreateDefaultDevEnvironment(ns string) *v1.Environment {
return &v1.Environment{
ObjectMeta: metav1.ObjectMeta{
Name: kube.LabelValueDevEnvironment,
Labels: map[string]string{kube.LabelTeam: ns, kube.LabelEnvironment: kube.LabelValueDevEnvironment},
},
Spec: v1.EnvironmentSpec{
Namespace: ns,
Label: "Development",
PromotionStrategy: v1.PromotionStrategyTypeNever,
Kind: v1.EnvironmentKindTypeDevelopment,
TeamSettings: v1.TeamSettings{
UseGitOps: true,
AskOnCreate: false,
// TODO
//QuickstartLocations: kube.DefaultQuickstartLocations,
PromotionEngine: v1.PromotionEngineJenkins,
AppsRepository: kube.DefaultChartMuseumURL,
},
},
}
}
// GetEnrichedDevEnvironment lazily creates the dev namespace if it does not already exist and
// auto-detects the webhook engine if its not specified
func GetEnrichedDevEnvironment(kubeClient kubernetes.Interface, jxClient versioned.Interface, ns string) (*v1.Environment, error) {
env, err := EnsureDevEnvironmentSetup(jxClient, ns)
if err != nil {
return env, err
}
if env.Spec.WebHookEngine == v1.WebHookEngineNone {
env.Spec.WebHookEngine = v1.WebHookEngineProw
}
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 := naming.ToValidName(ns + "-edit-" + username)
labels := map[string]string{
kube.LabelTeam: ns,
kube.LabelEnvironment: username,
kube.LabelKind: kube.ValueKindEditNamespace,
kube.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(kube.ConfigMapIngressConfig, metav1.GetOptions{})
if err != nil {
cm, err := kubeClient.CoreV1().ConfigMaps(ns).Get(kube.ConfigMapExposecontroller, metav1.GetOptions{})
if err != nil {
return nil, err
}
oldCm, err := kubeClient.CoreV1().ConfigMaps(editNS).Get(kube.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
}
}
} else {
oldCm, err := kubeClient.CoreV1().ConfigMaps(editNS).Get(kube.ConfigMapIngressConfig, 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)
} else {
log.Logger().Infof("Namespace %s created ", name)
}
return err
}