Skip to content

Commit a5fec66

Browse files
committed
feat: allow tolerations to be specified in overcommit and overcommitclass
1 parent e5cb3e7 commit a5fec66

9 files changed

Lines changed: 146 additions & 3 deletions

api/v1alphav1/overcommit_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package v1alphav1
77

88
import (
9+
corev1 "k8s.io/api/core/v1"
910
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1011
)
1112

@@ -24,6 +25,8 @@ type OvercommitSpec struct {
2425
Labels map[string]string `json:"labels,omitempty"`
2526
// +kubebuilder:validation:Optional
2627
Annotations map[string]string `json:"annotations,omitempty"`
28+
// +kubebuilder:validation:Optional
29+
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
2730
}
2831

2932
// OvercommitStatus defines the observed state of Overcommit

api/v1alphav1/overcommitclass_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package v1alphav1
77

88
import (
9+
corev1 "k8s.io/api/core/v1"
910
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1011
"k8s.io/apimachinery/pkg/types"
1112
)
@@ -32,6 +33,8 @@ type OvercommitClassSpec struct {
3233
IsDefault bool `json:"isDefault,omitempty"`
3334
Labels map[string]string `json:"labels,omitempty"`
3435
Annotations map[string]string `json:"annotations,omitempty"`
36+
// +kubebuilder:validation:Optional
37+
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
3538
}
3639

3740
type ResourceStatus struct {

api/v1alphav1/zz_generated.deepcopy.go

Lines changed: 18 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/overcommit.inditex.dev_overcommitclasses.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,44 @@ spec:
7676
maximum: 1
7777
minimum: 0.0001
7878
type: number
79+
tolerations:
80+
items:
81+
description: |-
82+
The pod this Toleration is attached to tolerates any taint that matches
83+
the triple <key,value,effect> using the matching operator <operator>.
84+
properties:
85+
effect:
86+
description: |-
87+
Effect indicates the taint effect to match. Empty means match all taint effects.
88+
When specified, allowed values are NoSchedule, PreferNoSchedule and NoExecute.
89+
type: string
90+
key:
91+
description: |-
92+
Key is the taint key that the toleration applies to. Empty means match all taint keys.
93+
If the key is empty, operator must be Exists; this combination means to match all values and all keys.
94+
type: string
95+
operator:
96+
description: |-
97+
Operator represents a key's relationship to the value.
98+
Valid operators are Exists and Equal. Defaults to Equal.
99+
Exists is equivalent to wildcard for value, so that a pod can
100+
tolerate all taints of a particular category.
101+
type: string
102+
tolerationSeconds:
103+
description: |-
104+
TolerationSeconds represents the period of time the toleration (which must be
105+
of effect NoExecute, otherwise this field is ignored) tolerates the taint. By default,
106+
it is not set, which means tolerate the taint forever (do not evict). Zero and
107+
negative values will be treated as 0 (evict immediately) by the system.
108+
format: int64
109+
type: integer
110+
value:
111+
description: |-
112+
Value is the taint value the toleration matches to.
113+
If the operator is Exists, the value should be empty, otherwise just a regular string.
114+
type: string
115+
type: object
116+
type: array
79117
required:
80118
- cpuOvercommit
81119
- excludedNamespaces

config/crd/bases/overcommit.inditex.dev_overcommits.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,44 @@ spec:
5555
overcommitLabel:
5656
minLength: 1
5757
type: string
58+
tolerations:
59+
items:
60+
description: |-
61+
The pod this Toleration is attached to tolerates any taint that matches
62+
the triple <key,value,effect> using the matching operator <operator>.
63+
properties:
64+
effect:
65+
description: |-
66+
Effect indicates the taint effect to match. Empty means match all taint effects.
67+
When specified, allowed values are NoSchedule, PreferNoSchedule and NoExecute.
68+
type: string
69+
key:
70+
description: |-
71+
Key is the taint key that the toleration applies to. Empty means match all taint keys.
72+
If the key is empty, operator must be Exists; this combination means to match all values and all keys.
73+
type: string
74+
operator:
75+
description: |-
76+
Operator represents a key's relationship to the value.
77+
Valid operators are Exists and Equal. Defaults to Equal.
78+
Exists is equivalent to wildcard for value, so that a pod can
79+
tolerate all taints of a particular category.
80+
type: string
81+
tolerationSeconds:
82+
description: |-
83+
TolerationSeconds represents the period of time the toleration (which must be
84+
of effect NoExecute, otherwise this field is ignored) tolerates the taint. By default,
85+
it is not set, which means tolerate the taint forever (do not evict). Zero and
86+
negative values will be treated as 0 (evict immediately) by the system.
87+
format: int64
88+
type: integer
89+
value:
90+
description: |-
91+
Value is the taint value the toleration matches to.
92+
If the operator is Exists, the value should be empty, otherwise just a regular string.
93+
type: string
94+
type: object
95+
type: array
5896
required:
5997
- overcommitLabel
6098
type: object

internal/resources/generate_resources_overcommit_class_controller_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func GenerateOvercommitClassControllerDeployment(overcommitObject overcommit.Ove
9494
},
9595
},
9696
},
97+
Tolerations: overcommitObject.Spec.Tolerations,
9798
},
9899
},
99100
},

internal/resources/generate_resources_pod_mutating_webhooks.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func CreateDeployment(class overcommit.OvercommitClass) *appsv1.Deployment {
9797
},
9898
},
9999
},
100+
Tolerations: class.Spec.Tolerations,
100101
Volumes: []corev1.Volume{
101102
{
102103
Name: "webhook-tls-secret",

internal/resources/generate_resources_pod_mutating_webhooks_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,47 @@ func TestCreateDeployment(t *testing.T) {
4242
}
4343
}
4444

45+
func TestCreateDeploymentWithTolerations(t *testing.T) {
46+
os.Setenv("POD_NAMESPACE", "test-namespace")
47+
os.Setenv("IMAGE_REGISTRY", "test-registry")
48+
os.Setenv("IMAGE_REPOSITORY", "test-repo")
49+
os.Setenv("APP_VERSION", "v1.0.0")
50+
51+
class := overcommit.OvercommitClass{
52+
ObjectMeta: metav1.ObjectMeta{
53+
Name: "test-class",
54+
},
55+
Spec: overcommit.OvercommitClassSpec{
56+
Labels: map[string]string{"key": "value"},
57+
Annotations: map[string]string{"annotation-key": "annotation-value"},
58+
Tolerations: []corev1.Toleration{
59+
{
60+
Key: "key1",
61+
Operator: corev1.TolerationOpExists,
62+
},
63+
},
64+
},
65+
}
66+
67+
deployment := CreateDeployment(class)
68+
69+
if deployment.ObjectMeta.Name != "test-class-overcommit-webhook" {
70+
t.Errorf("Expected deployment name 'test-class-overcommit-webhook', got '%s'", deployment.ObjectMeta.Name)
71+
}
72+
73+
if deployment.Spec.Replicas == nil || *deployment.Spec.Replicas != 1 {
74+
t.Errorf("Expected replicas to be 1, got '%v'", deployment.Spec.Replicas)
75+
}
76+
77+
if len(deployment.Spec.Template.Spec.Tolerations) != 1 {
78+
t.Errorf("Expected 1 toleration, got '%d'", len(deployment.Spec.Template.Spec.Tolerations))
79+
}
80+
81+
if deployment.Spec.Template.Spec.Tolerations[0].Key != "key1" {
82+
t.Errorf("Expected toleration key 'key1', got '%s'", deployment.Spec.Template.Spec.Tolerations[0].Key)
83+
}
84+
}
85+
4586
func TestCreateService(t *testing.T) {
4687
os.Setenv("POD_NAMESPACE", "test-namespace")
4788

internal/resources/generate_resources_validating_webhooks.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ func GeneratePodValidatingDeployment(overcommitObject overcommit.Overcommit) *ap
128128
},
129129
},
130130
},
131+
Tolerations: overcommitObject.Spec.Tolerations,
131132
Volumes: []corev1.Volume{
132133
{
133134
Name: "webhook-cert",
@@ -299,6 +300,8 @@ func GenerateOvercommitClassValidatingDeployment(overcommitObject overcommit.Ove
299300
},
300301
},
301302
},
303+
//TODO: parameterize tolerations
304+
Tolerations: []corev1.Toleration{},
302305
Volumes: []corev1.Volume{
303306
{
304307
Name: "webhook-cert",

0 commit comments

Comments
 (0)