/
deployment.go
125 lines (110 loc) · 3.35 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2021 The Tiyo authors
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package kubernetes
import (
"context"
"time"
"github.com/notapipeline/tiyo/pkg/pipeline"
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"
)
func (kube *Kubernetes) GetDeployment(name string) *appsv1.Deployment {
deployment, _ := kube.ClientSet.AppsV1().Deployments(kube.Config.Kubernetes.Namespace).Get(
context.TODO(), name, metav1.GetOptions{})
return deployment
}
func (kube *Kubernetes) DeploymentExists(name string) bool {
list, err := kube.ClientSet.AppsV1().Deployments(kube.Config.Kubernetes.Namespace).List(
context.TODO(), metav1.ListOptions{})
if err != nil {
log.Error(err)
}
for _, item := range list.Items {
if item.Name == name {
return true
}
}
return false
}
func (kube *Kubernetes) GetDeploymentContainers(instances []*pipeline.Command) []corev1.Container {
containers := make([]corev1.Container, 0)
for _, instance := range instances {
container := corev1.Container{
Name: instance.Name,
Image: instance.Tag,
ImagePullPolicy: corev1.PullAlways,
Ports: kube.GetContainerPorts(instance),
}
containers = append(containers, container)
}
return containers
}
func (kube *Kubernetes) DestroyDeployment(pipeline string, container *pipeline.Container) {
var name string = pipeline + "-" + container.Name
log.Info("Deleting Deployment ", name)
client := kube.ClientSet.AppsV1().Deployments(kube.Config.Kubernetes.Namespace)
policy := metav1.DeletePropagationForeground
client.Delete(context.TODO(), name, metav1.DeleteOptions{
PropagationPolicy: &policy,
})
for {
if !kube.DeploymentExists(name) {
log.Info("Deleted deployment ", name)
break
}
log.Info("Still deleting deployment ", name)
time.Sleep(1 * time.Second)
}
if kube.ServiceExists(name) {
kube.DestroyService(name)
}
}
func (kube *Kubernetes) CreateDeployment(pipeline string, container *pipeline.Container) {
var name string = pipeline + "-" + container.Name
instances := container.GetChildren()
// If the deployment exists delete and recreate it.
if kube.DeploymentExists(name) {
container.State = "Terminating"
kube.DestroyDeployment(pipeline, container)
}
client := kube.ClientSet.AppsV1().Deployments(kube.Config.Kubernetes.Namespace)
deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: kube.Config.Kubernetes.Namespace,
Labels: map[string]string{
"app": pipeline,
},
},
Spec: appsv1.DeploymentSpec{
Replicas: &container.Scale,
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": name,
},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": name,
},
},
Spec: corev1.PodSpec{
Containers: kube.GetDeploymentContainers(instances),
},
},
},
}
result, err := client.Create(context.TODO(), deployment, metav1.CreateOptions{})
if err != nil {
panic(err)
}
container.State = "Creating"
log.Info("Created deployment ", result.GetObjectMeta().GetName())
kube.CreateService(name, instances)
}