forked from projectcalico/calico
/
health.go
228 lines (198 loc) · 6.19 KB
/
health.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright (c) 2018 Tigera, Inc. All rights reserved.
//
// 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 health
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"github.com/dtest11/calico/node/pkg/health/bird"
)
var felixReadinessEp string
var felixLivenessEp string
func init() {
felixPort := os.Getenv("FELIX_HEALTHPORT")
if felixPort == "" {
felixPort = "9099"
}
if _, err := strconv.Atoi(felixPort); err != nil {
log.Panicf("Failed to parse value for port %q", felixPort)
}
felixHost := os.Getenv("FELIX_HEALTHHOST")
if felixHost == "" {
felixHost = "localhost"
}
felixReadinessEp = "http://" + felixHost + ":" + felixPort + "/readiness"
felixLivenessEp = "http://" + felixHost + ":" + felixPort + "/liveness"
}
func Run(bird, bird6, felixReady, felixLive, birdLive, bird6Live bool, thresholdTime time.Duration) {
livenessChecks := felixLive || birdLive || bird6Live
readinessChecks := bird || felixReady || bird6
if !livenessChecks && !readinessChecks {
fmt.Printf("calico/node check error: must specify at least one of -bird-live, -bird6-live, -felix-live, -bird, -bird6, or -felix\n")
os.Exit(1)
}
ctx, cancel := context.WithTimeout(context.Background(), thresholdTime)
defer cancel()
g, ctx := errgroup.WithContext(ctx)
if felixLive {
g.Go(func() error {
if err := checkFelixHealth(ctx, felixLivenessEp, "liveness"); err != nil {
return fmt.Errorf("calico/node is not ready: Felix is not live: %+v", err)
}
return nil
})
}
if birdLive {
g.Go(func() error {
if err := checkServiceIsLive([]string{"confd", "bird"}); err != nil {
return fmt.Errorf("calico/node is not ready: bird/confd is not live: %+v", err)
}
return nil
})
}
if bird6Live {
g.Go(func() error {
if err := checkServiceIsLive([]string{"confd", "bird6"}); err != nil {
return fmt.Errorf("calico/node is not ready: bird6/confd is not live: %+v", err)
}
return nil
})
}
if felixReady {
g.Go(func() error {
if err := checkFelixHealth(ctx, felixReadinessEp, "readiness"); err != nil {
return fmt.Errorf("calico/node is not ready: felix is not ready: %+v", err)
}
return nil
})
}
if bird {
g.Go(func() error {
if err := checkBIRDReady("4", thresholdTime); err != nil {
return fmt.Errorf("calico/node is not ready: BIRD is not ready: %+v", err)
}
return nil
})
}
if bird6 {
g.Go(func() error {
if err := checkBIRDReady("6", thresholdTime); err != nil {
return fmt.Errorf("calico/node is not ready: BIRD6 is not ready: %+v", err)
}
return nil
})
}
if err := g.Wait(); err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
}
func checkServiceIsLive(services []string) error {
for _, service := range services {
err := checkService(service)
if err != nil {
return err
}
}
return nil
}
func checkService(serviceName string) error {
out, err := exec.Command("sv", "status", fmt.Sprintf("/etc/service/enabled/%s", serviceName)).Output()
if err != nil {
return err
}
var cmdOutput = string(out)
if !strings.HasPrefix(cmdOutput, "run") {
return fmt.Errorf(fmt.Sprintf("Service %s is not running. Output << %s >>", serviceName, strings.Trim(cmdOutput, "\n")))
}
return nil
}
// checkBIRDReady checks if BIRD is ready by connecting to the BIRD
// socket to gather all BGP peer connection status, and overall graceful
// restart status.
func checkBIRDReady(ipv string, thresholdTime time.Duration) error {
// Stat nodename file to get the modified time of the file.
nodenameFileStat, err := os.Stat("/var/lib/calico/nodename")
if err != nil {
return fmt.Errorf("Failed to stat() nodename file: %v", err)
}
// Check for unestablished peers
peers, err := bird.GetPeers(ipv)
log.Debugf("peers: %v", peers)
if err != nil {
return err
}
s := []string{}
// numEstablishedPeer keeps count of number of peers with bgp state established.
numEstablishedPeer := 0
for _, peer := range peers {
if peer.BGPState == "Established" {
numEstablishedPeer += 1
} else {
s = append(s, peer.PeerIP)
}
}
log.Infof("Number of node(s) with BGP peering established = %v", numEstablishedPeer)
if len(peers) == 0 {
// In case of no BGP peers return bird to be ready.
log.Debugf("There are no bgp peers, returning ready.")
} else if time.Since(nodenameFileStat.ModTime()) < thresholdTime {
if len(s) > 0 {
// When we first start up, only report ready if all our peerings are established.
// This prevents rolling update from proceeding until BGP is back up.
return fmt.Errorf("BGP not established with %+v", strings.Join(s, ","))
}
// Check for GR
gr, err := bird.GRInProgress(ipv)
if err != nil {
return err
} else if gr {
return errors.New("graceful restart in progress")
}
} else if numEstablishedPeer > 0 {
// After a while, only require a single peering to be up. This prevents the whole mesh
// from reporting not-ready if some nodes go down.
log.Debugf("There exist(s) %v calico node(s) with BGP peering established.", numEstablishedPeer)
} else {
return fmt.Errorf("BGP not established with %+v", strings.Join(s, ","))
}
return nil
}
// checkFelixHealth checks if felix is ready or live by making an http request to
// Felix's readiness or liveness endpoint.
func checkFelixHealth(ctx context.Context, endpoint, probeType string) error {
c := &http.Client{}
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
req = req.WithContext(ctx)
if err != nil {
return err
}
resp, err := c.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
return fmt.Errorf("%s probe reporting %d", probeType, resp.StatusCode)
}
return nil
}