Skip to content

Commit 7c62ede

Browse files
authored
refactor Policy controller (#6388)
1 parent 890af8e commit 7c62ede

File tree

3 files changed

+113
-101
lines changed

3 files changed

+113
-101
lines changed

internal/k8s/controller.go

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -548,14 +548,6 @@ func (nsi *namespacedInformer) addVirtualServerRouteHandler(handlers cache.Resou
548548
nsi.cacheSyncs = append(nsi.cacheSyncs, informer.HasSynced)
549549
}
550550

551-
func (nsi *namespacedInformer) addPolicyHandler(handlers cache.ResourceEventHandlerFuncs) {
552-
informer := nsi.confSharedInformerFactory.K8s().V1().Policies().Informer()
553-
informer.AddEventHandler(handlers)
554-
nsi.policyLister = informer.GetStore()
555-
556-
nsi.cacheSyncs = append(nsi.cacheSyncs, informer.HasSynced)
557-
}
558-
559551
func (lbc *LoadBalancerController) addNamespaceHandler(handlers cache.ResourceEventHandlerFuncs, nsLabel string) {
560552
optionsModifier := func(options *meta_v1.ListOptions) {
561553
options.LabelSelector = nsLabel
@@ -1172,64 +1164,6 @@ func (lbc *LoadBalancerController) cleanupUnwatchedNamespacedResources(nsi *name
11721164
nsi.stop()
11731165
}
11741166

1175-
func (lbc *LoadBalancerController) syncPolicy(task task) {
1176-
key := task.Key
1177-
var obj interface{}
1178-
var polExists bool
1179-
var err error
1180-
1181-
ns, _, _ := cache.SplitMetaNamespaceKey(key)
1182-
obj, polExists, err = lbc.getNamespacedInformer(ns).policyLister.GetByKey(key)
1183-
if err != nil {
1184-
lbc.syncQueue.Requeue(task, err)
1185-
return
1186-
}
1187-
1188-
glog.V(2).Infof("Adding, Updating or Deleting Policy: %v\n", key)
1189-
1190-
if polExists && lbc.HasCorrectIngressClass(obj) {
1191-
pol := obj.(*conf_v1.Policy)
1192-
err := validation.ValidatePolicy(pol, lbc.isNginxPlus, lbc.enableOIDC, lbc.appProtectEnabled)
1193-
if err != nil {
1194-
msg := fmt.Sprintf("Policy %v/%v is invalid and was rejected: %v", pol.Namespace, pol.Name, err)
1195-
lbc.recorder.Eventf(pol, api_v1.EventTypeWarning, "Rejected", msg)
1196-
1197-
if lbc.reportCustomResourceStatusEnabled() {
1198-
err = lbc.statusUpdater.UpdatePolicyStatus(pol, conf_v1.StateInvalid, "Rejected", msg)
1199-
if err != nil {
1200-
glog.V(3).Infof("Failed to update policy %s status: %v", key, err)
1201-
}
1202-
}
1203-
} else {
1204-
msg := fmt.Sprintf("Policy %v/%v was added or updated", pol.Namespace, pol.Name)
1205-
lbc.recorder.Eventf(pol, api_v1.EventTypeNormal, "AddedOrUpdated", msg)
1206-
1207-
if lbc.reportCustomResourceStatusEnabled() {
1208-
err = lbc.statusUpdater.UpdatePolicyStatus(pol, conf_v1.StateValid, "AddedOrUpdated", msg)
1209-
if err != nil {
1210-
glog.V(3).Infof("Failed to update policy %s status: %v", key, err)
1211-
}
1212-
}
1213-
}
1214-
}
1215-
1216-
// it is safe to ignore the error
1217-
namespace, name, _ := ParseNamespaceName(key)
1218-
1219-
resources := lbc.configuration.FindResourcesForPolicy(namespace, name)
1220-
resourceExes := lbc.createExtendedResources(resources)
1221-
1222-
// Only VirtualServers support policies
1223-
if len(resourceExes.VirtualServerExes) == 0 {
1224-
return
1225-
}
1226-
1227-
warnings, updateErr := lbc.configurator.AddOrUpdateVirtualServers(resourceExes.VirtualServerExes)
1228-
lbc.updateResourcesStatusAndEvents(resources, warnings, updateErr)
1229-
1230-
// Note: updating the status of a policy based on a reload is not needed.
1231-
}
1232-
12331167
func (lbc *LoadBalancerController) syncVirtualServer(task task) {
12341168
key := task.Key
12351169
var obj interface{}

internal/k8s/handlers.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -326,41 +326,6 @@ func createVirtualServerRouteHandlers(lbc *LoadBalancerController) cache.Resourc
326326
}
327327
}
328328

329-
func createPolicyHandlers(lbc *LoadBalancerController) cache.ResourceEventHandlerFuncs {
330-
return cache.ResourceEventHandlerFuncs{
331-
AddFunc: func(obj interface{}) {
332-
pol := obj.(*conf_v1.Policy)
333-
glog.V(3).Infof("Adding Policy: %v", pol.Name)
334-
lbc.AddSyncQueue(pol)
335-
},
336-
DeleteFunc: func(obj interface{}) {
337-
pol, isPol := obj.(*conf_v1.Policy)
338-
if !isPol {
339-
deletedState, ok := obj.(cache.DeletedFinalStateUnknown)
340-
if !ok {
341-
glog.V(3).Infof("Error received unexpected object: %v", obj)
342-
return
343-
}
344-
pol, ok = deletedState.Obj.(*conf_v1.Policy)
345-
if !ok {
346-
glog.V(3).Infof("Error DeletedFinalStateUnknown contained non-Policy object: %v", deletedState.Obj)
347-
return
348-
}
349-
}
350-
glog.V(3).Infof("Removing Policy: %v", pol.Name)
351-
lbc.AddSyncQueue(pol)
352-
},
353-
UpdateFunc: func(old, cur interface{}) {
354-
curPol := cur.(*conf_v1.Policy)
355-
oldPol := old.(*conf_v1.Policy)
356-
if !reflect.DeepEqual(oldPol.Spec, curPol.Spec) {
357-
glog.V(3).Infof("Policy %v changed, syncing", curPol.Name)
358-
lbc.AddSyncQueue(curPol)
359-
}
360-
},
361-
}
362-
}
363-
364329
// areResourcesDifferent returns true if the resources are different based on their spec.
365330
func areResourcesDifferent(oldresource, resource *unstructured.Unstructured) (bool, error) {
366331
oldSpec, found, err := unstructured.NestedMap(oldresource.Object, "spec")

internal/k8s/policy.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package k8s
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
7+
"github.com/golang/glog"
8+
conf_v1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1"
9+
"github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/validation"
10+
api_v1 "k8s.io/api/core/v1"
11+
"k8s.io/client-go/tools/cache"
12+
)
13+
14+
func createPolicyHandlers(lbc *LoadBalancerController) cache.ResourceEventHandlerFuncs {
15+
return cache.ResourceEventHandlerFuncs{
16+
AddFunc: func(obj interface{}) {
17+
pol := obj.(*conf_v1.Policy)
18+
glog.V(3).Infof("Adding Policy: %v", pol.Name)
19+
lbc.AddSyncQueue(pol)
20+
},
21+
DeleteFunc: func(obj interface{}) {
22+
pol, isPol := obj.(*conf_v1.Policy)
23+
if !isPol {
24+
deletedState, ok := obj.(cache.DeletedFinalStateUnknown)
25+
if !ok {
26+
glog.V(3).Infof("Error received unexpected object: %v", obj)
27+
return
28+
}
29+
pol, ok = deletedState.Obj.(*conf_v1.Policy)
30+
if !ok {
31+
glog.V(3).Infof("Error DeletedFinalStateUnknown contained non-Policy object: %v", deletedState.Obj)
32+
return
33+
}
34+
}
35+
glog.V(3).Infof("Removing Policy: %v", pol.Name)
36+
lbc.AddSyncQueue(pol)
37+
},
38+
UpdateFunc: func(old, cur interface{}) {
39+
curPol := cur.(*conf_v1.Policy)
40+
oldPol := old.(*conf_v1.Policy)
41+
if !reflect.DeepEqual(oldPol.Spec, curPol.Spec) {
42+
glog.V(3).Infof("Policy %v changed, syncing", curPol.Name)
43+
lbc.AddSyncQueue(curPol)
44+
}
45+
},
46+
}
47+
}
48+
49+
func (nsi *namespacedInformer) addPolicyHandler(handlers cache.ResourceEventHandlerFuncs) {
50+
informer := nsi.confSharedInformerFactory.K8s().V1().Policies().Informer()
51+
informer.AddEventHandler(handlers) //nolint:errcheck,gosec
52+
nsi.policyLister = informer.GetStore()
53+
54+
nsi.cacheSyncs = append(nsi.cacheSyncs, informer.HasSynced)
55+
}
56+
57+
func (lbc *LoadBalancerController) syncPolicy(task task) {
58+
key := task.Key
59+
var obj interface{}
60+
var polExists bool
61+
var err error
62+
63+
ns, _, _ := cache.SplitMetaNamespaceKey(key)
64+
obj, polExists, err = lbc.getNamespacedInformer(ns).policyLister.GetByKey(key)
65+
if err != nil {
66+
lbc.syncQueue.Requeue(task, err)
67+
return
68+
}
69+
70+
glog.V(2).Infof("Adding, Updating or Deleting Policy: %v\n", key)
71+
72+
if polExists && lbc.HasCorrectIngressClass(obj) {
73+
pol := obj.(*conf_v1.Policy)
74+
err := validation.ValidatePolicy(pol, lbc.isNginxPlus, lbc.enableOIDC, lbc.appProtectEnabled)
75+
if err != nil {
76+
msg := fmt.Sprintf("Policy %v/%v is invalid and was rejected: %v", pol.Namespace, pol.Name, err)
77+
lbc.recorder.Eventf(pol, api_v1.EventTypeWarning, "Rejected", msg)
78+
79+
if lbc.reportCustomResourceStatusEnabled() {
80+
err = lbc.statusUpdater.UpdatePolicyStatus(pol, conf_v1.StateInvalid, "Rejected", msg)
81+
if err != nil {
82+
glog.V(3).Infof("Failed to update policy %s status: %v", key, err)
83+
}
84+
}
85+
} else {
86+
msg := fmt.Sprintf("Policy %v/%v was added or updated", pol.Namespace, pol.Name)
87+
lbc.recorder.Eventf(pol, api_v1.EventTypeNormal, "AddedOrUpdated", msg)
88+
89+
if lbc.reportCustomResourceStatusEnabled() {
90+
err = lbc.statusUpdater.UpdatePolicyStatus(pol, conf_v1.StateValid, "AddedOrUpdated", msg)
91+
if err != nil {
92+
glog.V(3).Infof("Failed to update policy %s status: %v", key, err)
93+
}
94+
}
95+
}
96+
}
97+
98+
// it is safe to ignore the error
99+
namespace, name, _ := ParseNamespaceName(key)
100+
101+
resources := lbc.configuration.FindResourcesForPolicy(namespace, name)
102+
resourceExes := lbc.createExtendedResources(resources)
103+
104+
// Only VirtualServers support policies
105+
if len(resourceExes.VirtualServerExes) == 0 {
106+
return
107+
}
108+
109+
warnings, updateErr := lbc.configurator.AddOrUpdateVirtualServers(resourceExes.VirtualServerExes)
110+
lbc.updateResourcesStatusAndEvents(resources, warnings, updateErr)
111+
112+
// Note: updating the status of a policy based on a reload is not needed.
113+
}

0 commit comments

Comments
 (0)