Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: catalog source pod scheduling config overrides #2512

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions pkg/controller/registry/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,32 @@ func Pod(source *v1alpha1.CatalogSource, name string, image string, saName strin
},
}

// Override scheduling options if specified
if source.Spec.GrpcPodConfig != nil {
grpcPodConfig := source.Spec.GrpcPodConfig

// Override node selector
if grpcPodConfig.NodeSelector != nil {
pod.Spec.NodeSelector = make(map[string]string, len(grpcPodConfig.NodeSelector))
for key, value := range grpcPodConfig.NodeSelector {
pod.Spec.NodeSelector[key] = value
}
anik120 marked this conversation as resolved.
Show resolved Hide resolved
}

// Override priority class name
if grpcPodConfig.PriorityClassName != nil {
pod.Spec.PriorityClassName = *grpcPodConfig.PriorityClassName
}

// Override tolerations
if grpcPodConfig.Tolerations != nil {
pod.Spec.Tolerations = make([]v1.Toleration, len(grpcPodConfig.Tolerations))
for index, toleration := range grpcPodConfig.Tolerations {
pod.Spec.Tolerations[index] = *toleration.DeepCopy()
}
}
}

// Set priorityclass if its annotation exists
if prio, ok := annotations[CatalogPriorityClassKey]; ok && prio != "" {
pod.Spec.PriorityClassName = prio
Expand Down
176 changes: 176 additions & 0 deletions pkg/controller/registry/reconciler/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,179 @@ func TestPodContainerSecurityContext(t *testing.T) {
gotContainerSecCtx := gotPod.Spec.Containers[0].SecurityContext
require.Equal(t, expectedContainerSecCtx, gotContainerSecCtx)
}

func TestPodSchedulingOverrides(t *testing.T) {
// This test ensures that any overriding pod scheduling configuration elements
// defined in spec.grpcPodConfig are applied to the catalog source pod created
// when spec.sourceType = 'grpc' and spec.image is set.
var tolerationSeconds int64 = 120
var overriddenPriorityClassName = "some-prio-class"
var overriddenNodeSelectors = map[string]string{
"label": "value",
"label2": "value2",
}
var defaultNodeSelectors = map[string]string{
"kubernetes.io/os": "linux",
}
var defaultPriorityClassName = ""

var overriddenTolerations = []corev1.Toleration{
{
Key: "some/key",
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoExecute,
TolerationSeconds: &tolerationSeconds,
},
{
Key: "someother/key",
Operator: corev1.TolerationOpEqual,
Effect: corev1.TaintEffectNoSchedule,
},
}

testCases := []struct {
title string
catalogSource *v1alpha1.CatalogSource
expectedNodeSelectors map[string]string
expectedTolerations []corev1.Toleration
expectedPriorityClassName string
annotations map[string]string
}{
{
title: "no overrides",
catalogSource: &v1alpha1.CatalogSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "testns",
},
Spec: v1alpha1.CatalogSourceSpec{
SourceType: v1alpha1.SourceTypeGrpc,
Image: "repo/image:tag",
},
},
expectedTolerations: nil,
expectedPriorityClassName: defaultPriorityClassName,
expectedNodeSelectors: defaultNodeSelectors,
}, {
title: "override node selectors",
catalogSource: &v1alpha1.CatalogSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "testns",
},
Spec: v1alpha1.CatalogSourceSpec{
SourceType: v1alpha1.SourceTypeGrpc,
Image: "repo/image:tag",
GrpcPodConfig: &v1alpha1.GrpcPodConfig{
NodeSelector: overriddenNodeSelectors,
},
},
},
expectedTolerations: nil,
expectedPriorityClassName: defaultPriorityClassName,
expectedNodeSelectors: overriddenNodeSelectors,
}, {
title: "override priority class name",
catalogSource: &v1alpha1.CatalogSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "testns",
},
Spec: v1alpha1.CatalogSourceSpec{
SourceType: v1alpha1.SourceTypeGrpc,
Image: "repo/image:tag",
GrpcPodConfig: &v1alpha1.GrpcPodConfig{
PriorityClassName: &overriddenPriorityClassName,
},
},
},
expectedTolerations: nil,
expectedPriorityClassName: overriddenPriorityClassName,
expectedNodeSelectors: defaultNodeSelectors,
}, {
title: "doesn't override priority class name when its nil",
catalogSource: &v1alpha1.CatalogSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "testns",
},
Spec: v1alpha1.CatalogSourceSpec{
SourceType: v1alpha1.SourceTypeGrpc,
Image: "repo/image:tag",
GrpcPodConfig: &v1alpha1.GrpcPodConfig{
PriorityClassName: nil,
},
},
},
expectedTolerations: nil,
expectedPriorityClassName: defaultPriorityClassName,
expectedNodeSelectors: defaultNodeSelectors,
}, {
title: "Override node tolerations",
catalogSource: &v1alpha1.CatalogSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "testns",
},
Spec: v1alpha1.CatalogSourceSpec{
SourceType: v1alpha1.SourceTypeGrpc,
Image: "repo/image:tag",
GrpcPodConfig: &v1alpha1.GrpcPodConfig{
Tolerations: overriddenTolerations,
},
},
},
expectedTolerations: overriddenTolerations,
expectedPriorityClassName: defaultPriorityClassName,
expectedNodeSelectors: defaultNodeSelectors,
}, {
title: "Override all the things",
catalogSource: &v1alpha1.CatalogSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "testns",
},
Spec: v1alpha1.CatalogSourceSpec{
SourceType: v1alpha1.SourceTypeGrpc,
Image: "repo/image:tag",
GrpcPodConfig: &v1alpha1.GrpcPodConfig{
NodeSelector: overriddenNodeSelectors,
PriorityClassName: &overriddenPriorityClassName,
Tolerations: overriddenTolerations,
},
},
},
expectedTolerations: overriddenTolerations,
expectedPriorityClassName: overriddenPriorityClassName,
expectedNodeSelectors: overriddenNodeSelectors,
}, {
title: "priorityClassName annotation takes precedence",
perdasilva marked this conversation as resolved.
Show resolved Hide resolved
catalogSource: &v1alpha1.CatalogSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "testns",
},
Spec: v1alpha1.CatalogSourceSpec{
SourceType: v1alpha1.SourceTypeGrpc,
Image: "repo/image:tag",
GrpcPodConfig: &v1alpha1.GrpcPodConfig{
PriorityClassName: &overriddenPriorityClassName,
},
},
},
expectedTolerations: nil,
annotations: map[string]string{
CatalogPriorityClassKey: "some-OTHER-prio-class",
},
expectedPriorityClassName: "some-OTHER-prio-class",
expectedNodeSelectors: defaultNodeSelectors,
},
}

for _, testCase := range testCases {
pod := Pod(testCase.catalogSource, "hello", "busybox", "", map[string]string{}, testCase.annotations, int32(0), int32(0))
require.Equal(t, testCase.expectedNodeSelectors, pod.Spec.NodeSelector)
require.Equal(t, testCase.expectedPriorityClassName, pod.Spec.PriorityClassName)
require.Equal(t, testCase.expectedTolerations, pod.Spec.Tolerations)
}
}
Loading