-
Notifications
You must be signed in to change notification settings - Fork 47
/
plugin_impl_probe.go
166 lines (143 loc) · 4.82 KB
/
plugin_impl_probe.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
// Copyright (c) 2017 Cisco and/or its affiliates.
//
// 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 probe
import (
"encoding/json"
"net/http"
"github.com/unrolled/render"
"go.ligato.io/cn-infra/v2/health/statuscheck"
"go.ligato.io/cn-infra/v2/health/statuscheck/model/status"
"go.ligato.io/cn-infra/v2/infra"
prom "go.ligato.io/cn-infra/v2/rpc/prometheus"
"go.ligato.io/cn-infra/v2/rpc/rest"
"go.ligato.io/cn-infra/v2/servicelabel"
)
const (
livenessProbePath = "/liveness" // liveness probe URL
readinessProbePath = "/readiness" // readiness probe URL
)
// Plugin struct holds all plugin-related data.
type Plugin struct {
Deps
// NonFatalPlugins is a list of plugin names. Error reported by a plugin
// from the list is not propagated into overall agent status.
NonFatalPlugins []string
}
// Deps lists dependencies of REST plugin.
type Deps struct {
infra.PluginDeps
ServiceLabel servicelabel.ReaderAPI
StatusCheck statuscheck.StatusReader // inject
HTTP rest.HTTPHandlers // inject
Prometheus prom.API // inject
}
// ExposedStatus groups the information exposed via readiness and liveness probe
type ExposedStatus struct {
status.AgentStatus
PluginStatus map[string]*status.PluginStatus
// NonFatalPlugins is a configured list of plugins whose
// errors are not reflected in overall state.
NonFatalPlugins []string
}
// Init does nothing
func (p *Plugin) Init() error {
return nil
}
// AfterInit registers HTTP handlers for liveness and readiness probes.
func (p *Plugin) AfterInit() error {
if p.StatusCheck == nil {
p.Log.Warnf("Unable to register probe handlers, StatusCheck is nil")
return nil
}
if p.HTTP != nil {
p.Log.Infof("Starting health http-probe on port %v", p.HTTP.GetPort())
p.HTTP.RegisterHTTPHandler(livenessProbePath, p.livenessProbeHandler, "GET")
p.HTTP.RegisterHTTPHandler(readinessProbePath, p.readinessProbeHandler, "GET")
} else {
p.Log.Info("Unable to register http-probe handler, HTTP is nil")
}
if p.Prometheus != nil {
if err := p.registerPrometheusProbe(); err != nil {
return err
}
} else {
p.Log.Info("Unable to register prometheus-probe handler, Prometheus is nil")
}
return nil
}
// Close frees resources
func (p *Plugin) Close() error {
return nil
}
// readinessProbeHandler handles k8s readiness probe.
func (p *Plugin) readinessProbeHandler(formatter *render.Render) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
ifStat := p.StatusCheck.GetInterfaceStats()
agentStat := p.getAgentStatus()
agentStat.InterfaceStats = &ifStat
agentStatJSON, _ := json.Marshal(agentStat)
if agentStat.State == status.OperationalState_OK {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
w.Write(agentStatJSON)
}
}
// livenessProbeHandler handles k8s liveness probe.
func (p *Plugin) livenessProbeHandler(formatter *render.Render) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
stat := p.getAgentStatus()
statJSON, _ := json.Marshal(stat)
if stat.State == status.OperationalState_INIT || stat.State == status.OperationalState_OK {
w.WriteHeader(http.StatusOK)
w.Write(statJSON)
} else {
w.WriteHeader(http.StatusInternalServerError)
w.Write(statJSON)
}
}
}
// getAgentStatus return overall agent status + status of the plugins
// the method takes into account non-fatal plugin settings
func (p *Plugin) getAgentStatus() ExposedStatus {
exposedStatus := ExposedStatus{
AgentStatus: p.StatusCheck.GetAgentStatus(),
PluginStatus: p.StatusCheck.GetAllPluginStatus(),
NonFatalPlugins: p.NonFatalPlugins,
}
// check whether error is caused by one of the plugins in NonFatalPlugin list
if exposedStatus.AgentStatus.State == status.OperationalState_ERROR && len(p.NonFatalPlugins) > 0 {
for k, v := range exposedStatus.PluginStatus {
if v.State == status.OperationalState_ERROR {
if isInSlice(p.NonFatalPlugins, k) {
// treat error reported by this plugin as non fatal
exposedStatus.AgentStatus.State = status.OperationalState_OK
} else {
exposedStatus.AgentStatus.State = status.OperationalState_ERROR
break
}
}
}
}
return exposedStatus
}
func isInSlice(haystack []string, needle string) bool {
for _, el := range haystack {
if el == needle {
return true
}
}
return false
}