/
schema_affinity_spec.go
192 lines (182 loc) · 7.83 KB
/
schema_affinity_spec.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package kubernetes
import (
"regexp"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
func affinityFields() map[string]*schema.Schema {
return map[string]*schema.Schema{
"node_affinity": {
Type: schema.TypeList,
Description: "Node affinity scheduling rules for the pod.",
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: nodeAffinityFields(),
},
},
"pod_affinity": {
Type: schema.TypeList,
Description: "Inter-pod topological affinity. rules that specify that certain pods should be placed in the same topological domain (e.g. same node, same rack, same zone, same power domain, etc.)",
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: podAffinityFields(),
},
},
"pod_anti_affinity": {
Type: schema.TypeList,
Description: "Inter-pod topological affinity. rules that specify that certain pods should be placed in the same topological domain (e.g. same node, same rack, same zone, same power domain, etc.)",
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: podAffinityFields(),
},
},
}
}
func nodeAffinityFields() map[string]*schema.Schema {
return map[string]*schema.Schema{
"required_during_scheduling_ignored_during_execution": {
Type: schema.TypeList,
Description: "If the affinity requirements specified by this field are not met at scheduling time, the pod will not be scheduled onto the node. If the affinity requirements specified by this field cease to be met at some point during pod execution (e.g. due to a node label update), the system may or may not try to eventually evict the pod from its node.",
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: nodeSelectorFields(),
},
},
"preferred_during_scheduling_ignored_during_execution": {
Type: schema.TypeList,
Description: "The scheduler will prefer to schedule pods to nodes that satisfy the affinity expressions specified by this field, but it may choose a node that violates one or more of the expressions. The node that is most preferred is the one with the greatest sum of weights, i.e. for each node that meets all of the scheduling requirements (resource request, RequiredDuringScheduling affinity expressions, etc.), compute a sum by iterating through the elements of this field and adding 'weight' to the sum if the node matches the corresponding MatchExpressions; the node(s) with the highest sum are the most preferred.",
Optional: true,
Elem: &schema.Resource{
Schema: preferredSchedulingTermFields(),
},
},
}
}
func nodeSelectorFields() map[string]*schema.Schema {
return map[string]*schema.Schema{
"node_selector_term": {
Type: schema.TypeList,
Description: "List of node selector terms. The terms are ORed.",
Optional: true,
Elem: &schema.Resource{
Schema: nodeSelectorRequirementsFields(),
},
},
}
}
func preferredSchedulingTermFields() map[string]*schema.Schema {
return map[string]*schema.Schema{
"weight": {
Type: schema.TypeInt,
Description: "weight is in the range 1-100",
Required: true,
},
"preference": {
Type: schema.TypeList,
Description: "A node selector term, associated with the corresponding weight.",
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: nodeSelectorRequirementsFields(),
},
},
}
}
func nodeSelectorRequirementsFields() map[string]*schema.Schema {
return map[string]*schema.Schema{
"match_expressions": {
Type: schema.TypeList,
Description: "List of node selector requirements. The requirements are ANDed.",
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Description: "The label key that the selector applies to.",
Optional: true,
},
"operator": {
Type: schema.TypeString,
Description: "Operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists, DoesNotExist. Gt, and Lt.",
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"In", "NotIn", "Exists", "DoesNotExist", "Gt", "Lt"}, false),
},
"values": {
Type: schema.TypeSet,
Description: "Values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. If the operator is Gt or Lt, the values array must have a single element, which will be interpreted as an integer. This array is replaced during a strategic merge patch.",
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
},
},
}
}
func podAffinityFields() map[string]*schema.Schema {
return map[string]*schema.Schema{
"required_during_scheduling_ignored_during_execution": {
Type: schema.TypeList,
Description: "If the affinity requirements specified by this field are not met at scheduling time, the pod will not be scheduled onto the node. If the affinity requirements specified by this field cease to be met at some point during pod execution (e.g. due to a pod label update), the system may or may not try to eventually evict the pod from its node. When there are multiple elements, the lists of nodes corresponding to each PodAffinityTerm are intersected, i.e. all terms must be satisfied.",
Optional: true,
Elem: &schema.Resource{
Schema: podAffinityTermFields(),
},
},
"preferred_during_scheduling_ignored_during_execution": {
Type: schema.TypeList,
Description: "The scheduler will prefer to schedule pods to nodes that satisfy the anti-affinity expressions specified by this field, but it may choose a node that violates one or more of the expressions. The node that is most preferred is the one with the greatest sum of weights, i.e. for each node that meets all of the scheduling requirements (resource request, RequiredDuringScheduling anti-affinity expressions, etc.), compute a sum by iterating through the elements of this field and adding 'weight' to the sum if the node matches the corresponding MatchExpressions; the node(s) with the highest sum are the most preferred.",
Optional: true,
Elem: &schema.Resource{
Schema: weightedPodAffinityTermFields(),
},
},
}
}
func podAffinityTermFields() map[string]*schema.Schema {
return map[string]*schema.Schema{
"label_selector": {
Type: schema.TypeList,
Description: "A label query over a set of resources, in this case pods.",
Optional: true,
Elem: &schema.Resource{
Schema: labelSelectorFields(true),
},
},
"namespaces": {
Type: schema.TypeSet,
Description: "namespaces specifies which namespaces the labelSelector applies to (matches against); null or empty list means 'this pod's namespace'",
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"topology_key": {
Type: schema.TypeString,
Description: "empty topology key is interpreted by the scheduler as 'all topologies'",
Optional: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^.+$`), "value cannot be empty"),
},
}
}
func weightedPodAffinityTermFields() map[string]*schema.Schema {
return map[string]*schema.Schema{
"weight": {
Type: schema.TypeInt,
Description: "weight associated with matching the corresponding podAffinityTerm, in the range 1-100",
Required: true,
},
"pod_affinity_term": {
Type: schema.TypeList,
Description: "A pod affinity term, associated with the corresponding weight",
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: podAffinityTermFields(),
},
},
}
}