Skip to content

Commit

Permalink
ssa: make dry-run error typed
Browse files Browse the repository at this point in the history
This enriches the options a consumer has to debug the error, as it now
provides access to the Unstructured which produced the error. While
also allowing the upcoming `jsondiff` sub-package to reuse and emit
the error.

Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco committed Oct 7, 2023
1 parent e1cb374 commit dd98209
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 31 deletions.
76 changes: 76 additions & 0 deletions ssa/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2023 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package ssa

import (
"fmt"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

// DryRunErr is an error that occurs during a server-side dry-run apply.
type DryRunErr struct {
underlyingErr error
involvedObject *unstructured.Unstructured
}

// NewDryRunErr returns a new DryRunErr.
func NewDryRunErr(err error, involvedObject *unstructured.Unstructured) *DryRunErr {
return &DryRunErr{
underlyingErr: err,
involvedObject: involvedObject,
}
}

// InvolvedObject returns the involved object.
func (e *DryRunErr) InvolvedObject() *unstructured.Unstructured {
return e.involvedObject
}

// Error returns the error message.
func (e *DryRunErr) Error() string {
if e.involvedObject == nil {
return e.underlyingErr.Error()
}

if apierrors.IsNotFound(e.Unwrap()) {
if e.involvedObject.GetNamespace() != "" {
return fmt.Sprintf("%s namespace not specified: %s", FmtUnstructured(e.involvedObject), e.Unwrap().Error())
}
return fmt.Sprintf("%s not found: %s", FmtUnstructured(e.involvedObject), e.Unwrap().Error())
}

reason := fmt.Sprintf("%s", apierrors.ReasonForError(e.Unwrap()))

// Detect managed field conflict.
if status, ok := apierrors.StatusCause(e.Unwrap(), metav1.CauseTypeFieldManagerConflict); ok {
reason = fmt.Sprintf("%s", status.Type)
}

if reason != "" {
reason = fmt.Sprintf(" (%s)", reason)
}

return fmt.Sprintf("%s dry-run failed%s: %s", FmtUnstructured(e.involvedObject), reason, e.underlyingErr.Error())
}

// Unwrap returns the underlying error.
func (e *DryRunErr) Unwrap() error {
return e.underlyingErr
}
4 changes: 2 additions & 2 deletions ssa/manager_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (m *ResourceManager) Apply(ctx context.Context, object *unstructured.Unstru
return m.Apply(ctx, object, opts)
}

return nil, m.validationError(dryRunObject, err)
return nil, NewDryRunErr(err, dryRunObject)
}

patched, err := m.cleanupMetadata(ctx, object, existingObject, opts.Cleanup)
Expand Down Expand Up @@ -191,7 +191,7 @@ func (m *ResourceManager) ApplyAll(ctx context.Context, objects []*unstructured.
}

if err != nil {
return m.validationError(dryRunObject, err)
return NewDryRunErr(err, dryRunObject)
}
}

Expand Down
2 changes: 1 addition & 1 deletion ssa/manager_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func TestApply_Force(t *testing.T) {

// verify that the error message does not contain sensitive information
expectedErr := fmt.Sprintf(
"%s dry-run failed, reason: Invalid: Secret \"%s\" is invalid: data: Forbidden: field is immutable when `immutable` is set",
"%s dry-run failed (Invalid): Secret \"%s\" is invalid: data: Forbidden: field is immutable when `immutable` is set",
FmtUnstructured(secret), secret.GetName())
if diff := cmp.Diff(expectedErr, err.Error()); diff != "" {
t.Errorf("Mismatch from expected value (-want +got):\n%s", diff)
Expand Down
29 changes: 1 addition & 28 deletions ssa/manager_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ package ssa

import (
"context"
"fmt"

apiequality "k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -61,7 +57,7 @@ func (m *ResourceManager) Diff(ctx context.Context, object *unstructured.Unstruc

dryRunObject := object.DeepCopy()
if err := m.dryRunApply(ctx, dryRunObject); err != nil {
return nil, nil, nil, m.validationError(dryRunObject, err)
return nil, nil, nil, NewDryRunErr(err, dryRunObject)
}

if dryRunObject.GetResourceVersion() == "" {
Expand Down Expand Up @@ -121,26 +117,3 @@ func prepareObjectForDiff(object *unstructured.Unstructured) *unstructured.Unstr
}
return deepCopy
}

// validationError formats the given error and hides sensitive data
// if the error was caused by an invalid Kubernetes secrets.
func (m *ResourceManager) validationError(object *unstructured.Unstructured, err error) error {
if apierrors.IsNotFound(err) {
return fmt.Errorf("%s namespace not specified: %w", FmtUnstructured(object), err)
}

reason := fmt.Sprintf("%v", apierrors.ReasonForError(err))

// detect managed field conflict
if status, ok := apierrors.StatusCause(err, metav1.CauseTypeFieldManagerConflict); ok {
reason = fmt.Sprintf("%v", status.Type)
}

if reason != "" {
reason = fmt.Sprintf(", reason: %s", reason)
}

return fmt.Errorf("%s dry-run failed%s: %w",
FmtUnstructured(object), reason, err)

}

0 comments on commit dd98209

Please sign in to comment.