Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/install/installer/pkg/cluster/affinity.go
2501 views
1
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cluster
6
7
import (
8
corev1 "k8s.io/api/core/v1"
9
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10
)
11
12
// Valid characters for affinities are alphanumeric, -, _, . and one / as a subdomain prefix
13
const (
14
AffinityLabelMeta = "gitpod.io/workload_meta"
15
AffinityLabelIDE = "gitpod.io/workload_ide"
16
AffinityLabelServices = "gitpod.io/workload_services"
17
AffinityLabelWorkspacesRegular = "gitpod.io/workload_workspace_regular"
18
AffinityLabelWorkspacesHeadless = "gitpod.io/workload_workspace_headless"
19
20
HostnameTopologyKey = "kubernetes.io/hostname"
21
)
22
23
var AffinityListMeta = []string{
24
AffinityLabelMeta,
25
AffinityLabelIDE,
26
AffinityLabelServices,
27
}
28
29
var AffinityListWorkspace = []string{
30
AffinityLabelServices,
31
AffinityLabelWorkspacesRegular,
32
AffinityLabelWorkspacesHeadless,
33
}
34
35
var AffinityList = func() []string {
36
list := []string{}
37
38
list = append(list, AffinityListMeta...)
39
list = append(list, AffinityListWorkspace...)
40
41
return list
42
}()
43
44
func WithPodAntiAffinityHostname(component string) *corev1.PodAntiAffinity {
45
return &corev1.PodAntiAffinity{
46
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{
47
{
48
Weight: 100,
49
PodAffinityTerm: corev1.PodAffinityTerm{
50
LabelSelector: &metav1.LabelSelector{
51
MatchExpressions: []metav1.LabelSelectorRequirement{{
52
Key: "component",
53
Operator: "In",
54
Values: []string{component},
55
}},
56
},
57
TopologyKey: HostnameTopologyKey,
58
},
59
},
60
},
61
}
62
}
63
64
func defaultLabels(component string) map[string]string {
65
return map[string]string{
66
"app": "gitpod",
67
"component": component,
68
}
69
}
70
71
func WithHostnameTopologySpread(component string) []corev1.TopologySpreadConstraint {
72
return []corev1.TopologySpreadConstraint{
73
{
74
LabelSelector: &metav1.LabelSelector{MatchLabels: defaultLabels(component)},
75
MaxSkew: 1,
76
TopologyKey: HostnameTopologyKey,
77
WhenUnsatisfiable: corev1.ScheduleAnyway,
78
},
79
}
80
}
81
82
func WithNodeAffinityHostnameAntiAffinity(component string, orLabels ...string) *corev1.Affinity {
83
var terms []corev1.NodeSelectorTerm
84
85
for _, lbl := range orLabels {
86
terms = append(terms, corev1.NodeSelectorTerm{
87
MatchExpressions: []corev1.NodeSelectorRequirement{
88
{
89
Key: lbl,
90
Operator: corev1.NodeSelectorOpExists,
91
},
92
},
93
})
94
}
95
96
return &corev1.Affinity{
97
NodeAffinity: &corev1.NodeAffinity{
98
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
99
NodeSelectorTerms: terms,
100
},
101
},
102
PodAntiAffinity: WithPodAntiAffinityHostname(component),
103
}
104
}
105
106
func WithNodeAffinity(orLabels ...string) *corev1.Affinity {
107
var terms []corev1.NodeSelectorTerm
108
109
for _, lbl := range orLabels {
110
terms = append(terms, corev1.NodeSelectorTerm{
111
MatchExpressions: []corev1.NodeSelectorRequirement{
112
{
113
Key: lbl,
114
Operator: corev1.NodeSelectorOpExists,
115
},
116
},
117
})
118
}
119
120
return &corev1.Affinity{
121
NodeAffinity: &corev1.NodeAffinity{
122
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
123
NodeSelectorTerms: terms,
124
},
125
},
126
}
127
}
128
129