/
util.go
135 lines (124 loc) · 3.81 KB
/
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package pod
import (
"strings"
saasv1alpha1 "github.com/3scale-ops/saas-operator/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
// HTTPProbe returns an HTTP corev1.Probe struct
func HTTPProbe(path string, port intstr.IntOrString, scheme corev1.URIScheme, cfg saasv1alpha1.ProbeSpec) *corev1.Probe {
if cfg.IsDeactivated() {
return nil
}
return &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: path,
Port: port,
Scheme: scheme,
},
},
InitialDelaySeconds: *cfg.InitialDelaySeconds,
TimeoutSeconds: *cfg.TimeoutSeconds,
PeriodSeconds: *cfg.PeriodSeconds,
SuccessThreshold: *cfg.SuccessThreshold,
FailureThreshold: *cfg.FailureThreshold,
}
}
func HTTPProbeWithHeaders(path string, port intstr.IntOrString, scheme corev1.URIScheme, cfg saasv1alpha1.ProbeSpec, headers map[string]string) *corev1.Probe {
if probe := HTTPProbe(path, port, scheme, cfg); probe != nil {
if probe.HTTPGet.HTTPHeaders == nil {
probe.HTTPGet.HTTPHeaders = []corev1.HTTPHeader{}
}
for header, value := range headers {
probe.HTTPGet.HTTPHeaders = append(probe.HTTPGet.HTTPHeaders, corev1.HTTPHeader{Name: header, Value: value})
}
return probe
}
return nil
}
// TCPProbe returns a TCP corev1.Probe struct
func TCPProbe(port intstr.IntOrString, cfg saasv1alpha1.ProbeSpec) *corev1.Probe {
if cfg.IsDeactivated() {
return nil
}
return &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
TCPSocket: &corev1.TCPSocketAction{
Port: port,
},
},
InitialDelaySeconds: *cfg.InitialDelaySeconds,
TimeoutSeconds: *cfg.TimeoutSeconds,
PeriodSeconds: *cfg.PeriodSeconds,
SuccessThreshold: *cfg.SuccessThreshold,
FailureThreshold: *cfg.FailureThreshold,
}
}
// ExecProbe returns a exec type corev1.Probe struct
func ExecProbe(command string, cfg saasv1alpha1.ProbeSpec) *corev1.Probe {
if cfg.IsDeactivated() {
return nil
}
return &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{Exec: &corev1.ExecAction{
Command: strings.Split(command, " "),
}},
InitialDelaySeconds: *cfg.InitialDelaySeconds,
TimeoutSeconds: *cfg.TimeoutSeconds,
PeriodSeconds: *cfg.PeriodSeconds,
SuccessThreshold: *cfg.SuccessThreshold,
FailureThreshold: *cfg.FailureThreshold,
}
}
// Affinity returns a corev1.Affinity struct
func Affinity(podAntiAffinitySelector map[string]string, nodeAffinity *corev1.NodeAffinity) *corev1.Affinity {
return &corev1.Affinity{
NodeAffinity: nodeAffinity,
PodAntiAffinity: &corev1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{
{
Weight: 100,
PodAffinityTerm: corev1.PodAffinityTerm{
TopologyKey: corev1.LabelHostname,
LabelSelector: &metav1.LabelSelector{
MatchLabels: podAntiAffinitySelector,
},
},
},
{
Weight: 99,
PodAffinityTerm: corev1.PodAffinityTerm{
TopologyKey: corev1.LabelTopologyZone,
LabelSelector: &metav1.LabelSelector{
MatchLabels: podAntiAffinitySelector,
},
},
},
},
},
}
}
// ContainerPortTCP returns a TCP corev1.ContainerPort
func ContainerPortTCP(name string, port int32) corev1.ContainerPort {
return corev1.ContainerPort{
Name: name,
ContainerPort: port,
Protocol: corev1.ProtocolTCP,
}
}
// ContainerPorts returns a list of corev1.ContainerPort
func ContainerPorts(ports ...corev1.ContainerPort) []corev1.ContainerPort {
list := []corev1.ContainerPort{}
return append(list, ports...)
}
func Image(image saasv1alpha1.ImageSpec) string {
return *image.Name + ":" + *image.Tag
}
func ImagePullSecrets(ips *string) []corev1.LocalObjectReference {
if ips != nil {
return []corev1.LocalObjectReference{{Name: *ips}}
}
return nil
}