-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud-main.go
383 lines (361 loc) · 15.5 KB
/
cloud-main.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package cmd
import (
"context"
"fmt"
"reflect"
"strconv"
"time"
kubeflowv1 "github.com/StatCan/profiles-controller/pkg/apis/kubeflow/v1"
"github.com/StatCan/profiles-controller/pkg/controllers/profiles"
kubeflowclientset "github.com/StatCan/profiles-controller/pkg/generated/clientset/versioned"
kubeflowinformers "github.com/StatCan/profiles-controller/pkg/generated/informers/externalversions"
"github.com/StatCan/profiles-controller/pkg/signals"
istionetworkingv1beta1 "istio.io/api/networking/v1beta1"
istionetworkingclient "istio.io/client-go/pkg/apis/networking/v1beta1"
istioclientset "istio.io/client-go/pkg/clientset/versioned"
istioinformers "istio.io/client-go/pkg/informers/externalversions"
_ "k8s.io/client-go/plugin/pkg/client/auth/azure"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
// project packages
)
const CLOUD_MAIN_GITLAB_HOST = "gitlab.k8s.cloud.statcan.ca"
const ISTIO_EGRESS_GATEWAY_SVC = "cloud-main-egress-gateway"
const ISTIO_SERVICE_ENTRY_NAME = "cloud-main-hosts"
const CLOUD_MAIN_SYSTEM_NAMESPACE = "cloud-main-system"
const HTTPS_PORT = 443
const SSH_PORT = 22
var cloudMainCmd = &cobra.Command{
Use: "cloud-main",
Short: "Configure cloud-main resources",
Long: `Configure resources that enable connectivity to certain cloud-main services for StatCan employees.`,
Run: func(cmd *cobra.Command, args []string) {
// Setup signals so we can shutdown cleanly
stopCh := signals.SetupSignalHandler()
// Create Kubernetes configuration
cfg, err := clientcmd.BuildConfigFromFlags(apiserver, kubeconfig)
if err != nil {
klog.Fatalf("Error building kubeconfig: %v", err)
}
// Get clients
kubeflowClient, err := kubeflowclientset.NewForConfig(cfg)
if err != nil {
klog.Fatalf("Error building Kubeflow client: %v", err)
}
istioClient, err := istioclientset.NewForConfig(cfg)
if err != nil {
klog.Fatalf("error building Istio client: %v", err)
}
// Setup Informer Factories
kubeflowInformerFactory := kubeflowinformers.NewSharedInformerFactory(kubeflowClient, time.Minute*(time.Duration(requeue_time)))
istioInformerFactory := istioinformers.NewSharedInformerFactory(istioClient, time.Minute*(time.Duration(requeue_time)))
// Setup informers
virtualServiceInformer := istioInformerFactory.Networking().V1beta1().VirtualServices()
virtualServiceLister := virtualServiceInformer.Lister()
serviceEntryInformer := istioInformerFactory.Networking().V1beta1().ServiceEntries()
serviceEntryLister := serviceEntryInformer.Lister()
destinationRuleInformer := istioInformerFactory.Networking().V1beta1().DestinationRules()
destinationRuleLister := destinationRuleInformer.Lister()
// Initialize cloud-main controller
controller := profiles.NewController(
kubeflowInformerFactory.Kubeflow().V1().Profiles(),
func(profile *kubeflowv1.Profile) error {
// The virtual service should only be created for namespaces consisting only of
// StatCan employees
val, labelExists := profile.ObjectMeta.Labels["state.aaw.statcan.gc.ca/exists-non-cloud-main-user"]
existsNonEmployeeUser, _ := strconv.ParseBool(val)
if labelExists && !existsNonEmployeeUser {
// _ _ _ _
// __ _(_)_ __| |_ _ _ __ _| | ___ ___ _ ____ _(_) ___ ___
// \ \ / / | '__| __| | | |/ _` | | / __|/ _ \ '__\ \ / / |/ __/ _ \
// \ V /| | | | |_| |_| | (_| | | \__ \ __/ | \ V /| | (_| __/
// \_/ |_|_| \__|\__,_|\__,_|_| |___/\___|_| \_/ |_|\___\___|
virtualService, err := generateCloudMainVirtualService(profile)
if err != nil {
return err
}
currentVirtualService, err := virtualServiceLister.VirtualServices(profile.Name).Get(virtualService.Name)
// If the virtual service is not found, create the virtual service.
if errors.IsNotFound(err) {
// Always create the virtual service
klog.Infof("Creating Istio virtualservice %s/%s", virtualService.Namespace, virtualService.Name)
currentVirtualService, err = istioClient.NetworkingV1beta1().VirtualServices(virtualService.Namespace).Create(
context.Background(), virtualService, metav1.CreateOptions{},
)
} else if !reflect.DeepEqual(virtualService.Spec, currentVirtualService.Spec) {
klog.Infof("Updating Istio virtualservice %s/%s", virtualService.Namespace, virtualService.Name)
currentVirtualService = virtualService
_, err = istioClient.NetworkingV1beta1().VirtualServices(virtualService.Namespace).Update(
context.Background(), currentVirtualService, metav1.UpdateOptions{},
)
}
// _ _
// ___ ___ _ ____ _(_) ___ ___ ___ _ __ | |_ _ __ _ _
// / __|/ _ \ '__\ \ / / |/ __/ _ \ / _ \ '_ \| __| '__| | | |
// \__ \ __/ | \ V /| | (_| __/ | __/ | | | |_| | | |_| |
// |___/\___|_| \_/ |_|\___\___| \___|_| |_|\__|_| \__, |
// |___/
serviceEntry, err := generateCloudMainServiceEntry(profile)
if err != nil {
return err
}
currentServiceEntry, err := serviceEntryLister.ServiceEntries(profile.Name).Get(serviceEntry.Name)
// If service entry not found, create it
if errors.IsNotFound(err) {
klog.Infof("Creating Istio serviceentry %s/%s", serviceEntry.Namespace, serviceEntry.Name)
currentServiceEntry, err = istioClient.NetworkingV1beta1().ServiceEntries(serviceEntry.Namespace).Create(
context.Background(), serviceEntry, metav1.CreateOptions{},
)
} else if !reflect.DeepEqual(serviceEntry.Spec, currentServiceEntry.Spec) {
currentServiceEntry = serviceEntry
_, err = istioClient.NetworkingV1beta1().ServiceEntries(serviceEntry.Namespace).Update(
context.Background(), currentServiceEntry, metav1.UpdateOptions{},
)
}
// _ _ _ _ _ _
// __| | ___ ___| |_(_)_ __ __ _| |_(_) ___ _ __ _ __ _ _| | ___
// / _` |/ _ \/ __| __| | '_ \ / _` | __| |/ _ \| '_ \ | '__| | | | |/ _ \
// | (_| | __/\__ \ |_| | | | | (_| | |_| | (_) | | | | | | | |_| | | __/
// \__,_|\___||___/\__|_|_| |_|\__,_|\__|_|\___/|_| |_| |_| \__,_|_|\___|
destinationRule, err := generateCloudMainDestinationRule(profile)
if err != nil {
return err
}
currentDestinationRule, err := destinationRuleLister.DestinationRules(profile.Name).Get(destinationRule.Name)
// If destination rule not found, create it
if errors.IsNotFound(err) {
klog.Infof("Creating Istio destinationrule %s/%s", destinationRule.Namespace, destinationRule.Name)
currentDestinationRule, err = istioClient.NetworkingV1beta1().DestinationRules(destinationRule.Namespace).Create(
context.Background(), destinationRule, metav1.CreateOptions{},
)
} else if !reflect.DeepEqual(destinationRule.Spec, currentDestinationRule.Spec) {
currentDestinationRule = destinationRule
_, err = istioClient.NetworkingV1beta1().DestinationRules(destinationRule.Namespace).Update(
context.Background(), currentDestinationRule, metav1.UpdateOptions{},
)
}
} else {
// If the profile is missing the exists-non-cloud-main-user label or it is set to
// 'true', then delete any of the per-namespace cloud main system istio components
// that might be deployed.
// _ _ _ _
// __ _(_)_ __| |_ _ _ __ _| | ___ ___ _ ____ _(_) ___ ___
// \ \ / / | '__| __| | | |/ _` | | / __|/ _ \ '__\ \ / / |/ __/ _ \
// \ V /| | | | |_| |_| | (_| | | \__ \ __/ | \ V /| | (_| __/
// \_/ |_|_| \__|\__,_|\__,_|_| |___/\___|_| \_/ |_|\___\___|
virtualService, err := generateCloudMainVirtualService(profile)
currentVirtualService, err := virtualServiceLister.VirtualServices(profile.Name).Get(virtualService.Name)
if errors.IsNotFound(err) {
klog.Infof("virtualservice %s/%s doesn't exist.", virtualService.Namespace, virtualService.Name)
} else {
klog.Infof("removing virtualservice %s/%s because %s contains a non-employee user.", currentVirtualService.Namespace, currentVirtualService.Name, profile.Name)
err = istioClient.NetworkingV1beta1().VirtualServices(virtualService.Namespace).Delete(
context.Background(), currentVirtualService.Name, metav1.DeleteOptions{},
)
}
// _ _
// ___ ___ _ ____ _(_) ___ ___ ___ _ __ | |_ _ __ _ _
// / __|/ _ \ '__\ \ / / |/ __/ _ \ / _ \ '_ \| __| '__| | | |
// \__ \ __/ | \ V /| | (_| __/ | __/ | | | |_| | | |_| |
// |___/\___|_| \_/ |_|\___\___| \___|_| |_|\__|_| \__, |
// |___/
serviceEntry, err := generateCloudMainServiceEntry(profile)
currentServiceEntry, err := serviceEntryLister.ServiceEntries(profile.Name).Get(serviceEntry.Name)
if errors.IsNotFound(err) {
klog.Infof("serviceentry %s/%s doesn't exist.", serviceEntry.Namespace, serviceEntry.Name)
} else {
klog.Infof("removing serviceentry %s/%s because %s contains a non-employee user.", currentServiceEntry.Namespace, currentServiceEntry.Name, profile.Name)
err = istioClient.NetworkingV1beta1().ServiceEntries(serviceEntry.Namespace).Delete(
context.Background(), currentServiceEntry.Name, metav1.DeleteOptions{},
)
}
// _ _ _ _ _ _
// __| | ___ ___| |_(_)_ __ __ _| |_(_) ___ _ __ _ __ _ _| | ___
// / _` |/ _ \/ __| __| | '_ \ / _` | __| |/ _ \| '_ \ | '__| | | | |/ _ \
// | (_| | __/\__ \ |_| | | | | (_| | |_| | (_) | | | | | | | |_| | | __/
// \__,_|\___||___/\__|_|_| |_|\__,_|\__|_|\___/|_| |_| |_| \__,_|_|\___|
destinationRule, err := generateCloudMainDestinationRule(profile)
currentDestinationRule, err := destinationRuleLister.DestinationRules(profile.Name).Get(destinationRule.Name)
if errors.IsNotFound(err) {
klog.Infof("destinationrule %s/%s doesn't exist.", destinationRule.Namespace, destinationRule.Name)
} else {
klog.Infof("removing destinationrule %s/%s because %s contains a non-employee user.", currentDestinationRule.Namespace, currentDestinationRule.Name, profile.Name)
err = istioClient.NetworkingV1beta1().DestinationRules(destinationRule.Namespace).Delete(
context.Background(), currentDestinationRule.Name, metav1.DeleteOptions{},
)
}
}
return nil
},
)
// Declare Event Handlers for Informers
virtualServiceInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: func(old, new interface{}) {
newDep := new.(*istionetworkingclient.VirtualService)
oldDep := old.(*istionetworkingclient.VirtualService)
if newDep.ResourceVersion == oldDep.ResourceVersion {
return
}
controller.HandleObject(new)
},
DeleteFunc: controller.HandleObject,
})
// Start informers
kubeflowInformerFactory.Start(stopCh)
istioInformerFactory.Start(stopCh)
// Wait for caches to sync
klog.Info("Waiting for istio VirtualService informer caches to sync")
if ok := cache.WaitForCacheSync(stopCh, virtualServiceInformer.Informer().HasSynced); !ok {
klog.Fatalf("failed to wait for caches to sync")
}
// Run the controller
if err = controller.Run(2, stopCh); err != nil {
klog.Fatalf("error running controller: %v", err)
}
},
}
func generateCloudMainDestinationRule(profile *kubeflowv1.Profile) (*istionetworkingclient.DestinationRule, error) {
// Get namespace from profile
namespace := profile.Name
// Create destination rule
destinationRule := istionetworkingclient.DestinationRule{
ObjectMeta: metav1.ObjectMeta{
Name: "direct-through-cloud-main-egress-gateway",
Namespace: namespace,
// Indicate that the profile owns the ServiceEntry
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(profile, kubeflowv1.SchemeGroupVersion.WithKind("Profile")),
},
},
Spec: istionetworkingv1beta1.DestinationRule{
Host: fmt.Sprintf("%s.%s.svc.cluster.local", ISTIO_EGRESS_GATEWAY_SVC, CLOUD_MAIN_SYSTEM_NAMESPACE),
Subsets: []*istionetworkingv1beta1.Subset{
{
Name: ISTIO_SERVICE_ENTRY_NAME,
},
},
ExportTo: []string{
namespace,
CLOUD_MAIN_SYSTEM_NAMESPACE,
},
},
}
return &destinationRule, nil
}
func generateCloudMainServiceEntry(profile *kubeflowv1.Profile) (*istionetworkingclient.ServiceEntry, error) {
// Get namespace from profile
namespace := profile.Name
// Create service entry
serviceEntry := istionetworkingclient.ServiceEntry{
ObjectMeta: metav1.ObjectMeta{
Name: ISTIO_SERVICE_ENTRY_NAME,
Namespace: namespace,
// Indicate that the profile owns the ServiceEntry
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(profile, kubeflowv1.SchemeGroupVersion.WithKind("Profile")),
},
},
Spec: istionetworkingv1beta1.ServiceEntry{
Hosts: []string{
CLOUD_MAIN_GITLAB_HOST,
},
Ports: []*istionetworkingv1beta1.Port{
{
Number: HTTPS_PORT,
Name: "tls",
Protocol: "TLS",
},
},
Resolution: istionetworkingv1beta1.ServiceEntry_DNS,
ExportTo: []string{
namespace,
CLOUD_MAIN_SYSTEM_NAMESPACE,
},
},
}
return &serviceEntry, nil
}
func generateCloudMainVirtualService(profile *kubeflowv1.Profile) (*istionetworkingclient.VirtualService, error) {
// Get namespace from profile
namespace := profile.Name
// Create virtual service
virtualService := istionetworkingclient.VirtualService{
ObjectMeta: metav1.ObjectMeta{
Name: "direct-through-cloud-main-egress-gateway",
Namespace: namespace,
// Indicate that the profile owns the virtualservice resource
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(profile, kubeflowv1.SchemeGroupVersion.WithKind("Profile")),
},
},
Spec: istionetworkingv1beta1.VirtualService{
Hosts: []string{
CLOUD_MAIN_GITLAB_HOST,
},
Gateways: []string{
"mesh",
fmt.Sprintf("%s/cloud-main-egress-gateway", CLOUD_MAIN_SYSTEM_NAMESPACE),
},
Tls: []*istionetworkingv1beta1.TLSRoute{
{
Match: []*istionetworkingv1beta1.TLSMatchAttributes{
{
Gateways: []string{
"mesh",
},
Port: HTTPS_PORT,
SniHosts: []string{
CLOUD_MAIN_GITLAB_HOST,
},
},
},
Route: []*istionetworkingv1beta1.RouteDestination{
{
Destination: &istionetworkingv1beta1.Destination{
Host: fmt.Sprintf("%s.%s.svc.cluster.local", ISTIO_EGRESS_GATEWAY_SVC, CLOUD_MAIN_SYSTEM_NAMESPACE),
Subset: ISTIO_SERVICE_ENTRY_NAME,
Port: &istionetworkingv1beta1.PortSelector{
Number: HTTPS_PORT,
},
},
},
},
},
{
Match: []*istionetworkingv1beta1.TLSMatchAttributes{
{
Gateways: []string{
fmt.Sprintf("%s/cloud-main-egress-gateway", CLOUD_MAIN_SYSTEM_NAMESPACE),
},
Port: HTTPS_PORT,
SniHosts: []string{
CLOUD_MAIN_GITLAB_HOST,
},
},
},
Route: []*istionetworkingv1beta1.RouteDestination{{
Destination: &istionetworkingv1beta1.Destination{
Host: CLOUD_MAIN_GITLAB_HOST,
Port: &istionetworkingv1beta1.PortSelector{
Number: HTTPS_PORT,
},
},
Weight: 100,
}},
},
},
ExportTo: []string{
namespace,
CLOUD_MAIN_SYSTEM_NAMESPACE,
},
},
}
return &virtualService, nil
}
func init() {
rootCmd.AddCommand(cloudMainCmd)
}