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: truncate agent pod annotation so scheduler doesn't fail when annoation limit is exceeded #3314

Merged
merged 11 commits into from
Dec 11, 2024
23 changes: 18 additions & 5 deletions 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 @@ -42,8 +43,20 @@ func parsePod(object []byte) (*corev1.Pod, error) {
return &pod, nil
}

func getImageAnnotationKey(containerName string) string {
return fmt.Sprintf("%s/original-image-%s", annotationPrefix, containerName)
func getImageAnnotationKey(ctx context.Context, containerName string) string {
annotationName := fmt.Sprintf("original-image-%s", containerName)
// 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)
AustinAbro321 marked this conversation as resolved.
Show resolved Hide resolved
return key
}

func mutatePod(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster.Cluster) (*operations.Result, error) {
Expand Down Expand Up @@ -88,7 +101,7 @@ func mutatePod(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster.Clu
if err != nil {
return nil, err
}
updatedAnnotations[getImageAnnotationKey(container.Name)] = container.Image
updatedAnnotations[getImageAnnotationKey(ctx, container.Name)] = container.Image
patches = append(patches, operations.ReplacePatchOperation(path, replacement))
}

Expand All @@ -99,7 +112,7 @@ func mutatePod(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster.Clu
if err != nil {
return nil, err
}
updatedAnnotations[getImageAnnotationKey(container.Name)] = container.Image
updatedAnnotations[getImageAnnotationKey(ctx, container.Name)] = container.Image
patches = append(patches, operations.ReplacePatchOperation(path, replacement))
}

Expand All @@ -110,7 +123,7 @@ func mutatePod(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster.Clu
if err != nil {
return nil, err
}
updatedAnnotations[getImageAnnotationKey(container.Name)] = container.Image
updatedAnnotations[getImageAnnotationKey(ctx, container.Name)] = container.Image
patches = append(patches, operations.ReplacePatchOperation(path, replacement))
}

Expand Down
28 changes: 28 additions & 0 deletions src/internal/agent/hooks/pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,31 @@ func TestPodMutationWebhook(t *testing.T) {
})
}
}
func TestGetImageAnnotationKey(t *testing.T) {
t.Parallel()
tests := []struct {
containerName string
expectedKey string
}{
{
containerName: "nginx",
expectedKey: "zarf.dev/original-image-nginx",
},
{
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",
AustinAbro321 marked this conversation as resolved.
Show resolved Hide resolved
},
{
containerName: "remove-trailing-hyphen----",
expectedKey: "zarf.dev/original-image-remove-trailing-hyphen",
},
}

for _, tt := range tests {
t.Run(tt.containerName, func(t *testing.T) {
t.Parallel()
key := getImageAnnotationKey(context.Background(), tt.containerName)
require.Equal(t, tt.expectedKey, key)
})
}
}
2 changes: 1 addition & 1 deletion src/test/packages/36-health-checks/ready-pod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ spec:
image: ghcr.io/stefanprodan/podinfo:6.4.0
command: ["sh", "-c", "sleep 3"]
containers:
- name: podinfo
- name: ready-pod-with-long-name-to-test-agent-truncate-annotation-name
image: ghcr.io/stefanprodan/podinfo:6.4.0
Loading