-
Notifications
You must be signed in to change notification settings - Fork 93
/
deployment_status.go
151 lines (125 loc) · 4.95 KB
/
deployment_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
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 AnalyzeDeploymentStatus struct {
analyzer *troubleshootv1beta2.DeploymentStatus
}
func (a *AnalyzeDeploymentStatus) Title() string {
if a.analyzer.CheckName != "" {
return a.analyzer.CheckName
}
if a.analyzer.Name != "" && a.analyzer.Namespace != "" {
return fmt.Sprintf("%s/%s Deployment Status", a.analyzer.Name, a.analyzer.Name)
}
if a.analyzer.Name != "" {
return fmt.Sprintf("%s Deployment Status", a.analyzer.Name)
}
if a.analyzer.Namespace != "" {
return fmt.Sprintf("%s Deployment Status", a.analyzer.Namespace)
}
return "Deployment Status"
}
func (a *AnalyzeDeploymentStatus) IsExcluded() (bool, error) {
return isExcluded(a.analyzer.Exclude)
}
func (a *AnalyzeDeploymentStatus) Analyze(getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
results, err := analyzeDeploymentStatus(a.analyzer, findFiles)
if err != nil {
return nil, err
}
for i := range results {
results[i].Strict = a.analyzer.Strict.BoolOrDefaultFalse()
}
return results, nil
}
func analyzeDeploymentStatus(analyzer *troubleshootv1beta2.DeploymentStatus, getFileContents getChildCollectedFileContents) ([]*AnalyzeResult, error) {
if analyzer.Name == "" {
return analyzeAllDeploymentStatuses(analyzer, getFileContents)
} else {
return analyzeOneDeploymentStatus(analyzer, getFileContents)
}
}
func analyzeOneDeploymentStatus(analyzer *troubleshootv1beta2.DeploymentStatus, getFileContents getChildCollectedFileContents) ([]*AnalyzeResult, error) {
excludeFiles := []string{}
files, err := getFileContents(filepath.Join(constants.CLUSTER_RESOURCES_DIR, constants.CLUSTER_RESOURCES_DEPLOYMENTS, fmt.Sprintf("%s.json", analyzer.Namespace)), excludeFiles)
if err != nil {
return nil, errors.Wrap(err, "failed to read collected deployments from namespace")
}
var result *AnalyzeResult
for _, collected := range files { // only 1 file here
var exists bool = true
var readyReplicas int
var deployments appsv1.DeploymentList
if err := json.Unmarshal(collected, &deployments); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal deployment list")
}
var status *appsv1.DeploymentStatus
for _, deployment := range deployments.Items {
if deployment.Name == analyzer.Name {
status = deployment.Status.DeepCopy()
}
}
if status == nil {
exists = false
readyReplicas = 0
} else {
readyReplicas = int(status.ReadyReplicas)
}
result, err = commonStatus(analyzer.Outcomes, analyzer.Name, "kubernetes_deployment_status", "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17", readyReplicas, exists, "deployment")
if err != nil {
return nil, errors.Wrap(err, "failed to process status")
}
}
if result == nil {
return nil, nil
}
return []*AnalyzeResult{result}, nil
}
func analyzeAllDeploymentStatuses(analyzer *troubleshootv1beta2.DeploymentStatus, getFileContents getChildCollectedFileContents) ([]*AnalyzeResult, error) {
excludeFiles := []string{}
fileNames := make([]string, 0)
if analyzer.Namespace != "" {
fileNames = append(fileNames, filepath.Join(constants.CLUSTER_RESOURCES_DIR, constants.CLUSTER_RESOURCES_DEPLOYMENTS, fmt.Sprintf("%s.json", analyzer.Namespace)))
}
for _, ns := range analyzer.Namespaces {
fileNames = append(fileNames, filepath.Join(constants.CLUSTER_RESOURCES_DIR, constants.CLUSTER_RESOURCES_DEPLOYMENTS, fmt.Sprintf("%s.json", ns)))
}
// no namespace specified, so we need to analyze all deployments
if len(fileNames) == 0 {
fileNames = append(fileNames, filepath.Join(constants.CLUSTER_RESOURCES_DIR, constants.CLUSTER_RESOURCES_DEPLOYMENTS, "*.json"))
}
results := []*AnalyzeResult{}
for _, fileName := range fileNames {
files, err := getFileContents(fileName, excludeFiles)
if err != nil {
return nil, errors.Wrap(err, "failed to read collected deployments from file")
}
for _, collected := range files {
var deployments appsv1.DeploymentList
if err := json.Unmarshal(collected, &deployments); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal deployment list")
}
for _, deployment := range deployments.Items {
if deployment.Status.Replicas == deployment.Status.AvailableReplicas {
continue
}
result := &AnalyzeResult{
Title: fmt.Sprintf("%s/%s Deployment Status", deployment.Namespace, deployment.Name),
IconKey: "kubernetes_deployment_status",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17",
IsFail: true,
Message: fmt.Sprintf("The deployment %s/%s has %d/%d replicas", deployment.Namespace, deployment.Name, deployment.Status.ReadyReplicas, deployment.Status.Replicas),
}
results = append(results, result)
}
}
}
return results, nil
}