Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying namespaces when analyzing cluster resources #515

Merged
merged 2 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 24 additions & 6 deletions pkg/analyze/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,32 @@ import (
_ "embed"
)

//go:embed files/deployments.json
var collectedDeployments string
//go:embed files/deployments/default.json
var defaultDeployments string

//go:embed files/deployments/monitoring.json
var monitoringDeployments string

//go:embed files/deployments/kube-system.json
var kubeSystemDeployments string

//go:embed files/nodes.json
var collectedNodes string

//go:embed files/jobs.json
var collectedJobs string
//go:embed files/jobs/test.json
var testJobs string

//go:embed files/jobs/projectcontour.json
var projectcontourJobs string

//go:embed files/replicasets/default.json
var defaultReplicaSets string

//go:embed files/replicasets/rook-ceph.json
var rookCephReplicaSets string

//go:embed files/statefulsets/default.json
var defaultStatefulSets string

//go:embed files/replicasets.json
var collectedReplicaSets string
//go:embed files/statefulsets/monitoring.json
var monitoringStatefulSets string
52 changes: 30 additions & 22 deletions pkg/analyze/deployment_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,39 +59,47 @@ func analyzeOneDeploymentStatus(analyzer *troubleshootv1beta2.DeploymentStatus,
}

func analyzeAllDeploymentStatuses(analyzer *troubleshootv1beta2.DeploymentStatus, getFileContents func(string) (map[string][]byte, error)) ([]*AnalyzeResult, error) {
var fileName string
fileNames := make([]string, 0)
if analyzer.Namespace != "" {
fileName = filepath.Join("cluster-resources", "deployments", fmt.Sprintf("%s.json", analyzer.Namespace))
} else {
fileName = filepath.Join("cluster-resources", "deployments", "*.json")
fileNames = append(fileNames, filepath.Join("cluster-resources", "deployments", fmt.Sprintf("%s.json", analyzer.Namespace)))
}
for _, ns := range analyzer.Namespaces {
fileNames = append(fileNames, filepath.Join("cluster-resources", "deployments", fmt.Sprintf("%s.json", ns)))
}

files, err := getFileContents(fileName)
if err != nil {
return nil, errors.Wrap(err, "failed to read collected deployments from file")
// no namespace specified, so we need to analyze all deployments
if len(fileNames) == 0 {
fileNames = append(fileNames, filepath.Join("cluster-resources", "deployments", "*.json"))
}

results := []*AnalyzeResult{}
for _, collected := range files {
var deployments []appsv1.Deployment
if err := json.Unmarshal(collected, &deployments); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal deployment list")
for _, fileName := range fileNames {
files, err := getFileContents(fileName)
if err != nil {
return nil, errors.Wrap(err, "failed to read collected deployments from file")
}

for _, deployment := range deployments {
if deployment.Status.Replicas == deployment.Status.AvailableReplicas {
continue
for _, collected := range files {
var deployments []appsv1.Deployment
if err := json.Unmarshal(collected, &deployments); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal deployment list")
}

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),
}
for _, deployment := range deployments {
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)
results = append(results, result)
}
}
}

Expand Down
52 changes: 47 additions & 5 deletions pkg/analyze/deployment_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func Test_deploymentStatus(t *testing.T) {
},
},
files: map[string][]byte{
"cluster-resources/deployments/default.json": []byte(collectedDeployments),
"cluster-resources/deployments/default.json": []byte(defaultDeployments),
"cluster-resources/deployments/monitoring.json": []byte(monitoringDeployments),
"cluster-resources/deployments/kube-system.json": []byte(kubeSystemDeployments),
},
},
{
Expand Down Expand Up @@ -80,7 +82,9 @@ func Test_deploymentStatus(t *testing.T) {
},
},
files: map[string][]byte{
"cluster-resources/deployments/default.json": []byte(collectedDeployments),
"cluster-resources/deployments/default.json": []byte(defaultDeployments),
"cluster-resources/deployments/monitoring.json": []byte(monitoringDeployments),
"cluster-resources/deployments/kube-system.json": []byte(kubeSystemDeployments),
},
},
{
Expand Down Expand Up @@ -120,7 +124,40 @@ func Test_deploymentStatus(t *testing.T) {
},
},
files: map[string][]byte{
"cluster-resources/deployments/default.json": []byte(collectedDeployments),
"cluster-resources/deployments/default.json": []byte(defaultDeployments),
"cluster-resources/deployments/monitoring.json": []byte(monitoringDeployments),
"cluster-resources/deployments/kube-system.json": []byte(kubeSystemDeployments),
},
},
{
name: "multiple namespaces, 2/3",
analyzer: troubleshootv1beta2.DeploymentStatus{
Namespaces: []string{"default", "monitoring"},
},
expectResult: []*AnalyzeResult{
{
IsPass: false,
IsWarn: false,
IsFail: true,
Title: "default/kotsadm-web Deployment Status",
Message: "The deployment default/kotsadm-web has 1/2 replicas",
IconKey: "kubernetes_deployment_status",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17",
},
{
IsPass: false,
IsWarn: false,
IsFail: true,
Title: "monitoring/prometheus-operator Deployment Status",
Message: "The deployment monitoring/prometheus-operator has 1/2 replicas",
IconKey: "kubernetes_deployment_status",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17",
},
},
files: map[string][]byte{
"cluster-resources/deployments/default.json": []byte(defaultDeployments),
"cluster-resources/deployments/monitoring.json": []byte(monitoringDeployments),
"cluster-resources/deployments/kube-system.json": []byte(kubeSystemDeployments),
},
},
}
Expand All @@ -130,14 +167,19 @@ func Test_deploymentStatus(t *testing.T) {
req := require.New(t)

getFiles := func(n string) (map[string][]byte, error) {
if file, ok := test.files[n]; ok {
return map[string][]byte{n: file}, nil
}
return test.files, nil
}

actual, err := analyzeDeploymentStatus(&test.analyzer, getFiles)
req.NoError(err)

assert.Equal(t, test.expectResult, actual)

req.Equal(len(test.expectResult), len(actual))
for _, a := range actual {
assert.Contains(t, test.expectResult, a)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@
},
"status": {
"observedGeneration": 1,
"replicas": 1,
"replicas": 2,
"updatedReplicas": 1,
"readyReplicas": 1,
"availableReplicas": 1,
Expand Down