...
1
2
3
4 package flowcontrol
5
6 import (
7 "context"
8 "fmt"
9 "net/http"
10 "net/url"
11
12 flowcontrolapi "k8s.io/api/flowcontrol/v1beta2"
13 "k8s.io/apimachinery/pkg/runtime/schema"
14 "k8s.io/client-go/rest"
15 "k8s.io/client-go/transport"
16 )
17
18 const pingPath = "/livez/ping"
19
20
21
22
23 func IsEnabled(ctx context.Context, config *rest.Config) (bool, error) {
24
25
26
27
28
29 tcfg, err := config.TransportConfig()
30 if err != nil {
31 return false, fmt.Errorf("building transport config: %w", err)
32 }
33 roudtripper, err := transport.New(tcfg)
34 if err != nil {
35 return false, fmt.Errorf("building round tripper: %w", err)
36 }
37
38
39 url, err := serverURL(config)
40 if err != nil {
41 return false, fmt.Errorf("building server URL: %w", err)
42 }
43
44
45
46
47
48 url.Path = pingPath
49
50
51 req, err := http.NewRequestWithContext(ctx, "HEAD", url.String(), nil)
52 if err != nil {
53 return false, fmt.Errorf("building request: %w", err)
54 }
55
56 if config.UserAgent != "" {
57 req.Header.Set("User-Agent", config.UserAgent)
58 }
59
60
61
62
63
64 resp, err := roudtripper.RoundTrip(req)
65 if err != nil {
66 return false, fmt.Errorf("making %s request: %w", pingPath, err)
67 }
68
69 if resp.Body != nil {
70
71 err := resp.Body.Close()
72 if err != nil {
73 return false, fmt.Errorf("closing response body: %v", err)
74 }
75 }
76
77
78
79
80
81
82 key := flowcontrolapi.ResponseHeaderMatchedFlowSchemaUID
83 if value := resp.Header.Get(key); value != "" {
84
85
86 return true, nil
87 }
88 return false, nil
89 }
90
91
92
93
94 func serverURL(config *rest.Config) (*url.URL, error) {
95
96
97 hasCA := len(config.CAFile) != 0 || len(config.CAData) != 0
98 hasCert := len(config.CertFile) != 0 || len(config.CertData) != 0
99 defaultTLS := hasCA || hasCert || config.Insecure
100 host := config.Host
101 if host == "" {
102 host = "localhost"
103 }
104
105 hostURL, _, err := rest.DefaultServerURL(host, config.APIPath, schema.GroupVersion{}, defaultTLS)
106 return hostURL, err
107 }
108
View as plain text