Skip to content

Commit 323b56c

Browse files
authored
Merge pull request #1326 from judemars/automated-cherry-pick-of-#1232-upstream-release-1.7
Automated cherry pick of #1232: Use errors.As so we can detect wrapped errors, and check for
2 parents 3d1fd21 + a9020f8 commit 323b56c

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

pkg/gce-cloud-provider/compute/gce.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package gcecloudprovider
1616

1717
import (
1818
"context"
19+
"errors"
1920
"fmt"
2021
"net/http"
2122
"os"
@@ -240,8 +241,8 @@ func getProjectAndZone(config *ConfigFile) (string, string, error) {
240241
// isGCEError returns true if given error is a googleapi.Error with given
241242
// reason (e.g. "resourceInUseByAnotherResource")
242243
func IsGCEError(err error, reason string) bool {
243-
apiErr, ok := err.(*googleapi.Error)
244-
if !ok {
244+
var apiErr *googleapi.Error
245+
if !errors.As(err, &apiErr) {
245246
return false
246247
}
247248

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
package gcecloudprovider
19+
20+
import (
21+
"errors"
22+
"fmt"
23+
"net/http"
24+
"testing"
25+
26+
"google.golang.org/api/googleapi"
27+
)
28+
29+
func TestIsGCEError(t *testing.T) {
30+
testCases := []struct {
31+
name string
32+
inputErr error
33+
reason string
34+
expIsGCEError bool
35+
}{
36+
{
37+
name: "Not googleapi.Error",
38+
inputErr: errors.New("I am not a googleapi.Error"),
39+
reason: "notFound",
40+
expIsGCEError: false,
41+
},
42+
{
43+
name: "googleapi.Error not found error",
44+
inputErr: &googleapi.Error{
45+
Code: http.StatusNotFound,
46+
Errors: []googleapi.ErrorItem{
47+
{
48+
Reason: "notFound",
49+
},
50+
},
51+
Message: "Not found",
52+
},
53+
reason: "notFound",
54+
expIsGCEError: true,
55+
},
56+
{
57+
name: "wrapped googleapi.Error",
58+
inputErr: fmt.Errorf("encountered not found: %w", &googleapi.Error{
59+
Code: http.StatusNotFound,
60+
Errors: []googleapi.ErrorItem{
61+
{
62+
Reason: "notFound",
63+
},
64+
},
65+
Message: "Not found",
66+
},
67+
),
68+
reason: "notFound",
69+
expIsGCEError: true,
70+
},
71+
{
72+
name: "nil error",
73+
inputErr: nil,
74+
reason: "notFound",
75+
expIsGCEError: false,
76+
},
77+
}
78+
79+
for _, tc := range testCases {
80+
t.Logf("Running test: %v", tc.name)
81+
isGCEError := IsGCEError(tc.inputErr, tc.reason)
82+
if tc.expIsGCEError != isGCEError {
83+
t.Fatalf("Got isGCEError '%t', expected '%t'", isGCEError, tc.expIsGCEError)
84+
}
85+
}
86+
}

test/remote/instance.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,8 @@ func generateMetadataWithPublicKey(pubKeyFile string) (*compute.Metadata, error)
388388
// isGCEError returns true if given error is a googleapi.Error with given
389389
// reason (e.g. "resourceInUseByAnotherResource")
390390
func isGCEError(err error, reason string) bool {
391-
apiErr, ok := err.(*googleapi.Error)
392-
if !ok {
391+
var apiErr *googleapi.Error
392+
if !errors.As(err, &apiErr) {
393393
return false
394394
}
395395

0 commit comments

Comments
 (0)