istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/leaderelection/k8sleaderelection/healthzadaptor.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package k8sleaderelection 18 19 import ( 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. 32 type HealthzAdaptor struct { 33 pointerLock sync.Mutex 34 le *LeaderElector 35 timeout time.Duration 36 } 37 38 // Name returns the name of the health check we are implementing. 39 func (l *HealthzAdaptor) Name() string { 40 return "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. 45 func (l *HealthzAdaptor) Check(req *http.Request) error { 46 l.pointerLock.Lock() 47 defer l.pointerLock.Unlock() 48 if l.le == nil { 49 return nil 50 } 51 return l.le.Check(l.timeout) 52 } 53 54 // SetLeaderElection ties a leader election object to a HealthzAdaptor 55 func (l *HealthzAdaptor) SetLeaderElection(le *LeaderElector) { 56 l.pointerLock.Lock() 57 defer l.pointerLock.Unlock() 58 l.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. 64 func NewLeaderHealthzAdaptor(timeout time.Duration) *HealthzAdaptor { 65 result := &HealthzAdaptor{ 66 timeout: timeout, 67 } 68 return result 69 }