Skip to content
This repository was archived by the owner on May 28, 2021. It is now read-only.

Commit 9aa1d88

Browse files
KashifSaadatprydie
authored andcommitted
Add support for defining Tolerations in the ClusterSpec (#222)
Signed-off-by: Kashif Saadat <kashifsaadat@gmail.com>
1 parent 0fb2215 commit 9aa1d88

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

pkg/apis/mysql/v1alpha1/types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ type ClusterSpec struct {
6666
SSLSecret *corev1.LocalObjectReference `json:"sslSecret,omitempty"`
6767
// SecurityContext holds pod-level security attributes and common container settings.
6868
SecurityContext *corev1.PodSecurityContext `json:"securityContext,omitempty"`
69+
// Tolerations allows specifying a list of tolerations for controlling which
70+
// set of nodes a pod can be scheduled on
71+
Tolerations *[]corev1.Toleration `json:"tolerations,omitempty"`
6972
}
7073

7174
// ClusterConditionType represents a valid condition of a Cluster.

pkg/resources/statefulsets/statefulset.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,5 +394,8 @@ func NewForCluster(cluster *v1alpha1.Cluster, images operatoropts.Images, servic
394394
if cluster.Spec.SecurityContext != nil {
395395
ss.Spec.Template.Spec.SecurityContext = cluster.Spec.SecurityContext
396396
}
397+
if cluster.Spec.Tolerations != nil {
398+
ss.Spec.Template.Spec.Tolerations = *cluster.Spec.Tolerations
399+
}
397400
return ss
398401
}

pkg/resources/statefulsets/statefulset_test.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,45 @@ func TestClusterCustomSecurityContext(t *testing.T) {
179179

180180
statefulSet := NewForCluster(cluster, mockOperatorConfig().Images, "mycluster")
181181

182-
if statefulSet.Spec.Template.Spec.SecurityContext != nil {
182+
if assert.NotNil(t, statefulSet.Spec.Template.Spec.SecurityContext, "StatefulSet Spec is missing SecurityContext definition") {
183183
assert.EqualValues(t, userID, *statefulSet.Spec.Template.Spec.SecurityContext.RunAsUser, "SecurityContext Spec runAsUser does not have expected value")
184184
assert.Equal(t, userID, *statefulSet.Spec.Template.Spec.SecurityContext.FSGroup, "SecurityContext Spec fsGroup does not have expected value")
185-
} else {
186-
t.Errorf("StatefulSet Spec is missing SecurityContext definition")
185+
}
186+
}
187+
188+
func TestClusterWithTolerations(t *testing.T) {
189+
cluster := &v1alpha1.Cluster{
190+
Spec: v1alpha1.ClusterSpec{
191+
Tolerations: &[]corev1.Toleration{
192+
{
193+
Key: "nodetype1",
194+
Operator: corev1.TolerationOpEqual,
195+
Value: "true",
196+
Effect: corev1.TaintEffectNoSchedule,
197+
},
198+
{
199+
Key: "nodetype1",
200+
Operator: corev1.TolerationOpEqual,
201+
Value: "true",
202+
Effect: corev1.TaintEffectNoExecute,
203+
},
204+
},
205+
},
206+
}
207+
208+
statefulSet := NewForCluster(cluster, mockOperatorConfig().Images, "mycluster")
209+
210+
if assert.NotNil(t, statefulSet.Spec.Template.Spec.Tolerations, "StatefulSet Spec is missing Tolerations") {
211+
if assert.Len(t, statefulSet.Spec.Template.Spec.Tolerations, 2) {
212+
assert.Equal(t, "nodetype1", statefulSet.Spec.Template.Spec.Tolerations[0].Key, "ClusterSpec.Tolerations[0].Key does not have expected value")
213+
assert.Equal(t, corev1.TolerationOpEqual, statefulSet.Spec.Template.Spec.Tolerations[0].Operator, "ClusterSpec.Tolerations[0].Operator does not have expected value")
214+
assert.Equal(t, "true", statefulSet.Spec.Template.Spec.Tolerations[0].Value, "ClusterSpec.Tolerations[0].Value does not have expected value")
215+
assert.Equal(t, corev1.TaintEffectNoSchedule, statefulSet.Spec.Template.Spec.Tolerations[0].Effect, "ClusterSpec.Tolerations[0].Effect does not have expected value")
216+
217+
assert.Equal(t, "nodetype1", statefulSet.Spec.Template.Spec.Tolerations[1].Key, "ClusterSpec.Tolerations[1].Key does not have expected value")
218+
assert.Equal(t, corev1.TolerationOpEqual, statefulSet.Spec.Template.Spec.Tolerations[1].Operator, "ClusterSpec.Tolerations[1].Operator does not have expected value")
219+
assert.Equal(t, "true", statefulSet.Spec.Template.Spec.Tolerations[1].Value, "ClusterSpec.Tolerations[1].Value does not have expected value")
220+
assert.Equal(t, corev1.TaintEffectNoExecute, statefulSet.Spec.Template.Spec.Tolerations[1].Effect, "ClusterSpec.Tolerations[1].Effect does not have expected value")
221+
}
187222
}
188223
}

0 commit comments

Comments
 (0)