diff --git a/operator/controllers/execution/scans/parse_reconciler.go b/operator/controllers/execution/scans/parse_reconciler.go index 8b7c4ff242..bd2df9d975 100644 --- a/operator/controllers/execution/scans/parse_reconciler.go +++ b/operator/controllers/execution/scans/parse_reconciler.go @@ -196,6 +196,7 @@ func (r *ScanReconciler) startParser(scan *executionv1.Scan) error { }, }, } + job.Spec.Template.Labels = util.MergeStringMaps(job.Spec.Template.Labels, scan.ObjectMeta.DeepCopy().Labels) // Merge Env from ParserTemplate job.Spec.Template.Spec.Containers[0].Env = append( diff --git a/operator/controllers/execution/scans/scan_reconciler.go b/operator/controllers/execution/scans/scan_reconciler.go index e4a4870b4a..f90c6a8d20 100644 --- a/operator/controllers/execution/scans/scan_reconciler.go +++ b/operator/controllers/execution/scans/scan_reconciler.go @@ -220,6 +220,8 @@ func (r *ScanReconciler) constructJobForScan(scan *executionv1.Scan, scanTypeSpe Spec: *scanTypeSpec.JobTemplate.Spec.DeepCopy(), } + job.Spec.Template.Labels = util.MergeStringMaps(job.Spec.Template.Labels, scan.ObjectMeta.DeepCopy().Labels) + //add recommend kubernetes "managed by" label, to tell the SCB container autodiscovery to ignore the scan pod podLabels := job.Spec.Template.Labels if podLabels == nil { diff --git a/operator/utils/string_maps_merge.go b/operator/utils/string_maps_merge.go new file mode 100644 index 0000000000..41177b9a89 --- /dev/null +++ b/operator/utils/string_maps_merge.go @@ -0,0 +1,15 @@ +// SPDX-FileCopyrightText: the secureCodeBox authors +// +// SPDX-License-Identifier: Apache-2.0 + +package utils + +func MergeStringMaps(maps ...map[string]string) map[string]string { + result := map[string]string{} + for _, m := range maps { + for key, value := range m { + result[key] = value + } + } + return result +} diff --git a/operator/utils/string_maps_merge_test.go b/operator/utils/string_maps_merge_test.go new file mode 100644 index 0000000000..1cddf23c3e --- /dev/null +++ b/operator/utils/string_maps_merge_test.go @@ -0,0 +1,34 @@ +// SPDX-FileCopyrightText: the secureCodeBox authors +// +// SPDX-License-Identifier: Apache-2.0 + +package utils + +import ( + "fmt" + "reflect" + "testing" +) + +type testDataMaps struct { + inOne map[string]string + inTwo map[string]string + out map[string]string +} + +func TestStringMapsMerge(t *testing.T) { + var tests = []testDataMaps{ + { + inOne: map[string]string{"foo": "1", "bar": "2"}, + inTwo: map[string]string{"x": "3", "y": "4"}, + out: map[string]string{"foo": "1", "bar": "2", "x": "3", "y": "4"}, + }, + } + + for _, test := range tests { + actual := MergeStringMaps(test.inOne, test.inTwo) + if !reflect.DeepEqual(actual, test.out) { + t.Error(fmt.Errorf("mergeStringMaps(\"%s\", \"%s\") returned \"%s\", expected \"%s\"", test.inOne, test.inTwo, actual, test.out)) + } + } +}