-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdeployment.go
276 lines (228 loc) · 10.1 KB
/
deployment.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package deployment
import (
"bytes"
"fmt"
"strings"
"time"
"github.com/benkeil/check-k8s/cmd/api"
"github.com/benkeil/check-k8s/pkg/utils"
"github.com/benkeil/icinga-checks-library"
"k8s.io/api/apps/v1"
"k8s.io/client-go/kubernetes"
)
type (
// CheckDeployment interface to check a deployment
CheckDeployment interface {
CheckUpdateStrategy(CheckUpdateStrategyOptions) icinga.Result
CheckAvailableReplicas(CheckAvailableReplicasOptions) icinga.Result
CheckPodRestarts(CheckPodRestartsOptions) icinga.Result
CheckProbesDefined(CheckProbesDefinedOptions) icinga.Result
CheckContainerDefined(CheckContainerDefinedOptions) icinga.Result
CheckAll(CheckAllOptions) icinga.Results
}
checkDeploymentImpl struct {
Client kubernetes.Interface
Name string
Namespace string
}
)
// NewCheckDeployment creates a new instance of CheckDeployment
func NewCheckDeployment(client kubernetes.Interface, name string, namespace string) CheckDeployment {
return &checkDeploymentImpl{Client: client, Name: name, Namespace: namespace}
}
// CheckAllOptions contains options needed to run all deployment checks
type CheckAllOptions struct {
Client kubernetes.Interface
CheckUpdateStrategyOptions CheckUpdateStrategyOptions
CheckAvailableReplicasOptions CheckAvailableReplicasOptions
CheckPodRestartsOptions CheckPodRestartsOptions
CheckProbesDefinedOptions CheckProbesDefinedOptions
CheckContainerDefinedOptions CheckContainerDefinedOptions
}
// CheckAll runs all tests and returns an instance of ServiceCheckResults
func (c *checkDeploymentImpl) CheckAll(options CheckAllOptions) icinga.Results {
results := icinga.NewResults()
results.Add(c.CheckUpdateStrategy(options.CheckUpdateStrategyOptions))
results.Add(c.CheckAvailableReplicas(options.CheckAvailableReplicasOptions))
results.Add(c.CheckPodRestarts(options.CheckPodRestartsOptions))
results.Add(c.CheckProbesDefined(options.CheckProbesDefinedOptions))
results.Add(c.CheckContainerDefined(options.CheckContainerDefinedOptions))
return results
}
// CheckUpdateStrategyOptions contains options needed to run CheckUpdateStrategy check
type CheckUpdateStrategyOptions struct {
Result string
UpdateStrategy string
}
// CheckUpdateStrategy checks if the deployment has the RollingUpdate strategy
func (c *checkDeploymentImpl) CheckUpdateStrategy(options CheckUpdateStrategyOptions) icinga.Result {
name := "Deployment.UpdateStretegy"
var updateStretegy v1.DeploymentStrategyType
switch options.UpdateStrategy {
case "RollingUpdate":
updateStretegy = v1.RollingUpdateDeploymentStrategyType
case "Recreate":
updateStretegy = v1.RecreateDeploymentStrategyType
default:
icinga.NewResult("CheckUpdateStrategy", icinga.ServiceStatusUnknown, fmt.Sprintf("invalid DeploymentStrategy: %v", options.UpdateStrategy)).Exit()
}
statusCheck, err := icinga.NewStatusCheckCompare(options.Result)
if err != nil {
return icinga.NewResult(name, icinga.ServiceStatusUnknown, fmt.Sprintf("can't compare status: %v", err))
}
deployment, err := api.GetDeployment(c.Client, api.GetDeploymentOptions{Name: c.Name, Namespace: c.Namespace})
if err != nil {
return icinga.NewResult("GetDeployment", icinga.ServiceStatusUnknown, fmt.Sprintf("cant't get deployment: %v", err))
}
comparator := func() bool {
return updateStretegy != deployment.Spec.Strategy.Type
}
status := statusCheck.Compare(comparator)
return icinga.NewResult(name, status, fmt.Sprintf("deployment has update strategy %s", updateStretegy))
}
// CheckAvailableReplicasOptions contains options needed to run CheckAvailableReplicas check
type CheckAvailableReplicasOptions struct {
ThresholdWarning string
ThresholdCritical string
}
// CheckAvailableReplicas checks if the deployment has a minimum of available replicas
func (c *checkDeploymentImpl) CheckAvailableReplicas(options CheckAvailableReplicasOptions) icinga.Result {
name := "Deployment.AvailableReplicas"
statusCheck, err := icinga.NewStatusCheck(options.ThresholdWarning, options.ThresholdCritical)
if err != nil {
return icinga.NewResult(name, icinga.ServiceStatusUnknown, fmt.Sprintf("can't check status: %v", err))
}
deployment, err := api.GetDeployment(c.Client, api.GetDeploymentOptions{Name: c.Name, Namespace: c.Namespace})
if err != nil {
return icinga.NewResult("GetDeployment", icinga.ServiceStatusUnknown, fmt.Sprintf("cant't get deployment: %v", err))
}
replicas := deployment.Status.AvailableReplicas
status := statusCheck.CheckInt32(replicas)
message := fmt.Sprintf("deployment has %v available replica(s)", replicas)
return icinga.NewResult(name, status, message)
}
// CheckPodRestartsOptions contains options needed to run CheckPodRestarts check
type CheckPodRestartsOptions struct {
Result string
Duration string
}
// CheckPodRestarts checks if the deployment has a minimum of available replicas
func (c *checkDeploymentImpl) CheckPodRestarts(options CheckPodRestartsOptions) icinga.Result {
name := "Deployment.PodRestarts"
deployment, err := api.GetDeployment(c.Client, api.GetDeploymentOptions{Name: c.Name, Namespace: c.Namespace})
if err != nil {
return icinga.NewResultUnknownMessage("GetDeployment", fmt.Sprintf("cant't get deployment: %v", err))
}
podList, err := api.GetPods(c.Client, api.GetPodOptions{LabelSelector: deployment.Spec.Selector})
if err != nil {
return icinga.NewResultUnknownMessage("GetPods", fmt.Sprintf("cant't get deployment: %v", err))
}
duration, err := time.ParseDuration(options.Duration)
if err != nil {
return icinga.NewResultUnknownMessage("ParseDuration", fmt.Sprintf("can't parse duration: %v", err))
}
statusCheck, err := icinga.NewStatusCheckCompare(options.Result)
if err != nil {
return icinga.NewResult(name, icinga.ServiceStatusUnknown, fmt.Sprintf("can't check status: %v", err))
}
// maybe we need a field selector for the events to check that
// kubectl get event -n production-mls --field-selector=involvedObject.name=mls-67799db556-4wbtw
// kubectl get event -n production-mls --field-selector=involvedObject.name=mls-67799db556-4wbtw,reason=Killing
// Reasons:
// - Unhealthy
// - Killing
// Types:
// - Warning
// contains faild containers grouped by pod name
failedContainerMap := make(map[string][]string)
for _, pod := range podList.Items {
for _, containerStatus := range pod.Status.ContainerStatuses {
terminated := containerStatus.LastTerminationState.Terminated
if terminated != nil && time.Since(terminated.FinishedAt.Time).Minutes() < duration.Minutes() {
failedContainerMap[pod.GetObjectMeta().GetName()] = append(failedContainerMap[pod.GetObjectMeta().GetName()], containerStatus.Name)
}
}
}
status := statusCheck.CompareBool(len(failedContainerMap) > 0)
message := icinga.DefaultSuccessMessage
if status != icinga.ServiceStatusOk {
var buffer bytes.Buffer
for podName, containers := range failedContainerMap {
buffer.WriteString(fmt.Sprintf("%v: %v ", podName, containers))
}
message = buffer.String()
}
return icinga.NewResult(name, status, strings.Trim(message, "\n"))
}
// CheckProbesDefinedOptions contains options needed to run CheckProbesDefined check
type CheckProbesDefinedOptions struct {
Result string
ProbesDefined []string
}
// CheckProbesDefined checks if the deployment has the RollingUpdate strategy
func (c *checkDeploymentImpl) CheckProbesDefined(options CheckProbesDefinedOptions) icinga.Result {
name := "Deployment.ProbesDefined"
statusCheck, err := icinga.NewStatusCheckCompare(options.Result)
if err != nil {
return icinga.NewResult(name, icinga.ServiceStatusUnknown, fmt.Sprintf("can't compare status: %v", err))
}
deployment, err := api.GetDeployment(c.Client, api.GetDeploymentOptions{Name: c.Name, Namespace: c.Namespace})
if err != nil {
return icinga.NewResult("GetDeployment", icinga.ServiceStatusUnknown, fmt.Sprintf("cant't get deployment: %v", err))
}
missingProbes := []string{}
for _, container := range deployment.Spec.Template.Spec.Containers {
if (len(options.ProbesDefined) > 0 && utils.Contains(container.Name, options.ProbesDefined)) || len(options.ProbesDefined) == 0 {
if container.ReadinessProbe == nil || container.LivenessProbe == nil {
missingProbes = append(missingProbes, container.Name)
}
}
}
status := statusCheck.CompareBool(len(missingProbes) > 0)
message := icinga.DefaultSuccessMessage
if status != icinga.ServiceStatusOk {
message = fmt.Sprintf("containers without probes: %v", missingProbes)
}
return icinga.NewResult(name, status, message)
}
// CheckContainerDefinedOptions contains options needed to run CheckContainerDefined check
type CheckContainerDefinedOptions struct {
Result string
ContainerDefined []string
}
// CheckContainerDefined checks if the deployment has the RollingUpdate strategy
func (c *checkDeploymentImpl) CheckContainerDefined(options CheckContainerDefinedOptions) icinga.Result {
name := "Deployment.ContainerDefined"
if len(options.ContainerDefined) == 0 {
return icinga.NewResultUnknownMessage(name, fmt.Sprint("no containers defined to check"))
}
statusCheck, err := icinga.NewStatusCheckCompare(options.Result)
if err != nil {
return icinga.NewResultUnknownMessage(name, fmt.Sprintf("can't compare status: %v", err))
}
deployment, err := api.GetDeployment(c.Client, api.GetDeploymentOptions{Name: c.Name, Namespace: c.Namespace})
if err != nil {
return icinga.NewResultUnknownMessage("GetDeployment", fmt.Sprintf("cant't get deployment: %v", err))
}
foundContainers := make(map[string]bool)
for _, container := range options.ContainerDefined {
foundContainers[container] = false
}
for _, container := range deployment.Spec.Template.Spec.Containers {
if utils.Contains(container.Name, options.ContainerDefined) {
foundContainers[container.Name] = true
}
}
missingContainer := []string{}
for container, found := range foundContainers {
if !found {
missingContainer = append(missingContainer, container)
}
}
status := statusCheck.CompareBool(len(missingContainer) > 0)
message := icinga.DefaultSuccessMessage
if status != icinga.ServiceStatusOk {
message = fmt.Sprintf("missing containers: %v", missingContainer)
}
return icinga.NewResult(name, status, message)
}