|
| 1 | +package lineagecontrollerwebhook |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + admissionv1 "k8s.io/api/admission/v1" |
| 10 | + "k8s.io/apimachinery/pkg/api/errors" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/apimachinery/pkg/runtime" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/client/interceptor" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 17 | + |
| 18 | + cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1" |
| 19 | +) |
| 20 | + |
| 21 | +func appDef(name, kind string) *cozyv1alpha1.ApplicationDefinition { |
| 22 | + d := &cozyv1alpha1.ApplicationDefinition{ |
| 23 | + TypeMeta: metav1.TypeMeta{APIVersion: "cozystack.io/v1alpha1", Kind: "ApplicationDefinition"}, |
| 24 | + ObjectMeta: metav1.ObjectMeta{Name: name}, |
| 25 | + } |
| 26 | + d.Spec.Application.Kind = kind |
| 27 | + return d |
| 28 | +} |
| 29 | + |
| 30 | +func admissionReq(t *testing.T, op admissionv1.Operation, obj, old *cozyv1alpha1.ApplicationDefinition) admission.Request { |
| 31 | + t.Helper() |
| 32 | + raw, err := json.Marshal(obj) |
| 33 | + if err != nil { |
| 34 | + t.Fatal(err) |
| 35 | + } |
| 36 | + req := admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{Operation: op, Name: obj.Name, Object: runtime.RawExtension{Raw: raw}}} |
| 37 | + if old != nil { |
| 38 | + oldRaw, err := json.Marshal(old) |
| 39 | + if err != nil { |
| 40 | + t.Fatal(err) |
| 41 | + } |
| 42 | + req.OldObject = runtime.RawExtension{Raw: oldRaw} |
| 43 | + } |
| 44 | + return req |
| 45 | +} |
| 46 | + |
| 47 | +func TestAppDefKindValidator(t *testing.T) { |
| 48 | + scheme := newWebhookScheme(t) |
| 49 | + existing := appDef("redis", "Redis") |
| 50 | + cl := fake.NewClientBuilder().WithScheme(scheme).WithObjects(existing, appDef("shadow", "Redis")).Build() |
| 51 | + v := NewApplicationDefinitionKindValidator(cl, scheme) |
| 52 | + |
| 53 | + cases := []struct { |
| 54 | + name string |
| 55 | + req admission.Request |
| 56 | + allowed bool |
| 57 | + }{ |
| 58 | + {"create with a kind another definition owns is refused", admissionReq(t, admissionv1.Create, appDef("tap-redis", "Redis"), nil), false}, |
| 59 | + {"create with a free kind is allowed", admissionReq(t, admissionv1.Create, appDef("pg", "Postgres"), nil), true}, |
| 60 | + {"update of the owner itself is allowed", admissionReq(t, admissionv1.Update, appDef("redis", "Redis"), appDef("redis", "Redis")), true}, |
| 61 | + // A duplicate that predates the check keeps upgrading; ownership, not |
| 62 | + // admission, keeps it from managing the kind's releases. |
| 63 | + {"update of a pre-existing duplicate that keeps its kind is allowed", admissionReq(t, admissionv1.Update, appDef("shadow", "Redis"), appDef("shadow", "Redis")), true}, |
| 64 | + {"update switching to a claimed kind is refused", admissionReq(t, admissionv1.Update, appDef("pg", "Redis"), appDef("pg", "Postgres")), false}, |
| 65 | + } |
| 66 | + for _, tc := range cases { |
| 67 | + t.Run(tc.name, func(t *testing.T) { |
| 68 | + resp := v.Handle(context.TODO(), tc.req) |
| 69 | + if resp.Allowed != tc.allowed { |
| 70 | + t.Fatalf("allowed = %v, want %v (%v)", resp.Allowed, tc.allowed, resp.Result) |
| 71 | + } |
| 72 | + }) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func helmOwned(d *cozyv1alpha1.ApplicationDefinition, release string) *cozyv1alpha1.ApplicationDefinition { |
| 77 | + d.Annotations = map[string]string{"meta.helm.sh/release-name": release, "meta.helm.sh/release-namespace": "cozy-system"} |
| 78 | + return d |
| 79 | +} |
| 80 | + |
| 81 | +// A Helm release that renames its definition object creates the new one before |
| 82 | +// it deletes the old one, so both briefly declare the kind. Refusing that would |
| 83 | +// fail the platform upgrade that carries the rename. |
| 84 | +func TestAppDefKindValidatorAllowsRenameWithinOneHelmRelease(t *testing.T) { |
| 85 | + scheme := newWebhookScheme(t) |
| 86 | + cl := fake.NewClientBuilder().WithScheme(scheme).WithObjects(helmOwned(appDef("redis", "Redis"), "redis-rd")).Build() |
| 87 | + v := NewApplicationDefinitionKindValidator(cl, scheme) |
| 88 | + |
| 89 | + if resp := v.Handle(context.TODO(), admissionReq(t, admissionv1.Create, helmOwned(appDef("redis-v2", "Redis"), "redis-rd"), nil)); !resp.Allowed { |
| 90 | + t.Fatalf("rename within one Helm release refused: %v", resp.Result) |
| 91 | + } |
| 92 | + resp := v.Handle(context.TODO(), admissionReq(t, admissionv1.Create, helmOwned(appDef("tap-redis", "Redis"), "tap-redis-rd"), nil)) |
| 93 | + if resp.Allowed { |
| 94 | + t.Fatal("a definition from another Helm release must still be refused") |
| 95 | + } |
| 96 | + otherNS := helmOwned(appDef("redis-elsewhere", "Redis"), "redis-rd") |
| 97 | + otherNS.Annotations["meta.helm.sh/release-namespace"] = "tenant-foo" |
| 98 | + if resp := v.Handle(context.TODO(), admissionReq(t, admissionv1.Create, otherNS, nil)); resp.Allowed { |
| 99 | + t.Fatal("a release of the same name in another namespace is a different release and must be refused") |
| 100 | + } |
| 101 | + if !strings.Contains(resp.Result.Message, "redis") { |
| 102 | + t.Fatalf("the refusal must name the definition that declares the kind, got %q", resp.Result.Message) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// The check is feedback, not the guarantee, so failing to read the existing |
| 107 | +// definitions must let the request through rather than refuse it: a 500 answer |
| 108 | +// is a verdict, which failurePolicy Ignore does not cover. |
| 109 | +func TestAppDefKindValidatorFailsOpenOnListError(t *testing.T) { |
| 110 | + scheme := newWebhookScheme(t) |
| 111 | + cl := interceptor.NewClient(fake.NewClientBuilder().WithScheme(scheme).Build(), interceptor.Funcs{ |
| 112 | + List: func(context.Context, client.WithWatch, client.ObjectList, ...client.ListOption) error { |
| 113 | + return errors.NewServiceUnavailable("cache not started") |
| 114 | + }, |
| 115 | + }) |
| 116 | + v := NewApplicationDefinitionKindValidator(cl, scheme) |
| 117 | + resp := v.Handle(context.TODO(), admissionReq(t, admissionv1.Create, appDef("tap-redis", "Redis"), nil)) |
| 118 | + if !resp.Allowed { |
| 119 | + t.Fatalf("a failed read must not refuse the request: %v", resp.Result) |
| 120 | + } |
| 121 | + if len(resp.Warnings) == 0 { |
| 122 | + t.Fatal("letting a request through unchecked must say so in a warning") |
| 123 | + } |
| 124 | +} |
0 commit comments