Skip to content

Commit 46c0846

Browse files
committed
chore: remove redundant orderKappsValidateErr
1 parent 9f572ba commit 46c0846

File tree

2 files changed

+0
-141
lines changed

2 files changed

+0
-141
lines changed

internal/operator-controller/rukpak/preflights/crdupgradesafety/checks_test.go

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -907,84 +907,6 @@ func TestType(t *testing.T) {
907907
}
908908
}
909909

910-
func TestOrderKappsValidateErr(t *testing.T) {
911-
testErr1 := errors.New("fallback1")
912-
testErr2 := errors.New("fallback2")
913-
914-
generateErrors := func(n int, base string) []error {
915-
var result []error
916-
for i := n; i >= 0; i-- {
917-
result = append(result, fmt.Errorf("%s%d", base, i))
918-
}
919-
return result
920-
}
921-
922-
joinedAndNested := func(format string, errs ...error) error {
923-
return fmt.Errorf(format, errors.Join(errs...))
924-
}
925-
926-
testCases := []struct {
927-
name string
928-
inpuError error
929-
expectedError error
930-
}{
931-
{
932-
name: "fallback: initial error was not error.Join'ed",
933-
inpuError: testErr1,
934-
expectedError: testErr1,
935-
},
936-
{
937-
name: "fallback: nested error was not wrapped",
938-
inpuError: errors.Join(testErr1),
939-
expectedError: testErr1,
940-
},
941-
{
942-
name: "fallback: multiple nested errors, one was not wrapped",
943-
inpuError: errors.Join(testErr2, fmt.Errorf("%w", testErr1)),
944-
expectedError: errors.Join(testErr2, fmt.Errorf("%w", testErr1)),
945-
},
946-
{
947-
name: "fallback: nested error did not contain \":\"",
948-
inpuError: errors.Join(fmt.Errorf("%w", testErr1)),
949-
expectedError: testErr1,
950-
},
951-
{
952-
name: "fallback: multiple nested errors, one did not contain \":\"",
953-
inpuError: errors.Join(joinedAndNested("fail: %w", testErr2), joinedAndNested("%w", testErr1)),
954-
expectedError: errors.Join(fmt.Errorf("fail: %w", testErr2), testErr1),
955-
},
956-
{
957-
name: "fallback: nested error was not error.Join'ed",
958-
inpuError: errors.Join(fmt.Errorf("fail: %w", testErr1)),
959-
expectedError: fmt.Errorf("fail: %w", testErr1),
960-
},
961-
{
962-
name: "fallback: multiple nested errors, one was not error.Join'ed",
963-
inpuError: errors.Join(joinedAndNested("fail: %w", testErr2), fmt.Errorf("fail: %w", testErr1)),
964-
expectedError: fmt.Errorf("fail: %w\nfail: %w", testErr2, testErr1),
965-
},
966-
{
967-
name: "ensures order for a single group of multiple deeply nested errors",
968-
inpuError: errors.Join(joinedAndNested("fail: %w", testErr2, testErr1)),
969-
expectedError: fmt.Errorf("fail: %w\n%w", testErr1, testErr2),
970-
},
971-
{
972-
name: "ensures order for multiple groups of deeply nested errors",
973-
inpuError: errors.Join(
974-
joinedAndNested("fail: %w", testErr2, testErr1),
975-
joinedAndNested("validation: %w", generateErrors(5, "err")...),
976-
),
977-
expectedError: fmt.Errorf("fail: %w\n%w\nvalidation: err0\nerr1\nerr2\nerr3\nerr4\nerr5", testErr1, testErr2),
978-
},
979-
}
980-
for _, tc := range testCases {
981-
t.Run(tc.name, func(t *testing.T) {
982-
err := orderKappsValidateErr(tc.inpuError)
983-
require.EqualError(t, err, tc.expectedError.Error())
984-
})
985-
}
986-
}
987-
988910
func TestServedVersionValidator(t *testing.T) {
989911
validationErr1 := errors.New(`version "v1alpha1", field "^" has unknown change, refusing to determine that change is safe`)
990912
validationErr2 := errors.New(`version upgrade "v1alpha1" to "v1alpha2", field "^": fail`)

internal/operator-controller/rukpak/preflights/crdupgradesafety/crdupgradesafety.go

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package crdupgradesafety
22

33
import (
4-
"cmp"
54
"context"
65
"errors"
76
"fmt"
8-
"slices"
97
"strings"
108

119
"helm.sh/helm/v3/pkg/release"
@@ -119,64 +117,3 @@ func (p *Preflight) runPreflight(ctx context.Context, rel *release.Release) erro
119117

120118
return errors.Join(validateErrors...)
121119
}
122-
123-
// orderKappsValidateErr is meant as a temporary solution to the problem
124-
// of randomly ordered multi-line validation error returned by kapp's validator.Validate()
125-
//
126-
// The problem is that kapp's field validations are performed in map iteration order, which is not fixed.
127-
// Errors from those validations are then error.Join'ed, fmt.Errorf'ed and error.Join'ed again,
128-
// which means original messages are available at 3rd level of nesting, and this is where we need to
129-
// sort them to ensure we do not enter into constant reconciliation loop because of random order of
130-
// failure message we ultimately set in ClusterExtension's status conditions.
131-
//
132-
// This helper attempts to do that and falls back to original unchanged error message
133-
// in case of any unforeseen issues which likely mean that the internals of validator.Validate
134-
// have changed.
135-
//
136-
// For full context see:
137-
// github.com/operator-framework/operator-controller/issues/1456 (original issue and comments)
138-
// github.com/carvel-dev/kapp/pull/1047 (PR to ensure order in upstream)
139-
//
140-
// TODO: remove this once ordering has been handled by the upstream.
141-
func orderKappsValidateErr(err error) error {
142-
joinedValidationErrs, ok := err.(interface{ Unwrap() []error })
143-
if !ok {
144-
return err
145-
}
146-
147-
// nolint: prealloc
148-
var errs []error
149-
for _, validationErr := range joinedValidationErrs.Unwrap() {
150-
unwrappedValidationErr := errors.Unwrap(validationErr)
151-
// validator.Validate did not error.Join'ed validation errors
152-
// kapp's internals changed - fallback to original error
153-
if unwrappedValidationErr == nil {
154-
return err
155-
}
156-
157-
prefix, _, ok := strings.Cut(validationErr.Error(), ":")
158-
// kapp's internal error format changed - fallback to original error
159-
if !ok {
160-
return err
161-
}
162-
163-
// attempt to unwrap and sort field errors
164-
joinedFieldErrs, ok := unwrappedValidationErr.(interface{ Unwrap() []error })
165-
// ChangeValidator did not error.Join'ed field validation errors
166-
// kapp's internals changed - fallback to original error
167-
if !ok {
168-
return err
169-
}
170-
171-
// ensure order of the field validation errors
172-
unwrappedFieldErrs := joinedFieldErrs.Unwrap()
173-
slices.SortFunc(unwrappedFieldErrs, func(a, b error) int {
174-
return cmp.Compare(a.Error(), b.Error())
175-
})
176-
177-
// re-join the sorted field errors keeping the original error prefix from kapp
178-
errs = append(errs, fmt.Errorf("%s: %w", prefix, errors.Join(unwrappedFieldErrs...)))
179-
}
180-
181-
return errors.Join(errs...)
182-
}

0 commit comments

Comments
 (0)