Skip to content

Commit

Permalink
Merge pull request #477 from replicatedhq/divolgin/selectors
Browse files Browse the repository at this point in the history
replicaset analyzer supports label selectors
  • Loading branch information
divolgin committed Oct 29, 2021
2 parents 5d9f14f + 7cb6d90 commit b4c97e3
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 40 deletions.
5 changes: 5 additions & 0 deletions config/crds/troubleshoot.sh_analyzers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -869,10 +869,15 @@ spec:
type: object
type: object
type: array
selector:
items:
type: string
type: array
required:
- name
- namespace
- outcomes
- selector
type: object
secret:
properties:
Expand Down
5 changes: 5 additions & 0 deletions config/crds/troubleshoot.sh_preflights.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -869,10 +869,15 @@ spec:
type: object
type: object
type: array
selector:
items:
type: string
type: array
required:
- name
- namespace
- outcomes
- selector
type: object
secret:
properties:
Expand Down
5 changes: 5 additions & 0 deletions config/crds/troubleshoot.sh_supportbundles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -900,10 +900,15 @@ spec:
type: object
type: object
type: array
selector:
items:
type: string
type: array
required:
- name
- namespace
- outcomes
- selector
type: object
secret:
properties:
Expand Down
39 changes: 28 additions & 11 deletions pkg/analyze/replicaset_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/pkg/errors"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/labels"
)

func analyzeReplicaSetStatus(analyzer *troubleshootv1beta2.ReplicaSetStatus, getFileContents func(string) (map[string][]byte, error)) ([]*AnalyzeResult, error) {
Expand Down Expand Up @@ -74,6 +75,11 @@ func analyzeAllReplicaSetStatuses(analyzer *troubleshootv1beta2.ReplicaSetStatus
return nil, errors.Wrap(err, "failed to read collected replicaset from file")
}

labelSelector, err := labels.Parse(strings.Join(analyzer.Selector, ","))
if err != nil {
return nil, errors.Wrap(err, "failed to parse selector")
}

results := []*AnalyzeResult{}
for _, collected := range files {
var replicasets []appsv1.ReplicaSet
Expand All @@ -82,20 +88,31 @@ func analyzeAllReplicaSetStatuses(analyzer *troubleshootv1beta2.ReplicaSetStatus
}

for _, replicaset := range replicasets {
if replicaset.Spec.Replicas == nil && replicaset.Status.AvailableReplicas == 1 { // default is 1
if !labelSelector.Matches(labels.Set(replicaset.Labels)) {
continue
}

if replicaset.Spec.Replicas != nil && *replicaset.Spec.Replicas == replicaset.Status.AvailableReplicas {
continue
}

result := &AnalyzeResult{
Title: fmt.Sprintf("%s/%s ReplicaSet Status", replicaset.Namespace, replicaset.Name),
IconKey: "kubernetes_deployment_status", // TODO: need new icon
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17", // TODO: need new icon
IsFail: true,
Message: fmt.Sprintf("The replicaset %s/%s is not ready", replicaset.Namespace, replicaset.Name),
var result *AnalyzeResult
if len(analyzer.Outcomes) > 0 {
result, err = replicasetStatus(analyzer.Outcomes, &replicaset)
if err != nil {
return nil, errors.Wrap(err, "failed to process status")
}
} else {
if replicaset.Spec.Replicas == nil && replicaset.Status.AvailableReplicas == 1 { // default is 1
continue
}
if replicaset.Spec.Replicas != nil && *replicaset.Spec.Replicas == replicaset.Status.AvailableReplicas {
continue
}

result = &AnalyzeResult{
Title: fmt.Sprintf("%s/%s ReplicaSet Status", replicaset.Namespace, replicaset.Name),
IconKey: "kubernetes_deployment_status", // TODO: need new icon
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17", // TODO: need new icon
IsFail: true,
Message: fmt.Sprintf("The replicaset %s/%s is not ready", replicaset.Namespace, replicaset.Name),
}
}

results = append(results, result)
Expand Down
32 changes: 6 additions & 26 deletions pkg/analyze/replicaset_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func Test_analyzeReplicaSetStatus(t *testing.T) {
},
},
Namespace: "rook-ceph",
Name: "csi-cephfsplugin-provisioner-56d4db5b99",
Selector: []string{"app=csi-cephfsplugin-provisioner"},
},
expectResult: []*AnalyzeResult{
{
Expand All @@ -84,37 +84,17 @@ func Test_analyzeReplicaSetStatus(t *testing.T) {
},
},
{
name: "pass because 1 is available",
name: "find the one failing replicaset",
analyzer: troubleshootv1beta2.ReplicaSetStatus{
Outcomes: []*troubleshootv1beta2.Outcome{
{
Pass: &troubleshootv1beta2.SingleOutcome{
When: "available = 1",
Message: "pass",
},
},
{
Warn: &troubleshootv1beta2.SingleOutcome{
When: "ready = 1",
Message: "warn",
},
},
{
Fail: &troubleshootv1beta2.SingleOutcome{
Message: "default fail",
},
},
},
Namespace: "rook-ceph",
Name: "csi-rbdplugin-provisioner-d84959fcb",
},
expectResult: []*AnalyzeResult{
{
IsPass: true,
IsPass: false,
IsWarn: false,
IsFail: false,
Title: "csi-rbdplugin-provisioner-d84959fcb Status",
Message: "pass",
IsFail: true,
Title: "rook-ceph/rook-ceph-mds-rook-shared-fs-b-7895f484f5 ReplicaSet Status",
Message: "The replicaset rook-ceph/rook-ceph-mds-rook-shared-fs-b-7895f484f5 is not ready",
IconKey: "kubernetes_deployment_status",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17",
},
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/troubleshoot/v1beta2/analyzer_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type ReplicaSetStatus struct {
Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"`
Namespace string `json:"namespace" yaml:"namespace"`
Name string `json:"name" yaml:"name"`
Selector []string `json:"selector" yaml:"selector"`
}

type ClusterPodStatuses struct {
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/troubleshoot/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion schemas/analyzer-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,8 @@
"required": [
"name",
"namespace",
"outcomes"
"outcomes",
"selector"
],
"properties": {
"checkName": {
Expand Down Expand Up @@ -1308,6 +1309,12 @@
}
}
}
},
"selector": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
Expand Down
9 changes: 8 additions & 1 deletion schemas/preflight-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,8 @@
"required": [
"name",
"namespace",
"outcomes"
"outcomes",
"selector"
],
"properties": {
"checkName": {
Expand Down Expand Up @@ -1308,6 +1309,12 @@
}
}
}
},
"selector": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
Expand Down
9 changes: 8 additions & 1 deletion schemas/supportbundle-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,8 @@
"required": [
"name",
"namespace",
"outcomes"
"outcomes",
"selector"
],
"properties": {
"checkName": {
Expand Down Expand Up @@ -1354,6 +1355,12 @@
}
}
}
},
"selector": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
Expand Down

0 comments on commit b4c97e3

Please sign in to comment.