From 4dd49959fa22ef44687029f0f557f33f42ae61e7 Mon Sep 17 00:00:00 2001 From: k8s-infra-cherrypick-robot <90416843+k8s-infra-cherrypick-robot@users.noreply.github.com> Date: Thu, 28 Aug 2025 10:41:09 -0700 Subject: [PATCH] Metrics: Fix `nginx_ingress_controller_config_last_reload_successful`. (#13860) Signed-off-by: Roman Orudzhov Co-authored-by: Roman Orudzhov --- internal/ingress/controller/controller.go | 22 ++++++++++++++-------- internal/ingress/controller/nginx.go | 2 ++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/internal/ingress/controller/controller.go b/internal/ingress/controller/controller.go index 0894c0dfc..87a343962 100644 --- a/internal/ingress/controller/controller.go +++ b/internal/ingress/controller/controller.go @@ -192,7 +192,18 @@ func (n *NGINXController) syncIngress(interface{}) error { n.metricCollector.SetSSLExpireTime(servers) n.metricCollector.SetSSLInfo(servers) + hash, err := hashstructure.Hash(pcfg, hashstructure.FormatV1, &hashstructure.HashOptions{ + TagName: "json", + }) + if err != nil { + klog.Errorf("unexpected error hashing configuration: %v", err) + } + if n.runningConfig.Equal(pcfg) { + if !n.lastConfigSuccess { + n.metricCollector.ConfigSuccess(hash, true) + n.lastConfigSuccess = true + } klog.V(3).Infof("No configuration change detected, skipping backend reload") return nil } @@ -202,19 +213,13 @@ func (n *NGINXController) syncIngress(interface{}) error { if !utilingress.IsDynamicConfigurationEnough(pcfg, n.runningConfig) { klog.InfoS("Configuration changes detected, backend reload required") - hash, err := hashstructure.Hash(pcfg, hashstructure.FormatV1, &hashstructure.HashOptions{ - TagName: "json", - }) - if err != nil { - klog.Errorf("unexpected error hashing configuration: %v", err) - } - pcfg.ConfigurationChecksum = fmt.Sprintf("%v", hash) err = n.OnUpdate(*pcfg) if err != nil { n.metricCollector.IncReloadErrorCount() n.metricCollector.ConfigSuccess(hash, false) + n.lastConfigSuccess = false klog.Errorf("Unexpected failure reloading the backend:\n%v", err) n.recorder.Eventf(k8s.IngressPodDetails, apiv1.EventTypeWarning, "RELOAD", fmt.Sprintf("Error reloading NGINX: %v", err)) return err @@ -223,6 +228,7 @@ func (n *NGINXController) syncIngress(interface{}) error { klog.InfoS("Backend successfully reloaded") n.metricCollector.ConfigSuccess(hash, true) n.metricCollector.IncReloadCount() + n.lastConfigSuccess = true n.recorder.Eventf(k8s.IngressPodDetails, apiv1.EventTypeNormal, "RELOAD", "NGINX reload triggered due to a change in configuration") } @@ -243,7 +249,7 @@ func (n *NGINXController) syncIngress(interface{}) error { } retriesRemaining := retry.Steps - err := wait.ExponentialBackoff(retry, func() (bool, error) { + err = wait.ExponentialBackoff(retry, func() (bool, error) { err := n.configureDynamically(pcfg) if err == nil { klog.V(2).Infof("Dynamic reconfiguration succeeded.") diff --git a/internal/ingress/controller/nginx.go b/internal/ingress/controller/nginx.go index 20fad5afb..adb524560 100644 --- a/internal/ingress/controller/nginx.go +++ b/internal/ingress/controller/nginx.go @@ -264,6 +264,8 @@ type NGINXController struct { validationWebhookServer *http.Server command NginxExecTester + + lastConfigSuccess bool } // Start starts a new NGINX master process running in the foreground.