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

fix: app names with non-alphanumeric characters in position 63 break syncs (issue #18237) #18256

Merged
merged 3 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions util/argo/resource_tracking.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package argo

import (
"fmt"
"regexp"
"strings"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -155,6 +156,13 @@ func (rt *resourceTracking) SetAppInstance(un *unstructured.Unstructured, key, v
}
if len(val) > LabelMaxLength {
val = val[:LabelMaxLength]
// Prevent errors if the truncated name ends in a special character.
// See https://github.com/argoproj/argo-cd/issues/18237.
okEndPattern := regexp.MustCompile("[a-zA-Z0-9]$")
RobinsonZ marked this conversation as resolved.
Show resolved Hide resolved

for !okEndPattern.MatchString(val) {
RobinsonZ marked this conversation as resolved.
Show resolved Hide resolved
val = val[:len(val)-1]
}
}
err = argokube.SetAppInstanceLabel(un, key, val)
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions util/argo/resource_tracking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,46 @@ func TestSetAppInstanceAnnotationAndLabel(t *testing.T) {
assert.Equal(t, "my-app", app)
}

func TestSetAppInstanceAnnotationAndLabelLongName(t *testing.T) {
yamlBytes, err := os.ReadFile("testdata/svc.yaml")
assert.Nil(t, err)
var obj unstructured.Unstructured
err = yaml.Unmarshal(yamlBytes, &obj)
assert.Nil(t, err)

resourceTracking := NewResourceTracking()

err = resourceTracking.SetAppInstance(&obj, common.LabelKeyAppInstance, "my-app-with-an-extremely-long-name-that-is-over-sixty-three-characters", "", TrackingMethodAnnotationAndLabel)
assert.Nil(t, err)

// the annotation should still work, so the name from GetAppName should not be truncated
app := resourceTracking.GetAppName(&obj, common.LabelKeyAppInstance, TrackingMethodAnnotationAndLabel)
assert.Equal(t, "my-app-with-an-extremely-long-name-that-is-over-sixty-three-characters", app)

// the label should be truncated to 63 characters
assert.Equal(t, obj.GetLabels()[common.LabelKeyAppInstance], "my-app-with-an-extremely-long-name-that-is-over-sixty-three-cha")
}

func TestSetAppInstanceAnnotationAndLabelLongNameBadEnding(t *testing.T) {
yamlBytes, err := os.ReadFile("testdata/svc.yaml")
assert.Nil(t, err)
var obj unstructured.Unstructured
err = yaml.Unmarshal(yamlBytes, &obj)
assert.Nil(t, err)

resourceTracking := NewResourceTracking()

err = resourceTracking.SetAppInstance(&obj, common.LabelKeyAppInstance, "the-very-suspicious-name-with-precisely-sixty-three-characters-with-hyphen", "", TrackingMethodAnnotationAndLabel)
assert.Nil(t, err)

// the annotation should still work, so the name from GetAppName should not be truncated
app := resourceTracking.GetAppName(&obj, common.LabelKeyAppInstance, TrackingMethodAnnotationAndLabel)
assert.Equal(t, "the-very-suspicious-name-with-precisely-sixty-three-characters-with-hyphen", app)

// the label should be truncated to 63 characters, AND the hyphen should be removed
assert.Equal(t, obj.GetLabels()[common.LabelKeyAppInstance], "the-very-suspicious-name-with-precisely-sixty-three-characters")
}

func TestSetAppInstanceAnnotationNotFound(t *testing.T) {
yamlBytes, err := os.ReadFile("testdata/svc.yaml")
assert.Nil(t, err)
Expand Down
Loading