Skip to content

Commit

Permalink
move App Protect DOS controller code to it's own file
Browse files Browse the repository at this point in the history
  • Loading branch information
pdabelf5 committed Aug 20, 2024
1 parent 65f1782 commit 6dcf5bf
Show file tree
Hide file tree
Showing 3 changed files with 297 additions and 280 deletions.
297 changes: 297 additions & 0 deletions internal/k8s/appprotect_dos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

package k8s

import (
"fmt"
"reflect"

"github.com/golang/glog"
"github.com/nginxinc/kubernetes-ingress/internal/k8s/appprotectdos"
"github.com/nginxinc/kubernetes-ingress/pkg/apis/dos/v1beta1"
api_v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/tools/cache"
)

func createAppProtectDosPolicyHandlers(lbc *LoadBalancerController) cache.ResourceEventHandlerFuncs {
handlers := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
pol := obj.(*unstructured.Unstructured)
glog.V(3).Infof("Adding AppProtectDosPolicy: %v", pol.GetName())
lbc.AddSyncQueue(pol)
},
UpdateFunc: func(oldObj, obj interface{}) {
oldPol := oldObj.(*unstructured.Unstructured)
newPol := obj.(*unstructured.Unstructured)
different, err := areResourcesDifferent(oldPol, newPol)
if err != nil {
glog.V(3).Infof("Error when comparing policy %v", err)
lbc.AddSyncQueue(newPol)

Check warning on line 35 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L22-L35

Added lines #L22 - L35 were not covered by tests
}
if different {
glog.V(3).Infof("ApDosPolicy %v changed, syncing", oldPol.GetName())
lbc.AddSyncQueue(newPol)

Check warning on line 39 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L37-L39

Added lines #L37 - L39 were not covered by tests
}
},
DeleteFunc: func(obj interface{}) {
lbc.AddSyncQueue(obj)
},

Check warning on line 44 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L42-L44

Added lines #L42 - L44 were not covered by tests
}
return handlers

Check warning on line 46 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L46

Added line #L46 was not covered by tests
}

func createAppProtectDosLogConfHandlers(lbc *LoadBalancerController) cache.ResourceEventHandlerFuncs {
handlers := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
conf := obj.(*unstructured.Unstructured)
glog.V(3).Infof("Adding AppProtectDosLogConf: %v", conf.GetName())
lbc.AddSyncQueue(conf)
},
UpdateFunc: func(oldObj, obj interface{}) {
oldConf := oldObj.(*unstructured.Unstructured)
newConf := obj.(*unstructured.Unstructured)
different, err := areResourcesDifferent(oldConf, newConf)
if err != nil {
glog.V(3).Infof("Error when comparing DosLogConfs %v", err)
lbc.AddSyncQueue(newConf)

Check warning on line 62 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L49-L62

Added lines #L49 - L62 were not covered by tests
}
if different {
glog.V(3).Infof("ApDosLogConf %v changed, syncing", oldConf.GetName())
lbc.AddSyncQueue(newConf)

Check warning on line 66 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L64-L66

Added lines #L64 - L66 were not covered by tests
}
},
DeleteFunc: func(obj interface{}) {
lbc.AddSyncQueue(obj)
},

Check warning on line 71 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L69-L71

Added lines #L69 - L71 were not covered by tests
}
return handlers

Check warning on line 73 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L73

Added line #L73 was not covered by tests
}

func createAppProtectDosProtectedResourceHandlers(lbc *LoadBalancerController) cache.ResourceEventHandlerFuncs {
handlers := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
conf := obj.(*v1beta1.DosProtectedResource)
glog.V(3).Infof("Adding DosProtectedResource: %v", conf.GetName())
lbc.AddSyncQueue(conf)
},
UpdateFunc: func(oldObj, obj interface{}) {
oldConf := oldObj.(*v1beta1.DosProtectedResource)
newConf := obj.(*v1beta1.DosProtectedResource)

Check warning on line 85 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L76-L85

Added lines #L76 - L85 were not covered by tests

if !reflect.DeepEqual(oldConf.Spec, newConf.Spec) {
glog.V(3).Infof("DosProtectedResource %v changed, syncing", oldConf.GetName())
lbc.AddSyncQueue(newConf)

Check warning on line 89 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L87-L89

Added lines #L87 - L89 were not covered by tests
}
},
DeleteFunc: func(obj interface{}) {
lbc.AddSyncQueue(obj)
},

Check warning on line 94 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L92-L94

Added lines #L92 - L94 were not covered by tests
}
return handlers

Check warning on line 96 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L96

Added line #L96 was not covered by tests
}

// addAppProtectDosPolicyHandler creates dynamic informers for custom appprotectdos policy resource
func (nsi *namespacedInformer) addAppProtectDosPolicyHandler(handlers cache.ResourceEventHandlerFuncs) {
informer := nsi.dynInformerFactory.ForResource(appprotectdos.DosPolicyGVR).Informer()
informer.AddEventHandler(handlers) //nolint:errcheck,gosec
nsi.appProtectDosPolicyLister = informer.GetStore()

Check warning on line 103 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L100-L103

Added lines #L100 - L103 were not covered by tests

nsi.cacheSyncs = append(nsi.cacheSyncs, informer.HasSynced)

Check warning on line 105 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L105

Added line #L105 was not covered by tests
}

// addAppProtectDosLogConfHandler creates dynamic informer for custom appprotectdos logging config resource
func (nsi *namespacedInformer) addAppProtectDosLogConfHandler(handlers cache.ResourceEventHandlerFuncs) {
informer := nsi.dynInformerFactory.ForResource(appprotectdos.DosLogConfGVR).Informer()
informer.AddEventHandler(handlers) //nolint:errcheck,gosec
nsi.appProtectDosLogConfLister = informer.GetStore()

Check warning on line 112 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L109-L112

Added lines #L109 - L112 were not covered by tests

nsi.cacheSyncs = append(nsi.cacheSyncs, informer.HasSynced)

Check warning on line 114 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L114

Added line #L114 was not covered by tests
}

// addAppProtectDosLogConfHandler creates dynamic informers for custom appprotectdos logging config resource
func (nsi *namespacedInformer) addAppProtectDosProtectedResourceHandler(handlers cache.ResourceEventHandlerFuncs) {
informer := nsi.confSharedInformerFactory.Appprotectdos().V1beta1().DosProtectedResources().Informer()
informer.AddEventHandler(handlers) //nolint:errcheck,gosec
nsi.appProtectDosProtectedLister = informer.GetStore()

Check warning on line 121 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L118-L121

Added lines #L118 - L121 were not covered by tests

nsi.cacheSyncs = append(nsi.cacheSyncs, informer.HasSynced)

Check warning on line 123 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L123

Added line #L123 was not covered by tests
}

func (lbc *LoadBalancerController) syncAppProtectDosPolicy(task task) {
key := task.Key
glog.V(3).Infof("Syncing AppProtectDosPolicy %v", key)
var obj interface{}
var polExists bool
var err error

Check warning on line 131 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L126-L131

Added lines #L126 - L131 were not covered by tests

ns, _, _ := cache.SplitMetaNamespaceKey(key)
obj, polExists, err = lbc.getNamespacedInformer(ns).appProtectDosPolicyLister.GetByKey(key)
if err != nil {
lbc.syncQueue.Requeue(task, err)
return

Check warning on line 137 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L133-L137

Added lines #L133 - L137 were not covered by tests
}

var changes []appprotectdos.Change
var problems []appprotectdos.Problem

Check warning on line 141 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L140-L141

Added lines #L140 - L141 were not covered by tests

if !polExists {
glog.V(2).Infof("Deleting APDosPolicy: %v\n", key)
changes, problems = lbc.dosConfiguration.DeletePolicy(key)
} else {
glog.V(2).Infof("Adding or Updating APDosPolicy: %v\n", key)
changes, problems = lbc.dosConfiguration.AddOrUpdatePolicy(obj.(*unstructured.Unstructured))

Check warning on line 148 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L143-L148

Added lines #L143 - L148 were not covered by tests
}

lbc.processAppProtectDosChanges(changes)
lbc.processAppProtectDosProblems(problems)

Check warning on line 152 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L151-L152

Added lines #L151 - L152 were not covered by tests
}

func (lbc *LoadBalancerController) syncAppProtectDosLogConf(task task) {
key := task.Key
glog.V(3).Infof("Syncing APDosLogConf %v", key)
var obj interface{}
var confExists bool
var err error

Check warning on line 160 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L155-L160

Added lines #L155 - L160 were not covered by tests

ns, _, _ := cache.SplitMetaNamespaceKey(key)
obj, confExists, err = lbc.getNamespacedInformer(ns).appProtectDosLogConfLister.GetByKey(key)
if err != nil {
lbc.syncQueue.Requeue(task, err)
return

Check warning on line 166 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L162-L166

Added lines #L162 - L166 were not covered by tests
}

var changes []appprotectdos.Change
var problems []appprotectdos.Problem

Check warning on line 170 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L169-L170

Added lines #L169 - L170 were not covered by tests

if !confExists {
glog.V(2).Infof("Deleting APDosLogConf: %v\n", key)
changes, problems = lbc.dosConfiguration.DeleteLogConf(key)
} else {
glog.V(2).Infof("Adding or Updating APDosLogConf: %v\n", key)
changes, problems = lbc.dosConfiguration.AddOrUpdateLogConf(obj.(*unstructured.Unstructured))

Check warning on line 177 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L172-L177

Added lines #L172 - L177 were not covered by tests
}

lbc.processAppProtectDosChanges(changes)
lbc.processAppProtectDosProblems(problems)

Check warning on line 181 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L180-L181

Added lines #L180 - L181 were not covered by tests
}

func (lbc *LoadBalancerController) syncDosProtectedResource(task task) {
key := task.Key
glog.V(3).Infof("Syncing DosProtectedResource %v", key)
var obj interface{}
var confExists bool
var err error

Check warning on line 189 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L184-L189

Added lines #L184 - L189 were not covered by tests

ns, _, _ := cache.SplitMetaNamespaceKey(key)
obj, confExists, err = lbc.getNamespacedInformer(ns).appProtectDosProtectedLister.GetByKey(key)
if err != nil {
lbc.syncQueue.Requeue(task, err)
return

Check warning on line 195 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L191-L195

Added lines #L191 - L195 were not covered by tests
}

var changes []appprotectdos.Change
var problems []appprotectdos.Problem

Check warning on line 199 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L198-L199

Added lines #L198 - L199 were not covered by tests

if confExists {
glog.V(2).Infof("Adding or Updating DosProtectedResource: %v\n", key)
changes, problems = lbc.dosConfiguration.AddOrUpdateDosProtectedResource(obj.(*v1beta1.DosProtectedResource))
} else {
glog.V(2).Infof("Deleting DosProtectedResource: %v\n", key)
changes, problems = lbc.dosConfiguration.DeleteProtectedResource(key)

Check warning on line 206 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L201-L206

Added lines #L201 - L206 were not covered by tests
}

lbc.processAppProtectDosChanges(changes)
lbc.processAppProtectDosProblems(problems)

Check warning on line 210 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L209-L210

Added lines #L209 - L210 were not covered by tests
}

func (lbc *LoadBalancerController) processAppProtectDosChanges(changes []appprotectdos.Change) {
glog.V(3).Infof("Processing %v App Protect Dos changes", len(changes))

Check warning on line 214 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L213-L214

Added lines #L213 - L214 were not covered by tests

for _, c := range changes {
if c.Op == appprotectdos.AddOrUpdate {
switch impl := c.Resource.(type) {
case *appprotectdos.DosProtectedResourceEx:
glog.V(3).Infof("handling change UPDATE OR ADD for DOS protected %s/%s", impl.Obj.Namespace, impl.Obj.Name)
resources := lbc.configuration.FindResourcesForAppProtectDosProtected(impl.Obj.Namespace, impl.Obj.Name)
resourceExes := lbc.createExtendedResources(resources)
warnings, err := lbc.configurator.AddOrUpdateResourcesThatUseDosProtected(resourceExes.IngressExes, resourceExes.MergeableIngresses, resourceExes.VirtualServerExes)
lbc.updateResourcesStatusAndEvents(resources, warnings, err)
msg := fmt.Sprintf("Configuration for %s/%s was added or updated", impl.Obj.Namespace, impl.Obj.Name)
lbc.recorder.Event(impl.Obj, api_v1.EventTypeNormal, "AddedOrUpdated", msg)
case *appprotectdos.DosPolicyEx:
msg := "Configuration was added or updated"
lbc.recorder.Event(impl.Obj, api_v1.EventTypeNormal, "AddedOrUpdated", msg)
case *appprotectdos.DosLogConfEx:
eventType := api_v1.EventTypeNormal
eventTitle := "AddedOrUpdated"
msg := "Configuration was added or updated"
if impl.ErrorMsg != "" {
msg += fmt.Sprintf(" ; with warning(s): %s", impl.ErrorMsg)
eventTitle = "AddedOrUpdatedWithWarning"
eventType = api_v1.EventTypeWarning

Check warning on line 237 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L216-L237

Added lines #L216 - L237 were not covered by tests
}
lbc.recorder.Event(impl.Obj, eventType, eventTitle, msg)

Check warning on line 239 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L239

Added line #L239 was not covered by tests
}
} else if c.Op == appprotectdos.Delete {
switch impl := c.Resource.(type) {
case *appprotectdos.DosPolicyEx:
lbc.configurator.DeleteAppProtectDosPolicy(impl.Obj)

Check warning on line 244 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L241-L244

Added lines #L241 - L244 were not covered by tests

case *appprotectdos.DosLogConfEx:
lbc.configurator.DeleteAppProtectDosLogConf(impl.Obj)

Check warning on line 247 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L246-L247

Added lines #L246 - L247 were not covered by tests

case *appprotectdos.DosProtectedResourceEx:
glog.V(3).Infof("handling change DELETE for DOS protected %s/%s", impl.Obj.Namespace, impl.Obj.Name)
resources := lbc.configuration.FindResourcesForAppProtectDosProtected(impl.Obj.Namespace, impl.Obj.Name)
resourceExes := lbc.createExtendedResources(resources)
warnings, err := lbc.configurator.AddOrUpdateResourcesThatUseDosProtected(resourceExes.IngressExes, resourceExes.MergeableIngresses, resourceExes.VirtualServerExes)
lbc.updateResourcesStatusAndEvents(resources, warnings, err)

Check warning on line 254 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L249-L254

Added lines #L249 - L254 were not covered by tests
}
}
}
}

func (lbc *LoadBalancerController) processAppProtectDosProblems(problems []appprotectdos.Problem) {
glog.V(3).Infof("Processing %v App Protect Dos problems", len(problems))

Check warning on line 261 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L260-L261

Added lines #L260 - L261 were not covered by tests

for _, p := range problems {
eventType := api_v1.EventTypeWarning
lbc.recorder.Event(p.Object, eventType, p.Reason, p.Message)

Check warning on line 265 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L263-L265

Added lines #L263 - L265 were not covered by tests
}
}

func (lbc *LoadBalancerController) cleanupUnwatchedAppDosResources(nsi *namespacedInformer) {
for _, obj := range nsi.appProtectDosPolicyLister.List() {
dosPol := obj.((*unstructured.Unstructured))
namespace := dosPol.GetNamespace()
name := dosPol.GetName()

Check warning on line 273 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L269-L273

Added lines #L269 - L273 were not covered by tests

changes, problems := lbc.dosConfiguration.DeletePolicy(namespace + "/" + name)
lbc.processAppProtectDosChanges(changes)
lbc.processAppProtectDosProblems(problems)

Check warning on line 277 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L275-L277

Added lines #L275 - L277 were not covered by tests
}
for _, obj := range nsi.appProtectDosProtectedLister.List() {
dosPol := obj.((*unstructured.Unstructured))
namespace := dosPol.GetNamespace()
name := dosPol.GetName()

Check warning on line 282 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L279-L282

Added lines #L279 - L282 were not covered by tests

changes, problems := lbc.dosConfiguration.DeleteProtectedResource(namespace + "/" + name)
lbc.processAppProtectDosChanges(changes)
lbc.processAppProtectDosProblems(problems)

Check warning on line 286 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L284-L286

Added lines #L284 - L286 were not covered by tests
}
for _, obj := range nsi.appProtectDosLogConfLister.List() {
dosPol := obj.((*unstructured.Unstructured))
namespace := dosPol.GetNamespace()
name := dosPol.GetName()

Check warning on line 291 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L288-L291

Added lines #L288 - L291 were not covered by tests

changes, problems := lbc.dosConfiguration.DeleteLogConf(namespace + "/" + name)
lbc.processAppProtectDosChanges(changes)
lbc.processAppProtectDosProblems(problems)

Check warning on line 295 in internal/k8s/appprotect_dos.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/appprotect_dos.go#L293-L295

Added lines #L293 - L295 were not covered by tests
}
}
Loading

0 comments on commit 6dcf5bf

Please sign in to comment.