Skip to content

Commit 0fb50d2

Browse files
committed
Update org policy violation check
1 parent f015679 commit 0fb50d2

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ const (
7979
)
8080

8181
var (
82-
ssAlreadyExistsRegex = regexp.MustCompile("The resource [^']+ already exists")
83-
gcpViolationRegex = regexp.MustCompile("violates constraint constraints/gcp.")
82+
ssAlreadyExistsRegex = regexp.MustCompile("The resource [^']+ already exists")
83+
gcpErrorCodeRegex = regexp.MustCompile(`Error (\d+)`)
84+
orgPolicyConstraintErrorCodes = []string{"400", "412"}
8485
)
8586

8687
// ResourceType indicates the type of a compute resource.
@@ -455,5 +456,13 @@ func IsSnapshotAlreadyExistsError(err error) bool {
455456

456457
// IsGCPOrgViolationError checks if the error is a GCP organization policy violation error.
457458
func IsGCPOrgViolationError(err error) bool {
458-
return gcpViolationRegex.MatchString(err.Error())
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
459468
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,20 @@ func TestErrorIsGCPViolationRegex(t *testing.T) {
108108
expectedResult bool
109109
}{
110110
{
111-
name: "is gcp org violation error",
112-
inputErr: errors.New("Your api request violates constraint constraints/gcp.resourceLocations"),
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'"),
113113
expectedResult: true,
114114
},
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+
},
115125
{
116126
name: "is not gcp org violation error",
117127
inputErr: errors.New("Some incorrect error message"),
@@ -135,12 +145,12 @@ func TestErrorIsSnapshotExistsError(t *testing.T) {
135145
expectedResult bool
136146
}{
137147
{
138-
name: "is ss error",
139-
inputErr: errors.New("The resource projects/dcme-pre-opt-mdp-rmp-00/global/snapshots/snapshot-3c208602-d815-40ae-a61e-3259e3bd29ca already exists, alreadyExists"),
148+
name: "is snapshot already exists error",
149+
inputErr: errors.New("The resource projects/test-project/global/snapshots/snapshot-xyz already exists, alreadyExists"),
140150
expectedResult: true,
141151
},
142152
{
143-
name: "is not ss already exists error",
153+
name: "is not snapshot already exists error",
144154
inputErr: errors.New("Some incorrect error message"),
145155
expectedResult: false,
146156
},

0 commit comments

Comments
 (0)