istio
69 строк · 2.3 Кб
1/*
2Copyright 2015 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package k8sleaderelection
18
19import (
20"net/http"
21"sync"
22"time"
23)
24
25// HealthzAdaptor associates the /healthz endpoint with the LeaderElection object.
26// It helps deal with the /healthz endpoint being set up prior to the LeaderElection.
27// This contains the code needed to act as an adaptor between the leader
28// election code the health check code. It allows us to provide health
29// status about the leader election. Most specifically about if the leader
30// has failed to renew without exiting the process. In that case we should
31// report not healthy and rely on the kubelet to take down the process.
32type HealthzAdaptor struct {
33pointerLock sync.Mutex
34le *LeaderElector
35timeout time.Duration
36}
37
38// Name returns the name of the health check we are implementing.
39func (l *HealthzAdaptor) Name() string {
40return "leaderElection"
41}
42
43// Check is called by the healthz endpoint handler.
44// It fails (returns an error) if we own the lease but had not been able to renew it.
45func (l *HealthzAdaptor) Check(req *http.Request) error {
46l.pointerLock.Lock()
47defer l.pointerLock.Unlock()
48if l.le == nil {
49return nil
50}
51return l.le.Check(l.timeout)
52}
53
54// SetLeaderElection ties a leader election object to a HealthzAdaptor
55func (l *HealthzAdaptor) SetLeaderElection(le *LeaderElector) {
56l.pointerLock.Lock()
57defer l.pointerLock.Unlock()
58l.le = le
59}
60
61// NewLeaderHealthzAdaptor creates a basic healthz adaptor to monitor a leader election.
62// timeout determines the time beyond the lease expiry to be allowed for timeout.
63// checks within the timeout period after the lease expires will still return healthy.
64func NewLeaderHealthzAdaptor(timeout time.Duration) *HealthzAdaptor {
65result := &HealthzAdaptor{
66timeout: timeout,
67}
68return result
69}
70