Skip to content

Commit ede9ae8

Browse files
authored
Fix invalid exit code on success (#1196)
Signed-off-by: Fiachra Corcoran <[email protected]>
1 parent 6f7e5e6 commit ede9ae8

File tree

2 files changed

+10
-24
lines changed

2 files changed

+10
-24
lines changed

functions/go/kubeconform/kubeconform.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,20 @@ func Run(rl *fn.ResourceList) (bool, error) {
7878
)
7979

8080
var results fn.Results
81-
var exit error
81+
hasValidationErrors := false
8282
for _, obj := range rl.Items {
8383
objResults, err := runKubeconformForObject(obj, args)
8484
if err != nil {
85-
if exit == nil {
86-
exit = fmt.Errorf("KRM validation failed")
87-
}
88-
results = append(results, objResults...)
89-
continue
85+
return false, err
86+
}
87+
if len(objResults) > 0 {
88+
hasValidationErrors = true
9089
}
90+
results = append(results, objResults...)
9191
}
9292

9393
rl.Results = append(rl.Results, results...)
94-
if exit != nil {
95-
return false, nil
96-
}
97-
return true, nil
94+
return !hasValidationErrors, nil
9895
}
9996

10097
func runKubeconformForObject(obj *fn.KubeObject, args []string) (fn.Results, error) {
@@ -138,21 +135,15 @@ func runKubeconformForObject(obj *fn.KubeObject, args []string) (fn.Results, err
138135
}
139136

140137
var results fn.Results
141-
var hasError bool
142138
for _, res := range kubeConf.Resources {
143139
if res.Status != "statusValid" {
144-
hasError = true
145140
for _, e := range res.ValidationErrors {
146141
results = append(results, ConfigObjectResult(e.Msg, e.Path, obj, fn.Error))
147142
}
148143
}
149144
}
150-
var validationError error
151-
if hasError {
152-
validationError = fmt.Errorf("validation failed for one or more resources")
153-
}
154145

155-
return results, validationError
146+
return results, nil
156147
}
157148

158149
func ConfigObjectResult(msg string, path string, obj *fn.KubeObject, severity fn.Severity) *fn.Result {

functions/go/kubeconform/main.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,17 @@ import (
2121
"os"
2222
)
2323

24-
// nolint
2524
func main() {
26-
2725
cmd := &cobra.Command{
2826
Short: generated.KubeconformShort,
2927
Long: generated.KubeconformLong,
3028
RunE: func(cmd *cobra.Command, args []string) error {
3129
return fn.AsMain(fn.ResourceListProcessorFunc(Run))
3230
},
33-
SilenceUsage: true, // don't print usage on error
34-
SilenceErrors: true, // suppress default error printing to stderr
31+
SilenceUsage: true,
32+
SilenceErrors: true,
3533
}
3634

37-
// Optionally add flags here if you want CLI users to override FunctionConfig
38-
//cmd.Flags().BoolP("help", "h", false, "Show help")
39-
4035
if err := cmd.Execute(); err != nil {
4136
os.Exit(1)
4237
}

0 commit comments

Comments
 (0)