@@ -25,36 +25,91 @@ import (
2525 . "github.com/onsi/gomega"
2626 admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
2727 corev1 "k8s.io/api/core/v1"
28+ networkingv1 "k8s.io/api/networking/v1"
2829 k8sErrors "k8s.io/apimachinery/pkg/api/errors"
2930 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3031 "k8s.io/apimachinery/pkg/types"
3132
32- testutils "go.goms.io/fleet/test/e2e/v1alpha1/utils "
33+ placementv1beta1 "go.goms.io/fleet/apis/placement/v1beta1 "
3334)
3435
3536const (
3637 managedByLabel = "fleet.azure.com/managed-by"
3738 managedByLabelValue = "arm"
3839 vapName = "aks-fleet-managed-by-arm"
39- vapBindingName = "aks-fleet-managed-by-arm"
4040)
4141
42+ var managedByLabelMap = map [string ]string {
43+ managedByLabel : managedByLabelValue ,
44+ }
45+
4246// Helper functions for creating managed resources
43- func createManagedNamespace (name string ) * corev1.Namespace {
47+ func createUnmanagedNamespace (name string ) * corev1.Namespace {
4448 return & corev1.Namespace {
4549 ObjectMeta : metav1.ObjectMeta {
4650 Name : name ,
47- Labels : map [string ]string {
48- managedByLabel : managedByLabelValue ,
51+ },
52+ }
53+ }
54+ func createManagedNamespace (name string ) * corev1.Namespace {
55+ ns := createUnmanagedNamespace (name )
56+ ns .Labels = managedByLabelMap
57+ return ns
58+ }
59+
60+ func createManagedResourceQuota (ns , name string ) * corev1.ResourceQuota {
61+ return & corev1.ResourceQuota {
62+ ObjectMeta : metav1.ObjectMeta {
63+ Name : name ,
64+ Namespace : ns ,
65+ Labels : managedByLabelMap ,
66+ },
67+ }
68+ }
69+
70+ func createManagedNetworkPolicy (ns , name string ) * networkingv1.NetworkPolicy {
71+ return & networkingv1.NetworkPolicy {
72+ ObjectMeta : metav1.ObjectMeta {
73+ Name : name ,
74+ Namespace : ns ,
75+ Labels : managedByLabelMap ,
76+ },
77+ }
78+ }
79+
80+ func createManagedCRP (name string ) * placementv1beta1.ClusterResourcePlacement {
81+ return & placementv1beta1.ClusterResourcePlacement {
82+ ObjectMeta : metav1.ObjectMeta {
83+ Name : name ,
84+ Labels : managedByLabelMap ,
85+ },
86+ Spec : placementv1beta1.PlacementSpec {
87+ ResourceSelectors : []placementv1beta1.ResourceSelectorTerm {
88+ {
89+ Group : "" ,
90+ Version : "v1" ,
91+ Kind : "Namespace" ,
92+ },
4993 },
5094 },
5195 }
5296}
5397
54- func createUnmanagedNamespace (name string ) * corev1. Namespace {
55- return & corev1. Namespace {
98+ func createManagedResourcePlacement (name string ) * placementv1beta1. ResourcePlacement {
99+ return & placementv1beta1. ResourcePlacement {
56100 ObjectMeta : metav1.ObjectMeta {
57- Name : name ,
101+ Name : name ,
102+ Namespace : "default" ,
103+ Labels : managedByLabelMap ,
104+ },
105+ Spec : placementv1beta1.PlacementSpec {
106+ ResourceSelectors : []placementv1beta1.ResourceSelectorTerm {
107+ {
108+ Group : "" ,
109+ Version : "v1" ,
110+ Kind : "Pod" ,
111+ },
112+ },
58113 },
59114 }
60115}
@@ -72,12 +127,12 @@ func expectDeniedByVAP(err error) {
72127}
73128
74129var _ = Describe ("ValidatingAdmissionPolicy for Managed Resources" , Label ("managedresource" ), Ordered , func () {
75- BeforeAll ( func () {
130+ It ( "The VAP and its binding should exist" , func () {
76131 var vap admissionregistrationv1.ValidatingAdmissionPolicy
77132 Expect (sysMastersClient .Get (ctx , types.NamespacedName {Name : vapName }, & vap )).Should (Succeed (), "ValidatingAdmissionPolicy should be installed" )
78133
79134 var vapBinding admissionregistrationv1.ValidatingAdmissionPolicyBinding
80- Expect (sysMastersClient .Get (ctx , types.NamespacedName {Name : vapBindingName }, & vapBinding )).Should (Succeed (), "ValidatingAdmissionPolicyBinding should be installed" )
135+ Expect (sysMastersClient .Get (ctx , types.NamespacedName {Name : vapName }, & vapBinding )).Should (Succeed (), "ValidatingAdmissionPolicyBinding should be installed" )
81136 })
82137
83138 It ("should allow operations on unmanaged namespace for non-system:masters user" , func () {
@@ -93,7 +148,7 @@ var _ = Describe("ValidatingAdmissionPolicy for Managed Resources", Label("manag
93148 }
94149 ns .Annotations = map [string ]string {"test" : "annotation" }
95150 return notMasterUser .Update (ctx , & ns )
96- }, testutils . PollTimeout , testutils . PollInterval ).Should (Succeed ())
151+ }, eventuallyDuration , eventuallyInterval ).Should (Succeed ())
97152
98153 By ("expecting successful DELETE operation on unmanaged namespace" )
99154 Expect (notMasterUser .Delete (ctx , unmanagedNS )).To (Succeed ())
@@ -123,7 +178,6 @@ var _ = Describe("ValidatingAdmissionPolicy for Managed Resources", Label("manag
123178 if err != nil {
124179 Expect (k8sErrors .IsNotFound (err )).To (BeTrue ())
125180 }
126-
127181 Expect (sysMastersClient .Create (ctx , managedNS )).To (Succeed ())
128182 var ns corev1.Namespace
129183 err = sysMastersClient .Get (ctx , types.NamespacedName {Name : managedNS .Name }, & ns )
@@ -151,7 +205,7 @@ var _ = Describe("ValidatingAdmissionPolicy for Managed Resources", Label("manag
151205 return updateErr
152206 }
153207 return nil
154- }, testutils . PollTimeout , testutils . PollInterval ).Should (Succeed ())
208+ }, eventuallyDuration , eventuallyInterval ).Should (Succeed ())
155209
156210 expectDeniedByVAP (updateErr )
157211 })
@@ -165,12 +219,76 @@ var _ = Describe("ValidatingAdmissionPolicy for Managed Resources", Label("manag
165219 }
166220 ns .Annotations = map [string ]string {"test" : "annotation" }
167221 By ("expecting denial of UPDATE operation on managed namespace" )
168- updateErr = notMasterUser .Update (ctx , & ns )
222+ updateErr = sysMastersClient .Update (ctx , & ns )
169223 if k8sErrors .IsConflict (updateErr ) {
170224 return updateErr
171225 }
172226 return nil
173- }, testutils .PollTimeout , testutils .PollInterval ).Should (Succeed ())
227+ }, eventuallyDuration , eventuallyInterval ).Should (Succeed ())
228+ })
229+
230+ Context ("For other resources in scope" , func () {
231+ It ("should deny creating managed resource quotas" , func () {
232+ rq := createManagedResourceQuota ("default" , "default" )
233+ err := notMasterUser .Create (ctx , rq )
234+ expectDeniedByVAP (err )
235+ })
236+
237+ It ("should deny creating managed network policy" , func () {
238+ np := createManagedNetworkPolicy ("default" , "default" )
239+ err := notMasterUser .Create (ctx , np )
240+ expectDeniedByVAP (err )
241+ })
242+
243+ It ("should deny creating managed CRP" , func () {
244+ crp := createManagedCRP ("test-crp" )
245+ err := notMasterUser .Create (ctx , crp )
246+ expectDeniedByVAP (err )
247+ })
248+
249+ It ("general expected behavior of other resources" , func () {
250+ rq := createManagedResourceQuota ("default" , "default" )
251+ np := createManagedNetworkPolicy ("default" , "default" )
252+ crp := createManagedCRP ("test-crp" )
253+ err := sysMastersClient .Create (ctx , rq )
254+ Expect (err ).To (BeNil (), "system:masters user should create managed ResourceQuota" )
255+ err = sysMastersClient .Create (ctx , np )
256+ Expect (err ).To (BeNil (), "system:masters user should create managed NetworkPolicy" )
257+ err = sysMastersClient .Create (ctx , crp )
258+ Expect (err ).To (BeNil (), "system:masters user should create managed CRP" )
259+
260+ work := createManagedResourcePlacement ("test-work" )
261+ err = notMasterUser .Create (ctx , work )
262+ expectDeniedByVAP (err )
263+
264+ var updateErr error
265+ Eventually (func () error {
266+ var urq corev1.ResourceQuota
267+ if err := sysMastersClient .Get (ctx , types.NamespacedName {Name : "default" , Namespace : "default" }, & urq ); err != nil {
268+ return err
269+ }
270+ urq .Annotations = map [string ]string {"test" : "annotation" }
271+ By ("expecting denial of UPDATE operation on managed resource quota" )
272+ updateErr = notMasterUser .Update (ctx , & urq )
273+ if k8sErrors .IsConflict (updateErr ) {
274+ return updateErr
275+ }
276+ return nil
277+ }, eventuallyDuration , eventuallyInterval ).Should (Succeed ())
278+ expectDeniedByVAP (updateErr )
279+
280+ err = notMasterUser .Delete (ctx , np )
281+ expectDeniedByVAP (err )
282+ err = notMasterUser .Delete (ctx , crp )
283+ expectDeniedByVAP (err )
284+
285+ err = sysMastersClient .Delete (ctx , rq )
286+ Expect (err ).To (BeNil (), "system:masters user should delete managed ResourceQuota" )
287+ err = sysMastersClient .Delete (ctx , np )
288+ Expect (err ).To (BeNil (), "system:masters user should delete managed NetworkPolicy" )
289+ err = sysMastersClient .Delete (ctx , crp )
290+ Expect (err ).To (BeNil (), "system:masters user should delete managed CRP" )
291+ })
174292 })
175293
176294 AfterAll (func () {
0 commit comments