diff --git a/README.md b/README.md index 6baedb81..00edcb90 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_SCHEDULE`: 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..38d3bbb6 100644 --- a/internal/kubenurse/server.go +++ b/internal/kubenurse/server.go @@ -17,6 +17,8 @@ import ( "k8s.io/client-go/kubernetes" ) +const defaultCheckSchedule = 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 + checkSchedule 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_SCHEDULE func New(ctx context.Context, k8s kubernetes.Interface) (*Server, error) { mux := http.NewServeMux() + checkSchedule := defaultCheckSchedule + + if v, ok := os.LookupEnv("KUBENURSE_CHECK_SCHEDULE"); ok { + var err error + checkSchedule, 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, + checkSchedule: checkSchedule, + 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.checkSchedule) }() wg.Add(1)