From adad9197cdf8548188b46b00e1673a3376d25057 Mon Sep 17 00:00:00 2001 From: Paul Laffitte Date: Mon, 24 Jul 2023 14:34:46 +0200 Subject: [PATCH] fix: don't leave open file descriptors after collecting metrics --- cmd/cache/main.go | 5 ++--- controllers/collector.go | 3 +-- controllers/healthz.go | 21 +++++++++++++++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 controllers/healthz.go diff --git a/cmd/cache/main.go b/cmd/cache/main.go index 1f94cc26..d44f66f7 100644 --- a/cmd/cache/main.go +++ b/cmd/cache/main.go @@ -13,7 +13,6 @@ import ( _ "k8s.io/client-go/plugin/pkg/client/auth" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/controller-runtime/pkg/webhook" @@ -94,11 +93,11 @@ func main() { mgr.GetWebhookServer().Register("/mutate-core-v1-pod", &webhook.Admission{Handler: &imageRewriter}) //+kubebuilder:scaffold:builder - if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { + if err := mgr.AddHealthzCheck("healthz", controllers.MakeChecker(controllers.Healthz)); err != nil { setupLog.Error(err, "unable to set up health check") os.Exit(1) } - if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil { + if err := mgr.AddReadyzCheck("readyz", controllers.MakeChecker(controllers.Readyz)); err != nil { setupLog.Error(err, "unable to set up ready check") os.Exit(1) } diff --git a/controllers/collector.go b/controllers/collector.go index 0a1fc2b7..ca4a3735 100644 --- a/controllers/collector.go +++ b/controllers/collector.go @@ -2,7 +2,6 @@ package controllers import ( "context" - "net/http" "strconv" kuikenixiov1alpha1 "github.com/enix/kube-image-keeper/api/v1alpha1" @@ -46,7 +45,7 @@ var ( Name: "up", Help: "Whether or not this replica is healthy.", }, func() float64 { - if resp, err := http.Get("http://" + ProbeAddr + "/healthz"); err != nil || (resp != nil && resp.StatusCode != http.StatusOK) { + if err := Healthz(); err != nil { return 0 } return 1 diff --git a/controllers/healthz.go b/controllers/healthz.go new file mode 100644 index 00000000..fb1e3505 --- /dev/null +++ b/controllers/healthz.go @@ -0,0 +1,21 @@ +package controllers + +import ( + "net/http" + + "sigs.k8s.io/controller-runtime/pkg/healthz" +) + +func MakeChecker(check func() error) healthz.Checker { + return func(req *http.Request) error { + return check() + } +} + +func Healthz() error { + return nil +} + +func Readyz() error { + return nil +}