From 35982548c16323786d7a628c99056acfd7c725ad Mon Sep 17 00:00:00 2001 From: Mahmoud Gaballah Date: Wed, 26 Oct 2022 16:20:06 +0200 Subject: [PATCH] introduce a feature flags to control the frequency of checks Signed-off-by: Mahmoud Gaballah --- README.md | 11 ++++++----- internal/kubenurse/server.go | 25 ++++++++++++++++++++----- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 6baedb81..8fba37a9 100644 --- a/README.md +++ b/README.md @@ -62,11 +62,12 @@ kubenurse is configured with environment variables: - `KUBENURSE_NAMESPACE`: Namespace in which to look for the neighbour kubenurses - `KUBENURSE_NEIGHBOUR_FILTER`: A Kubernetes label selector (eg. `app=kubenurse`) to filter neighbour kubenurses - `KUBENURSE_ALLOW_UNSCHEDULABLE`: If this is `"true"`, path checks to neighbouring kubenurses are made even if they are running on unschedulable nodes. -- `KUBENURSE_CHECK_API_SERVER_DIRECT`: If this is `"true"` kubenurse will perform the check [API Server Direct](###API Server Direct). default is "true" -- `KUBENURSE_CHECK_API_SERVER_DNS`: If this is `"true"`, kubenurse will perform the check [API Server DNS](###API Server DNS). default is "true" -- `KUBENURSE_CHECK_ME_INGRESS`: If this is `"true"`, kubenurse will perform the check [Me Ingress](###Me Ingress). default is "true" -- `KUBENURSE_CHECK_ME_SERVICE`: If this is `"true"`, kubenurse will perform the check [Me Service](###Me Service). default is "true" -- `KUBENURSE_CHECK_NEIGHBOURHOOD`: If this is `"true"`, kubenurse will perform the check [Neighbourhood](###Neighbourhood). default is "true" +- `KUBENURSE_CHECK_API_SERVER_DIRECT`: If this is `"true"` kubenurse will perform the check [API Server Direct](#API Server Direct). default is "true" +- `KUBENURSE_CHECK_API_SERVER_DNS`: If this is `"true"`, kubenurse will perform the check [API Server DNS](#API Server DNS). default is "true" +- `KUBENURSE_CHECK_ME_INGRESS`: If this is `"true"`, kubenurse will perform the check [Me Ingress](#Me Ingress). default is "true" +- `KUBENURSE_CHECK_ME_SERVICE`: If this is `"true"`, kubenurse will perform the check [Me Service](#Me Service). default is "true" +- `KUBENURSE_CHECK_NEIGHBOURHOOD`: If this is `"true"`, kubenurse will perform the check [Neighbourhood](#Neighbourhood). default is "true" +- `KUBENURSE_CHECK_INTERVAL`: the frequency to perform kubenurse checks. the string should be formatted for [time.ParseDuration](https://pkg.go.dev/time#ParseDuration). defaults to `5s` - `KUBENURSE_USE_TLS`: If this is `"true"`, enable TLS endpoint on port 8443 - `KUBENURSE_CERT_FILE`: Certificate to use with TLS endpoint - `KUBENURSE_CERT_KEY`: Key to use with TLS endpoint diff --git a/internal/kubenurse/server.go b/internal/kubenurse/server.go index 1b7c2f81..5bb586fb 100644 --- a/internal/kubenurse/server.go +++ b/internal/kubenurse/server.go @@ -17,6 +17,8 @@ import ( "k8s.io/client-go/kubernetes" ) +const defaultCheckInterval = 5 * time.Second + // Server is used to build the kubenurse http/https server(s). type Server struct { http http.Server @@ -25,7 +27,8 @@ type Server struct { checker *servicecheck.Checker // Configuration options - useTLS bool + useTLS bool + checkInterval time.Duration // If we want to consider kubenurses on unschedulable nodes allowUnschedulable bool @@ -48,9 +51,21 @@ type Server struct { // * KUBENURSE_CHECK_ME_INGRESS // * KUBENURSE_CHECK_ME_SERVICE // * KUBENURSE_CHECK_NEIGHBOURHOOD +// * KUBENURSE_CHECK_INTERVAL func New(ctx context.Context, k8s kubernetes.Interface) (*Server, error) { mux := http.NewServeMux() + checkInterval := defaultCheckInterval + + if v, ok := os.LookupEnv("KUBENURSE_CHECK_INTERVAL"); ok { + var err error + checkInterval, err = time.ParseDuration(v) + + if err != nil { + return nil, err + } + } + server := &Server{ http: http.Server{ Addr: ":8080", @@ -70,9 +85,9 @@ func New(ctx context.Context, k8s kubernetes.Interface) (*Server, error) { //nolint:goconst // No need to make "true" a constant in my opinion, readability is better like this. useTLS: os.Getenv("KUBENURSE_USE_TLS") == "true", allowUnschedulable: os.Getenv("KUBENURSE_ALLOW_UNSCHEDULABLE") == "true", - - mu: new(sync.Mutex), - ready: true, + checkInterval: checkInterval, + mu: new(sync.Mutex), + ready: true, } promRegistry := prometheus.NewRegistry() @@ -132,7 +147,7 @@ func (s *Server) Run() error { go func() { defer wg.Done() - s.checker.RunScheduled(5 * time.Second) + s.checker.RunScheduled(s.checkInterval) }() wg.Add(1)