Skip to content

Commit 5bbffb8

Browse files
author
Nont
committed
Add logs and e2e test cases
Signed-off-by: Nont <nont@duck.com>
1 parent 140603d commit 5bbffb8

4 files changed

Lines changed: 61 additions & 12 deletions

File tree

cmd/hubagent/main.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,6 @@ func main() {
196196
exitWithErrorFunc()
197197
}
198198

199-
if err := managedresource.EnsureVAP(ctx, mgr.GetClient(), true); err != nil {
200-
klog.Errorf("unable to create managed resource validating admission policy: %s", err)
201-
exitWithErrorFunc()
202-
}
203-
204199
// +kubebuilder:scaffold:builder
205200

206201
wg.Add(1)
@@ -216,6 +211,18 @@ func main() {
216211
klog.InfoS("The controller manager has exited")
217212
}()
218213

214+
// Wait for the cache to sync before creating managed resources
215+
if !mgr.GetCache().WaitForCacheSync(ctx) {
216+
klog.Error("failed to wait for cache sync")
217+
exitWithErrorFunc()
218+
}
219+
220+
if err := managedresource.EnsureVAP(ctx, mgr.GetClient(), true); err != nil {
221+
klog.Errorf("unable to create managed resource validating admission policy: %s", err)
222+
exitWithErrorFunc()
223+
}
224+
klog.Info("managed resource validating admission policy is successfully setup")
225+
219226
// Wait for the controller manager and the scheduler to exit.
220227
wg.Wait()
221228
}

pkg/controllers/internalmembercluster/v1beta1/member_controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
217217
if err := managedresource.EnsureVAP(ctx, r.memberClient, false); err != nil {
218218
return ctrl.Result{}, err
219219
}
220+
klog.V(2).InfoS("Successfully installed managed resource validating admission policy")
220221
if err := r.startAgents(ctx, &imc); err != nil {
221222
return ctrl.Result{}, err
222223
}
@@ -249,6 +250,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
249250
if err := managedresource.EnsureNoVAP(ctx, r.memberClient, false); err != nil {
250251
return ctrl.Result{}, err
251252
}
253+
klog.V(2).InfoS("Successfully uninstalled managed resource validating admission policy")
252254
if err := r.stopAgents(ctx, &imc); err != nil {
253255
return ctrl.Result{}, err
254256
}

test/e2e/managed_resource_vap_test.go

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131
"k8s.io/apimachinery/pkg/types"
32+
"sigs.k8s.io/controller-runtime/pkg/client"
3233

3334
placementv1beta1 "go.goms.io/fleet/apis/placement/v1beta1"
3435
)
@@ -126,14 +127,43 @@ func expectDeniedByVAP(err error) {
126127
), "Error should indicate policy violation")
127128
}
128129

129-
var _ = Describe("ValidatingAdmissionPolicy for Managed Resources", Label("managedresource"), Ordered, func() {
130-
It("The VAP and its binding should exist", func() {
131-
var vap admissionregistrationv1.ValidatingAdmissionPolicy
132-
Expect(sysMastersClient.Get(ctx, types.NamespacedName{Name: vapName}, &vap)).Should(Succeed(), "ValidatingAdmissionPolicy should be installed")
130+
func checkVAPAndBindingExistence(client client.Client) {
131+
By(fmt.Sprintf("Checking existence of ValidatingAdmissionPolicy: %s", vapName))
132+
var vap admissionregistrationv1.ValidatingAdmissionPolicy
133+
Expect(client.Get(ctx, types.NamespacedName{Name: vapName}, &vap)).To(Succeed(),
134+
fmt.Sprintf("ValidatingAdmissionPolicy %s should exist", vapName))
133135

134-
var vapBinding admissionregistrationv1.ValidatingAdmissionPolicyBinding
135-
Expect(sysMastersClient.Get(ctx, types.NamespacedName{Name: vapName}, &vapBinding)).Should(Succeed(), "ValidatingAdmissionPolicyBinding should be installed")
136-
})
136+
By(fmt.Sprintf("Checking existence of ValidatingAdmissionPolicyBinding: %s", vapName))
137+
var vapBinding admissionregistrationv1.ValidatingAdmissionPolicyBinding
138+
Expect(client.Get(ctx, types.NamespacedName{Name: vapName}, &vapBinding)).To(Succeed(),
139+
fmt.Sprintf("ValidatingAdmissionPolicyBinding %s should exist", vapName))
140+
141+
By("Verifying VAP has validations configured")
142+
Expect(vap.Spec.Validations).ToNot(BeEmpty(), "VAP should have validation rules")
143+
144+
By("Verifying VAP binding references the correct policy")
145+
Expect(vapBinding.Spec.PolicyName).To(Equal(vapName),
146+
fmt.Sprintf("VAP binding should reference policy %s", vapName))
147+
148+
By("Verifying VAP binding has validation actions configured")
149+
Expect(vapBinding.Spec.ValidationActions).ToNot(BeEmpty(), "VAP binding should have validation actions")
150+
}
151+
152+
func checkVAPAndBindingAbsence(client client.Client) {
153+
By(fmt.Sprintf("Checking absence of ValidatingAdmissionPolicy: %s", vapName))
154+
var vap admissionregistrationv1.ValidatingAdmissionPolicy
155+
err := client.Get(ctx, types.NamespacedName{Name: vapName}, &vap)
156+
Expect(k8sErrors.IsNotFound(err)).To(BeTrue(),
157+
fmt.Sprintf("ValidatingAdmissionPolicy %s should not exist", vapName))
158+
159+
By(fmt.Sprintf("Checking absence of ValidatingAdmissionPolicyBinding: %s", vapName))
160+
var vapBinding admissionregistrationv1.ValidatingAdmissionPolicyBinding
161+
err = client.Get(ctx, types.NamespacedName{Name: vapName}, &vapBinding)
162+
Expect(k8sErrors.IsNotFound(err)).To(BeTrue(),
163+
fmt.Sprintf("ValidatingAdmissionPolicyBinding %s should not exist", vapName))
164+
}
165+
166+
var _ = Describe("ValidatingAdmissionPolicy for Managed Resources", Label("managedresource"), Ordered, func() {
137167

138168
It("should allow operations on unmanaged namespace for non-system:masters user", func() {
139169
unmanagedNS := createUnmanagedNamespace("test-unmanaged-ns")

test/e2e/setup_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,9 @@ func beforeSuiteForAllProcesses() {
380380
_, err := os.Stat(fleetBinaryPath)
381381
Expect(os.IsNotExist(err)).To(BeFalse(), fmt.Sprintf("kubectl-fleet binary not found at %s", fleetBinaryPath))
382382
})
383+
384+
// Expect that the managedResource VAP and its binding to exist when hub starts
385+
checkVAPAndBindingExistence(hubClient)
383386
}
384387

385388
func maxDuration(a, b time.Duration) time.Duration {
@@ -394,6 +397,10 @@ func beforeSuiteForProcess1() {
394397

395398
setAllMemberClustersToJoin()
396399
checkIfAllMemberClustersHaveJoined()
400+
for _, c := range allMemberClusters {
401+
checkVAPAndBindingExistence(c.KubeClient)
402+
}
403+
397404
checkIfAzurePropertyProviderIsWorking()
398405

399406
// Simulate that member cluster 4 become unhealthy, and member cluster 5 has left the fleet.
@@ -411,5 +418,8 @@ var _ = SynchronizedAfterSuite(func() {}, func() {
411418
deleteTestResourceCRD()
412419
setAllMemberClustersToLeave()
413420
checkIfAllMemberClustersHaveLeft()
421+
for _, c := range allMemberClusters {
422+
checkVAPAndBindingAbsence(c.KubeClient)
423+
}
414424
cleanupInvalidClusters()
415425
})

0 commit comments

Comments
 (0)