Skip to content

Commit d570744

Browse files
authored
commands/.../scorecard: add suggestions (#961)
**Description of the change:** Add support for printing suggestions to improve scorecard score **Motivation for the change:** This provides a better experience for the user as it directly tells them how to improve their score
1 parent b3a0efc commit d570744

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

commands/operator-sdk/cmd/scorecard/basic_tests.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ func checkSpecAndStat(runtimeClient client.Client, obj *unstructured.Unstructure
6666
if err != nil && err != wait.ErrWaitTimeout {
6767
return err
6868
}
69+
if testSpec.earnedPoints != 1 {
70+
scSuggestions = append(scSuggestions, "Add a 'spec' field to your Custom Resource")
71+
}
72+
if testStat.earnedPoints != 1 {
73+
scSuggestions = append(scSuggestions, "Add a 'status' field to your Custom Resource")
74+
}
6975
return nil
7076
}
7177

@@ -93,6 +99,7 @@ func checkStatusUpdate(runtimeClient client.Client, obj *unstructured.Unstructur
9399
err = modifySpecAndCheck(specMap, obj)
94100
if err != nil {
95101
test.earnedPoints = 0
102+
scSuggestions = append(scSuggestions, "Make sure that the 'status' block is always updated to reflect changes after the 'spec' block is changed")
96103
scTests = append(scTests, test)
97104
return nil
98105
}
@@ -211,5 +218,8 @@ func writingIntoCRsHasEffect(obj *unstructured.Unstructured) (string, error) {
211218
}
212219
}
213220
scTests = append(scTests, test)
221+
if test.earnedPoints != 1 {
222+
scSuggestions = append(scSuggestions, "The operator should write into objects to update state. No PUT or POST requests from you operator were recorded by the scorecard.")
223+
}
214224
return buf.String(), nil
215225
}

commands/operator-sdk/cmd/scorecard/olm_tests.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ func crdsHaveResources(csv *olmAPI.ClusterServiceVersion) {
3333
}
3434
}
3535
scTests = append(scTests, test)
36+
if test.earnedPoints == 0 {
37+
scSuggestions = append(scSuggestions, "Add resources to owned CRDs")
38+
}
3639
}
3740

3841
// annotationsContainExamples makes sure that the CSVs list at least 1 example for the CR
@@ -42,6 +45,9 @@ func annotationsContainExamples(csv *olmAPI.ClusterServiceVersion) {
4245
test.earnedPoints = 1
4346
}
4447
scTests = append(scTests, test)
48+
if test.earnedPoints == 0 {
49+
scSuggestions = append(scSuggestions, "Add an alm-examples annotation to your CSV to pass the " + test.name + " test")
50+
}
4551
}
4652

4753
// statusDescriptors makes sure that all status fields found in the created CR has a matching descriptor in the CSV
@@ -73,11 +79,15 @@ func statusDescriptors(csv *olmAPI.ClusterServiceVersion, runtimeClient client.C
7379
for _, statDesc := range crd.StatusDescriptors {
7480
if statDesc.Path == key {
7581
test.earnedPoints++
82+
delete(statusBlock, key)
7683
break
7784
}
7885
}
7986
}
8087
scTests = append(scTests, test)
88+
for key := range statusBlock {
89+
scSuggestions = append(scSuggestions, "Add a status descriptor for "+key)
90+
}
8191
return nil
8292
}
8393

@@ -110,10 +120,14 @@ func specDescriptors(csv *olmAPI.ClusterServiceVersion, runtimeClient client.Cli
110120
for _, specDesc := range crd.SpecDescriptors {
111121
if specDesc.Path == key {
112122
test.earnedPoints++
123+
delete(specBlock, key)
113124
break
114125
}
115126
}
116127
}
117128
scTests = append(scTests, test)
129+
for key := range specBlock {
130+
scSuggestions = append(scSuggestions, "Add a spec descriptor for " + key)
131+
}
118132
return nil
119133
}

commands/operator-sdk/cmd/scorecard/scorecard.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type cleanupFn func() error
8080
var (
8181
kubeconfig *rest.Config
8282
scTests []scorecardTest
83+
scSuggestions []string
8384
dynamicDecoder runtime.Decoder
8485
runtimeClient client.Client
8586
restMapper *restmapper.DeferredDiscoveryRESTMapper
@@ -262,5 +263,9 @@ func ScorecardTests(cmd *cobra.Command, args []string) error {
262263
}
263264
}
264265
fmt.Printf("\nTotal Score: %d/%d points\n", totalEarned, totalMax)
266+
for _, suggestion := range scSuggestions {
267+
// 33 is yellow (specifically, the same shade of yellow that logrus uses for warnings)
268+
fmt.Printf("\x1b[%dmSUGGESTION:\x1b[0m %s\n", 33, suggestion)
269+
}
265270
return nil
266271
}

0 commit comments

Comments
 (0)