Skip to content

Commit da8c1ea

Browse files
committed
fix(lineage-webhook): keep a rendered false only on create
The cacert controller re-runs admission after a selector change by dropping managed-by-cozystack and keeping the other labels, so the UPDATE carries the "false" this webhook wrote on an earlier admission. Reading that as the chart's narrowing froze the old verdict: a projection, once revoked, could not be granted again. The chart's narrowing only matters on CREATE, since an existing Secret already carries managed-by-cozystack and skips the webhook. Assisted-by: LLM Signed-off-by: Timofei Larkin <[email protected]>
1 parent c8c1af4 commit da8c1ea

2 files changed

Lines changed: 48 additions & 11 deletions

File tree

‎internal/lineagecontrollerwebhook/webhook.go‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
"github.com/cozystack/cozystack/pkg/lineage"
12+
admissionv1 "k8s.io/api/admission/v1"
1213
corev1 "k8s.io/api/core/v1"
1314
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1415
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -123,7 +124,7 @@ func (h *LineageControllerWebhook) Handle(ctx context.Context, req admission.Req
123124
logger.Error(err, "error computing lineage labels")
124125
return admission.Errored(500, fmt.Errorf("error computing lineage labels: %w", err))
125126
}
126-
labels, err := h.computeLabels(ctx, obj, owner)
127+
labels, err := h.computeLabels(ctx, obj, owner, req.Operation)
127128
if err != nil {
128129
logger.Error(err, "error computing lineage labels")
129130
return admission.Errored(500, fmt.Errorf("error computing lineage labels: %w", err))
@@ -159,7 +160,7 @@ func (h *LineageControllerWebhook) getOwner(ctx context.Context, o *unstructured
159160
return obj, err
160161
}
161162

162-
func (h *LineageControllerWebhook) computeLabels(ctx context.Context, obj *unstructured.Unstructured, owner *unstructured.Unstructured) (map[string]string, error) {
163+
func (h *LineageControllerWebhook) computeLabels(ctx context.Context, obj *unstructured.Unstructured, owner *unstructured.Unstructured, op admissionv1.Operation) (map[string]string, error) {
163164
if owner == nil {
164165
return nil, nil
165166
}
@@ -194,10 +195,13 @@ func (h *LineageControllerWebhook) computeLabels(ctx context.Context, obj *unstr
194195
resourceSelectors := h.getResourceSelectors(obj.GroupVersionKind().GroupKind(), crd)
195196

196197
selected := matchResourceToExcludeInclude(ctx, obj.GetName(), templateLabels, obj.GetLabels(), resourceSelectors)
197-
// The object may narrow the verdict and never widen it: a rendered "false"
198-
// is kept even where the definition selects the name, a rendered "true" is
199-
// replaced by the computed verdict.
200-
if obj.GetLabels()[corev1alpha1.TenantResourceLabelKey] == "false" {
198+
// On CREATE the object may narrow the verdict and never widen it: a
199+
// rendered "false" is kept even where the definition selects the name, a
200+
// rendered "true" is replaced. Only on CREATE: a re-admission (UPDATE with
201+
// managed-by-cozystack dropped, as the cacert controller does on a selector
202+
// change) carries the "false" this webhook wrote earlier, and keeping it
203+
// would block a grant.
204+
if op == admissionv1.Create && obj.GetLabels()[corev1alpha1.TenantResourceLabelKey] == "false" {
201205
selected = false
202206
}
203207
labels[corev1alpha1.TenantResourceLabelKey] = "false"

‎internal/lineagecontrollerwebhook/webhook_tenantresource_test.go‎

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ import (
1616
)
1717

1818
// The tenantresource verdict is the definition's, but an object may narrow it
19-
// and never widen it: a rendered "false" survives a selector that matches the
20-
// name, a rendered "true" does not survive one that does not.
19+
// on CREATE and never widen it: a rendered "false" survives a selector that
20+
// matches the name, a rendered "true" does not survive one that does not. A
21+
// re-admission UPDATE gets the definition's verdict whatever the label says.
2122
func TestHandle_TenantResourceLabelNarrowsOnly(t *testing.T) {
2223
tenantResourcePath := "/metadata/labels/" + strings.ReplaceAll(corev1alpha1.TenantResourceLabelKey, "/", "~1")
2324

2425
tests := []struct {
2526
name string
27+
op admissionv1.Operation
2628
secret string
2729
rendered string
2830
// want is the value the label must hold once the patch is applied;
@@ -31,22 +33,41 @@ func TestHandle_TenantResourceLabelNarrowsOnly(t *testing.T) {
3133
}{
3234
{
3335
name: "a rendered false is kept where the definition selects the name",
36+
op: admissionv1.Create,
3437
secret: "harbor-demo-credentials",
3538
rendered: "false",
3639
want: "",
3740
},
3841
{
3942
name: "a rendered true is overwritten where the definition does not select the name",
43+
op: admissionv1.Create,
4044
secret: "harbor-demo-other",
4145
rendered: "true",
4246
want: "false",
4347
},
4448
{
4549
name: "no rendered label gets the definition's verdict",
50+
op: admissionv1.Create,
4651
secret: "harbor-demo-credentials",
4752
rendered: "",
4853
want: "true",
4954
},
55+
{
56+
// The cacert controller's re-admission after a selector change:
57+
// managed-by-cozystack dropped, the old verdict still on the object.
58+
name: "a re-admission grants where the definition now selects the name",
59+
op: admissionv1.Update,
60+
secret: "harbor-demo-credentials",
61+
rendered: "false",
62+
want: "true",
63+
},
64+
{
65+
name: "a re-admission revokes where the definition no longer selects the name",
66+
op: admissionv1.Update,
67+
secret: "harbor-demo-other",
68+
rendered: "true",
69+
want: "false",
70+
},
5071
}
5172

5273
for _, tt := range tests {
@@ -78,13 +99,25 @@ func TestHandle_TenantResourceLabelNarrowsOnly(t *testing.T) {
7899
t.Fatalf("marshal object: %v", err)
79100
}
80101

81-
resp := w.Handle(context.Background(), admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{
102+
req := admissionv1.AdmissionRequest{
82103
Kind: metav1.GroupVersionKind{Version: "v1", Kind: "Secret"},
83104
Namespace: "tenant-demo",
84105
Name: tt.secret,
85-
Operation: admissionv1.Create,
106+
Operation: tt.op,
86107
Object: runtime.RawExtension{Raw: raw},
87-
}})
108+
}
109+
if tt.op == admissionv1.Update {
110+
old := obj.DeepCopy()
111+
oldLabels := old.GetLabels()
112+
oldLabels[ManagedObjectKey] = "true"
113+
old.SetLabels(oldLabels)
114+
oldRaw, err := json.Marshal(old)
115+
if err != nil {
116+
t.Fatalf("marshal old object: %v", err)
117+
}
118+
req.OldObject = runtime.RawExtension{Raw: oldRaw}
119+
}
120+
resp := w.Handle(context.Background(), admission.Request{AdmissionRequest: req})
88121
if !resp.Allowed {
89122
t.Fatalf("expected allowed response, got %+v", resp.Result)
90123
}

0 commit comments

Comments
 (0)