-
Notifications
You must be signed in to change notification settings - Fork 244
/
deploymentconfigs.go
217 lines (197 loc) · 6.72 KB
/
deploymentconfigs.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
package testingutil
import (
"fmt"
"github.com/openshift/odo/pkg/util"
v1 "github.com/openshift/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func getContainer(componentName string, applicationName string, ports []corev1.ContainerPort,
envFromSources []corev1.EnvFromSource) corev1.Container {
return corev1.Container{
Name: fmt.Sprintf("%v-%v", componentName, applicationName),
Image: fmt.Sprintf("%v-%v", componentName, applicationName),
Ports: ports,
EnvFrom: envFromSources,
}
}
func getDeploymentConfig(namespace string, componentName string, componentType string, applicationName string, containers []corev1.Container) v1.DeploymentConfig {
return v1.DeploymentConfig{
TypeMeta: metav1.TypeMeta{
Kind: "DeploymentConfig",
APIVersion: "apps.openshift.io/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%v-%v", componentName, applicationName),
Namespace: namespace,
Labels: map[string]string{
"app": "app",
"app.kubernetes.io/instance": componentName,
"app.kubernetes.io/name": componentType,
"app.kubernetes.io/part-of": applicationName,
},
Annotations: map[string]string{ // Convert into separate function when other source types required in tests
"app.kubernetes.io/component-source-type": "git",
"app.openshift.io/vcs-uri": fmt.Sprintf("https://github.com/%s/%s.git", componentName, applicationName),
},
},
Spec: v1.DeploymentConfigSpec{
Replicas: 1,
Selector: map[string]string{
"deploymentconfig": fmt.Sprintf("%v-%v", componentName, applicationName),
},
Template: &corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"deploymentconfig": fmt.Sprintf("%v-%v", componentName, applicationName),
},
},
Spec: corev1.PodSpec{
Containers: containers,
},
},
},
}
}
func FakeDeploymentConfigs() *v1.DeploymentConfigList {
var componentName string
var applicationName string
var componentType string
// DC1 with multiple containers each with multiple ports
componentType = "python"
componentName = "python"
applicationName = "app"
c1 := getContainer(componentName, applicationName, []corev1.ContainerPort{
{
Name: fmt.Sprintf("%v-%v-p1", componentName, applicationName),
ContainerPort: 8080,
Protocol: corev1.ProtocolTCP,
},
{
Name: fmt.Sprintf("%v-%v-p2", componentName, applicationName),
ContainerPort: 9090,
Protocol: corev1.ProtocolUDP,
},
}, nil)
c2 := getContainer(componentName, applicationName, []corev1.ContainerPort{
{
Name: fmt.Sprintf("%v-%v-p1", componentName, applicationName),
ContainerPort: 10080,
Protocol: corev1.ProtocolTCP,
},
{
Name: fmt.Sprintf("%v-%v-p2", componentName, applicationName),
ContainerPort: 10090,
Protocol: corev1.ProtocolUDP,
},
}, nil)
dc1 := getDeploymentConfig("myproject", componentName, componentType, applicationName, []corev1.Container{c1, c2})
// DC2 with single container and single port
componentType = "nodejs"
componentName = "nodejs"
applicationName = "app"
c3 := getContainer(componentName, applicationName, []corev1.ContainerPort{
{
Name: fmt.Sprintf("%v-%v-p1", componentName, applicationName),
ContainerPort: 8080,
Protocol: corev1.ProtocolTCP,
},
}, []corev1.EnvFromSource{
{
SecretRef: &corev1.SecretEnvSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "s1",
},
},
},
{
SecretRef: &corev1.SecretEnvSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "s2",
},
},
},
})
dc2 := getDeploymentConfig("myproject", componentName, componentType, applicationName, []corev1.Container{c3})
// DC3 with single container and multiple ports
componentType = "wildfly"
componentName = "wildfly"
applicationName = "app"
c4 := getContainer(componentName, applicationName, []corev1.ContainerPort{
{
Name: fmt.Sprintf("%v-%v-p1", componentName, applicationName),
ContainerPort: 8080,
Protocol: corev1.ProtocolTCP,
},
{
Name: fmt.Sprintf("%v-%v-p1", componentName, applicationName),
ContainerPort: 8090,
Protocol: corev1.ProtocolTCP,
},
}, nil)
dc3 := getDeploymentConfig("myproject", componentName, componentType, applicationName, []corev1.Container{c4})
return &v1.DeploymentConfigList{
Items: []v1.DeploymentConfig{
dc1,
dc2,
dc3,
},
}
}
// mountedStorage is the map of the storage to be mounted
// key is the path for the mount, value is the pvc
func OneFakeDeploymentConfigWithMounts(componentName, componentType, applicationName string, mountedStorage map[string]*corev1.PersistentVolumeClaim) *v1.DeploymentConfig {
c := getContainer(componentName, applicationName, []corev1.ContainerPort{
{
Name: fmt.Sprintf("%v-%v-p1", componentName, applicationName),
ContainerPort: 8080,
Protocol: corev1.ProtocolTCP,
},
{
Name: fmt.Sprintf("%v-%v-p2", componentName, applicationName),
ContainerPort: 9090,
Protocol: corev1.ProtocolUDP,
},
}, nil)
dc := getDeploymentConfig("myproject", componentName, componentType, applicationName, []corev1.Container{c})
supervisorDPVC := FakePVC(getAppRootVolumeName(dc.Name), "1Gi", nil)
for path, pvc := range mountedStorage {
volumeName := generateVolumeNameFromPVC(pvc.Name)
dc.Spec.Template.Spec.Volumes = append(dc.Spec.Template.Spec.Volumes, corev1.Volume{
Name: volumeName,
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: pvc.Name,
},
},
})
dc.Spec.Template.Spec.Containers[0].VolumeMounts = append(dc.Spec.Template.Spec.Containers[0].VolumeMounts, corev1.VolumeMount{
Name: volumeName,
MountPath: path,
})
}
// now append the supervisorD volume
dc.Spec.Template.Spec.Volumes = append(dc.Spec.Template.Spec.Volumes, corev1.Volume{
Name: getAppRootVolumeName(dc.Name),
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: supervisorDPVC.Name,
},
},
})
// now append the supervisorD volume mount
dc.Spec.Template.Spec.Containers[0].VolumeMounts = append(dc.Spec.Template.Spec.Containers[0].VolumeMounts, corev1.VolumeMount{
Name: getAppRootVolumeName(dc.Name),
MountPath: "/opt/app-root",
SubPath: "app-root",
})
return &dc
}
// generateVolumeNameFromPVC generates a random volume name based on the name
// of the given PVC
func generateVolumeNameFromPVC(pvc string) string {
return fmt.Sprintf("%v-%v-volume", pvc, util.GenerateRandomString(5))
}
func getAppRootVolumeName(dcName string) string {
return fmt.Sprintf("%s-s2idata", dcName)
}