Skip to content

Commit

Permalink
Container names must end with an alphanumeric character
Browse files Browse the repository at this point in the history
  • Loading branch information
abayer authored and knative-prow-robot committed Mar 5, 2019
1 parent beb5d7b commit 3328a04
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
7 changes: 6 additions & 1 deletion pkg/names/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package names

import (
"fmt"

utilrand "k8s.io/apimachinery/pkg/util/rand"
"regexp"
)

// NameGenerator generates names for objects. Some backends may have more information
Expand Down Expand Up @@ -61,5 +61,10 @@ func (simpleNameGenerator) RestrictLength(base string) string {
if len(base) > maxNameLength {
base = base[:maxNameLength]
}
var isAlphaNumeric = regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString

for !isAlphaNumeric(base[len(base)-1:]) {
base = base[:len(base)-1]
}
return base
}
36 changes: 34 additions & 2 deletions pkg/reconciler/v1alpha1/taskrun/resources/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestMakePod(t *testing.T) {
desc: "very-long-step-name",
b: v1alpha1.BuildSpec{
Steps: []corev1.Container{{
Name: "a-sixty-three-character-step-name-to-trigger-max-length-checkxx",
Name: "a-very-long-character-step-name-to-trigger-max-len----and-invalid-characters",
Image: "image",
}},
},
Expand All @@ -203,7 +203,39 @@ func TestMakePod(t *testing.T) {
WorkingDir: workspaceDir,
}},
Containers: []corev1.Container{{
Name: "build-step-a-sixty-three-character-step-name-to-trigger-max-len",
Name: "build-step-a-very-long-character-step-name-to-trigger-max-len",
Image: "image",
Env: implicitEnvVars,
VolumeMounts: implicitVolumeMounts,
WorkingDir: workspaceDir,
},
nopContainer,
},
Volumes: implicitVolumes,
},
}, {
desc: "step-name-ends-with-non-alphanumeric",
b: v1alpha1.BuildSpec{
Steps: []corev1.Container{{
Name: "ends-with-invalid-%%__$$",
Image: "image",
}},
},
bAnnotations: map[string]string{
"simple-annotation-key": "simple-annotation-val",
},
want: &corev1.PodSpec{
RestartPolicy: corev1.RestartPolicyNever,
InitContainers: []corev1.Container{{
Name: containerPrefix + credsInit + "-9l9zj",
Image: *credsImage,
Args: []string{},
Env: implicitEnvVars,
VolumeMounts: implicitVolumeMounts,
WorkingDir: workspaceDir,
}},
Containers: []corev1.Container{{
Name: "build-step-ends-with-invalid",
Image: "image",
Env: implicitEnvVars,
VolumeMounts: implicitVolumeMounts,
Expand Down

0 comments on commit 3328a04

Please sign in to comment.