-
Notifications
You must be signed in to change notification settings - Fork 93
/
statefulset_status.go
160 lines (135 loc) · 5.25 KB
/
statefulset_status.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
package analyzer
import (
"encoding/json"
"fmt"
"path/filepath"
"github.com/pkg/errors"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"github.com/replicatedhq/troubleshoot/pkg/constants"
appsv1 "k8s.io/api/apps/v1"
)
type AnalyzeStatefulsetStatus struct {
analyzer *troubleshootv1beta2.StatefulsetStatus
}
func (a *AnalyzeStatefulsetStatus) Title() string {
if a.analyzer.CheckName != "" {
return a.analyzer.CheckName
}
if a.analyzer.Name != "" && a.analyzer.Namespace != "" {
return fmt.Sprintf("%s/%s Statefulset Status", a.analyzer.Namespace, a.analyzer.Name)
}
if a.analyzer.Name != "" {
return fmt.Sprintf("%s Statefulset Status", a.analyzer.Name)
}
if a.analyzer.Namespace != "" {
return fmt.Sprintf("%s Statefulset Status", a.analyzer.Namespace)
}
return "Statefulset Status"
}
func (a *AnalyzeStatefulsetStatus) IsExcluded() (bool, error) {
return isExcluded(a.analyzer.Exclude)
}
func (a *AnalyzeStatefulsetStatus) Analyze(getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
results, err := analyzeStatefulsetStatus(a.analyzer, findFiles)
if err != nil {
return nil, err
}
for i := range results {
results[i].Strict = a.analyzer.Strict.BoolOrDefaultFalse()
}
return results, nil
}
func analyzeStatefulsetStatus(analyzer *troubleshootv1beta2.StatefulsetStatus, getFileContents getChildCollectedFileContents) ([]*AnalyzeResult, error) {
if analyzer.Name == "" {
return analyzeAllStatefulsetStatuses(analyzer, getFileContents)
} else {
return analyzeOneStatefulsetStatus(analyzer, getFileContents)
}
}
func analyzeOneStatefulsetStatus(analyzer *troubleshootv1beta2.StatefulsetStatus, getFileContents getChildCollectedFileContents) ([]*AnalyzeResult, error) {
excludeFiles := []string{}
files, err := getFileContents(filepath.Join(constants.CLUSTER_RESOURCES_DIR, constants.CLUSTER_RESOURCES_STATEFULSETS, fmt.Sprintf("%s.json", analyzer.Namespace)), excludeFiles)
if err != nil {
return nil, errors.Wrap(err, "failed to read collected statefulsets from namespace")
}
var result *AnalyzeResult
for _, collected := range files { // only 1 file here
var exists bool = true
var readyReplicas int
var statefulsets appsv1.StatefulSetList
if err := json.Unmarshal(collected, &statefulsets); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal statefulset list")
}
var statefulset *appsv1.StatefulSet
for _, s := range statefulsets.Items {
if s.Name == analyzer.Name {
statefulset = s.DeepCopy()
break
}
}
if statefulset == nil {
exists = false
readyReplicas = 0
} else {
readyReplicas = int(statefulset.Status.ReadyReplicas)
}
if len(analyzer.Outcomes) > 0 {
result, err = commonStatus(analyzer.Outcomes, analyzer.Name, "kubernetes_statefulset_status", "https://troubleshoot.sh/images/analyzer-icons/statefulset-status.svg?w=23&h=14", readyReplicas, exists, "statefulset")
if err != nil {
return nil, errors.Wrap(err, "failed to process status")
}
} else {
result = getDefaultStatefulSetResult(statefulset)
}
}
if result == nil {
return nil, nil
}
return []*AnalyzeResult{result}, nil
}
func analyzeAllStatefulsetStatuses(analyzer *troubleshootv1beta2.StatefulsetStatus, getFileContents getChildCollectedFileContents) ([]*AnalyzeResult, error) {
fileNames := make([]string, 0)
if analyzer.Namespace != "" {
fileNames = append(fileNames, filepath.Join(constants.CLUSTER_RESOURCES_DIR, constants.CLUSTER_RESOURCES_STATEFULSETS, fmt.Sprintf("%s.json", analyzer.Namespace)))
}
for _, ns := range analyzer.Namespaces {
fileNames = append(fileNames, filepath.Join(constants.CLUSTER_RESOURCES_DIR, constants.CLUSTER_RESOURCES_STATEFULSETS, fmt.Sprintf("%s.json", ns)))
}
// no namespace specified, so we need to analyze all statefulsets
if len(fileNames) == 0 {
fileNames = append(fileNames, filepath.Join(constants.CLUSTER_RESOURCES_DIR, constants.CLUSTER_RESOURCES_STATEFULSETS, "*.json"))
}
excludeFiles := []string{}
results := []*AnalyzeResult{}
for _, fileName := range fileNames {
files, err := getFileContents(fileName, excludeFiles)
if err != nil {
return nil, errors.Wrap(err, "failed to read collected statefulsets from namespace")
}
for _, collected := range files {
var statefulsets appsv1.StatefulSetList
if err := json.Unmarshal(collected, &statefulsets); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal statefulset list")
}
for _, statefulset := range statefulsets.Items {
result := getDefaultStatefulSetResult(&statefulset)
if result != nil {
results = append(results, result)
}
}
}
}
return results, nil
}
func getDefaultStatefulSetResult(statefulset *appsv1.StatefulSet) *AnalyzeResult {
if statefulset.Status.Replicas == statefulset.Status.ReadyReplicas {
return nil
}
return &AnalyzeResult{
Title: fmt.Sprintf("%s/%s Statefulset Status", statefulset.Namespace, statefulset.Name),
IconKey: "kubernetes_statefulset_status",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/statefulset-status.svg?w=23&h=14",
IsFail: true,
Message: fmt.Sprintf("The statefulset %s/%s has %d/%d replicas", statefulset.Namespace, statefulset.Name, statefulset.Status.ReadyReplicas, statefulset.Status.Replicas),
}
}