Skip to content

Commit

Permalink
refactor: replace glog in k8s package 1 (#6575)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdabelf5 authored Oct 2, 2024
1 parent 677c054 commit 3f79c6d
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 134 deletions.
48 changes: 24 additions & 24 deletions internal/k8s/appprotect_dos.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"reflect"

"github.com/golang/glog"
"github.com/nginxinc/kubernetes-ingress/internal/k8s/appprotectdos"
nl "github.com/nginxinc/kubernetes-ingress/internal/logger"
"github.com/nginxinc/kubernetes-ingress/pkg/apis/dos/v1beta1"
api_v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -16,19 +16,19 @@ func createAppProtectDosPolicyHandlers(lbc *LoadBalancerController) cache.Resour
handlers := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
pol := obj.(*unstructured.Unstructured)
glog.V(3).Infof("Adding AppProtectDosPolicy: %v", pol.GetName())
nl.Debugf(lbc.logger, "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)
different, err := areResourcesDifferent(lbc.logger, oldPol, newPol)
if err != nil {
glog.V(3).Infof("Error when comparing policy %v", err)
nl.Debugf(lbc.logger, "Error when comparing policy %v", err)
lbc.AddSyncQueue(newPol)
}
if different {
glog.V(3).Infof("ApDosPolicy %v changed, syncing", oldPol.GetName())
nl.Debugf(lbc.logger, "ApDosPolicy %v changed, syncing", oldPol.GetName())
lbc.AddSyncQueue(newPol)
}
},
Expand All @@ -43,19 +43,19 @@ func createAppProtectDosLogConfHandlers(lbc *LoadBalancerController) cache.Resou
handlers := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
conf := obj.(*unstructured.Unstructured)
glog.V(3).Infof("Adding AppProtectDosLogConf: %v", conf.GetName())
nl.Debugf(lbc.logger, "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)
different, err := areResourcesDifferent(lbc.logger, oldConf, newConf)
if err != nil {
glog.V(3).Infof("Error when comparing DosLogConfs %v", err)
nl.Debugf(lbc.logger, "Error when comparing DosLogConfs %v", err)
lbc.AddSyncQueue(newConf)
}
if different {
glog.V(3).Infof("ApDosLogConf %v changed, syncing", oldConf.GetName())
nl.Debugf(lbc.logger, "ApDosLogConf %v changed, syncing", oldConf.GetName())
lbc.AddSyncQueue(newConf)
}
},
Expand All @@ -70,15 +70,15 @@ func createAppProtectDosProtectedResourceHandlers(lbc *LoadBalancerController) c
handlers := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
conf := obj.(*v1beta1.DosProtectedResource)
glog.V(3).Infof("Adding DosProtectedResource: %v", conf.GetName())
nl.Debugf(lbc.logger, "Adding DosProtectedResource: %v", conf.GetName())
lbc.AddSyncQueue(conf)
},
UpdateFunc: func(oldObj, obj interface{}) {
oldConf := oldObj.(*v1beta1.DosProtectedResource)
newConf := obj.(*v1beta1.DosProtectedResource)

if !reflect.DeepEqual(oldConf.Spec, newConf.Spec) {
glog.V(3).Infof("DosProtectedResource %v changed, syncing", oldConf.GetName())
nl.Debugf(lbc.logger, "DosProtectedResource %v changed, syncing", oldConf.GetName())
lbc.AddSyncQueue(newConf)
}
},
Expand Down Expand Up @@ -118,7 +118,7 @@ func (nsi *namespacedInformer) addAppProtectDosProtectedResourceHandler(handlers

func (lbc *LoadBalancerController) syncAppProtectDosPolicy(task task) {
key := task.Key
glog.V(3).Infof("Syncing AppProtectDosPolicy %v", key)
nl.Debugf(lbc.logger, "Syncing AppProtectDosPolicy %v", key)
var obj interface{}
var polExists bool
var err error
Expand All @@ -134,10 +134,10 @@ func (lbc *LoadBalancerController) syncAppProtectDosPolicy(task task) {
var problems []appprotectdos.Problem

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

Expand All @@ -147,7 +147,7 @@ func (lbc *LoadBalancerController) syncAppProtectDosPolicy(task task) {

func (lbc *LoadBalancerController) syncAppProtectDosLogConf(task task) {
key := task.Key
glog.V(3).Infof("Syncing APDosLogConf %v", key)
nl.Debugf(lbc.logger, "Syncing APDosLogConf %v", key)
var obj interface{}
var confExists bool
var err error
Expand All @@ -163,10 +163,10 @@ func (lbc *LoadBalancerController) syncAppProtectDosLogConf(task task) {
var problems []appprotectdos.Problem

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

Expand All @@ -176,7 +176,7 @@ func (lbc *LoadBalancerController) syncAppProtectDosLogConf(task task) {

func (lbc *LoadBalancerController) syncDosProtectedResource(task task) {
key := task.Key
glog.V(3).Infof("Syncing DosProtectedResource %v", key)
nl.Debugf(lbc.logger, "Syncing DosProtectedResource %v", key)
var obj interface{}
var confExists bool
var err error
Expand All @@ -192,10 +192,10 @@ func (lbc *LoadBalancerController) syncDosProtectedResource(task task) {
var problems []appprotectdos.Problem

if confExists {
glog.V(2).Infof("Adding or Updating DosProtectedResource: %v\n", key)
nl.Debugf(lbc.logger, "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)
nl.Debugf(lbc.logger, "Deleting DosProtectedResource: %v\n", key)
changes, problems = lbc.dosConfiguration.DeleteProtectedResource(key)
}

Expand All @@ -204,13 +204,13 @@ func (lbc *LoadBalancerController) syncDosProtectedResource(task task) {
}

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

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)
nl.Debugf(lbc.logger, "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)
Expand Down Expand Up @@ -240,7 +240,7 @@ func (lbc *LoadBalancerController) processAppProtectDosChanges(changes []appprot
lbc.configurator.DeleteAppProtectDosLogConf(impl.Obj)

case *appprotectdos.DosProtectedResourceEx:
glog.V(3).Infof("handling change DELETE for DOS protected %s/%s", impl.Obj.Namespace, impl.Obj.Name)
nl.Debugf(lbc.logger, "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)
Expand All @@ -251,7 +251,7 @@ func (lbc *LoadBalancerController) processAppProtectDosChanges(changes []appprot
}

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

for _, p := range problems {
eventType := api_v1.EventTypeWarning
Expand Down
56 changes: 28 additions & 28 deletions internal/k8s/appprotect_waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"fmt"
"strings"

"github.com/golang/glog"
"github.com/nginxinc/kubernetes-ingress/internal/configs"
"github.com/nginxinc/kubernetes-ingress/internal/k8s/appprotect"
"github.com/nginxinc/kubernetes-ingress/internal/k8s/appprotectcommon"
nl "github.com/nginxinc/kubernetes-ingress/internal/logger"
conf_v1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1"
api_v1 "k8s.io/api/core/v1"
networking "k8s.io/api/networking/v1"
Expand All @@ -19,19 +19,19 @@ func createAppProtectPolicyHandlers(lbc *LoadBalancerController) cache.ResourceE
handlers := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
pol := obj.(*unstructured.Unstructured)
glog.V(3).Infof("Adding AppProtectPolicy: %v", pol.GetName())
nl.Debugf(lbc.logger, "Adding AppProtectPolicy: %v", pol.GetName())
lbc.AddSyncQueue(pol)
},
UpdateFunc: func(oldObj, obj interface{}) {
oldPol := oldObj.(*unstructured.Unstructured)
newPol := obj.(*unstructured.Unstructured)
different, err := areResourcesDifferent(oldPol, newPol)
different, err := areResourcesDifferent(lbc.logger, oldPol, newPol)
if err != nil {
glog.V(3).Infof("Error when comparing policy %v", err)
nl.Debugf(lbc.logger, "Error when comparing policy %v", err)
lbc.AddSyncQueue(newPol)
}
if different {
glog.V(3).Infof("ApPolicy %v changed, syncing", oldPol.GetName())
nl.Debugf(lbc.logger, "ApPolicy %v changed, syncing", oldPol.GetName())
lbc.AddSyncQueue(newPol)
}
},
Expand All @@ -46,19 +46,19 @@ func createAppProtectLogConfHandlers(lbc *LoadBalancerController) cache.Resource
handlers := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
conf := obj.(*unstructured.Unstructured)
glog.V(3).Infof("Adding AppProtectLogConf: %v", conf.GetName())
nl.Debugf(lbc.logger, "Adding AppProtectLogConf: %v", conf.GetName())
lbc.AddSyncQueue(conf)
},
UpdateFunc: func(oldObj, obj interface{}) {
oldConf := oldObj.(*unstructured.Unstructured)
newConf := obj.(*unstructured.Unstructured)
different, err := areResourcesDifferent(oldConf, newConf)
different, err := areResourcesDifferent(lbc.logger, oldConf, newConf)
if err != nil {
glog.V(3).Infof("Error when comparing LogConfs %v", err)
nl.Debugf(lbc.logger, "Error when comparing LogConfs %v", err)
lbc.AddSyncQueue(newConf)
}
if different {
glog.V(3).Infof("ApLogConf %v changed, syncing", oldConf.GetName())
nl.Debugf(lbc.logger, "ApLogConf %v changed, syncing", oldConf.GetName())
lbc.AddSyncQueue(newConf)
}
},
Expand All @@ -73,19 +73,19 @@ func createAppProtectUserSigHandlers(lbc *LoadBalancerController) cache.Resource
handlers := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
sig := obj.(*unstructured.Unstructured)
glog.V(3).Infof("Adding AppProtectUserSig: %v", sig.GetName())
nl.Debugf(lbc.logger, "Adding AppProtectUserSig: %v", sig.GetName())
lbc.AddSyncQueue(sig)
},
UpdateFunc: func(oldObj, obj interface{}) {
oldSig := oldObj.(*unstructured.Unstructured)
newSig := obj.(*unstructured.Unstructured)
different, err := areResourcesDifferent(oldSig, newSig)
different, err := areResourcesDifferent(lbc.logger, oldSig, newSig)
if err != nil {
glog.V(3).Infof("Error when comparing UserSigs %v", err)
nl.Debugf(lbc.logger, "Error when comparing UserSigs %v", err)
lbc.AddSyncQueue(newSig)
}
if different {
glog.V(3).Infof("ApUserSig %v changed, syncing", oldSig.GetName())
nl.Debugf(lbc.logger, "ApUserSig %v changed, syncing", oldSig.GetName())
lbc.AddSyncQueue(newSig)
}
},
Expand Down Expand Up @@ -125,7 +125,7 @@ func (nsi *namespacedInformer) addAppProtectUserSigHandler(handlers cache.Resour

func (lbc *LoadBalancerController) syncAppProtectPolicy(task task) {
key := task.Key
glog.V(3).Infof("Syncing AppProtectPolicy %v", key)
nl.Debugf(lbc.logger, "Syncing AppProtectPolicy %v", key)

var obj interface{}
var polExists bool
Expand All @@ -142,11 +142,11 @@ func (lbc *LoadBalancerController) syncAppProtectPolicy(task task) {
var problems []appprotect.Problem

if !polExists {
glog.V(2).Infof("Deleting AppProtectPolicy: %v\n", key)
nl.Debugf(lbc.logger, "Deleting AppProtectPolicy: %v\n", key)

changes, problems = lbc.appProtectConfiguration.DeletePolicy(key)
} else {
glog.V(2).Infof("Adding or Updating AppProtectPolicy: %v\n", key)
nl.Debugf(lbc.logger, "Adding or Updating AppProtectPolicy: %v\n", key)

changes, problems = lbc.appProtectConfiguration.AddOrUpdatePolicy(obj.(*unstructured.Unstructured))
}
Expand All @@ -157,7 +157,7 @@ func (lbc *LoadBalancerController) syncAppProtectPolicy(task task) {

func (lbc *LoadBalancerController) syncAppProtectLogConf(task task) {
key := task.Key
glog.V(3).Infof("Syncing AppProtectLogConf %v", key)
nl.Debugf(lbc.logger, "Syncing AppProtectLogConf %v", key)
var obj interface{}
var confExists bool
var err error
Expand All @@ -173,11 +173,11 @@ func (lbc *LoadBalancerController) syncAppProtectLogConf(task task) {
var problems []appprotect.Problem

if !confExists {
glog.V(2).Infof("Deleting AppProtectLogConf: %v\n", key)
nl.Debugf(lbc.logger, "Deleting AppProtectLogConf: %v\n", key)

changes, problems = lbc.appProtectConfiguration.DeleteLogConf(key)
} else {
glog.V(2).Infof("Adding or Updating AppProtectLogConf: %v\n", key)
nl.Debugf(lbc.logger, "Adding or Updating AppProtectLogConf: %v\n", key)

changes, problems = lbc.appProtectConfiguration.AddOrUpdateLogConf(obj.(*unstructured.Unstructured))
}
Expand All @@ -188,7 +188,7 @@ func (lbc *LoadBalancerController) syncAppProtectLogConf(task task) {

func (lbc *LoadBalancerController) syncAppProtectUserSig(task task) {
key := task.Key
glog.V(3).Infof("Syncing AppProtectUserSig %v", key)
nl.Debugf(lbc.logger, "Syncing AppProtectUserSig %v", key)
var obj interface{}
var sigExists bool
var err error
Expand All @@ -204,11 +204,11 @@ func (lbc *LoadBalancerController) syncAppProtectUserSig(task task) {
var problems []appprotect.Problem

if !sigExists {
glog.V(2).Infof("Deleting AppProtectUserSig: %v\n", key)
nl.Debugf(lbc.logger, "Deleting AppProtectUserSig: %v\n", key)

change, problems = lbc.appProtectConfiguration.DeleteUserSig(key)
} else {
glog.V(2).Infof("Adding or Updating AppProtectUserSig: %v\n", key)
nl.Debugf(lbc.logger, "Adding or Updating AppProtectUserSig: %v\n", key)

change, problems = lbc.appProtectConfiguration.AddOrUpdateUserSig(obj.(*unstructured.Unstructured))
}
Expand Down Expand Up @@ -353,7 +353,7 @@ func (lbc *LoadBalancerController) getAppProtectPolicy(ing *networking.Ingress)
}

func (lbc *LoadBalancerController) processAppProtectChanges(changes []appprotect.Change) {
glog.V(3).Infof("Processing %v App Protect changes", len(changes))
nl.Debugf(lbc.logger, "Processing %v App Protect changes", len(changes))

for _, c := range changes {
if c.Op == appprotect.AddOrUpdate {
Expand Down Expand Up @@ -463,13 +463,13 @@ func (lbc *LoadBalancerController) processAppProtectUserSigChange(change appprot

warnings, err := lbc.configurator.RefreshAppProtectUserSigs(change.UserSigs, delPols, allIngExes, allMergeableIngresses, allVsExes)
if err != nil {
glog.Errorf("Error when refreshing App Protect Policy User defined signatures: %v", err)
nl.Errorf(lbc.logger, "Error when refreshing App Protect Policy User defined signatures: %v", err)
}
lbc.updateResourcesStatusAndEvents(allResources, warnings, err)
}

func (lbc *LoadBalancerController) processAppProtectProblems(problems []appprotect.Problem) {
glog.V(3).Infof("Processing %v App Protect problems", len(problems))
nl.Debugf(lbc.logger, "Processing %v App Protect problems", len(problems))

for _, p := range problems {
eventType := api_v1.EventTypeWarning
Expand All @@ -479,7 +479,7 @@ func (lbc *LoadBalancerController) processAppProtectProblems(problems []appprote

func (lbc *LoadBalancerController) cleanupUnwatchedAppWafResources(nsi *namespacedInformer) {
for _, obj := range nsi.appProtectPolicyLister.List() {
glog.V(3).Infof("Cleaning up unwatched appprotect policies in namespace: %v", nsi.namespace)
nl.Debugf(lbc.logger, "Cleaning up unwatched appprotect policies in namespace: %v", nsi.namespace)
appPol := obj.((*unstructured.Unstructured))
namespace := appPol.GetNamespace()
name := appPol.GetName()
Expand All @@ -489,7 +489,7 @@ func (lbc *LoadBalancerController) cleanupUnwatchedAppWafResources(nsi *namespac
lbc.processAppProtectProblems(problems)
}
for _, obj := range nsi.appProtectLogConfLister.List() {
glog.V(3).Infof("Cleaning up unwatched approtect logconfs in namespace: %v", nsi.namespace)
nl.Debugf(lbc.logger, "Cleaning up unwatched approtect logconfs in namespace: %v", nsi.namespace)
appLogConf := obj.((*unstructured.Unstructured))
namespace := appLogConf.GetNamespace()
name := appLogConf.GetName()
Expand All @@ -499,7 +499,7 @@ func (lbc *LoadBalancerController) cleanupUnwatchedAppWafResources(nsi *namespac
lbc.processAppProtectProblems(problems)
}
for _, obj := range nsi.appProtectUserSigLister.List() {
glog.V(3).Infof("Cleaning up unwatched usersigs in namespace: %v", nsi.namespace)
nl.Debugf(lbc.logger, "Cleaning up unwatched usersigs in namespace: %v", nsi.namespace)
appUserSig := obj.((*unstructured.Unstructured))
namespace := appUserSig.GetNamespace()
name := appUserSig.GetName()
Expand Down
Loading

0 comments on commit 3f79c6d

Please sign in to comment.