/
k8s_util.go
117 lines (111 loc) · 2.88 KB
/
k8s_util.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
/*
* *******************************************************************************
* * Copyright (c) 2023 Datasance Teknoloji A.S.
* *
* * This program and the accompanying materials are made available under the
* * terms of the Eclipse Public License v. 2.0 which is available at
* * http://www.eclipse.org/legal/epl-2.0
* *
* * SPDX-License-Identifier: EPL-2.0
* *******************************************************************************
*
*/
package install
import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
func newDeployment(namespace string, ms *microservice) *appsv1.Deployment {
maxUnavailable := intstr.FromInt(0)
maxSurge := intstr.FromInt(1)
strategy := appsv1.DeploymentStrategy{
Type: appsv1.RollingUpdateDeploymentStrategyType,
RollingUpdate: &appsv1.RollingUpdateDeployment{
MaxUnavailable: &maxUnavailable,
MaxSurge: &maxSurge,
},
}
dep := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: ms.name,
Namespace: namespace,
Labels: map[string]string{
"name": ms.name,
},
},
Spec: appsv1.DeploymentSpec{
Replicas: &ms.replicas,
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"name": ms.name,
},
},
Strategy: strategy,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"name": ms.name,
},
},
Spec: corev1.PodSpec{
ServiceAccountName: ms.name,
},
},
},
}
containers := &dep.Spec.Template.Spec.Containers
for idx := range ms.containers {
msCont := &ms.containers[idx]
cont := corev1.Container{
Name: msCont.name,
Image: msCont.image,
ImagePullPolicy: corev1.PullPolicy(msCont.imagePullPolicy),
Args: msCont.args,
ReadinessProbe: msCont.readinessProbe,
Ports: msCont.ports,
Env: msCont.env,
Command: msCont.command,
}
*containers = append(*containers, cont)
}
return dep
}
func newServiceAccount(namespace string, ms *microservice) *corev1.ServiceAccount {
return &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: ms.name,
Namespace: namespace,
},
}
}
func newRoleBinding(namespace string, ms *microservice) *rbacv1.RoleBinding {
return &rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: ms.name,
Namespace: namespace,
},
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
Name: ms.name,
},
},
RoleRef: rbacv1.RoleRef{
Kind: "Role",
Name: ms.name,
APIGroup: "rbac.authorization.k8s.io",
},
}
}
func newRole(namespace string, ms *microservice) *rbacv1.Role {
return &rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: ms.name,
Namespace: namespace,
},
Rules: ms.rbacRules,
}
}