This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
/
deploy.go
115 lines (97 loc) · 4.15 KB
/
deploy.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
package codewind
import (
"deploy-pfe/pkg/che"
"deploy-pfe/pkg/constants"
log "github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
// DeployCodewind takes in a `codewind` object and deploys Codewind and the performance dashboard into the specified namespace
func DeployCodewind(clientset *kubernetes.Clientset, codewind Codewind, namespace string) error {
// See if a PVC for the PFE workspace already exists, if not, create one
_, err := clientset.CoreV1().PersistentVolumeClaims(namespace).Get(codewind.PVCName, metav1.GetOptions{})
if err != nil {
// Create a PVC for PFE
// Determine if we're running on OpenShift on IKS (and thus need to use the ibm-file-bronze storage class)
storageClass := ""
sc, err := clientset.StorageV1().StorageClasses().Get(constants.ROKSStorageClass, metav1.GetOptions{})
if err == nil && sc != nil {
storageClass = sc.Name
log.Infof("Setting storage class to %s\n", storageClass)
}
// Get the name and uid for the Che workspace volume
chePvc := che.GetWorkspacePVC(clientset, namespace, codewind.WorkspaceID)
pvc := generatePVC(codewind, constants.PFEVolumeSize, storageClass, chePvc.GetObjectMeta().GetName(), chePvc.GetObjectMeta().GetUID())
_, err = clientset.CoreV1().PersistentVolumeClaims(namespace).Create(&pvc)
if err != nil {
log.Errorf("Unable to create Persistent Volume Claim for PFE: %v\n", err)
return err
}
}
// Deploy Codewind PFE
service := createPFEService(codewind)
deploy := createPFEDeploy(codewind)
log.Infoln("Deploying Codewind...")
_, err = clientset.CoreV1().Services(namespace).Create(&service)
if err != nil {
log.Errorf("Unable to create Codewind service: %v\n", err)
return err
}
_, err = clientset.AppsV1().Deployments(namespace).Create(&deploy)
if err != nil {
log.Errorf("Unable to create Codewind deployment: %v\n", err)
return err
}
// Deploy the Performance dashboard
performanceService := createPerformanceService(codewind)
performanceDeploy := createPerformanceDeploy(codewind)
log.Infoln("Deploying Codewind Performance Dashboard...")
_, err = clientset.CoreV1().Services(namespace).Create(&performanceService)
if err != nil {
log.Errorf("Error: Unable to create Codewind Performance service: %v\n", err)
return err
}
_, err = clientset.AppsV1().Deployments(namespace).Create(&performanceDeploy)
if err != nil {
log.Errorf("Error: Unable to create Codewind Performance deployment: %v\n", err)
return err
}
return nil
}
// createPFEDeploy creates a Kubernetes deploy for Codewind, marking the Che workspace as its owner
func createPFEDeploy(codewind Codewind) appsv1.Deployment {
labels := map[string]string{
"app": "codewind-pfe",
"codewindWorkspace": codewind.WorkspaceID,
}
volumes, volumeMounts := setPFEVolumes(codewind)
envVars := setPFEEnvVars(codewind)
return generateDeployment(codewind, constants.PFEPrefix, codewind.PFEImage, constants.PFEContainerPort, volumes, volumeMounts, envVars, labels)
}
// createPFEService creates a Kubernetes service for Codewind, exposing port 9191
func createPFEService(codewind Codewind) corev1.Service {
labels := map[string]string{
"app": "codewind-pfe",
"codewindWorkspace": codewind.WorkspaceID,
}
return generateService(codewind, constants.PFEPrefix, constants.PFEContainerPort, labels)
}
func createPerformanceDeploy(codewind Codewind) appsv1.Deployment {
labels := map[string]string{
"app": constants.PerformancePrefix,
"codewindWorkspace": codewind.WorkspaceID,
}
volumes := []corev1.Volume{}
volumeMounts := []corev1.VolumeMount{}
envVars := setPerformanceEnvVars(codewind)
return generateDeployment(codewind, constants.PerformancePrefix, codewind.PerformanceImage, constants.PerformanceContainerPort, volumes, volumeMounts, envVars, labels)
}
func createPerformanceService(codewind Codewind) corev1.Service {
labels := map[string]string{
"app": constants.PerformancePrefix,
"codewindWorkspace": codewind.WorkspaceID,
}
return generateService(codewind, constants.PerformancePrefix, constants.PerformanceContainerPort, labels)
}