Skip to content

Commit 81773a0

Browse files
committed
Filter multiattach errors
1 parent aa634ee commit 81773a0

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

pkg/common/utils.go

+14
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,9 @@ func CodeForError(sourceError error) codes.Code {
337337
return codes.Internal
338338
}
339339

340+
if code, err := isUserMultiAttachError(sourceError); err == nil {
341+
return code
342+
}
340343
if code, err := existingErrorCode(sourceError); err == nil {
341344
return code
342345
}
@@ -374,6 +377,17 @@ func isContextError(err error) (codes.Code, error) {
374377
return codes.Unknown, fmt.Errorf("Not a context error: %w", err)
375378
}
376379

380+
// isUserMultiAttachError returns an InvalidArgument if the error is
381+
// multi-attach detected from the API server. If we get this error from the API
382+
// server, it means that the kubelet doesn't know about the multiattch so it is
383+
// due to user configuration.
384+
func isUserMultiAttachError(err error) (codes.Code, error) {
385+
if strings.Contains(err.Error(), "The disk resource") && strings.Contains(err.Error(), "is already being used") {
386+
return codes.InvalidArgument, nil
387+
}
388+
return codes.Unknown, fmt.Errorf("Not a user multiattach error: %w", err)
389+
}
390+
377391
func existingErrorCode(err error) (codes.Code, error) {
378392
if err == nil {
379393
return codes.Unknown, fmt.Errorf("null error")

pkg/common/utils_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,11 @@ func TestCodeForError(t *testing.T) {
10161016
inputErr: nil,
10171017
expCode: codes.Internal,
10181018
},
1019+
{
1020+
name: "user multiattach error",
1021+
inputErr: fmt.Errorf("The disk resource 'projects/foo/disk/bar' is already being used by 'projects/foo/instances/1'"),
1022+
expCode: codes.InvalidArgument,
1023+
},
10191024
}
10201025

10211026
for _, tc := range testCases {
@@ -1077,6 +1082,34 @@ func TestIsContextError(t *testing.T) {
10771082
}
10781083
}
10791084

1085+
func TestIsUserMultiAttachError(t *testing.T) {
1086+
cases := []struct {
1087+
errorString string
1088+
expectedCode codes.Code
1089+
expectCode bool
1090+
}{
1091+
{
1092+
errorString: "The disk resource 'projects/foo/disk/bar' is already being used by 'projects/foo/instance/biz'",
1093+
expectedCode: codes.InvalidArgument,
1094+
expectCode: true,
1095+
},
1096+
{
1097+
errorString: "The disk resource is ok!",
1098+
expectCode: false,
1099+
},
1100+
}
1101+
for _, test := range cases {
1102+
code, err := isUserMultiAttachError(fmt.Errorf(test.errorString))
1103+
if test.expectCode {
1104+
if err != nil || code != test.expectedCode {
1105+
t.Errorf("Failed with non-nil error %v or bad code %v: %s", err, code, test.errorString)
1106+
}
1107+
} else if err == nil {
1108+
t.Errorf("Expected error for test but got none: %s", test.errorString)
1109+
}
1110+
}
1111+
}
1112+
10801113
func TestIsValidDiskEncryptionKmsKey(t *testing.T) {
10811114
cases := []struct {
10821115
diskEncryptionKmsKey string

0 commit comments

Comments
 (0)