From 24782f156b4f044d2f52db9bce7b03608f1fa038 Mon Sep 17 00:00:00 2001 From: Paul Abel <128620221+pdabelf5@users.noreply.github.com> Date: Tue, 1 Oct 2024 17:31:51 +0100 Subject: [PATCH] refactor: replace glog in controller.go (#6551) --- cmd/nginx-ingress/main.go | 1 + internal/k8s/configuration_test.go | 3 + internal/k8s/controller.go | 267 +++++++++++++++-------------- internal/k8s/controller_test.go | 30 ++++ 4 files changed, 174 insertions(+), 127 deletions(-) diff --git a/cmd/nginx-ingress/main.go b/cmd/nginx-ingress/main.go index 1353992ca9..31ce73f9a1 100644 --- a/cmd/nginx-ingress/main.go +++ b/cmd/nginx-ingress/main.go @@ -211,6 +211,7 @@ func main() { DynClient: dynClient, RestConfig: config, ResyncPeriod: 30 * time.Second, + LoggerContext: ctx, Namespace: watchNamespaces, SecretNamespace: watchSecretNamespaces, NginxConfigurator: cnf, diff --git a/internal/k8s/configuration_test.go b/internal/k8s/configuration_test.go index e7674a2ccc..fe3e676f30 100644 --- a/internal/k8s/configuration_test.go +++ b/internal/k8s/configuration_test.go @@ -1,10 +1,12 @@ package k8s import ( + "context" "testing" "time" "github.com/google/go-cmp/cmp" + nic_logger "github.com/nginxinc/kubernetes-ingress/internal/logger" conf_v1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1" "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/validation" networking "k8s.io/api/networking/v1" @@ -14,6 +16,7 @@ import ( func createTestConfiguration() *Configuration { lbc := LoadBalancerController{ ingressClass: "nginx", + logger: nic_logger.LoggerFromContext(context.Background()), } isPlus := false appProtectEnabled := false diff --git a/internal/k8s/controller.go b/internal/k8s/controller.go index 2604e7a897..39f09c91c0 100644 --- a/internal/k8s/controller.go +++ b/internal/k8s/controller.go @@ -19,6 +19,7 @@ package k8s import ( "context" "fmt" + "log/slog" "net" "os" "strconv" @@ -34,7 +35,6 @@ import ( "k8s.io/client-go/informers" "k8s.io/client-go/rest" - "github.com/golang/glog" "github.com/nginxinc/kubernetes-ingress/internal/k8s/secrets" "github.com/nginxinc/nginx-service-mesh/pkg/spiffe" "github.com/spiffe/go-spiffe/v2/workloadapi" @@ -65,6 +65,9 @@ import ( k8s_nginx "github.com/nginxinc/kubernetes-ingress/pkg/client/clientset/versioned" k8s_nginx_informers "github.com/nginxinc/kubernetes-ingress/pkg/client/informers/externalversions" + nic_logger "github.com/nginxinc/kubernetes-ingress/internal/logger" + "github.com/nginxinc/kubernetes-ingress/internal/logger/levels" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/dynamic" @@ -119,6 +122,7 @@ type LoadBalancerController struct { namespaceLabeledLister cache.Store syncQueue *taskQueue ctx context.Context + logger *slog.Logger cancel context.CancelFunc configurator *configs.Configurator watchNginxConfigMaps bool @@ -177,6 +181,7 @@ type NewLoadBalancerControllerInput struct { DynClient dynamic.Interface RestConfig *rest.Config ResyncPeriod time.Duration + LoggerContext context.Context Namespace []string SecretNamespace []string NginxConfigurator *configs.Configurator @@ -227,6 +232,7 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc confClient: input.ConfClient, dynClient: input.DynClient, restConfig: input.RestConfig, + logger: nic_logger.LoggerFromContext(input.LoggerContext), configurator: input.NginxConfigurator, defaultServerSecret: input.DefaultServerSecret, appProtectEnabled: input.AppProtectEnabled, @@ -253,7 +259,9 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc weightChangesDynamicReload: input.DynamicWeightChangesReload, } eventBroadcaster := record.NewBroadcaster() - eventBroadcaster.StartLogging(glog.Infof) + eventBroadcaster.StartLogging(func(format string, args ...interface{}) { + lbc.logger.Info(fmt.Sprintf(format, args...)) + }) eventBroadcaster.StartRecordingToSink(&core_v1.EventSinkImpl{ Interface: core_v1.New(input.KubeClient.CoreV1().RESTClient()).Events(""), }) @@ -265,7 +273,8 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc if input.SpireAgentAddress != "" { lbc.spiffeCertFetcher, err = spiffe.NewX509CertFetcher(input.SpireAgentAddress, nil) if err != nil { - glog.Fatalf("failed to initialize spiffe certfetcher: %v", err) + lbc.logger.Log(lbc.ctx, levels.LevelFatal, fmt.Sprintf("failed to initialize spiffe certfetcher: %v", err)) + os.Exit(1) } } @@ -283,7 +292,7 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc lbc.externalDNSController = ed_controller.NewController(ed_controller.BuildOpts(context.TODO(), lbc.namespaceList, lbc.recorder, lbc.confClient, input.ResyncPeriod, isDynamicNs)) } - glog.V(3).Infof("Nginx Ingress Controller has class: %v", input.IngressClass) + lbc.logger.Debug(fmt.Sprintf("Nginx Ingress Controller has class: %v", input.IngressClass)) lbc.namespacedInformers = make(map[string]*namespacedInformer) for _, ns := range lbc.namespaceList { @@ -305,7 +314,7 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc if input.ConfigMaps != "" { nginxConfigMapsNS, nginxConfigMapsName, err := ParseNamespaceName(input.ConfigMaps) if err != nil { - glog.Warning(err) + lbc.logger.Warn(fmt.Sprint(err)) } else { lbc.watchNginxConfigMaps = true lbc.addConfigMapHandler(createConfigMapHandlers(lbc, nginxConfigMapsName), nginxConfigMapsNS) @@ -363,7 +372,8 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc exporter, err := telemetry.NewExporter(exporterCfg) if err != nil { - glog.Fatalf("failed to initialize telemetry exporter: %v", err) + lbc.logger.Log(lbc.ctx, levels.LevelFatal, fmt.Sprintf("failed to initialize telemetry exporter: %v", err)) + os.Exit(1) } collectorConfig := telemetry.CollectorConfig{ Period: 24 * time.Hour, @@ -388,7 +398,8 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc telemetry.WithExporter(exporter), ) if err != nil { - glog.Fatalf("failed to initialize telemetry collector: %v", err) + lbc.logger.Log(lbc.ctx, levels.LevelFatal, fmt.Sprintf("failed to initialize telemetry collector: %v", err)) + os.Exit(1) } lbc.telemetryCollector = collector lbc.telemetryChan = make(chan struct{}) @@ -551,7 +562,8 @@ func (lbc *LoadBalancerController) Run() { _, _, err := lbc.spiffeCertFetcher.Start(lbc.ctx) lbc.addInternalRouteServer() if err != nil { - glog.Fatal(err) + lbc.logger.Log(lbc.ctx, levels.LevelFatal, fmt.Sprint(err)) + os.Exit(1) } // wait for initial bundle @@ -561,14 +573,15 @@ func (lbc *LoadBalancerController) Run() { case cert := <-lbc.spiffeCertFetcher.CertCh: lbc.syncSVIDRotation(cert) case <-timeoutch: - glog.Fatal("Failed to download initial spiffe trust bundle") + lbc.logger.Log(lbc.ctx, levels.LevelFatal, "Failed to download initial spiffe trust bundle") + os.Exit(1) } go func() { for { select { case err := <-lbc.spiffeCertFetcher.WatchErrCh: - glog.Errorf("error watching for SVID rotations: %v", err) + lbc.logger.Error(fmt.Sprintf("error watching for SVID rotations: %v", err)) return case cert := <-lbc.spiffeCertFetcher.CertCh: lbc.syncSVIDRotation(cert) @@ -619,7 +632,7 @@ func (lbc *LoadBalancerController) Run() { totalCacheSyncs = append(totalCacheSyncs, nif.cacheSyncs...) } - glog.V(3).Infof("Waiting for %d caches to sync", len(totalCacheSyncs)) + lbc.logger.Debug(fmt.Sprintf("Waiting for %d caches to sync", len(totalCacheSyncs))) if !cache.WaitForCacheSync(lbc.ctx.Done(), totalCacheSyncs...) { return @@ -627,7 +640,7 @@ func (lbc *LoadBalancerController) Run() { lbc.preSyncSecrets() - glog.V(3).Infof("Starting the queue with %d initial elements", lbc.syncQueue.Len()) + lbc.logger.Debug(fmt.Sprintf("Starting the queue with %d initial elements", lbc.syncQueue.Len())) go lbc.syncQueue.Run(time.Second, lbc.ctx.Done()) <-lbc.ctx.Done() @@ -697,14 +710,14 @@ func (lbc *LoadBalancerController) updateNumberOfIngressControllerReplicas(contr found = true _, err := lbc.configurator.AddOrUpdateIngress(ingress) if err != nil { - glog.Errorf("Error updating ratelimit for Ingress %s/%s: %s", ingress.Ingress.Namespace, ingress.Ingress.Name, err) + lbc.logger.Error(fmt.Sprintf("Error updating ratelimit for Ingress %s/%s: %s", ingress.Ingress.Namespace, ingress.Ingress.Name, err)) } } for _, ingress := range resourceExes.MergeableIngresses { found = true _, err := lbc.configurator.AddOrUpdateMergeableIngress(ingress) if err != nil { - glog.Errorf("Error updating ratelimit for Ingress %s/%s: %s", ingress.Master.Ingress.Namespace, ingress.Master.Ingress.Name, err) + lbc.logger.Error(fmt.Sprintf("Error updating ratelimit for Ingress %s/%s: %s", ingress.Master.Ingress.Namespace, ingress.Master.Ingress.Name, err)) } } @@ -716,7 +729,7 @@ func (lbc *LoadBalancerController) updateNumberOfIngressControllerReplicas(contr found = true _, err := lbc.configurator.AddOrUpdateVirtualServer(vserver) if err != nil { - glog.Errorf("Error updating ratelimit for VirtualServer %s/%s: %s", vserver.VirtualServer.Namespace, vserver.VirtualServer.Name, err) + lbc.logger.Error(fmt.Sprintf("Error updating ratelimit for VirtualServer %s/%s: %s", vserver.VirtualServer.Namespace, vserver.VirtualServer.Name, err)) } } } @@ -841,7 +854,7 @@ func (lbc *LoadBalancerController) updateAllConfigs() { resources := lbc.configuration.GetResources() - glog.V(3).Infof("Updating %v resources", len(resources)) + lbc.logger.Debug(fmt.Sprintf("Updating %v resources", len(resources))) resourceExes := lbc.createExtendedResources(resources) @@ -887,17 +900,17 @@ func (lbc *LoadBalancerController) preSyncSecrets() { break } objects := ni.secretLister.List() - glog.V(3).Infof("PreSync %d Secrets", len(objects)) + lbc.logger.Debug(fmt.Sprintf("PreSync %d Secrets", len(objects))) for _, obj := range objects { secret := obj.(*api_v1.Secret) if !secrets.IsSupportedSecretType(secret.Type) { - glog.V(3).Infof("Ignoring Secret %s/%s of unsupported type %s", secret.Namespace, secret.Name, secret.Type) + lbc.logger.Debug(fmt.Sprintf("Ignoring Secret %s/%s of unsupported type %s", secret.Namespace, secret.Name, secret.Type)) continue } - glog.V(3).Infof("Adding Secret: %s/%s", secret.Namespace, secret.Name) + lbc.logger.Debug(fmt.Sprintf("Adding Secret: %s/%s", secret.Namespace, secret.Name)) lbc.secretStore.AddOrUpdateSecret(secret) } } @@ -908,15 +921,15 @@ func (lbc *LoadBalancerController) sync(task task) { lbc.configurator.DisableReloads() lbc.batchSyncEnabled = true - glog.V(3).Infof("Batch processing %v items", lbc.syncQueue.Len()) + lbc.logger.Debug(fmt.Sprintf("Batch processing %v items", lbc.syncQueue.Len())) } - glog.V(3).Infof("Syncing %v", task.Key) + lbc.logger.Debug(fmt.Sprintf("Syncing %v", task.Key)) if lbc.spiffeCertFetcher != nil { lbc.syncLock.Lock() defer lbc.syncLock.Unlock() } if lbc.batchSyncEnabled && task.Kind != endpointslice { - glog.V(3).Infof("Task is not endpointslice - enabling batch reload") + lbc.logger.Debug("Task is not endpointslice - enabling batch reload") lbc.enableBatchReload = true } switch task.Kind { @@ -932,7 +945,7 @@ func (lbc *LoadBalancerController) sync(task task) { case endpointslice: resourcesFound := lbc.syncEndpointSlices(task) if lbc.batchSyncEnabled && resourcesFound { - glog.V(3).Infof("Endpointslice %v is referenced - enabling batch reload", task.Key) + lbc.logger.Debug(fmt.Sprintf("Endpointslice %v is referenced - enabling batch reload", task.Key)) lbc.enableBatchReload = true } case secret: @@ -978,7 +991,7 @@ func (lbc *LoadBalancerController) sync(task task) { lbc.updateAllConfigs() lbc.isNginxReady = true - glog.V(3).Infof("NGINX is ready") + lbc.logger.Debug("NGINX is ready") } if lbc.batchSyncEnabled && lbc.syncQueue.Len() == 0 { @@ -988,12 +1001,12 @@ func (lbc *LoadBalancerController) sync(task task) { lbc.updateAllConfigs() } else { if err := lbc.configurator.ReloadForBatchUpdates(lbc.enableBatchReload); err != nil { - glog.Errorf("error reloading for batch updates: %v", err) + lbc.logger.Error(fmt.Sprintf("error reloading for batch updates: %v", err)) } } lbc.enableBatchReload = false - glog.V(3).Infof("Batch sync completed - disabling batch reload") + lbc.logger.Debug("Batch sync completed - disabling batch reload") } } @@ -1015,7 +1028,7 @@ func (lbc *LoadBalancerController) cleanupUnwatchedNamespacedResources(nsi *name il, err := nsi.ingressLister.List() if err != nil { - glog.Warningf("unable to list Ingress resources for recently unwatched namespace %s", nsi.namespace) + lbc.logger.Warn(fmt.Sprintf("unable to list Ingress resources for recently unwatched namespace %s", nsi.namespace)) } else { for _, ing := range il.Items { ing := ing // address gosec G601 @@ -1025,7 +1038,7 @@ func (lbc *LoadBalancerController) cleanupUnwatchedNamespacedResources(nsi *name } delIngErrs := lbc.configurator.BatchDeleteIngresses(delIngressList) if len(delIngErrs) > 0 { - glog.Warningf("Received error(s) deleting Ingress configurations from unwatched namespace: %v", delIngErrs) + lbc.logger.Warn(fmt.Sprintf("Received error(s) deleting Ingress configurations from unwatched namespace: %v", delIngErrs)) } } @@ -1039,7 +1052,7 @@ func (lbc *LoadBalancerController) cleanupUnwatchedNamespacedResources(nsi *name } delVsErrs := lbc.configurator.BatchDeleteVirtualServers(delVsList) if len(delVsErrs) > 0 { - glog.Warningf("Received error(s) deleting VirtualServer configurations from unwatched namespace: %v", delVsErrs) + lbc.logger.Warn(fmt.Sprintf("Received error(s) deleting VirtualServer configurations from unwatched namespace: %v", delVsErrs)) } var delTsList []string @@ -1052,7 +1065,7 @@ func (lbc *LoadBalancerController) cleanupUnwatchedNamespacedResources(nsi *name var updatedTSExes []*configs.TransportServerEx delTsErrs := lbc.configurator.UpdateTransportServers(updatedTSExes, delTsList) if len(delTsErrs) > 0 { - glog.Warningf("Received error(s) deleting TransportServer configurations from unwatched namespace: %v", delVsErrs) + lbc.logger.Warn(fmt.Sprintf("Received error(s) deleting TransportServer configurations from unwatched namespace: %v", delVsErrs)) } for _, obj := range nsi.virtualServerRouteLister.List() { @@ -1073,16 +1086,16 @@ func (lbc *LoadBalancerController) cleanupUnwatchedNamespacedResources(nsi *name resources := lbc.configuration.FindResourcesForSecret(sec.Namespace, sec.Name) lbc.secretStore.DeleteSecret(key) - glog.V(2).Infof("Deleting Secret: %v\n", key) + lbc.logger.Debug(fmt.Sprintf("Deleting Secret: %v\n", key)) if len(resources) > 0 { lbc.handleRegularSecretDeletion(resources) } if lbc.isSpecialSecret(key) { - glog.Warningf("A special TLS Secret %v was removed. Retaining the Secret.", key) + lbc.logger.Warn(fmt.Sprintf("A special TLS Secret %v was removed. Retaining the Secret.", key)) } } - glog.V(3).Infof("Finished cleaning up configuration for unwatched resources in namespace: %v", nsi.namespace) + lbc.logger.Debug(fmt.Sprintf("Finished cleaning up configuration for unwatched resources in namespace: %v", nsi.namespace)) nsi.stop() } @@ -1103,11 +1116,11 @@ func (lbc *LoadBalancerController) syncVirtualServer(task task) { var problems []ConfigurationProblem if !vsExists { - glog.V(2).Infof("Deleting VirtualServer: %v\n", key) + lbc.logger.Debug(fmt.Sprintf("Deleting VirtualServer: %v\n", key)) changes, problems = lbc.configuration.DeleteVirtualServer(key) } else { - glog.V(2).Infof("Adding or Updating VirtualServer: %v\n", key) + lbc.logger.Debug(fmt.Sprintf("Adding or Updating VirtualServer: %v\n", key)) vs := obj.(*conf_v1.VirtualServer) changes, problems = lbc.configuration.AddOrUpdateVirtualServer(vs) @@ -1118,7 +1131,7 @@ func (lbc *LoadBalancerController) syncVirtualServer(task task) { } func (lbc *LoadBalancerController) processProblems(problems []ConfigurationProblem) { - glog.V(3).Infof("Processing %v problems", len(problems)) + lbc.logger.Debug(fmt.Sprintf("Processing %v problems", len(problems))) for _, p := range problems { eventType := api_v1.EventTypeWarning @@ -1134,23 +1147,23 @@ func (lbc *LoadBalancerController) processProblems(problems []ConfigurationProbl case *networking.Ingress: err := lbc.statusUpdater.ClearIngressStatus(*obj) if err != nil { - glog.V(3).Infof("Error when updating the status for Ingress %v/%v: %v", obj.Namespace, obj.Name, err) + lbc.logger.Error(fmt.Sprintf("Error when updating the status for Ingress %v/%v: %v", obj.Namespace, obj.Name, err)) } case *conf_v1.VirtualServer: err := lbc.statusUpdater.UpdateVirtualServerStatus(obj, state, p.Reason, p.Message) if err != nil { - glog.Errorf("Error when updating the status for VirtualServer %v/%v: %v", obj.Namespace, obj.Name, err) + lbc.logger.Error(fmt.Sprintf("Error when updating the status for VirtualServer %v/%v: %v", obj.Namespace, obj.Name, err)) } case *conf_v1.TransportServer: err := lbc.statusUpdater.UpdateTransportServerStatus(obj, state, p.Reason, p.Message) if err != nil { - glog.Errorf("Error when updating the status for TransportServer %v/%v: %v", obj.Namespace, obj.Name, err) + lbc.logger.Error(fmt.Sprintf("Error when updating the status for TransportServer %v/%v: %v", obj.Namespace, obj.Name, err)) } case *conf_v1.VirtualServerRoute: var emptyVSes []*conf_v1.VirtualServer err := lbc.statusUpdater.UpdateVirtualServerRouteStatusWithReferencedBy(obj, state, p.Reason, p.Message, emptyVSes) if err != nil { - glog.Errorf("Error when updating the status for VirtualServerRoute %v/%v: %v", obj.Namespace, obj.Name, err) + lbc.logger.Error(fmt.Sprintf("Error when updating the status for VirtualServerRoute %v/%v: %v", obj.Namespace, obj.Name, err)) } } } @@ -1158,7 +1171,7 @@ func (lbc *LoadBalancerController) processProblems(problems []ConfigurationProbl } func (lbc *LoadBalancerController) processChanges(changes []ResourceChange) { - glog.V(3).Infof("Processing %v changes", len(changes)) + lbc.logger.Debug(fmt.Sprintf("Processing %v changes", len(changes))) for _, c := range changes { if c.Op == AddOrUpdate { @@ -1193,7 +1206,7 @@ func (lbc *LoadBalancerController) processChanges(changes []ResourceChange) { deleteErr := lbc.configurator.DeleteVirtualServer(key, false) if deleteErr != nil { - glog.Errorf("Error when deleting configuration for VirtualServer %v: %v", key, deleteErr) + lbc.logger.Error(fmt.Sprintf("Error when deleting configuration for VirtualServer %v: %v", key, deleteErr)) } var vsExists bool @@ -1202,7 +1215,7 @@ func (lbc *LoadBalancerController) processChanges(changes []ResourceChange) { ns, _, _ := cache.SplitMetaNamespaceKey(key) _, vsExists, err = lbc.getNamespacedInformer(ns).virtualServerLister.GetByKey(key) if err != nil { - glog.Errorf("Error when getting VirtualServer for %v: %v", key, err) + lbc.logger.Error(fmt.Sprintf("Error when getting VirtualServer for %v: %v", key, err)) } if vsExists { @@ -1211,11 +1224,11 @@ func (lbc *LoadBalancerController) processChanges(changes []ResourceChange) { case *IngressConfiguration: key := getResourceKey(&impl.Ingress.ObjectMeta) - glog.V(2).Infof("Deleting Ingress: %v\n", key) + lbc.logger.Debug(fmt.Sprintf("Deleting Ingress: %v\n", key)) deleteErr := lbc.configurator.DeleteIngress(key, false) if deleteErr != nil { - glog.Errorf("Error when deleting configuration for Ingress %v: %v", key, deleteErr) + lbc.logger.Error(fmt.Sprintf("Error when deleting configuration for Ingress %v: %v", key, deleteErr)) } var ingExists bool @@ -1224,7 +1237,7 @@ func (lbc *LoadBalancerController) processChanges(changes []ResourceChange) { ns, _, _ := cache.SplitMetaNamespaceKey(key) _, ingExists, err = lbc.getNamespacedInformer(ns).ingressLister.GetByKeySafe(key) if err != nil { - glog.Errorf("Error when getting Ingress for %v: %v", key, err) + lbc.logger.Error(fmt.Sprintf("Error when getting Ingress for %v: %v", key, err)) } if ingExists { @@ -1236,7 +1249,7 @@ func (lbc *LoadBalancerController) processChanges(changes []ResourceChange) { deleteErr := lbc.configurator.DeleteTransportServer(key) if deleteErr != nil { - glog.Errorf("Error when deleting configuration for TransportServer %v: %v", key, deleteErr) + lbc.logger.Error(fmt.Sprintf("Error when deleting configuration for TransportServer %v: %v", key, deleteErr)) } var tsExists bool @@ -1245,7 +1258,7 @@ func (lbc *LoadBalancerController) processChanges(changes []ResourceChange) { ns, _, _ := cache.SplitMetaNamespaceKey(key) _, tsExists, err = lbc.getNamespacedInformer(ns).transportServerLister.GetByKey(key) if err != nil { - glog.Errorf("Error when getting TransportServer for %v: %v", key, err) + lbc.logger.Error(fmt.Sprintf("Error when getting TransportServer for %v: %v", key, err)) } if tsExists { lbc.updateTransportServerStatusAndEventsOnDelete(impl, c.Error, deleteErr) @@ -1288,7 +1301,7 @@ func (lbc *LoadBalancerController) UpdateVirtualServerStatusAndEventsOnDelete(vs if lbc.reportCustomResourceStatusEnabled() { err := lbc.statusUpdater.UpdateVirtualServerStatus(vsConfig.VirtualServer, state, eventTitle, msg) if err != nil { - glog.Errorf("Error when updating the status for VirtualServer %v/%v: %v", vsConfig.VirtualServer.Namespace, vsConfig.VirtualServer.Name, err) + lbc.logger.Error(fmt.Sprintf("Error when updating the status for VirtualServer %v/%v: %v", vsConfig.VirtualServer.Namespace, vsConfig.VirtualServer.Name, err)) } } } @@ -1322,7 +1335,7 @@ func (lbc *LoadBalancerController) UpdateIngressStatusAndEventsOnDelete(ingConfi if lbc.reportStatusEnabled() { err := lbc.statusUpdater.ClearIngressStatus(*ingConfig.Ingress) if err != nil { - glog.V(3).Infof("Error clearing Ingress status: %v", err) + lbc.logger.Debug(fmt.Sprintf("Error clearing Ingress status: %v", err)) } } } @@ -1428,7 +1441,7 @@ func (lbc *LoadBalancerController) updateMergeableIngressStatusAndEvents(ingConf err := lbc.statusUpdater.BulkUpdateIngressStatus(ings) if err != nil { - glog.V(3).Infof("error updating ing status: %v", err) + lbc.logger.Error(fmt.Sprintf("error updating ing status: %v", err)) } } } @@ -1462,7 +1475,7 @@ func (lbc *LoadBalancerController) updateRegularIngressStatusAndEvents(ingConfig if lbc.reportStatusEnabled() { err := lbc.statusUpdater.UpdateIngressStatus(*ingConfig.Ingress) if err != nil { - glog.V(3).Infof("error updating ing status: %v", err) + lbc.logger.Error(fmt.Sprintf("error updating ingress status: %v", err)) } } } @@ -1500,7 +1513,7 @@ func (lbc *LoadBalancerController) updateVirtualServerStatusAndEvents(vsConfig * if lbc.reportCustomResourceStatusEnabled() { err := lbc.statusUpdater.UpdateVirtualServerStatus(vsConfig.VirtualServer, state, eventTitle, msg) if err != nil { - glog.Errorf("Error when updating the status for VirtualServer %v/%v: %v", vsConfig.VirtualServer.Namespace, vsConfig.VirtualServer.Name, err) + lbc.logger.Error(fmt.Sprintf("Error when updating the status for VirtualServer %v/%v: %v", vsConfig.VirtualServer.Namespace, vsConfig.VirtualServer.Name, err)) } } @@ -1531,7 +1544,7 @@ func (lbc *LoadBalancerController) updateVirtualServerStatusAndEvents(vsConfig * vss := []*conf_v1.VirtualServer{vsConfig.VirtualServer} err := lbc.statusUpdater.UpdateVirtualServerRouteStatusWithReferencedBy(vsr, vsrState, vsrEventTitle, msg, vss) if err != nil { - glog.Errorf("Error when updating the status for VirtualServerRoute %v/%v: %v", vsr.Namespace, vsr.Name, err) + lbc.logger.Error(fmt.Sprintf("Error when updating the status for VirtualServerRoute %v/%v: %v", vsr.Namespace, vsr.Name, err)) } } } @@ -1554,11 +1567,11 @@ func (lbc *LoadBalancerController) syncVirtualServerRoute(task task) { var problems []ConfigurationProblem if !exists { - glog.V(2).Infof("Deleting VirtualServerRoute: %v\n", key) + lbc.logger.Debug(fmt.Sprintf("Deleting VirtualServerRoute: %v", key)) changes, problems = lbc.configuration.DeleteVirtualServerRoute(key) } else { - glog.V(2).Infof("Adding or Updating VirtualServerRoute: %v\n", key) + lbc.logger.Debug(fmt.Sprintf("Adding or Updating VirtualServerRoute: %v", key)) vsr := obj.(*conf_v1.VirtualServerRoute) changes, problems = lbc.configuration.AddOrUpdateVirtualServerRoute(vsr) @@ -1585,11 +1598,11 @@ func (lbc *LoadBalancerController) syncIngress(task task) { var problems []ConfigurationProblem if !ingExists { - glog.V(2).Infof("Deleting Ingress: %v\n", key) + lbc.logger.Debug(fmt.Sprintf("Deleting Ingress: %v", key)) changes, problems = lbc.configuration.DeleteIngress(key) } else { - glog.V(2).Infof("Adding or Updating Ingress: %v\n", key) + lbc.logger.Debug(fmt.Sprintf("Adding or Updating Ingress: %v", key)) changes, problems = lbc.configuration.AddOrUpdateIngress(ing) } @@ -1650,7 +1663,7 @@ func (lbc *LoadBalancerController) syncSecret(task task) { namespace, name, err := ParseNamespaceName(key) if err != nil { - glog.Warningf("Secret key %v is invalid: %v", key, err) + lbc.logger.Warn(fmt.Sprintf("Secret key %v is invalid: %v", key, err)) return } obj, secrExists, err = lbc.getNamespacedInformer(namespace).secretLister.GetByKey(key) @@ -1670,23 +1683,23 @@ func (lbc *LoadBalancerController) syncSecret(task task) { resources = removeDuplicateResources(resources) } - glog.V(2).Infof("Found %v Resources with Secret %v", len(resources), key) + lbc.logger.Debug(fmt.Sprintf("Found %v Resources with Secret %v", len(resources), key)) if !secrExists { lbc.secretStore.DeleteSecret(key) - glog.V(2).Infof("Deleting Secret: %v\n", key) + lbc.logger.Debug(fmt.Sprintf("Deleting Secret: %v", key)) if len(resources) > 0 { lbc.handleRegularSecretDeletion(resources) } if lbc.isSpecialSecret(key) { - glog.Warningf("A special TLS Secret %v was removed. Retaining the Secret.", key) + lbc.logger.Warn(fmt.Sprintf("A special TLS Secret %v was removed. Retaining the Secret.", key)) } return } - glog.V(2).Infof("Adding / Updating Secret: %v\n", key) + lbc.logger.Debug(fmt.Sprintf("Adding / Updating Secret: %v", key)) secret := obj.(*api_v1.Secret) @@ -1738,7 +1751,7 @@ func (lbc *LoadBalancerController) handleSecretUpdate(secret *api_v1.Secret, res warnings, addOrUpdateErr = lbc.configurator.AddOrUpdateResources(resourceExes, !lbc.configurator.DynamicSSLReloadEnabled()) if addOrUpdateErr != nil { - glog.Errorf("Error when updating Secret %v: %v", secretNsName, addOrUpdateErr) + lbc.logger.Error(fmt.Sprintf("Error when updating Secret %v: %v", secretNsName, addOrUpdateErr)) lbc.recorder.Eventf(secret, api_v1.EventTypeWarning, "UpdatedWithError", "%v was updated, but not applied: %v", secretNsName, addOrUpdateErr) } @@ -1750,7 +1763,7 @@ func (lbc *LoadBalancerController) handleSpecialSecretUpdate(secret *api_v1.Secr secretNsName := secret.Namespace + "/" + secret.Name err := secrets.ValidateTLSSecret(secret) if err != nil { - glog.Errorf("Couldn't validate the special Secret %v: %v", secretNsName, err) + lbc.logger.Error(fmt.Sprintf("Couldn't validate the special Secret %v: %v", secretNsName, err)) lbc.recorder.Eventf(secret, api_v1.EventTypeWarning, "Rejected", "the special Secret %v was rejected, using the previous version: %v", secretNsName, err) return } @@ -1764,7 +1777,7 @@ func (lbc *LoadBalancerController) handleSpecialSecretUpdate(secret *api_v1.Secr err = lbc.configurator.AddOrUpdateSpecialTLSSecrets(secret, specialSecretsToUpdate) if err != nil { - glog.Errorf("Error when updating the special Secret %v: %v", secretNsName, err) + lbc.logger.Error(fmt.Sprintf("Error when updating the special Secret %v: %v", secretNsName, err)) lbc.recorder.Eventf(secret, api_v1.EventTypeWarning, "UpdatedWithError", "the special Secret %v was updated, but not applied: %v", secretNsName, err) return } @@ -1792,7 +1805,7 @@ func (lbc *LoadBalancerController) updateVirtualServersStatusFromEvents() error vs := obj.(*conf_v1.VirtualServer) if !lbc.HasCorrectIngressClass(vs) { - glog.V(3).Infof("Ignoring VirtualServer %v based on class %v", vs.Name, vs.Spec.IngressClass) + lbc.logger.Debug(fmt.Sprintf("Ignoring VirtualServer %v based on class %v", vs.Name, vs.Spec.IngressClass)) continue } @@ -1836,7 +1849,7 @@ func (lbc *LoadBalancerController) updateVirtualServerRoutesStatusFromEvents() e vsr := obj.(*conf_v1.VirtualServerRoute) if !lbc.HasCorrectIngressClass(vsr) { - glog.V(3).Infof("Ignoring VirtualServerRoute %v based on class %v", vsr.Name, vsr.Spec.IngressClass) + lbc.logger.Debug(fmt.Sprintf("Ignoring VirtualServerRoute %v based on class %v", vsr.Name, vsr.Spec.IngressClass)) continue } @@ -1913,7 +1926,7 @@ func (lbc *LoadBalancerController) createIngressEx(ing *networking.Ingress, vali secretRef := lbc.secretStore.GetSecret(secretKey) if secretRef.Error != nil { - glog.Warningf("Error trying to get the secret %v for Ingress %v: %v", secretName, ing.Name, secretRef.Error) + lbc.logger.Warn(fmt.Sprintf("Error trying to get the secret %v for Ingress %v: %v", secretName, ing.Name, secretRef.Error)) } ingEx.SecretRefs[secretName] = secretRef @@ -1925,7 +1938,7 @@ func (lbc *LoadBalancerController) createIngressEx(ing *networking.Ingress, vali secretRef := lbc.secretStore.GetSecret(secretKey) if secretRef.Error != nil { - glog.Warningf("Error trying to get the secret %v for Ingress %v/%v: %v", secretName, ing.Namespace, ing.Name, secretRef.Error) + lbc.logger.Warn(fmt.Sprintf("Error trying to get the secret %v for Ingress %v/%v: %v", secretName, ing.Namespace, ing.Name, secretRef.Error)) } ingEx.SecretRefs[secretName] = secretRef @@ -1938,7 +1951,7 @@ func (lbc *LoadBalancerController) createIngressEx(ing *networking.Ingress, vali secretRef := lbc.secretStore.GetSecret(secretKey) if secretRef.Error != nil { - glog.Warningf("Error trying to get the secret %v for Ingress %v/%v: %v", secretName, ing.Namespace, ing.Name, secretRef.Error) + lbc.logger.Warn(fmt.Sprintf("Error trying to get the secret %v for Ingress %v/%v: %v", secretName, ing.Namespace, ing.Name, secretRef.Error)) } ingEx.SecretRefs[secretName] = secretRef @@ -1947,7 +1960,7 @@ func (lbc *LoadBalancerController) createIngressEx(ing *networking.Ingress, vali if apPolicyAntn, exists := ingEx.Ingress.Annotations[configs.AppProtectPolicyAnnotation]; exists { policy, err := lbc.getAppProtectPolicy(ing) if err != nil { - glog.Warningf("Error Getting App Protect policy %v for Ingress %v/%v: %v", apPolicyAntn, ing.Namespace, ing.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error Getting App Protect policy %v for Ingress %v/%v: %v", apPolicyAntn, ing.Namespace, ing.Name, err)) } else { ingEx.AppProtectPolicy = policy } @@ -1956,7 +1969,7 @@ func (lbc *LoadBalancerController) createIngressEx(ing *networking.Ingress, vali if apLogConfAntn, exists := ingEx.Ingress.Annotations[configs.AppProtectLogConfAnnotation]; exists { logConf, err := lbc.getAppProtectLogConfAndDst(ing) if err != nil { - glog.Warningf("Error Getting App Protect Log Config %v for Ingress %v/%v: %v", apLogConfAntn, ing.Namespace, ing.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error Getting App Protect Log Config %v for Ingress %v/%v: %v", apLogConfAntn, ing.Namespace, ing.Name, err)) } else { ingEx.AppProtectLogs = logConf } @@ -1967,7 +1980,7 @@ func (lbc *LoadBalancerController) createIngressEx(ing *networking.Ingress, vali if dosProtectedAnnotationValue, exists := ingEx.Ingress.Annotations[configs.AppProtectDosProtectedAnnotation]; exists { dosResEx, err := lbc.dosConfiguration.GetValidDosEx(ing.Namespace, dosProtectedAnnotationValue) if err != nil { - glog.Warningf("Error Getting Dos Protected Resource %v for Ingress %v/%v: %v", dosProtectedAnnotationValue, ing.Namespace, ing.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error Getting Dos Protected Resource %v for Ingress %v/%v: %v", dosProtectedAnnotationValue, ing.Namespace, ing.Name, err)) } if dosResEx != nil { ingEx.DosEx = dosResEx @@ -1987,7 +2000,7 @@ func (lbc *LoadBalancerController) createIngressEx(ing *networking.Ingress, vali var external bool svc, err := lbc.getServiceForIngressBackend(ing.Spec.DefaultBackend, ing.Namespace) if err != nil { - glog.V(3).Infof("Error getting service %v: %v", ing.Spec.DefaultBackend.Service.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting service %v: %v", ing.Spec.DefaultBackend.Service.Name, err)) } else { podEndps, external, err = lbc.getEndpointsForIngressBackend(ing.Spec.DefaultBackend, svc) if err == nil && external && lbc.isNginxPlus { @@ -1996,7 +2009,7 @@ func (lbc *LoadBalancerController) createIngressEx(ing *networking.Ingress, vali } if err != nil { - glog.Warningf("Error retrieving endpoints for the service %v: %v", ing.Spec.DefaultBackend.Service.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error retrieving endpoints for the service %v: %v", ing.Spec.DefaultBackend.Service.Name, err)) } if svc != nil && !external && hasUseClusterIP { @@ -2035,7 +2048,7 @@ func (lbc *LoadBalancerController) createIngressEx(ing *networking.Ingress, vali for _, rule := range ing.Spec.Rules { if !validHosts[rule.Host] { - glog.V(3).Infof("Skipping host %s for Ingress %s", rule.Host, ing.Name) + lbc.logger.Debug(fmt.Sprintf("Skipping host %s for Ingress %s", rule.Host, ing.Name)) continue } @@ -2048,14 +2061,14 @@ func (lbc *LoadBalancerController) createIngressEx(ing *networking.Ingress, vali path := path // address gosec G601 podEndps := []podEndpoint{} if validMinionPaths != nil && !validMinionPaths[path.Path] { - glog.V(3).Infof("Skipping path %s for minion Ingress %s", path.Path, ing.Name) + lbc.logger.Debug(fmt.Sprintf("Skipping path %s for minion Ingress %s", path.Path, ing.Name)) continue } var external bool svc, err := lbc.getServiceForIngressBackend(&path.Backend, ing.Namespace) if err != nil { - glog.V(3).Infof("Error getting service %v: %v", &path.Backend.Service.Name, err) + lbc.logger.Debug(fmt.Sprintf("Error getting service %v: %v", &path.Backend.Service.Name, err)) } else { podEndps, external, err = lbc.getEndpointsForIngressBackend(&path.Backend, svc) if err == nil && external && lbc.isNginxPlus { @@ -2064,7 +2077,7 @@ func (lbc *LoadBalancerController) createIngressEx(ing *networking.Ingress, vali } if err != nil { - glog.Warningf("Error retrieving endpoints for the service %v: %v", path.Backend.Service.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error retrieving endpoints for the service %v: %v", path.Backend.Service.Name, err)) } if svc != nil && !external && hasUseClusterIP { @@ -2130,7 +2143,7 @@ func (lbc *LoadBalancerController) createVirtualServerEx(virtualServer *conf_v1. scrtRef := lbc.secretStore.GetSecret(scrtKey) if scrtRef.Error != nil { - glog.Warningf("Error trying to get the secret %v for VirtualServer %v: %v", scrtKey, virtualServer.Name, scrtRef.Error) + lbc.logger.Warn(fmt.Sprintf("Error trying to get the secret %v for VirtualServer %v: %v", scrtKey, virtualServer.Name, scrtRef.Error)) } virtualServerEx.SecretRefs[scrtKey] = scrtRef @@ -2138,43 +2151,43 @@ func (lbc *LoadBalancerController) createVirtualServerEx(virtualServer *conf_v1. policies, policyErrors := lbc.getPolicies(virtualServer.Spec.Policies, virtualServer.Namespace) for _, err := range policyErrors { - glog.Warningf("Error getting policy for VirtualServer %s/%s: %v", virtualServer.Namespace, virtualServer.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting policy for VirtualServer %s/%s: %v", virtualServer.Namespace, virtualServer.Name, err)) } err := lbc.addJWTSecretRefs(virtualServerEx.SecretRefs, policies) if err != nil { - glog.Warningf("Error getting JWT secrets for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting JWT secrets for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err)) } err = lbc.addBasicSecretRefs(virtualServerEx.SecretRefs, policies) if err != nil { - glog.Warningf("Error getting Basic Auth secrets for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting Basic Auth secrets for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err)) } err = lbc.addIngressMTLSSecretRefs(virtualServerEx.SecretRefs, policies) if err != nil { - glog.Warningf("Error getting IngressMTLS secret for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting IngressMTLS secret for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err)) } err = lbc.addEgressMTLSSecretRefs(virtualServerEx.SecretRefs, policies) if err != nil { - glog.Warningf("Error getting EgressMTLS secrets for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting EgressMTLS secrets for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err)) } err = lbc.addOIDCSecretRefs(virtualServerEx.SecretRefs, policies) if err != nil { - glog.Warningf("Error getting OIDC secrets for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting OIDC secrets for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err)) } err = lbc.addAPIKeySecretRefs(virtualServerEx.SecretRefs, policies) if err != nil { - glog.Warningf("Error getting APIKey secrets for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting APIKey secrets for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err)) } err = lbc.addWAFPolicyRefs(virtualServerEx.ApPolRefs, virtualServerEx.LogConfRefs, policies) if err != nil { - glog.Warningf("Error getting App Protect resource for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting App Protect resource for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err)) } if virtualServer.Spec.Dos != "" { dosEx, err := lbc.dosConfiguration.GetValidDosEx(virtualServer.Namespace, virtualServer.Spec.Dos) if err != nil { - glog.Warningf("Error getting App Protect Dos resource for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting App Protect Dos resource for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err)) } if dosEx != nil { virtualServerEx.DosProtectedEx[""] = dosEx @@ -2195,7 +2208,7 @@ func (lbc *LoadBalancerController) createVirtualServerEx(virtualServer *conf_v1. backupEndpointsKey := configs.GenerateEndpointsKey(virtualServer.Namespace, u.Backup, u.Subselector, *u.BackupPort) backupEndps, external, err := lbc.getEndpointsForUpstream(virtualServer.Namespace, u.Backup, *u.BackupPort) if err != nil { - glog.Warningf("Error getting Endpoints for Upstream %v: %v", u.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting Endpoints for Upstream %v: %v", u.Name, err)) } if err == nil && external { externalNameSvcs[configs.GenerateExternalNameSvcKey(virtualServer.Namespace, u.Backup)] = true @@ -2211,7 +2224,7 @@ func (lbc *LoadBalancerController) createVirtualServerEx(virtualServer *conf_v1. if u.UseClusterIP { s, err := lbc.getServiceForUpstream(virtualServer.Namespace, u.Service, u.Port) if err != nil { - glog.Warningf("Error getting Service for Upstream %v: %v", u.Service, err) + lbc.logger.Warn(fmt.Sprintf("Error getting Service for Upstream %v: %v", u.Service, err)) } else { endps = append(endps, ipv6SafeAddrPort(s.Spec.ClusterIP, int32(u.Port))) } @@ -2232,7 +2245,7 @@ func (lbc *LoadBalancerController) createVirtualServerEx(virtualServer *conf_v1. } if err != nil { - glog.Warningf("Error getting Endpoints for Upstream %v: %v", u.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting Endpoints for Upstream %v: %v", u.Name, err)) } endps = getIPAddressesFromEndpoints(podEndps) @@ -2254,44 +2267,44 @@ func (lbc *LoadBalancerController) createVirtualServerEx(virtualServer *conf_v1. for _, r := range virtualServer.Spec.Routes { vsRoutePolicies, policyErrors := lbc.getPolicies(r.Policies, virtualServer.Namespace) for _, err := range policyErrors { - glog.Warningf("Error getting policy for VirtualServer %s/%s: %v", virtualServer.Namespace, virtualServer.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting policy for VirtualServer %s/%s: %v", virtualServer.Namespace, virtualServer.Name, err)) } policies = append(policies, vsRoutePolicies...) err = lbc.addJWTSecretRefs(virtualServerEx.SecretRefs, vsRoutePolicies) if err != nil { - glog.Warningf("Error getting JWT secrets for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting JWT secrets for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err)) } err = lbc.addBasicSecretRefs(virtualServerEx.SecretRefs, vsRoutePolicies) if err != nil { - glog.Warningf("Error getting Basic Auth secrets for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting Basic Auth secrets for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err)) } err = lbc.addEgressMTLSSecretRefs(virtualServerEx.SecretRefs, vsRoutePolicies) if err != nil { - glog.Warningf("Error getting EgressMTLS secrets for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting EgressMTLS secrets for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err)) } err = lbc.addWAFPolicyRefs(virtualServerEx.ApPolRefs, virtualServerEx.LogConfRefs, vsRoutePolicies) if err != nil { - glog.Warningf("Error getting WAF policies for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting WAF policies for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err)) } if r.Dos != "" { routeDosEx, err := lbc.dosConfiguration.GetValidDosEx(virtualServer.Namespace, r.Dos) if err != nil { - glog.Warningf("Error getting App Protect Dos resource for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting App Protect Dos resource for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err)) } virtualServerEx.DosProtectedEx[r.Path] = routeDosEx } err = lbc.addOIDCSecretRefs(virtualServerEx.SecretRefs, vsRoutePolicies) if err != nil { - glog.Warningf("Error getting OIDC secrets for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting OIDC secrets for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err)) } err = lbc.addAPIKeySecretRefs(virtualServerEx.SecretRefs, vsRoutePolicies) if err != nil { - glog.Warningf("Error getting APIKey secrets for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting APIKey secrets for VirtualServer %v/%v: %v", virtualServer.Namespace, virtualServer.Name, err)) } } @@ -2300,44 +2313,44 @@ func (lbc *LoadBalancerController) createVirtualServerEx(virtualServer *conf_v1. for _, sr := range vsr.Spec.Subroutes { vsrSubroutePolicies, policyErrors := lbc.getPolicies(sr.Policies, vsr.Namespace) for _, err := range policyErrors { - glog.Warningf("Error getting policy for VirtualServerRoute %s/%s: %v", vsr.Namespace, vsr.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting policy for VirtualServerRoute %s/%s: %v", vsr.Namespace, vsr.Name, err)) } policies = append(policies, vsrSubroutePolicies...) err = lbc.addJWTSecretRefs(virtualServerEx.SecretRefs, vsrSubroutePolicies) if err != nil { - glog.Warningf("Error getting JWT secrets for VirtualServerRoute %v/%v: %v", vsr.Namespace, vsr.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting JWT secrets for VirtualServerRoute %v/%v: %v", vsr.Namespace, vsr.Name, err)) } err = lbc.addBasicSecretRefs(virtualServerEx.SecretRefs, vsrSubroutePolicies) if err != nil { - glog.Warningf("Error getting Basic Auth secrets for VirtualServerRoute %v/%v: %v", vsr.Namespace, vsr.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting Basic Auth secrets for VirtualServerRoute %v/%v: %v", vsr.Namespace, vsr.Name, err)) } err = lbc.addEgressMTLSSecretRefs(virtualServerEx.SecretRefs, vsrSubroutePolicies) if err != nil { - glog.Warningf("Error getting EgressMTLS secrets for VirtualServerRoute %v/%v: %v", vsr.Namespace, vsr.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting EgressMTLS secrets for VirtualServerRoute %v/%v: %v", vsr.Namespace, vsr.Name, err)) } err = lbc.addOIDCSecretRefs(virtualServerEx.SecretRefs, vsrSubroutePolicies) if err != nil { - glog.Warningf("Error getting OIDC secrets for VirtualServerRoute %v/%v: %v", vsr.Namespace, vsr.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting OIDC secrets for VirtualServerRoute %v/%v: %v", vsr.Namespace, vsr.Name, err)) } err = lbc.addAPIKeySecretRefs(virtualServerEx.SecretRefs, vsrSubroutePolicies) if err != nil { - glog.Warningf("Error getting APIKey secrets for VirtualServerRoute %v/%v: %v", vsr.Namespace, vsr.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting APIKey secrets for VirtualServerRoute %v/%v: %v", vsr.Namespace, vsr.Name, err)) } err = lbc.addWAFPolicyRefs(virtualServerEx.ApPolRefs, virtualServerEx.LogConfRefs, vsrSubroutePolicies) if err != nil { - glog.Warningf("Error getting WAF policies for VirtualServerRoute %v/%v: %v", vsr.Namespace, vsr.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting WAF policies for VirtualServerRoute %v/%v: %v", vsr.Namespace, vsr.Name, err)) } if sr.Dos != "" { routeDosEx, err := lbc.dosConfiguration.GetValidDosEx(vsr.Namespace, sr.Dos) if err != nil { - glog.Warningf("Error getting App Protect Dos resource for VirtualServerRoute %v/%v: %v", vsr.Namespace, vsr.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting App Protect Dos resource for VirtualServerRoute %v/%v: %v", vsr.Namespace, vsr.Name, err)) } virtualServerEx.DosProtectedEx[sr.Path] = routeDosEx } @@ -2350,7 +2363,7 @@ func (lbc *LoadBalancerController) createVirtualServerEx(virtualServer *conf_v1. if u.UseClusterIP { s, err := lbc.getServiceForUpstream(vsr.Namespace, u.Service, u.Port) if err != nil { - glog.Warningf("Error getting Service for Upstream %v: %v", u.Service, err) + lbc.logger.Warn(fmt.Sprintf("Error getting Service for Upstream %v: %v", u.Service, err)) } else { endps = append(endps, fmt.Sprintf("%s:%d", s.Spec.ClusterIP, u.Port)) } @@ -2369,7 +2382,7 @@ func (lbc *LoadBalancerController) createVirtualServerEx(virtualServer *conf_v1. } } if err != nil { - glog.Warningf("Error getting Endpoints for Upstream %v: %v", u.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting Endpoints for Upstream %v: %v", u.Name, err)) } endps = getIPAddressesFromEndpoints(podEndps) @@ -2418,7 +2431,7 @@ func (lbc *LoadBalancerController) getAllPolicies() []*conf_v1.Policy { err := validation.ValidatePolicy(pol, lbc.isNginxPlus, lbc.enableOIDC, lbc.appProtectEnabled) if err != nil { - glog.V(3).Infof("Skipping invalid Policy %s/%s: %v", pol.Namespace, pol.Name, err) + lbc.logger.Debug(fmt.Sprintf("Skipping invalid Policy %s/%s: %v", pol.Namespace, pol.Name, err)) continue } @@ -2639,7 +2652,7 @@ func (lbc *LoadBalancerController) getTransportServerBackupEndpointsAndKey(trans backupEndpointsKey := configs.GenerateEndpointsKey(transportServer.Namespace, u.Backup, nil, *u.BackupPort) backupEndps, external, err := lbc.getEndpointsForUpstream(transportServer.Namespace, u.Backup, *u.BackupPort) if err != nil { - glog.Warningf("Error getting Endpoints for Upstream %v: %v", u.Name, err) + lbc.logger.Warn(fmt.Sprintf("Error getting Endpoints for Upstream %v: %v", u.Name, err)) } if err == nil && external { externalNameSvcs[configs.GenerateExternalNameSvcKey(transportServer.Namespace, u.Backup)] = true @@ -2712,7 +2725,7 @@ func (lbc *LoadBalancerController) getEndpointsForServiceWithSubselector(targetP var svcEndpointSlices []discovery_v1.EndpointSlice svcEndpointSlices, err = nsi.endpointSliceLister.GetServiceEndpointSlices(svc) if err != nil { - glog.V(3).Infof("Error getting endpointslices for service %s from the cache: %v", svc.Name, err) + lbc.logger.Debug(fmt.Sprintf("Error getting endpointslices for service %s from the cache: %v", svc.Name, err)) return nil, err } @@ -2796,7 +2809,7 @@ func getPodName(pod *api_v1.ObjectReference) string { func (lbc *LoadBalancerController) getHealthChecksForIngressBackend(backend *networking.IngressBackend, namespace string) *api_v1.Probe { svc, err := lbc.getServiceForIngressBackend(backend, namespace) if err != nil { - glog.V(3).Infof("Error getting service %v: %v", backend.Service.Name, err) + lbc.logger.Debug(fmt.Sprintf("Error getting service %v: %v", backend.Service.Name, err)) return nil } svcPort := lbc.getServicePortForIngressPort(backend.Service.Port, svc) @@ -2807,7 +2820,7 @@ func (lbc *LoadBalancerController) getHealthChecksForIngressBackend(backend *net nsi := lbc.getNamespacedInformer(svc.Namespace) pods, err = nsi.podLister.ListByNamespace(svc.Namespace, labels.Set(svc.Spec.Selector).AsSelector()) if err != nil { - glog.V(3).Infof("Error fetching pods for namespace %v: %v", svc.Namespace, err) + lbc.logger.Debug(fmt.Sprintf("Error fetching pods for namespace %v: %v", svc.Namespace, err)) return nil } return findProbeForPods(pods, svcPort) @@ -2866,13 +2879,13 @@ func (lbc *LoadBalancerController) getEndpointsForIngressBackend(backend *networ result = lbc.getExternalEndpointsForIngressBackend(backend, svc) return result, true, nil } - glog.V(3).Infof("Error getting endpoints for service %s from the cache: %v", svc.Name, err) + lbc.logger.Debug(fmt.Sprintf("Error getting endpoints for service %s from the cache: %v", svc.Name, err)) return nil, false, err } result, err = lbc.getEndpointsForPortFromEndpointSlices(endpointSlices, backend.Service.Port, svc) if err != nil { - glog.V(3).Infof("Error getting endpointslices for service %s port %v: %v", svc.Name, configs.GetBackendPortAsString(backend.Service.Port), err) + lbc.logger.Debug(fmt.Sprintf("Error getting endpointslices for service %s port %v: %v", svc.Name, configs.GetBackendPortAsString(backend.Service.Port), err)) return nil, false, err } return result, false, nil @@ -2931,7 +2944,7 @@ func (lbc *LoadBalancerController) getPodOwnerTypeAndNameFromAddress(ns, name st obj, exists, err = lbc.getNamespacedInformer(ns).podLister.GetByKey(fmt.Sprintf("%s/%s", ns, name)) if err != nil { - glog.Warningf("could not get pod by key %s/%s: %v", ns, name, err) + lbc.logger.Warn(fmt.Sprintf("could not get pod by key %s/%s: %v", ns, name, err)) return "", "" } if exists { @@ -3044,7 +3057,7 @@ func (lbc *LoadBalancerController) HasCorrectIngressClass(obj interface{}) bool class = *obj.Spec.IngressClassName } else if class != "" { // the annotation takes precedence over the field - glog.Warningln("Using the DEPRECATED annotation 'kubernetes.io/ingress.class'. The 'ingressClassName' field will be ignored.") + lbc.logger.Warn(fmt.Sprintln("Using the DEPRECATED annotation 'kubernetes.io/ingress.class'. The 'ingressClassName' field will be ignored.")) } return class == lbc.ingressClass @@ -3059,7 +3072,7 @@ func (lbc *LoadBalancerController) HasCorrectIngressClass(obj interface{}) bool func (lbc *LoadBalancerController) isHealthCheckEnabled(ing *networking.Ingress) bool { if healthCheckEnabled, exists, err := configs.GetMapKeyAsBool(ing.Annotations, "nginx.com/health-checks", ing); exists { if err != nil { - glog.Error(err) + lbc.logger.Error(fmt.Sprint(err)) } return healthCheckEnabled } @@ -3073,10 +3086,10 @@ func formatWarningMessages(w []string) string { func (lbc *LoadBalancerController) syncSVIDRotation(svidResponse *workloadapi.X509Context) { lbc.syncLock.Lock() defer lbc.syncLock.Unlock() - glog.V(3).Info("Rotating SPIFFE Certificates") + lbc.logger.Debug("Rotating SPIFFE Certificates") err := lbc.configurator.AddOrUpdateSpiffeCerts(svidResponse) if err != nil { - glog.Errorf("failed to rotate SPIFFE certificates: %v", err) + lbc.logger.Error(fmt.Sprintf("failed to rotate SPIFFE certificates: %v", err)) } } @@ -3088,7 +3101,7 @@ func (lbc *LoadBalancerController) IsNginxReady() bool { func (lbc *LoadBalancerController) addInternalRouteServer() { if lbc.internalRoutesEnabled { if err := lbc.configurator.AddInternalRouteConfig(); err != nil { - glog.Warningf("failed to configure internal route server: %v", err) + lbc.logger.Warn(fmt.Sprintf("failed to configure internal route server: %v", err)) } } } @@ -3307,7 +3320,7 @@ func (lbc *LoadBalancerController) haltIfVSConfigInvalid(vsNew *conf_v1.VirtualS deleteErr := lbc.configurator.DeleteVirtualServer(key, false) if deleteErr != nil { - glog.Errorf("Error when deleting configuration for VirtualServer %v: %v", key, deleteErr) + lbc.logger.Error(fmt.Sprintf("Error when deleting configuration for VirtualServer %v: %v", key, deleteErr)) } var vsExists bool @@ -3316,7 +3329,7 @@ func (lbc *LoadBalancerController) haltIfVSConfigInvalid(vsNew *conf_v1.VirtualS ns, _, _ := cache.SplitMetaNamespaceKey(key) _, vsExists, err = lbc.getNamespacedInformer(ns).virtualServerLister.GetByKey(key) if err != nil { - glog.Errorf("Error when getting VirtualServer for %v: %v", key, err) + lbc.logger.Error(fmt.Sprintf("Error when getting VirtualServer for %v: %v", key, err)) } if vsExists { @@ -3361,7 +3374,7 @@ func (lbc *LoadBalancerController) haltIfVSRConfigInvalid(vsrNew *conf_v1.Virtua } if vsEx == nil { - glog.V(3).Infof("VirtualServerRoute %s does not have a corresponding VirtualServer", vsrNew.Name) + lbc.logger.Debug(fmt.Sprintf("VirtualServerRoute %s does not have a corresponding VirtualServer", vsrNew.Name)) return true, nil } diff --git a/internal/k8s/controller_test.go b/internal/k8s/controller_test.go index a87f85b749..8fa7489683 100644 --- a/internal/k8s/controller_test.go +++ b/internal/k8s/controller_test.go @@ -1,6 +1,7 @@ package k8s import ( + "context" "errors" "fmt" "reflect" @@ -9,6 +10,7 @@ import ( "testing" "time" + nic_logger "github.com/nginxinc/kubernetes-ingress/internal/logger" "github.com/nginxinc/kubernetes-ingress/internal/telemetry" discovery_v1 "k8s.io/api/discovery/v1" @@ -44,6 +46,7 @@ func TestHasCorrectIngressClass(t *testing.T) { &LoadBalancerController{ ingressClass: ingressClass, metricsCollector: collectors.NewControllerFakeCollector(), + logger: nic_logger.LoggerFromContext(context.Background()), }, &networking.Ingress{ ObjectMeta: meta_v1.ObjectMeta{ @@ -56,6 +59,7 @@ func TestHasCorrectIngressClass(t *testing.T) { &LoadBalancerController{ ingressClass: ingressClass, metricsCollector: collectors.NewControllerFakeCollector(), + logger: nic_logger.LoggerFromContext(context.Background()), }, &networking.Ingress{ ObjectMeta: meta_v1.ObjectMeta{ @@ -68,6 +72,7 @@ func TestHasCorrectIngressClass(t *testing.T) { &LoadBalancerController{ ingressClass: ingressClass, metricsCollector: collectors.NewControllerFakeCollector(), + logger: nic_logger.LoggerFromContext(context.Background()), }, &networking.Ingress{ ObjectMeta: meta_v1.ObjectMeta{ @@ -80,6 +85,7 @@ func TestHasCorrectIngressClass(t *testing.T) { &LoadBalancerController{ ingressClass: ingressClass, metricsCollector: collectors.NewControllerFakeCollector(), + logger: nic_logger.LoggerFromContext(context.Background()), }, &networking.Ingress{ ObjectMeta: meta_v1.ObjectMeta{ @@ -92,6 +98,7 @@ func TestHasCorrectIngressClass(t *testing.T) { &LoadBalancerController{ ingressClass: ingressClass, metricsCollector: collectors.NewControllerFakeCollector(), + logger: nic_logger.LoggerFromContext(context.Background()), }, &networking.Ingress{ Spec: networking.IngressSpec{ @@ -104,6 +111,7 @@ func TestHasCorrectIngressClass(t *testing.T) { &LoadBalancerController{ ingressClass: ingressClass, metricsCollector: collectors.NewControllerFakeCollector(), + logger: nic_logger.LoggerFromContext(context.Background()), }, &networking.Ingress{ Spec: networking.IngressSpec{ @@ -116,6 +124,7 @@ func TestHasCorrectIngressClass(t *testing.T) { &LoadBalancerController{ ingressClass: ingressClass, metricsCollector: collectors.NewControllerFakeCollector(), + logger: nic_logger.LoggerFromContext(context.Background()), }, &networking.Ingress{ ObjectMeta: meta_v1.ObjectMeta{ @@ -131,6 +140,7 @@ func TestHasCorrectIngressClass(t *testing.T) { &LoadBalancerController{ ingressClass: ingressClass, metricsCollector: collectors.NewControllerFakeCollector(), + logger: nic_logger.LoggerFromContext(context.Background()), }, &networking.Ingress{ Spec: networking.IngressSpec{ @@ -143,6 +153,7 @@ func TestHasCorrectIngressClass(t *testing.T) { &LoadBalancerController{ ingressClass: ingressClass, metricsCollector: collectors.NewControllerFakeCollector(), + logger: nic_logger.LoggerFromContext(context.Background()), }, &networking.Ingress{ Spec: networking.IngressSpec{}, @@ -186,6 +197,7 @@ func TestIngressClassForCustomResources(t *testing.T) { t.Parallel() ctrl := &LoadBalancerController{ ingressClass: "nginx", + logger: nic_logger.LoggerFromContext(context.Background()), } tests := []struct { @@ -439,6 +451,7 @@ func TestGetServicePortForIngressPort(t *testing.T) { ingressClass: "nginx", configurator: cnf, metricsCollector: collectors.NewControllerFakeCollector(), + logger: nic_logger.LoggerFromContext(context.Background()), } svc := api_v1.Service{ TypeMeta: meta_v1.TypeMeta{}, @@ -507,6 +520,7 @@ func TestGetEndpointsFromEndpointSlices_DuplicateEndpointsInOneEndpointSlice(t * lbc := LoadBalancerController{ isNginxPlus: true, + logger: nic_logger.LoggerFromContext(context.Background()), } backendServicePort := networking.ServiceBackendPort{ @@ -597,6 +611,7 @@ func TestGetEndpointsFromEndpointSlices_TwoDifferentEndpointsInOnEndpointSlice(t lbc := LoadBalancerController{ isNginxPlus: true, + logger: nic_logger.LoggerFromContext(context.Background()), } backendServicePort := networking.ServiceBackendPort{ @@ -689,6 +704,7 @@ func TestGetEndpointsFromEndpointSlices_DuplicateEndpointsAcrossTwoEndpointSlice lbc := LoadBalancerController{ isNginxPlus: true, + logger: nic_logger.LoggerFromContext(context.Background()), } backendServicePort := networking.ServiceBackendPort{ @@ -810,6 +826,7 @@ func TestGetEndpointsFromEndpointSlices_TwoDifferentEndpointsInOnEndpointSliceOn lbc := LoadBalancerController{ isNginxPlus: true, + logger: nic_logger.LoggerFromContext(context.Background()), } backendServicePort := networking.ServiceBackendPort{ @@ -900,6 +917,7 @@ func TestGetEndpointsFromEndpointSlices_TwoDifferentEndpointsAcrossTwoEndpointSl lbc := LoadBalancerController{ isNginxPlus: true, + logger: nic_logger.LoggerFromContext(context.Background()), } backendServicePort := networking.ServiceBackendPort{ @@ -1000,6 +1018,7 @@ func TestGetEndpointsFromEndpointSlices_ErrorsOnInvalidTargetPort(t *testing.T) lbc := LoadBalancerController{ isNginxPlus: true, + logger: nic_logger.LoggerFromContext(context.Background()), } backendServicePort := networking.ServiceBackendPort{ @@ -1071,6 +1090,7 @@ func TestGetEndpointsFromEndpointSlices_ErrorsOnNoEndpointSlicesFound(t *testing t.Parallel() lbc := LoadBalancerController{ isNginxPlus: true, + logger: nic_logger.LoggerFromContext(context.Background()), } backendServicePort := networking.ServiceBackendPort{ @@ -2061,6 +2081,7 @@ func TestGetPoliciesGlobalWatch(t *testing.T) { lbc := LoadBalancerController{ isNginxPlus: true, namespacedInformers: nsi, + logger: nic_logger.LoggerFromContext(context.Background()), } policyRefs := []conf_v1.PolicyReference{ @@ -2162,6 +2183,7 @@ func TestGetPoliciesNamespacedWatch(t *testing.T) { lbc := LoadBalancerController{ isNginxPlus: true, namespacedInformers: nsi, + logger: nic_logger.LoggerFromContext(context.Background()), } policyRefs := []conf_v1.PolicyReference{ @@ -2784,6 +2806,7 @@ func TestAddJWTSecrets(t *testing.T) { Error: invalidErr, }, }), + logger: nic_logger.LoggerFromContext(context.Background()), } for _, test := range tests { @@ -2909,6 +2932,7 @@ func TestAddBasicSecrets(t *testing.T) { Error: invalidErr, }, }), + logger: nic_logger.LoggerFromContext(context.Background()), } for _, test := range tests { @@ -3032,6 +3056,7 @@ func TestAddIngressMTLSSecret(t *testing.T) { Error: invalidErr, }, }), + logger: nic_logger.LoggerFromContext(context.Background()), } for _, test := range tests { @@ -3251,6 +3276,7 @@ func TestAddEgressMTLSSecrets(t *testing.T) { Error: invalidErr, }, }), + logger: nic_logger.LoggerFromContext(context.Background()), } for _, test := range tests { @@ -3374,6 +3400,7 @@ func TestAddOidcSecret(t *testing.T) { Error: invalidErr, }, }), + logger: nic_logger.LoggerFromContext(context.Background()), } for _, test := range tests { @@ -3419,6 +3446,7 @@ func TestPreSyncSecrets(t *testing.T) { isNginxPlus: true, secretStore: secrets.NewEmptyFakeSecretsStore(), namespacedInformers: nsi, + logger: nic_logger.LoggerFromContext(context.Background()), } lbc.preSyncSecrets() @@ -3450,6 +3478,7 @@ func TestNewTelemetryCollector(t *testing.T) { input: NewLoadBalancerControllerInput{ KubeClient: fake.NewSimpleClientset(), EnableTelemetryReporting: true, + LoggerContext: context.Background(), }, expectedCollector: telemetry.Collector{ Config: telemetry.CollectorConfig{ @@ -3463,6 +3492,7 @@ func TestNewTelemetryCollector(t *testing.T) { input: NewLoadBalancerControllerInput{ KubeClient: fake.NewSimpleClientset(), EnableTelemetryReporting: false, + LoggerContext: context.Background(), }, expectedCollector: telemetry.Collector{}, },