generated from cybozu-go/neco-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from erikgb/namespace-terminating
fix: should ignore create errors when namespace terminating
- Loading branch information
Showing
6 changed files
with
84 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package errors | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
) | ||
|
||
// ErrorIs returns true if an error satisfies a particular condition. | ||
type ErrorIs func(err error) bool | ||
|
||
// Ignore ignores errors that satisfy any of the supplied ErrorIs functions | ||
// by returning nil. Errors that do not satisfy any of the supplied functions | ||
// are returned unmodified. | ||
func Ignore(err error, is ...ErrorIs) error { | ||
for _, f := range is { | ||
if f(err) { | ||
return nil | ||
} | ||
} | ||
return err | ||
} | ||
|
||
// IsNamespaceTerminating returns true if the error is a namespace is terminating error. | ||
func IsNamespaceTerminating(err error) bool { | ||
return apierrors.HasStatusCause(err, corev1.NamespaceTerminatingCause) | ||
} |
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,42 @@ | ||
package errors | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
) | ||
|
||
func TestIgnore(t *testing.T) { | ||
err := errors.New("error") | ||
|
||
type args struct { | ||
err error | ||
is []ErrorIs | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{name: "ignored", args: args{err: err, is: []ErrorIs{IsErrorAlways}}}, | ||
{name: "not ignored", args: args{err: err, is: []ErrorIs{IsErrorNever}}, wantErr: true}, | ||
{name: "ignored by first", args: args{err: err, is: []ErrorIs{IsErrorAlways, IsErrorNever}}}, | ||
{name: "ignored by second", args: args{err: err, is: []ErrorIs{IsErrorNever, IsErrorAlways}}}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := Ignore(tt.args.err, tt.args.is...); (err != nil) != tt.wantErr { | ||
t.Errorf("IgnoreAny() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
//goland:noinspection GoUnusedParameter | ||
func IsErrorAlways(err error) bool { | ||
return true | ||
} | ||
|
||
//goland:noinspection GoUnusedParameter | ||
func IsErrorNever(err error) bool { | ||
return false | ||
} |