@@ -160,7 +160,7 @@ var _ = Describe("Reconcile", func() {
160160 Expect (updated .Status .Policies ).To (BeEmpty ())
161161 })
162162
163- Context ("with matching compute policy" , func () {
163+ Context ("with mandatory compute policy" , func () {
164164 var computePolicy * vspherepolv1.ComputePolicy
165165
166166 BeforeEach (func () {
@@ -676,7 +676,7 @@ var _ = Describe("Reconcile", func() {
676676 Expect (policy .APIVersion ).To (Equal (vspherepolv1 .GroupVersion .String ()))
677677 Expect (policy .Kind ).To (Equal ("ComputePolicy" ))
678678 }
679- Expect (policyNames ).To (ConsistOf ("policy-1" , "policy-2" ))
679+ Expect (policyNames ).To (ConsistOf ("policy-1" ))
680680 })
681681 })
682682
@@ -1036,7 +1036,7 @@ var _ = Describe("Reconcile", func() {
10361036 }
10371037 })
10381038
1039- It ("should include explicitly referenced policies " , func () {
1039+ It ("should return an error if the explicit policy does not match " , func () {
10401040 req := ctrl.Request {
10411041 NamespacedName : types.NamespacedName {
10421042 Name : obj .Name ,
@@ -1045,15 +1045,10 @@ var _ = Describe("Reconcile", func() {
10451045 }
10461046
10471047 result , err := reconciler .Reconcile (ctx , req )
1048- Expect (err ).ToNot (HaveOccurred ())
1048+ Expect (err ).To (HaveOccurred ())
1049+ Expect (err .Error ()).To (ContainSubstring ("compute policy \" explicit-compute-policy\" does not match" ))
10491050 Expect (result ).To (Equal (ctrl.Result {}))
10501051
1051- var updated vspherepolv1.PolicyEvaluation
1052- Expect (client .Get (ctx , ctrlclient .ObjectKeyFromObject (obj ), & updated )).To (Succeed ())
1053- Expect (updated .Status .Policies ).To (HaveLen (1 ))
1054- Expect (updated .Status .Policies [0 ].APIVersion ).To (Equal (vspherepolv1 .GroupVersion .String ()))
1055- Expect (updated .Status .Policies [0 ].Kind ).To (Equal ("ComputePolicy" ))
1056- Expect (updated .Status .Policies [0 ].Generation ).To (Equal (computePolicy .Generation ))
10571052 })
10581053
10591054 Context ("when explicit policy does not exist" , func () {
@@ -1106,6 +1101,65 @@ var _ = Describe("Reconcile", func() {
11061101 Expect (updated .Status .Policies ).To (BeEmpty ())
11071102 })
11081103 })
1104+
1105+ Context ("when explicit policy matches" , func () {
1106+ BeforeEach (func () {
1107+ computePolicy = & vspherepolv1.ComputePolicy {
1108+ ObjectMeta : metav1.ObjectMeta {
1109+ Name : "explicit-matching-compute-policy" ,
1110+ Namespace : namespace ,
1111+ },
1112+ Spec : vspherepolv1.ComputePolicySpec {
1113+ EnforcementMode : vspherepolv1 .PolicyEnforcementModeMandatory ,
1114+ Match : & vspherepolv1.MatchSpec {
1115+ Workload : & vspherepolv1.MatchWorkloadSpec {
1116+ Labels : []metav1.LabelSelectorRequirement {
1117+ {
1118+ Key : "env" ,
1119+ Operator : metav1 .LabelSelectorOpIn ,
1120+ Values : []string {"prod" },
1121+ },
1122+ },
1123+ },
1124+ },
1125+ },
1126+ }
1127+ withObjs = append (withObjs , computePolicy )
1128+
1129+ obj .Spec .Policies = []vspherepolv1.LocalObjectRef {
1130+ {
1131+ APIVersion : vspherepolv1 .GroupVersion .String (),
1132+ Kind : "ComputePolicy" ,
1133+ Name : "explicit-matching-compute-policy" ,
1134+ },
1135+ }
1136+ obj .Spec .Workload = & vspherepolv1.PolicyEvaluationWorkloadSpec {
1137+ Labels : map [string ]string {
1138+ "env" : "prod" ,
1139+ },
1140+ }
1141+ })
1142+
1143+ It ("should add the policy to the status" , func () {
1144+ req := ctrl.Request {
1145+ NamespacedName : types.NamespacedName {
1146+ Name : obj .Name ,
1147+ Namespace : obj .Namespace ,
1148+ },
1149+ }
1150+ result , err := reconciler .Reconcile (ctx , req )
1151+ Expect (err ).ToNot (HaveOccurred ())
1152+ Expect (result ).To (Equal (ctrl.Result {}))
1153+
1154+ var updated vspherepolv1.PolicyEvaluation
1155+ Expect (client .Get (ctx , ctrlclient .ObjectKeyFromObject (obj ), & updated )).To (Succeed ())
1156+ Expect (updated .Status .Policies ).To (HaveLen (1 ))
1157+ Expect (updated .Status .Policies [0 ].Name ).To (Equal (computePolicy .Name ))
1158+ Expect (updated .Status .Policies [0 ].APIVersion ).To (Equal (vspherepolv1 .GroupVersion .String ()))
1159+ Expect (updated .Status .Policies [0 ].Kind ).To (Equal ("ComputePolicy" ))
1160+ Expect (updated .Status .Policies [0 ].Generation ).To (Equal (computePolicy .Generation ))
1161+ })
1162+ })
11091163 })
11101164
11111165 Context ("with duplicate policies" , func () {
@@ -1940,7 +1994,7 @@ var _ = Describe("Reconcile", func() {
19401994
19411995 result , err := reconciler .Reconcile (ctx , req )
19421996 Expect (err ).To (HaveOccurred ())
1943- Expect (err .Error ()).To (ContainSubstring ("failed to reconcile matching policies" ))
1997+ Expect (err .Error ()).To (ContainSubstring ("failed to reconcile mandatory policies" ))
19441998 Expect (result ).To (Equal (ctrl.Result {}))
19451999 })
19462000 })
@@ -2024,7 +2078,7 @@ var _ = Describe("Reconcile", func() {
20242078
20252079 result , err := reconciler .Reconcile (ctx , req )
20262080 Expect (err ).To (HaveOccurred ())
2027- Expect (err .Error ()).To (ContainSubstring ("failed to reconcile matching policies" ))
2081+ Expect (err .Error ()).To (ContainSubstring ("failed to reconcile mandatory policies" ))
20282082 Expect (err .Error ()).To (ContainSubstring ("failed to list compute policies" ))
20292083 Expect (result ).To (Equal (ctrl.Result {}))
20302084 })
0 commit comments