From 249e18b0a70e5fa2bc8e30a79daac748b9a05bb9 Mon Sep 17 00:00:00 2001 From: chenk Date: Wed, 17 Jan 2024 11:36:28 +0200 Subject: [PATCH] fix: use node-selector config (#283) Signed-off-by: chenk --- pkg/jobs/collector.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/pkg/jobs/collector.go b/pkg/jobs/collector.go index 4e51670..e5b857c 100644 --- a/pkg/jobs/collector.go +++ b/pkg/jobs/collector.go @@ -52,6 +52,7 @@ type jobCollector struct { collectorTimeout time.Duration resourceRequirements *corev1.ResourceRequirements nodeConfig bool + useNodeSelector bool } type CollectorOption func(*jobCollector) @@ -168,6 +169,12 @@ func WithCollectorTimeout(timeout time.Duration) CollectorOption { } } +func WithUseNodeSelector(useNodeSelector bool) CollectorOption { + return func(jc *jobCollector) { + jc.useNodeSelector = useNodeSelector + } +} + func NewCollector( cluster k8s.Cluster, opts ...CollectorOption, @@ -299,14 +306,13 @@ func (jb *jobCollector) ApplyAndCollect(ctx context.Context, nodeName string) (s // Apply deploy k8s job by template to specific node and namespace (for operator use case) func (jb *jobCollector) Apply(ctx context.Context, nodeName string) (*batchv1.Job, error) { - job, err := GetJob( + jobOptions := []JobOption{ WithNamespace(jb.namespace), WithLabels(jb.labels), withPodSecurityContext(jb.podSecurityContext), withSecurityContext(jb.securityContext), WithTolerations(jb.tolerations), WithJobServiceAccount(jb.serviceAccount), - WithNodeSelector(nodeName), WithJobTimeout(jb.collectorTimeout), WithNodeCollectorImageRef(jb.imageRef), WithAnnotation(jb.annotation), @@ -316,8 +322,11 @@ func (jb *jobCollector) Apply(ctx context.Context, nodeName string) (*batchv1.Jo WithContainerVolumeMounts(jb.volumeMounts), WithPriorityClassName(jb.priorityClassName), WithJobName(jb.name), - WithResourceRequirements(jb.resourceRequirements), - ) + WithResourceRequirements(jb.resourceRequirements)} + if jb.useNodeSelector { + jobOptions = append(jobOptions, WithNodeSelector(nodeName)) + } + job, err := GetJob(jobOptions...) if err != nil { return nil, fmt.Errorf("running node-collector job: %w", err) }