Skip to content

Commit

Permalink
Merge pull request #515 from replicatedhq/divolgin/namesapces
Browse files Browse the repository at this point in the history
Allow specifying namespaces when analyzing cluster resources
  • Loading branch information
divolgin committed Dec 17, 2021
2 parents 46cfa14 + 007edd1 commit 137b0ce
Show file tree
Hide file tree
Showing 20 changed files with 6,025 additions and 103 deletions.
30 changes: 24 additions & 6 deletions pkg/analyze/data_test.go
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
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
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)
}
})
}
}
Expand Up @@ -425,7 +425,7 @@
},
"status": {
"observedGeneration": 1,
"replicas": 1,
"replicas": 2,
"updatedReplicas": 1,
"readyReplicas": 1,
"availableReplicas": 1,
Expand Down

0 comments on commit 137b0ce

Please sign in to comment.