|
| 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