Skip to content

Commit

Permalink
Extracting validation error from apply dry run output
Browse files Browse the repository at this point in the history
Signed-off-by: Laszlo Fogas <laszlo@laszlo.cloud>
  • Loading branch information
laszlocph committed Feb 19, 2021
1 parent 528e118 commit 48ab6a0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion controllers/kustomization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ func (r *KustomizationReconciler) validate(ctx context.Context, kustomization ku
if errors.Is(err, context.DeadlineExceeded) {
return fmt.Errorf("validation timeout: %w", err)
}
return fmt.Errorf("validation failed: %s", string(output))
return fmt.Errorf("validation failed: %s", parseApplyError(output))
}
return nil
}
Expand Down
5 changes: 4 additions & 1 deletion controllers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ func parseApplyError(in []byte) string {
for _, line := range lines {
if line != "" &&
!strings.HasSuffix(line, "created") &&
!strings.HasSuffix(line, "created (dry run)") &&
!strings.HasSuffix(line, "configured") &&
!strings.HasSuffix(line, "unchanged") {
!strings.HasSuffix(line, "configured (dry run)") &&
!strings.HasSuffix(line, "unchanged") &&
!strings.HasSuffix(line, "unchanged (dry run)") {
errors += line + "\n"
}
}
Expand Down
32 changes: 32 additions & 0 deletions controllers/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package controllers

import (
"strings"
"testing"
)

func Test_parseApplyError(t *testing.T) {
filtered := parseApplyError([]byte(`
gitrepository.source.toolkit.fluxcd.io/flux-workspaces unchanged
ingressroute.traefik.containo.us/flux-receiver configured
service/notification-controller created
The Service "webhook-receiver" is invalid: spec.clusterIP: Invalid value: "10.200.133.61": field is immutable`))
filtered = strings.TrimSpace(filtered)
numLines := len(strings.Split(filtered, "\n"))
if numLines != 1 {
t.Errorf("Should filter out all but one line from the error output, but got %d lines", numLines)
}
}

func Test_parseApplyError_dryRun(t *testing.T) {
filtered := parseApplyError([]byte(`
gitrepository.source.toolkit.fluxcd.io/flux-workspaces unchanged (dry run)
ingressroute.traefik.containo.us/flux-receiver configured (dry run)
service/notification-controller created (dry run)
error: error validating data: unknown field "ima ge" in io.k8s.api.core.v1.Container`))
filtered = strings.TrimSpace(filtered)
numLines := len(strings.Split(filtered, "\n"))
if numLines != 1 {
t.Errorf("Should filter out all but one line from the error output, but got %d lines", numLines)
}
}

0 comments on commit 48ab6a0

Please sign in to comment.