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

Allow resource to be mutated without adding crc32 checksum #3436

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions src/internal/agent/hooks/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ package hooks

import "github.com/zarf-dev/zarf/src/internal/agent/operations"

const annotationDisableCRC32 = "zarf.dev/remove-checksum"

func getLabelPatch(currLabels map[string]string) operations.PatchOperation {
if currLabels == nil {
currLabels = make(map[string]string)
}
currLabels["zarf-agent"] = "patched"
return operations.ReplacePatchOperation("/metadata/labels", currLabels)
}

func hasRemoveChecksumAnnotation(annotations map[string]string) bool {
if val, ok := annotations[annotationDisableCRC32]; ok {
return val == "enable"
}
return false
}
12 changes: 11 additions & 1 deletion src/internal/agent/hooks/flux-ocirepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,17 @@ func mutateOCIRepo(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster
patchedURL = fmt.Sprintf("%s:%s", patchedURL, src.Spec.Reference.Tag)
}

patchedSrc, err := transform.ImageTransformHost(registryAddress, patchedURL)
var (
patchedSrc string
err error
)

if hasRemoveChecksumAnnotation(src.Annotations) {
patchedSrc, err = transform.ImageTransformHostWithoutChecksum(registryAddress, patchedURL)
} else {
patchedSrc, err = transform.ImageTransformHost(registryAddress, patchedURL)
}

if err != nil {
return nil, fmt.Errorf("unable to transform the OCIRepo URL: %w", err)
}
Expand Down
76 changes: 76 additions & 0 deletions src/internal/agent/hooks/flux-ocirepo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,82 @@ func TestFluxOCIMutationWebhook(t *testing.T) {
},
code: http.StatusOK,
},
{
name: "url should not include crc32 checksum",
admissionReq: createFluxOCIRepoAdmissionRequest(t, v1.Update, &flux.OCIRepository{
ObjectMeta: metav1.ObjectMeta{
Name: "mutate-this",
Annotations: map[string]string{
"zarf.dev/remove-checksum": "enable",
},
},
Spec: flux.OCIRepositorySpec{
URL: "oci://ghcr.io/stefanprodan/manifests/podinfo",
Reference: &flux.OCIRepositoryRef{
Tag: "6.4.0",
},
},
}),
patch: []operations.PatchOperation{
operations.ReplacePatchOperation(
"/spec/url",
"oci://127.0.0.1:31999/stefanprodan/manifests/podinfo",
),
operations.AddPatchOperation(
"/spec/secretRef",
fluxmeta.LocalObjectReference{Name: config.ZarfImagePullSecretName},
),
operations.ReplacePatchOperation(
"/spec/ref/tag",
"6.4.0",
),
operations.ReplacePatchOperation(
"/metadata/labels",
map[string]string{
"zarf-agent": "patched",
},
),
},
code: http.StatusOK,
},
{
name: "url should include crc32 checksum when annotation is not 'enable'",
admissionReq: createFluxOCIRepoAdmissionRequest(t, v1.Update, &flux.OCIRepository{
ObjectMeta: metav1.ObjectMeta{
Name: "mutate-this",
Annotations: map[string]string{
"zarf.dev/remove-checksum": "test",
},
},
Spec: flux.OCIRepositorySpec{
URL: "oci://ghcr.io/stefanprodan/manifests/podinfo",
Reference: &flux.OCIRepositoryRef{
Tag: "6.4.0",
},
},
}),
patch: []operations.PatchOperation{
operations.ReplacePatchOperation(
"/spec/url",
"oci://127.0.0.1:31999/stefanprodan/manifests/podinfo",
),
operations.AddPatchOperation(
"/spec/secretRef",
fluxmeta.LocalObjectReference{Name: config.ZarfImagePullSecretName},
),
operations.ReplacePatchOperation(
"/spec/ref/tag",
"6.4.0-zarf-2823281104",
),
operations.ReplacePatchOperation(
"/metadata/labels",
map[string]string{
"zarf-agent": "patched",
},
),
},
code: http.StatusOK,
},
}

ctx := context.Background()
Expand Down