-
Notifications
You must be signed in to change notification settings - Fork 20
fix: fix the validation webhook #359
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,141 @@ | ||||||||||||||||||
| package membercluster | ||||||||||||||||||
|
|
||||||||||||||||||
| import ( | ||||||||||||||||||
| "context" | ||||||||||||||||||
| "encoding/json" | ||||||||||||||||||
| "fmt" | ||||||||||||||||||
| "strings" | ||||||||||||||||||
| "testing" | ||||||||||||||||||
|
|
||||||||||||||||||
| admissionv1 "k8s.io/api/admission/v1" | ||||||||||||||||||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||||||||||||||||
| "k8s.io/apimachinery/pkg/runtime" | ||||||||||||||||||
| "k8s.io/apimachinery/pkg/types" | ||||||||||||||||||
|
|
||||||||||||||||||
| clusterv1beta1 "github.com/kubefleet-dev/kubefleet/apis/cluster/v1beta1" | ||||||||||||||||||
| "github.com/kubefleet-dev/kubefleet/pkg/utils" | ||||||||||||||||||
|
|
||||||||||||||||||
| fleetnetworkingv1alpha1 "go.goms.io/fleet-networking/api/v1alpha1" | ||||||||||||||||||
|
|
||||||||||||||||||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||||||||||||||||||
| "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||||||||||||||||||
| "sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| func TestHandleDeleteServiceExportValidation(t *testing.T) { | ||||||||||||||||||
| t.Parallel() | ||||||||||||||||||
|
|
||||||||||||||||||
| testCases := map[string]struct { | ||||||||||||||||||
| networkingEnabled bool | ||||||||||||||||||
| expectAllowed bool | ||||||||||||||||||
| expectedMessageSubstr string | ||||||||||||||||||
| validationMode clusterv1beta1.DeleteValidationMode | ||||||||||||||||||
|
Comment on lines
+29
to
+32
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
prefer "want" instead of expected
https://google.github.io/styleguide/go/decisions#got-before-want |
||||||||||||||||||
| }{ | ||||||||||||||||||
| "networking-disabled-allows-delete": { | ||||||||||||||||||
| networkingEnabled: false, | ||||||||||||||||||
| expectAllowed: true, | ||||||||||||||||||
| validationMode: clusterv1beta1.DeleteValidationModeStrict, | ||||||||||||||||||
| }, | ||||||||||||||||||
| "networking-enabled-denies-delete": { | ||||||||||||||||||
| networkingEnabled: true, | ||||||||||||||||||
| expectAllowed: false, | ||||||||||||||||||
| validationMode: clusterv1beta1.DeleteValidationModeStrict, | ||||||||||||||||||
| expectedMessageSubstr: "Please delete serviceExport", | ||||||||||||||||||
| }, | ||||||||||||||||||
| "delete-options-skip-bypasses-validation": { | ||||||||||||||||||
| networkingEnabled: true, | ||||||||||||||||||
| expectAllowed: true, | ||||||||||||||||||
| validationMode: clusterv1beta1.DeleteValidationModeSkip, | ||||||||||||||||||
| }, | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| for name, tc := range testCases { | ||||||||||||||||||
| tc := tc | ||||||||||||||||||
| t.Run(name, func(t *testing.T) { | ||||||||||||||||||
| t.Parallel() | ||||||||||||||||||
|
|
||||||||||||||||||
| mcName := fmt.Sprintf("member-%s", name) | ||||||||||||||||||
| namespaceName := fmt.Sprintf(utils.NamespaceNameFormat, mcName) | ||||||||||||||||||
| svcExport := newInternalServiceExport(mcName, namespaceName) | ||||||||||||||||||
|
|
||||||||||||||||||
| validator := newMemberClusterValidatorForTest(t, tc.networkingEnabled, svcExport) | ||||||||||||||||||
| mc := &clusterv1beta1.MemberCluster{ObjectMeta: metav1.ObjectMeta{Name: mcName}} | ||||||||||||||||||
| mc.Spec.DeleteOptions = &clusterv1beta1.DeleteOptions{ValidationMode: tc.validationMode} | ||||||||||||||||||
| req := buildDeleteRequestFromObject(t, mc) | ||||||||||||||||||
|
|
||||||||||||||||||
| resp := validator.Handle(context.Background(), req) | ||||||||||||||||||
| if resp.Allowed != tc.expectAllowed { | ||||||||||||||||||
| t.Fatalf("expected allowed=%t but got response: %+v", tc.expectAllowed, resp) | ||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Test outputs should output the actual value that the function returned before printing the value that was expected. A usual format for printing test outputs is “YourFunc(%v) = %v, want %v”. |
||||||||||||||||||
| } | ||||||||||||||||||
| if tc.expectedMessageSubstr != "" { | ||||||||||||||||||
| if resp.Result == nil || !strings.Contains(resp.Result.Message, tc.expectedMessageSubstr) { | ||||||||||||||||||
| t.Fatalf("expected message to contain %q but got: %v", tc.expectedMessageSubstr, resp.Result) | ||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| }) | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| func newMemberClusterValidatorForTest(t *testing.T, networkingEnabled bool, objs ...client.Object) *memberClusterValidator { | ||||||||||||||||||
| t.Helper() | ||||||||||||||||||
|
|
||||||||||||||||||
| scheme := runtime.NewScheme() | ||||||||||||||||||
| if err := clusterv1beta1.AddToScheme(scheme); err != nil { | ||||||||||||||||||
| t.Fatalf("failed to add member cluster scheme: %v", err) | ||||||||||||||||||
| } | ||||||||||||||||||
| if err := fleetnetworkingv1alpha1.AddToScheme(scheme); err != nil { | ||||||||||||||||||
| t.Fatalf("failed to add fleet networking scheme: %v", err) | ||||||||||||||||||
| } | ||||||||||||||||||
| scheme.AddKnownTypes(fleetnetworkingv1alpha1.GroupVersion, | ||||||||||||||||||
| &fleetnetworkingv1alpha1.InternalServiceExport{}, | ||||||||||||||||||
| &fleetnetworkingv1alpha1.InternalServiceExportList{}, | ||||||||||||||||||
| ) | ||||||||||||||||||
| metav1.AddToGroupVersion(scheme, fleetnetworkingv1alpha1.GroupVersion) | ||||||||||||||||||
|
|
||||||||||||||||||
| fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build() | ||||||||||||||||||
| decoder := admission.NewDecoder(scheme) | ||||||||||||||||||
|
|
||||||||||||||||||
| return &memberClusterValidator{ | ||||||||||||||||||
| client: fakeClient, | ||||||||||||||||||
| decoder: decoder, | ||||||||||||||||||
| networkingAgentsEnabled: networkingEnabled, | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| func buildDeleteRequestFromObject(t *testing.T, mc *clusterv1beta1.MemberCluster) admission.Request { | ||||||||||||||||||
| t.Helper() | ||||||||||||||||||
|
|
||||||||||||||||||
| raw, err := json.Marshal(mc) | ||||||||||||||||||
| if err != nil { | ||||||||||||||||||
| t.Fatalf("failed to marshal member cluster: %v", err) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| return admission.Request{ | ||||||||||||||||||
| AdmissionRequest: admissionv1.AdmissionRequest{ | ||||||||||||||||||
| Operation: admissionv1.Delete, | ||||||||||||||||||
| Name: mc.Name, | ||||||||||||||||||
| OldObject: runtime.RawExtension{Raw: raw}, | ||||||||||||||||||
| }, | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| func newInternalServiceExport(clusterID, namespace string) *fleetnetworkingv1alpha1.InternalServiceExport { | ||||||||||||||||||
| return &fleetnetworkingv1alpha1.InternalServiceExport{ | ||||||||||||||||||
| ObjectMeta: metav1.ObjectMeta{ | ||||||||||||||||||
| Name: "sample-service", | ||||||||||||||||||
| Namespace: namespace, | ||||||||||||||||||
| }, | ||||||||||||||||||
| Spec: fleetnetworkingv1alpha1.InternalServiceExportSpec{ | ||||||||||||||||||
| ServiceReference: fleetnetworkingv1alpha1.ExportedObjectReference{ | ||||||||||||||||||
| ClusterID: clusterID, | ||||||||||||||||||
| Kind: "Service", | ||||||||||||||||||
| Namespace: "work", | ||||||||||||||||||
| Name: "sample-service", | ||||||||||||||||||
| ResourceVersion: "1", | ||||||||||||||||||
| Generation: 1, | ||||||||||||||||||
| UID: types.UID("svc-uid"), | ||||||||||||||||||
| NamespacedName: "work/sample-service", | ||||||||||||||||||
| }, | ||||||||||||||||||
| }, | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit