Skip to content

Commit 25095b3

Browse files
authored
Merge pull request #2181 from k8s-infra-cherrypick-robot/cherry-pick-2118-to-release-1.21
[release-1.21] Added filtering for incorrect slo boosting logs
2 parents e6c9004 + 7ed2e9e commit 25095b3

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"net/http"
2222
"net/url"
2323
"os"
24+
"regexp"
2425
"runtime"
2526
"sync"
2627
"time"
@@ -77,6 +78,12 @@ const (
7778
gcpTagsRequestTokenBucketSize = 8
7879
)
7980

81+
var (
82+
ssAlreadyExistsRegex = regexp.MustCompile("The resource [^']+ already exists")
83+
gcpErrorCodeRegex = regexp.MustCompile(`Error (\d+)`)
84+
orgPolicyConstraintErrorCodes = []string{"400", "412"}
85+
)
86+
8087
// ResourceType indicates the type of a compute resource.
8188
type ResourceType string
8289

@@ -441,3 +448,21 @@ func IsGCENotFoundError(err error) bool {
441448
func IsGCEInvalidError(err error) bool {
442449
return IsGCEError(err, "invalid")
443450
}
451+
452+
// IsSnapshotAlreadyExistsError checks if the error is a snapshot already exists error.
453+
func IsSnapshotAlreadyExistsError(err error) bool {
454+
return ssAlreadyExistsRegex.MatchString(err.Error())
455+
}
456+
457+
// IsGCPOrgViolationError checks if the error is a GCP organization policy violation error.
458+
func IsGCPOrgViolationError(err error) bool {
459+
matches := gcpErrorCodeRegex.FindStringSubmatch(err.Error())
460+
if len(matches) > 1 {
461+
errorCode := matches[1]
462+
if slices.Contains(orgPolicyConstraintErrorCodes, errorCode) {
463+
return true
464+
}
465+
}
466+
467+
return false
468+
}

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,70 @@ func TestIsGCEError(t *testing.T) {
101101
}
102102
}
103103

104+
func TestErrorIsGCPViolationRegex(t *testing.T) {
105+
testCases := []struct {
106+
name string
107+
inputErr error
108+
expectedResult bool
109+
}{
110+
{
111+
name: "is gcp org violation error, error code 400",
112+
inputErr: errors.New("Failed to scale up: googleapi: Error 400: 'us-central1' violates constraint '`constraints/gcp.resourceLocations`' on the resource 'projects/test-project/locations/us-central1/clusters/test-cluster/nodePools/test-node-pool'"),
113+
expectedResult: true,
114+
},
115+
{
116+
name: "is gcp org violation error, error code 412",
117+
inputErr: errors.New("createSnapshot for content [snapcontent-xyz]: error occurred in createSnapshotWrapper: failed to take snapshot of the volume projects/test-project/regions/europe-west3/disks/pvc-test: \"rpc error: code = Internal desc = Failed to create snapshot: googleapi: Error 412: Location EU violates constraint constraints/gcp.resourceLocations on the resource projects/test-project/global/snapshots/snapshot-xyz., conditionNotMet\""),
118+
expectedResult: true,
119+
},
120+
{
121+
name: "is not gcp org violation, error doesn't match",
122+
inputErr: errors.New("createSnapshot for content [snapcontent-xyz]: error occurred in createSnapshotWrapper: failed to take snapshot of the volume projects/test-project/regions/europe-west3/disks/pvc-test: \"rpc error: code = Internal desc = Failed to create snapshot: googleapi: Error 500: Location EU violates constraint constraints/gcp.resourceLocations on the resource projects/test-project/global/snapshots/snapshot-xyz., conditionNotMet\""),
123+
expectedResult: false,
124+
},
125+
{
126+
name: "is not gcp org violation error",
127+
inputErr: errors.New("Some incorrect error message"),
128+
expectedResult: false,
129+
},
130+
}
131+
132+
for _, tc := range testCases {
133+
t.Logf("Running test: %v", tc.name)
134+
result := IsGCPOrgViolationError(tc.inputErr)
135+
if tc.expectedResult != result {
136+
t.Fatalf("Got '%t', expected '%t'", result, tc.expectedResult)
137+
}
138+
}
139+
}
140+
141+
func TestErrorIsSnapshotExistsError(t *testing.T) {
142+
testCases := []struct {
143+
name string
144+
inputErr error
145+
expectedResult bool
146+
}{
147+
{
148+
name: "is snapshot already exists error",
149+
inputErr: errors.New("The resource projects/test-project/global/snapshots/snapshot-xyz already exists, alreadyExists"),
150+
expectedResult: true,
151+
},
152+
{
153+
name: "is not snapshot already exists error",
154+
inputErr: errors.New("Some incorrect error message"),
155+
expectedResult: false,
156+
},
157+
}
158+
159+
for _, tc := range testCases {
160+
t.Logf("Running test: %v", tc.name)
161+
result := IsSnapshotAlreadyExistsError(tc.inputErr)
162+
if tc.expectedResult != result {
163+
t.Fatalf("Got '%t', expected '%t'", result, tc.expectedResult)
164+
}
165+
}
166+
}
167+
104168
func TestGetComputeVersion(t *testing.T) {
105169
testCases := []struct {
106170
name string

pkg/gce-pd-csi-driver/controller.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,16 @@ func (gceCS *GCEControllerServer) createPDSnapshot(ctx context.Context, project
16681668
if gce.IsGCEError(err, "notFound") {
16691669
return nil, status.Errorf(codes.NotFound, "Could not find volume with ID %v: %v", volKey.String(), err.Error())
16701670
}
1671+
1672+
// Identified as incorrect error handling
1673+
if gce.IsSnapshotAlreadyExistsError(err) {
1674+
return nil, status.Errorf(codes.AlreadyExists, "Snapshot already exists: %v", err.Error())
1675+
}
1676+
1677+
// Identified as incorrect error handling
1678+
if gce.IsGCPOrgViolationError(err) {
1679+
return nil, status.Errorf(codes.FailedPrecondition, "Violates GCP org policy: %v", err.Error())
1680+
}
16711681
return nil, common.LoggedError("Failed to create snapshot: ", err)
16721682
}
16731683
}

0 commit comments

Comments
 (0)