Skip to content

Commit

Permalink
fix: don't leave open file descriptors after collecting metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
plaffitt committed Jul 26, 2023
1 parent 15c2628 commit adad919
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
5 changes: 2 additions & 3 deletions cmd/cache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions controllers/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package controllers

import (
"context"
"net/http"
"strconv"

kuikenixiov1alpha1 "github.com/enix/kube-image-keeper/api/v1alpha1"
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions controllers/healthz.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit adad919

Please sign in to comment.