-
Notifications
You must be signed in to change notification settings - Fork 0
/
pod.go
79 lines (71 loc) · 2.12 KB
/
pod.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
// Copyright (c) 2023 Volvo Car Corporation
// SPDX-License-Identifier: Apache-2.0
package kubeutil
import (
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
// Resources creates a ResourceRequirements struct.
func Resources(cpuWant, memWant, cpuMax, memMax string) corev1.ResourceRequirements {
return corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse(cpuWant),
corev1.ResourceMemory: resource.MustParse(memWant),
},
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse(cpuMax),
corev1.ResourceMemory: resource.MustParse(memMax),
},
}
}
func AntiAffinityHostnameByLabel(key, value string) *corev1.PodAntiAffinity {
return &corev1.PodAntiAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{
{
LabelSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: key,
Operator: metav1.LabelSelectorOpIn,
Values: []string{value},
},
},
},
TopologyKey: LabelHostname,
},
},
}
}
func ProbeHTTP(path string, port int) corev1.ProbeHandler {
return corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: path,
Port: intstr.FromInt(port),
},
}
}
// EnvVarDownAPI returns corev1.EnvVar{
// Name: name,
// ValueFrom: &corev1.EnvVarSource{FieldRef: &corev1.ObjectFieldSelector{FieldPath: fieldPath}},
// }
func EnvVarDownAPI(name, fieldPath string) corev1.EnvVar {
return corev1.EnvVar{
Name: name,
ValueFrom: &corev1.EnvVarSource{FieldRef: &corev1.ObjectFieldSelector{FieldPath: fieldPath}},
}
}
// AnnotationPrometheus returns map[string]string{
// "prometheus.io/path": path,
// "prometheus.io/port": fmt.Sprintf("%d", port),
// "prometheus.io/scrape": "true",
// }
func AnnotationPrometheus(path string, port int32) map[string]string {
return map[string]string{
"prometheus.io/path": path,
"prometheus.io/port": fmt.Sprintf("%d", port),
"prometheus.io/scrape": "true",
}
}