/
deployment.go
102 lines (94 loc) · 2.26 KB
/
deployment.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
package deployment
import (
"fmt"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type Deployment struct {
Name string
Namespace string
ServerType string
SecretName string
Port int
Version string
Registry string
OpaTag string
CmsnrTag string
}
func NewDeployment(name, namespace, registry, serverType, secretName string, port int, version, cmsnrtag, opatag string) *appsv1.Deployment {
dep := Deployment{
Name: name,
Namespace: namespace,
ServerType: serverType,
SecretName: secretName,
Port: port,
Version: version,
Registry: registry,
CmsnrTag: cmsnrtag,
OpaTag: opatag,
}
return dep.newDeployment()
}
func (d *Deployment) newDeployment() *appsv1.Deployment {
return &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
Kind: "Deployment",
APIVersion: "apps/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: d.Name,
Namespace: d.Namespace,
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": d.Name,
},
},
Template: d.getTemplate(),
},
}
}
func (d *Deployment) getTemplate() corev1.PodTemplateSpec {
return corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: d.Name,
Labels: map[string]string{
"app": d.Name,
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Image: fmt.Sprintf("%s/cmsnr:%s", d.Registry, d.Version),
ImagePullPolicy: "Always",
Name: d.Name,
Args: []string{"server", "start", fmt.Sprintf("--registry=%s", d.Registry), fmt.Sprintf("--opa-tag=%s", d.OpaTag), fmt.Sprintf("--cmsnr-tag=%s", d.CmsnrTag), d.ServerType, fmt.Sprintf("-n=%s", d.Namespace)},
Ports: []corev1.ContainerPort{
{
Name: "https",
ContainerPort: int32(d.Port),
},
},
VolumeMounts: []corev1.VolumeMount{
{
Name: "webhook-certs",
MountPath: "/var/lib/cmsnr",
},
},
},
},
Volumes: []corev1.Volume{
{
Name: "webhook-certs",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: d.SecretName,
},
},
},
},
},
}
}