Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracting validation error from apply dry run output #280

Merged
merged 1 commit into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}