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

Bug 1777337: employ k8s label value validation when creating build pod build label… #72

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions pkg/build/apiserver/buildgenerator/generator.go
Expand Up @@ -1006,10 +1006,20 @@ func setBuildAnnotationAndLabel(bcCopy *buildv1.BuildConfig, build *buildv1.Buil
}

func labelValue(name string) string {
if len(name) <= validation.DNS1123LabelMaxLength {
return name
end := len(name)
newName := name
// first, try to truncate from the end to find a valid
// label
for end > 0 {
errStrs := validation.IsDNS1123Label(newName)
if len(errStrs) == 0 {
return newName
}
end--
newName = newName[:end]
}
return name[:validation.DNS1123LabelMaxLength]
klog.Warningf("In creating the value of the build label in the build pod, several attempts at manipulating %s to meet k8s label name requirements failed", name)
return name
}

// mergeMaps will merge to map[string]string instances, with
Expand Down
10 changes: 6 additions & 4 deletions pkg/build/apiserver/buildgenerator/generator_test.go
Expand Up @@ -914,10 +914,12 @@ func TestGenerateBuildFromConfig(t *testing.T) {
strategy := mockDockerStrategyForDockerImage(originalImage, &metav1.GetOptions{})
output := MockOutput()
resources := mockResources()
expectedLabel := "test-build-config-4"
bc := &buildv1.BuildConfig{
ObjectMeta: metav1.ObjectMeta{
UID: "test-uid",
Name: "test-build-config",
UID: "test-uid",
// Specify a name here that we cannot use as-is for a k8s ObjectMeta label
Name: "test-build-config-4.3.0.ipv6-2019-11-27-0001-build",
gabemontero marked this conversation as resolved.
Show resolved Hide resolved
Namespace: metav1.NamespaceDefault,
Labels: map[string]string{"testlabel": "testvalue"},
},
Expand Down Expand Up @@ -974,10 +976,10 @@ func TestGenerateBuildFromConfig(t *testing.T) {
if build.Annotations[buildv1.BuildConfigAnnotation] != bc.Name {
t.Errorf("Build does not contain annotation from BuildConfig")
}
if build.Labels[buildv1.BuildConfigLabel] != bc.Name {
if build.Labels[buildv1.BuildConfigLabel] != expectedLabel {
t.Errorf("Build does not contain labels from BuildConfig")
}
if build.Labels[buildv1.BuildConfigLabelDeprecated] != bc.Name {
if build.Labels[buildv1.BuildConfigLabelDeprecated] != expectedLabel {
t.Errorf("Build does not contain labels from BuildConfig")
}
if build.Status.Config.Name != bc.Name || build.Status.Config.Namespace != bc.Namespace || build.Status.Config.Kind != "BuildConfig" {
Expand Down