| 1 |
/* |
| 2 |
copyright 2023 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 agent |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"net/http" |
| 22 |
) |
| 23 |
|
| 24 |
// ReadinessManager supports checking if the agent is ready. |
| 25 |
type ReadinessManager interface { |
| 26 |
// Ready returns true the proxy server is ready. |
| 27 |
Ready() bool |
| 28 |
} |
| 29 |
|
| 30 |
var _ ReadinessManager = &ClientSet{} |
| 31 |
|
| 32 |
func (cs *ClientSet) Ready() bool { |
| 33 |
// Returns true if the agent is connected to at least one server. |
| 34 |
return cs.HealthyClientsCount() > 0 |
| 35 |
} |
| 36 |
|
| 37 |
// HealthChecker represents an entity capable of performing health checks. |
| 38 |
type HealthChecker interface { |
| 39 |
Name() string |
| 40 |
Check(req *http.Request) error |
| 41 |
} |
| 42 |
|
| 43 |
// ping implements the simplest possible healthz checker. |
| 44 |
type ping struct{} |
| 45 |
|
| 46 |
var Ping HealthChecker = &ping{} |
| 47 |
|
| 48 |
func (p *ping) Name() string { |
| 49 |
return "ping" |
| 50 |
} |
| 51 |
|
| 52 |
func (p *ping) Check(_ *http.Request) error { |
| 53 |
return nil |
| 54 |
} |
| 55 |
|
| 56 |
type serverConnected struct { |
| 57 |
rm ReadinessManager |
| 58 |
} |
| 59 |
|
| 60 |
var ServerConnected HealthChecker = &serverConnected{} |
| 61 |
|
| 62 |
func NewServerConnected(cs ReadinessManager) HealthChecker { |
| 63 |
return &serverConnected{ |
| 64 |
rm: cs, |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
func (s *serverConnected) Name() string { |
| 69 |
return "server-connected" |
| 70 |
} |
| 71 |
|
| 72 |
func (s *serverConnected) Check(_ *http.Request) error { |
| 73 |
if s.rm.Ready() { |
| 74 |
return nil |
| 75 |
} |
| 76 |
|
| 77 |
return fmt.Errorf("no servers connected") |
| 78 |
} |