Skip to content

Commit

Permalink
fix truncated hypens
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <[email protected]>
  • Loading branch information
AustinAbro321 committed Dec 10, 2024
1 parent 511ea2b commit 880409c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/internal/agent/hooks/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/config/lang"
Expand Down Expand Up @@ -44,12 +45,16 @@ func parsePod(object []byte) (*corev1.Pod, error) {

func getImageAnnotationKey(ctx context.Context, containerName string) string {
annotationName := fmt.Sprintf("original-image-%s", containerName)
// Kubernetes requires all annotation names to be to be no more than 63 characters
// The name segment is required and must be 63 characters or less, beginning and ending with
// an alphanumeric character ([a-z0-9A-Z]) with dashes (-), underscores (_), dots (.), and alphanumerics between.
// https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/#syntax-and-character-set
if len(annotationName) > 63 {
logger.From(ctx).Debug("truncating container name to fit Kubernetes 63 character annotation name limit", "container", containerName)
annotationName = annotationName[:63]
}
// container names follow RFC 1123 which allows only lowercase alphanumeric characters and hyphens
// this ensures we don't end with a hyphen
annotationName = strings.TrimRight(annotationName, "-")
key := fmt.Sprintf("%s/%s", annotationPrefix, annotationName)
return key
}
Expand Down
4 changes: 4 additions & 0 deletions src/internal/agent/hooks/pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ func TestGetImageAnnotationKey(t *testing.T) {
containerName: "a-very-long-container-name-that-exceeds-sixty-three-characters",
expectedKey: "zarf.dev/original-image-a-very-long-container-name-that-exceeds-sixty-th",
},
{
containerName: "remove-trailing-hyphen-",
expectedKey: "zarf.dev/original-image-remove-trailing-hyphen",
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 880409c

Please sign in to comment.