Skip to content

Commit 689eda4

Browse files
authored
Merge pull request #1560 from k8s-infra-cherrypick-robot/cherry-pick-1559-to-release-1.12
[release-1.12] Filter multiattach errors
2 parents d76c319 + 7d73389 commit 689eda4

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
@@ -335,6 +335,9 @@ func CodeForError(sourceError error) codes.Code {
335335
return codes.Internal
336336
}
337337

338+
if code, err := isUserMultiAttachError(sourceError); err == nil {
339+
return code
340+
}
338341
if code, err := existingErrorCode(sourceError); err == nil {
339342
return code
340343
}
@@ -372,6 +375,17 @@ func isContextError(err error) (codes.Code, error) {
372375
return codes.Unknown, fmt.Errorf("Not a context error: %w", err)
373376
}
374377

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

pkg/common/utils_test.go

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

10201025
for _, tc := range testCases {
@@ -1076,6 +1081,34 @@ func TestIsContextError(t *testing.T) {
10761081
}
10771082
}
10781083

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

0 commit comments

Comments
 (0)