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

Container names must end with an alphanumeric character #580

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
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