Bump csi-lib-utils to v0.9.0

This commit is contained in:
xing-yang
2020-12-04 16:41:37 +00:00
parent 7efebbedec
commit 6761111167
22 changed files with 140 additions and 1877 deletions

View File

@@ -20,6 +20,7 @@ import (
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
@@ -32,13 +33,21 @@ import (
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/client-go/tools/record"
"k8s.io/klog"
"k8s.io/klog/v2"
)
const (
defaultLeaseDuration = 15 * time.Second
defaultRenewDeadline = 10 * time.Second
defaultRetryPeriod = 5 * time.Second
DefaultHealthCheckTimeout = 20 * time.Second
// HealthCheckerAddress is the address at which the leader election health
// checker reports status.
// The caller sidecar should document this address in appropriate flag
// descriptions.
HealthCheckerAddress = "/healthz/leader-election"
)
// leaderElection is a convenience wrapper around client-go's leader election library.
@@ -55,6 +64,9 @@ type leaderElection struct {
// valid options are resourcelock.LeasesResourceLock, resourcelock.EndpointsResourceLock,
// and resourcelock.ConfigMapsResourceLock
resourceLock string
// healthCheck reports unhealthy if leader election fails to renew leadership
// within a timeout period.
healthCheck *leaderelection.HealthzAdaptor
leaseDuration time.Duration
renewDeadline time.Duration
@@ -134,6 +146,27 @@ func (l *leaderElection) WithContext(ctx context.Context) {
l.ctx = ctx
}
// Server represents any type that could serve HTTP requests for the leader
// election health check endpoint.
type Server interface {
Handle(pattern string, handler http.Handler)
}
// PrepareHealthCheck creates a health check for this leader election object
// with the given healthCheckTimeout and registers its HTTP handler to the given
// server at the path specified by the constant "healthCheckerAddress".
// healthCheckTimeout determines the max duration beyond lease expiration
// allowed before reporting unhealthy.
// The caller sidecar should document the handler address in appropriate flag
// descriptions.
func (l *leaderElection) PrepareHealthCheck(
s Server,
healthCheckTimeout time.Duration) {
l.healthCheck = leaderelection.NewLeaderHealthzAdaptor(healthCheckTimeout)
s.Handle(HealthCheckerAddress, adaptCheckToHandler(l.healthCheck.Check))
}
func (l *leaderElection) Run() error {
if l.identity == "" {
id, err := defaultLeaderElectionIdentity()
@@ -179,6 +212,7 @@ func (l *leaderElection) Run() error {
klog.V(3).Infof("new leader detected, current leader: %s", identity)
},
},
WatchDog: l.healthCheck,
}
ctx := l.ctx
@@ -220,3 +254,15 @@ func inClusterNamespace() string {
return "default"
}
// adaptCheckToHandler returns an http.HandlerFunc that serves the provided checks.
func adaptCheckToHandler(c func(r *http.Request) error) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := c(r)
if err != nil {
http.Error(w, fmt.Sprintf("internal server error: %v", err), http.StatusInternalServerError)
} else {
fmt.Fprint(w, "ok")
}
})
}