-
Notifications
You must be signed in to change notification settings - Fork 363
/
controlplane.go
177 lines (157 loc) · 6 KB
/
controlplane.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
package status
import (
"fmt"
"io"
"strings"
"text/template"
"time"
v1 "github.com/openshift/api/config/v1"
)
type assessmentState string
const (
assessmentStateProgressing assessmentState = "Progressing"
assessmentStateCompleted assessmentState = "Completed"
assessmentStatePending assessmentState = "Pending"
assessmentStateExcluded assessmentState = "Excluded"
assessmentStateDegraded assessmentState = "Degraded"
)
type operators struct {
Total int
Available int
Progressing int
Degraded int
}
type controlPlaneStatusDisplayData struct {
Assessment assessmentState
Completion float64
Duration time.Duration
Operators operators
}
const (
unavailableWarningThreshold = 5 * time.Minute
unavailableErrorThreshold = 20 * time.Minute
degradedWarningThreshold = 5 * time.Minute
degradedErrorThreshold = 40 * time.Minute
)
func coInsights(name string, available v1.ClusterOperatorStatusCondition, degraded v1.ClusterOperatorStatusCondition, evaluated time.Time) []updateInsight {
var insights []updateInsight
if available.Status == v1.ConditionFalse && evaluated.After(available.LastTransitionTime.Time.Add(unavailableWarningThreshold)) {
insight := updateInsight{
startedAt: available.LastTransitionTime.Time,
scope: updateInsightScope{scopeType: scopeTypeControlPlane, resources: []scopeResource{{kind: scopeKindClusterOperator, name: name}}},
impact: updateInsightImpact{
level: warningImpactLevel,
impactType: apiAvailabilityImpactType,
summary: fmt.Sprintf("Cluster Operator %s is unavailable (%s)", name, available.Reason),
description: available.Message,
},
remediation: updateInsightRemediation{reference: "https://github.com/openshift/runbooks/blob/master/alerts/cluster-monitoring-operator/ClusterOperatorDown.md"},
}
if available.Message == "" {
// Backfill the description if CO doesn't provide one
insight.impact.description = "<no message>"
}
if evaluated.After(available.LastTransitionTime.Time.Add(unavailableErrorThreshold)) {
insight.impact.level = errorImpactLevel
}
insights = append(insights, insight)
}
if degraded.Status == v1.ConditionTrue && evaluated.After(degraded.LastTransitionTime.Time.Add(degradedWarningThreshold)) {
insight := updateInsight{
startedAt: degraded.LastTransitionTime.Time,
scope: updateInsightScope{scopeType: scopeTypeControlPlane, resources: []scopeResource{{kind: scopeKindClusterOperator, name: name}}},
impact: updateInsightImpact{
level: warningImpactLevel,
impactType: apiAvailabilityImpactType,
summary: fmt.Sprintf("Cluster Operator %s is degraded (%s)", name, degraded.Reason),
description: degraded.Message,
},
remediation: updateInsightRemediation{reference: "https://github.com/openshift/runbooks/blob/master/alerts/cluster-monitoring-operator/ClusterOperatorDegraded.md"},
}
if degraded.Message == "" {
// Backfill the description if CO doesn't provide one
insight.impact.description = "<no message>"
}
if evaluated.After(degraded.LastTransitionTime.Time.Add(degradedErrorThreshold)) {
insight.impact.level = errorImpactLevel
}
insights = append(insights, insight)
}
return insights
}
func assessControlPlaneStatus(cv *v1.ClusterVersion, operators []v1.ClusterOperator, at time.Time) (controlPlaneStatusDisplayData, []updateInsight) {
var displayData controlPlaneStatusDisplayData
var completed int
var insights []updateInsight
targetVersion := cv.Status.Desired.Version
for _, operator := range operators {
var isPlatformOperator bool
for annotation := range operator.Annotations {
if strings.HasPrefix(annotation, "exclude.release.openshift.io/") ||
strings.HasPrefix(annotation, "include.release.openshift.io/") {
isPlatformOperator = true
break
}
}
if !isPlatformOperator {
continue
}
var updated bool
for _, version := range operator.Status.Versions {
if version.Name == "operator" && version.Version == targetVersion {
updated = true
completed++
break
}
}
available := v1.ClusterOperatorStatusCondition{}
progressing := v1.ClusterOperatorStatusCondition{}
degraded := v1.ClusterOperatorStatusCondition{}
displayData.Operators.Total++
for _, condition := range operator.Status.Conditions {
switch {
case condition.Type == v1.OperatorAvailable:
available = condition
case condition.Type == v1.OperatorProgressing:
progressing = condition
case condition.Type == v1.OperatorDegraded:
degraded = condition
}
}
if available.Status == v1.ConditionTrue {
displayData.Operators.Available++
}
if degraded.Status == v1.ConditionTrue {
displayData.Operators.Degraded++
}
if progressing.Status == v1.ConditionTrue && !updated {
displayData.Operators.Progressing++
}
insights = append(insights, coInsights(operator.Name, available, degraded, at)...)
}
if completed == displayData.Operators.Total {
displayData.Assessment = assessmentStateCompleted
} else {
displayData.Assessment = assessmentStateProgressing
}
if len(cv.Status.History) > 0 {
currentHistoryItem := cv.Status.History[0]
if currentHistoryItem.State == v1.CompletedUpdate {
displayData.Duration = currentHistoryItem.CompletionTime.Time.Sub(currentHistoryItem.StartedTime.Time)
} else {
displayData.Duration = at.Sub(currentHistoryItem.StartedTime.Time)
}
}
displayData.Completion = float64(completed) / float64(displayData.Operators.Total) * 100.0
return displayData, insights
}
var controlPlaneStatusTemplate = template.Must(template.New("controlPlaneStatus").Parse(controlPlaneStatusTemplateRaw))
func (d *controlPlaneStatusDisplayData) Write(f io.Writer) error {
return controlPlaneStatusTemplate.Execute(f, d)
}
const controlPlaneStatusTemplateRaw = `= Control Plane =
Assessment: {{ .Assessment }}
Completion: {{ printf "%.0f" .Completion }}%
Duration: {{ .Duration }}
Operator Status: {{ .Operators.Total }} Total, {{ .Operators.Available }} Available, {{ .Operators.Progressing }} Progressing, {{ .Operators.Degraded }} Degraded
`