Skip to content

Commit a544d9b

Browse files
committed
kmm: add support for tolerations
1 parent 4df6b87 commit a544d9b

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

pkg/kmm/module.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,50 @@ func (builder *ModuleBuilder) WithDevicePluginVolume(name string, configMapName
179179
return builder
180180
}
181181

182+
// WithToleration adds the specified toleration to the Module.
183+
func (builder *ModuleBuilder) WithToleration(key string, operator string,
184+
value string, effect string, seconds int64) *ModuleBuilder {
185+
if valid, _ := builder.validate(); !valid {
186+
return builder
187+
}
188+
189+
if key == "" {
190+
builder.errorMsg = "cannot redefine with empty 'key' value"
191+
192+
return builder
193+
}
194+
195+
if operator == "" {
196+
builder.errorMsg = "cannot redefine with empty 'operator' value"
197+
198+
return builder
199+
}
200+
201+
if effect == "" {
202+
builder.errorMsg = "cannot redefine with empty 'effect' value"
203+
204+
return builder
205+
}
206+
207+
if builder.Definition.Spec.Tolerations == nil {
208+
builder.Definition.Spec.Tolerations = []corev1.Toleration{}
209+
}
210+
211+
glog.V(100).Infof(
212+
"Appending toleration with key: %s, operator: %s, value: %s, effect: %s and seconds: %s",
213+
key, operator, value, effect, seconds)
214+
215+
builder.Definition.Spec.Tolerations = append(builder.Definition.Spec.Tolerations, []corev1.Toleration{{
216+
Key: key,
217+
Effect: corev1.TaintEffect(effect),
218+
Operator: corev1.TolerationOperator(operator),
219+
Value: value,
220+
TolerationSeconds: &seconds,
221+
}}...)
222+
223+
return builder
224+
}
225+
182226
// WithModuleLoaderContainer adds the specified ModuleLoader container to the Module.
183227
func (builder *ModuleBuilder) WithModuleLoaderContainer(
184228
container *moduleV1Beta1.ModuleLoaderContainerSpec) *ModuleBuilder {

pkg/kmm/module_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/openshift-kni/eco-goinfra/pkg/clients"
88
"github.com/openshift-kni/eco-goinfra/pkg/schemes/kmm/v1beta1"
99
"github.com/stretchr/testify/assert"
10+
v1 "k8s.io/api/core/v1"
1011
"k8s.io/apimachinery/pkg/api/errors"
1112
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1213
"k8s.io/apimachinery/pkg/runtime"
@@ -514,6 +515,62 @@ func TestModuleWithOptions(t *testing.T) {
514515
assert.Equal(t, "error", testBuilder.errorMsg)
515516
}
516517

518+
func TestModuleWithToleration(t *testing.T) {
519+
testCases := []struct {
520+
key string
521+
operator string
522+
value string
523+
effect string
524+
seconds int64
525+
expectedErr string
526+
}{
527+
{
528+
key: "",
529+
operator: "test",
530+
value: "test",
531+
effect: "test",
532+
seconds: 10,
533+
expectedErr: "cannot redefine with empty 'key' value",
534+
},
535+
{
536+
key: "test",
537+
operator: "",
538+
value: "test",
539+
effect: "test",
540+
seconds: 10,
541+
expectedErr: "cannot redefine with empty 'operator' value",
542+
},
543+
{
544+
key: "test",
545+
operator: "test",
546+
value: "test",
547+
effect: "",
548+
seconds: 10,
549+
expectedErr: "cannot redefine with empty 'effect' value",
550+
},
551+
{
552+
key: "testkey",
553+
operator: "Equals",
554+
value: "testvalue",
555+
effect: "NoSchedule",
556+
seconds: 0,
557+
expectedErr: "",
558+
},
559+
}
560+
for _, testCase := range testCases {
561+
testSettings := buildModuleTestClientWithDummyObject()
562+
testBuilder := buildValidTestModule(testSettings).WithToleration(
563+
testCase.key, testCase.operator, testCase.value, testCase.effect, testCase.seconds)
564+
565+
if testCase.expectedErr == "" {
566+
assert.Equal(t, testCase.key, testBuilder.Definition.Spec.Tolerations[0].Key)
567+
assert.Equal(t, v1.TaintEffect(testCase.effect), testBuilder.Definition.Spec.Tolerations[0].Effect)
568+
} else {
569+
assert.Equal(t, testCase.expectedErr, testBuilder.errorMsg)
570+
}
571+
}
572+
}
573+
517574
func TestModuleBuildModuleSpec(t *testing.T) {
518575
testCases := []struct {
519576
testModule *ModuleBuilder

0 commit comments

Comments
 (0)