File: readiness.go

package info (click to toggle)
golang-k8s-sigs-apiserver-network-proxy 0.33.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,068 kB
  • sloc: makefile: 220; sh: 118
file content (78 lines) | stat: -rw-r--r-- 1,762 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
copyright 2023 the kubernetes authors.

licensed under the apache license, version 2.0 (the "license");
you may not use this file except in compliance with the license.
you may obtain a copy of the license at

    http://www.apache.org/licenses/license-2.0

unless required by applicable law or agreed to in writing, software
distributed under the license is distributed on an "as is" basis,
without warranties or conditions of any kind, either express or implied.
see the license for the specific language governing permissions and
limitations under the license.
*/

package agent

import (
	"fmt"
	"net/http"
)

// ReadinessManager supports checking if the agent is ready.
type ReadinessManager interface {
	// Ready returns true the proxy server is ready.
	Ready() bool
}

var _ ReadinessManager = &ClientSet{}

func (cs *ClientSet) Ready() bool {
	// Returns true if the agent is connected to at least one server.
	return cs.HealthyClientsCount() > 0
}

// HealthChecker represents an entity capable of performing health checks.
type HealthChecker interface {
	Name() string
	Check(req *http.Request) error
}

// ping implements the simplest possible healthz checker.
type ping struct{}

var Ping HealthChecker = &ping{}

func (p *ping) Name() string {
	return "ping"
}

func (p *ping) Check(_ *http.Request) error {
	return nil
}

type serverConnected struct {
	rm ReadinessManager
}

var ServerConnected HealthChecker = &serverConnected{}

func NewServerConnected(cs ReadinessManager) HealthChecker {
	return &serverConnected{
		rm: cs,
	}
}

func (s *serverConnected) Name() string {
	return "server-connected"
}

func (s *serverConnected) Check(_ *http.Request) error {
	if s.rm.Ready() {
		return nil
	}

	return fmt.Errorf("no servers connected")
}