Skip to content

Commit b4c97e3

Browse files
authored
Merge pull request #477 from replicatedhq/divolgin/selectors
replicaset analyzer supports label selectors
2 parents 5d9f14f + 7cb6d90 commit b4c97e3

10 files changed

+79
-40
lines changed

config/crds/troubleshoot.sh_analyzers.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,10 +869,15 @@ spec:
869869
type: object
870870
type: object
871871
type: array
872+
selector:
873+
items:
874+
type: string
875+
type: array
872876
required:
873877
- name
874878
- namespace
875879
- outcomes
880+
- selector
876881
type: object
877882
secret:
878883
properties:

config/crds/troubleshoot.sh_preflights.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,10 +869,15 @@ spec:
869869
type: object
870870
type: object
871871
type: array
872+
selector:
873+
items:
874+
type: string
875+
type: array
872876
required:
873877
- name
874878
- namespace
875879
- outcomes
880+
- selector
876881
type: object
877882
secret:
878883
properties:

config/crds/troubleshoot.sh_supportbundles.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,10 +900,15 @@ spec:
900900
type: object
901901
type: object
902902
type: array
903+
selector:
904+
items:
905+
type: string
906+
type: array
903907
required:
904908
- name
905909
- namespace
906910
- outcomes
911+
- selector
907912
type: object
908913
secret:
909914
properties:

pkg/analyze/replicaset_status.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/pkg/errors"
1111
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
1212
appsv1 "k8s.io/api/apps/v1"
13+
"k8s.io/apimachinery/pkg/labels"
1314
)
1415

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

78+
labelSelector, err := labels.Parse(strings.Join(analyzer.Selector, ","))
79+
if err != nil {
80+
return nil, errors.Wrap(err, "failed to parse selector")
81+
}
82+
7783
results := []*AnalyzeResult{}
7884
for _, collected := range files {
7985
var replicasets []appsv1.ReplicaSet
@@ -82,20 +88,31 @@ func analyzeAllReplicaSetStatuses(analyzer *troubleshootv1beta2.ReplicaSetStatus
8288
}
8389

8490
for _, replicaset := range replicasets {
85-
if replicaset.Spec.Replicas == nil && replicaset.Status.AvailableReplicas == 1 { // default is 1
91+
if !labelSelector.Matches(labels.Set(replicaset.Labels)) {
8692
continue
8793
}
8894

89-
if replicaset.Spec.Replicas != nil && *replicaset.Spec.Replicas == replicaset.Status.AvailableReplicas {
90-
continue
91-
}
92-
93-
result := &AnalyzeResult{
94-
Title: fmt.Sprintf("%s/%s ReplicaSet Status", replicaset.Namespace, replicaset.Name),
95-
IconKey: "kubernetes_deployment_status", // TODO: need new icon
96-
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17", // TODO: need new icon
97-
IsFail: true,
98-
Message: fmt.Sprintf("The replicaset %s/%s is not ready", replicaset.Namespace, replicaset.Name),
95+
var result *AnalyzeResult
96+
if len(analyzer.Outcomes) > 0 {
97+
result, err = replicasetStatus(analyzer.Outcomes, &replicaset)
98+
if err != nil {
99+
return nil, errors.Wrap(err, "failed to process status")
100+
}
101+
} else {
102+
if replicaset.Spec.Replicas == nil && replicaset.Status.AvailableReplicas == 1 { // default is 1
103+
continue
104+
}
105+
if replicaset.Spec.Replicas != nil && *replicaset.Spec.Replicas == replicaset.Status.AvailableReplicas {
106+
continue
107+
}
108+
109+
result = &AnalyzeResult{
110+
Title: fmt.Sprintf("%s/%s ReplicaSet Status", replicaset.Namespace, replicaset.Name),
111+
IconKey: "kubernetes_deployment_status", // TODO: need new icon
112+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17", // TODO: need new icon
113+
IsFail: true,
114+
Message: fmt.Sprintf("The replicaset %s/%s is not ready", replicaset.Namespace, replicaset.Name),
115+
}
99116
}
100117

101118
results = append(results, result)

pkg/analyze/replicaset_status_test.go

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func Test_analyzeReplicaSetStatus(t *testing.T) {
6666
},
6767
},
6868
Namespace: "rook-ceph",
69-
Name: "csi-cephfsplugin-provisioner-56d4db5b99",
69+
Selector: []string{"app=csi-cephfsplugin-provisioner"},
7070
},
7171
expectResult: []*AnalyzeResult{
7272
{
@@ -84,37 +84,17 @@ func Test_analyzeReplicaSetStatus(t *testing.T) {
8484
},
8585
},
8686
{
87-
name: "pass because 1 is available",
87+
name: "find the one failing replicaset",
8888
analyzer: troubleshootv1beta2.ReplicaSetStatus{
89-
Outcomes: []*troubleshootv1beta2.Outcome{
90-
{
91-
Pass: &troubleshootv1beta2.SingleOutcome{
92-
When: "available = 1",
93-
Message: "pass",
94-
},
95-
},
96-
{
97-
Warn: &troubleshootv1beta2.SingleOutcome{
98-
When: "ready = 1",
99-
Message: "warn",
100-
},
101-
},
102-
{
103-
Fail: &troubleshootv1beta2.SingleOutcome{
104-
Message: "default fail",
105-
},
106-
},
107-
},
10889
Namespace: "rook-ceph",
109-
Name: "csi-rbdplugin-provisioner-d84959fcb",
11090
},
11191
expectResult: []*AnalyzeResult{
11292
{
113-
IsPass: true,
93+
IsPass: false,
11494
IsWarn: false,
115-
IsFail: false,
116-
Title: "csi-rbdplugin-provisioner-d84959fcb Status",
117-
Message: "pass",
95+
IsFail: true,
96+
Title: "rook-ceph/rook-ceph-mds-rook-shared-fs-b-7895f484f5 ReplicaSet Status",
97+
Message: "The replicaset rook-ceph/rook-ceph-mds-rook-shared-fs-b-7895f484f5 is not ready",
11898
IconKey: "kubernetes_deployment_status",
11999
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17",
120100
},

pkg/apis/troubleshoot/v1beta2/analyzer_shared.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ type ReplicaSetStatus struct {
7575
Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"`
7676
Namespace string `json:"namespace" yaml:"namespace"`
7777
Name string `json:"name" yaml:"name"`
78+
Selector []string `json:"selector" yaml:"selector"`
7879
}
7980

8081
type ClusterPodStatuses struct {

pkg/apis/troubleshoot/v1beta2/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

schemas/analyzer-troubleshoot-v1beta2.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,8 @@
12441244
"required": [
12451245
"name",
12461246
"namespace",
1247-
"outcomes"
1247+
"outcomes",
1248+
"selector"
12481249
],
12491250
"properties": {
12501251
"checkName": {
@@ -1308,6 +1309,12 @@
13081309
}
13091310
}
13101311
}
1312+
},
1313+
"selector": {
1314+
"type": "array",
1315+
"items": {
1316+
"type": "string"
1317+
}
13111318
}
13121319
}
13131320
},

schemas/preflight-troubleshoot-v1beta2.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,8 @@
12441244
"required": [
12451245
"name",
12461246
"namespace",
1247-
"outcomes"
1247+
"outcomes",
1248+
"selector"
12481249
],
12491250
"properties": {
12501251
"checkName": {
@@ -1308,6 +1309,12 @@
13081309
}
13091310
}
13101311
}
1312+
},
1313+
"selector": {
1314+
"type": "array",
1315+
"items": {
1316+
"type": "string"
1317+
}
13111318
}
13121319
}
13131320
},

schemas/supportbundle-troubleshoot-v1beta2.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,8 @@
12901290
"required": [
12911291
"name",
12921292
"namespace",
1293-
"outcomes"
1293+
"outcomes",
1294+
"selector"
12941295
],
12951296
"properties": {
12961297
"checkName": {
@@ -1354,6 +1355,12 @@
13541355
}
13551356
}
13561357
}
1358+
},
1359+
"selector": {
1360+
"type": "array",
1361+
"items": {
1362+
"type": "string"
1363+
}
13571364
}
13581365
}
13591366
},

0 commit comments

Comments
 (0)