-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a72928
commit 55d7aff
Showing
11 changed files
with
606 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,33 @@ | ||
package dryrun | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
const dryRunErrorPrefix = "DryRun event: " | ||
|
||
type DryRunError struct { | ||
GVK string | ||
Namespace, Name string | ||
EventType, Reason, Msg string | ||
Msg string | ||
} | ||
|
||
func NewDryRunError(kind schema.ObjectKind, meta metav1.ObjectMetaAccessor, eventtype, reason, messageFmt string, args ...interface{}) error { | ||
gvk := "unknown" | ||
if kind != nil { | ||
gvk = kind.GroupVersionKind().String() | ||
} | ||
|
||
namespace, name := "unknown", "unknown" | ||
if meta != nil { | ||
namespace = meta.GetObjectMeta().GetNamespace() | ||
name = meta.GetObjectMeta().GetName() | ||
} | ||
|
||
func NewDryRunError(messageFmt string, args ...interface{}) error { | ||
msg := fmt.Sprintf(messageFmt, args...) | ||
|
||
return &DryRunError{ | ||
GVK: gvk, | ||
Namespace: namespace, | ||
Name: name, | ||
EventType: eventtype, | ||
Reason: reason, | ||
Msg: msg, | ||
Msg: msg, | ||
} | ||
} | ||
|
||
func (e *DryRunError) Error() string { | ||
return fmt.Sprintf( | ||
"DryRun event GVK=%v, Namespace=%v, Name=%v, EventType=%v, Reason=%v, Message=%v", | ||
e.GVK, e.Namespace, e.Name, e.EventType, e.Reason, e.Msg, | ||
) | ||
return dryRunErrorPrefix + e.Msg | ||
} | ||
|
||
// isDryRunError returns true if the given error is a DryRunError. | ||
// | ||
// Note: we DO NOT want to export this as we do not want "special dry-run" cases in reconcilers. | ||
// Reconcilers should behave exactly the same during dry-run as during regular reconciles. | ||
func isDryRunError(err error) bool { | ||
dErr := &DryRunError{} | ||
return errors.As(err, &dErr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package dryrun | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
) | ||
|
||
type errorQueue struct { | ||
mu sync.Mutex // protects fields below | ||
|
||
active bool | ||
errs []error | ||
} | ||
|
||
var reconcileErrors = &errorQueue{} | ||
|
||
func AddTerminationError(err error) { | ||
reconcileErrors.mu.Lock() | ||
defer reconcileErrors.mu.Unlock() | ||
|
||
if !reconcileErrors.active { | ||
return | ||
} | ||
|
||
reconcileErrors.errs = append(reconcileErrors.errs, err) | ||
} | ||
|
||
func terminationError() error { | ||
reconcileErrors.mu.Lock() | ||
defer reconcileErrors.mu.Unlock() | ||
|
||
result := make([]error, len(reconcileErrors.errs)) | ||
result = append(result, reconcileErrors.errs...) | ||
|
||
return errors.Join(result...) | ||
} | ||
|
||
func clearTerminationErrors() { | ||
reconcileErrors.mu.Lock() | ||
defer reconcileErrors.mu.Unlock() | ||
|
||
reconcileErrors.errs = nil | ||
} | ||
|
||
func enableErrors() { | ||
reconcileErrors.mu.Lock() | ||
defer reconcileErrors.mu.Unlock() | ||
|
||
reconcileErrors.active = true | ||
} |
Oops, something went wrong.