Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.

Commit f384d04

Browse files
committed
#21 Use GeneratedName and trucate names over 58 chars
This lets us add a `-` and 5 random chars to ensure unquire names.
1 parent 3159a4a commit f384d04

File tree

3 files changed

+60
-11
lines changed

3 files changed

+60
-11
lines changed

operator/controllers/execution/scan_controller.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939

4040
"github.com/minio/minio-go/v6"
4141
executionv1 "github.com/secureCodeBox/secureCodeBox-v2-alpha/operator/apis/execution/v1"
42+
util "github.com/secureCodeBox/secureCodeBox-v2-alpha/operator/utils"
4243
)
4344

4445
// ScanReconciler reconciles a Scan object
@@ -399,10 +400,10 @@ func (r *ScanReconciler) startParser(scan *executionv1.Scan) error {
399400
var backOffLimit int32 = 3
400401
job := &batch.Job{
401402
ObjectMeta: metav1.ObjectMeta{
402-
Annotations: make(map[string]string),
403-
Name: fmt.Sprintf("parse-%s", scan.Name),
404-
Namespace: scan.Namespace,
405-
Labels: labels,
403+
Annotations: make(map[string]string),
404+
GenerateName: util.TruncateName(fmt.Sprintf("parse-%s", scan.Name)),
405+
Namespace: scan.Namespace,
406+
Labels: labels,
406407
},
407408
Spec: batch.JobSpec{
408409
BackoffLimit: &backOffLimit,
@@ -516,9 +517,9 @@ func (r *ScanReconciler) constructJobForScan(scan *executionv1.Scan, scanType *e
516517
labels["experimental.securecodebox.io/job-type"] = "scanner"
517518
job := &batch.Job{
518519
ObjectMeta: metav1.ObjectMeta{
519-
Labels: labels,
520-
Name: fmt.Sprintf("scan-%s", scan.Name),
521-
Namespace: scan.Namespace,
520+
Labels: labels,
521+
GenerateName: util.TruncateName(fmt.Sprintf("scan-%s", scan.Name)),
522+
Namespace: scan.Namespace,
522523
},
523524
Spec: *scanType.Spec.JobTemplate.Spec.DeepCopy(),
524525
}
@@ -1000,13 +1001,15 @@ func (r *ScanReconciler) createJobForHook(hook *executionv1.ScanCompletionHook,
10001001
} else if hook.Spec.Type == executionv1.ReadOnly {
10011002
labels["experimental.securecodebox.io/job-type"] = "read-only-hook"
10021003
}
1004+
labels["experimental.securecodebox.io/hook-name"] = hook.Name
1005+
10031006
var backOffLimit int32 = 3
10041007
job := &batch.Job{
10051008
ObjectMeta: metav1.ObjectMeta{
1006-
Annotations: make(map[string]string),
1007-
Name: fmt.Sprintf("%s-%s", hook.Name, scan.Name),
1008-
Namespace: scan.Namespace,
1009-
Labels: labels,
1009+
Annotations: make(map[string]string),
1010+
GenerateName: util.TruncateName(fmt.Sprintf("%s-%s", hook.Name, scan.Name)),
1011+
Namespace: scan.Namespace,
1012+
Labels: labels,
10101013
},
10111014
Spec: batch.JobSpec{
10121015
BackoffLimit: &backOffLimit,

operator/utils/truncatedname.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package utils
2+
3+
import "fmt"
4+
5+
// TruncateName Ensures that the name used for a kubernetes object doesn't exceed the 63 char length limit. This actually cuts of anything after char 57, so that we can use the randomly generated suffix from k8s `generateName`.
6+
func TruncateName(name string) string {
7+
if len(name) >= 57 {
8+
return fmt.Sprintf("%s-", name[0:57])
9+
}
10+
return fmt.Sprintf("%s-", name)
11+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type testData struct {
9+
in string
10+
out string
11+
}
12+
13+
func TestAbc(t *testing.T) {
14+
var tests = []testData{
15+
{
16+
in: "abc",
17+
out: "abc-",
18+
},
19+
{
20+
in: "scan-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
21+
out: "scan-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-",
22+
},
23+
{
24+
in: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
25+
out: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-",
26+
},
27+
}
28+
29+
for _, test := range tests {
30+
actual := TruncateName(test.in)
31+
if actual != test.out {
32+
t.Error(fmt.Errorf("TruncateName(\"%s\") returned \"%s\", expected \"%s\"", test.in, actual, test.out))
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)