Skip to content

Commit

Permalink
Merge pull request #72 from gabemontero/fix-invalid-label
Browse files Browse the repository at this point in the history
Bug 1777337: employ k8s label value validation when creating build pod build label…
  • Loading branch information
openshift-merge-robot committed Feb 18, 2020
2 parents 59ef366 + 4e30adb commit 8f111e6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
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",
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

0 comments on commit 8f111e6

Please sign in to comment.