Skip to content

Commit 83d9931

Browse files
authored
Merge pull request #3068 from 08volt/user-error-constraint
fix: categorize constraint violation error as UserError
2 parents 2267399 + 04bae66 commit 83d9931

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

pkg/l4/resources/user_error.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func IsUserError(err error) bool {
1919
utils.IsInvalidLoadBalancerSourceRangesSpecError(err) ||
2020
utils.IsInvalidLoadBalancerSourceRangesAnnotationError(err) ||
2121
utils.IsUnsupportedNetworkTierError(err) ||
22+
utils.IsConstraintViolationError(err) ||
2223
errors.As(err, &firewallErr) ||
2324
errors.As(err, &userErr)
2425
}

pkg/l4/resources/user_error_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package resources_test
22

33
import (
44
"fmt"
5+
"net/http"
56
"testing"
67

7-
"k8s.io/ingress-gce/pkg/l4/resources"
8-
8+
"google.golang.org/api/googleapi"
99
"k8s.io/ingress-gce/pkg/firewalls"
10+
"k8s.io/ingress-gce/pkg/l4/resources"
1011
"k8s.io/ingress-gce/pkg/utils"
1112
)
1213

@@ -55,6 +56,13 @@ func TestIsUserError(t *testing.T) {
5556
err: utils.NewConflictingPortsConfigurationError("8080-8090", "conflicting ports"),
5657
want: true,
5758
},
59+
{
60+
err: &googleapi.Error{
61+
Code: http.StatusPreconditionFailed,
62+
Message: "Constraint constraints/compute.restrictLoadBalancerCreationForTypes violated for projects/...",
63+
},
64+
want: true,
65+
},
5866
}
5967
for _, tC := range testCases {
6068
tC := tC

pkg/utils/utils.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@ func IsUnsupportedNetworkTierError(err error) bool {
268268
return errors.As(err, &netTierError)
269269
}
270270

271+
// IsConstraintViolationError checks if the error is a constraint violation error returned by GCP.
272+
func IsConstraintViolationError(err error) bool {
273+
return IsHTTPErrorCode(err, http.StatusPreconditionFailed) && strings.Contains(err.Error(), "Constraint") && strings.Contains(err.Error(), "violated")
274+
}
275+
271276
// IsIPConfigurationError checks if wrapped error is an IP configuration error.
272277
func IsIPConfigurationError(err error) bool {
273278
var ipConfigError *IPConfigurationError

pkg/utils/utils_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,3 +1867,53 @@ func TestLBBasedOnFinalizer(t *testing.T) {
18671867
})
18681868
}
18691869
}
1870+
1871+
func TestIsConstraintViolationError(t *testing.T) {
1872+
testCases := []struct {
1873+
desc string
1874+
err error
1875+
want bool
1876+
}{
1877+
{
1878+
desc: "nil error",
1879+
err: nil,
1880+
want: false,
1881+
},
1882+
{
1883+
desc: "random error",
1884+
err: errors.New("random error"),
1885+
want: false,
1886+
},
1887+
{
1888+
desc: "other google api error",
1889+
err: &googleapi.Error{
1890+
Code: http.StatusBadRequest,
1891+
Message: "invalid arguments",
1892+
},
1893+
want: false,
1894+
},
1895+
{
1896+
desc: "constraint violation google api error",
1897+
err: &googleapi.Error{
1898+
Code: http.StatusPreconditionFailed,
1899+
Message: "Constraint constraints/compute.restrictLoadBalancerCreationForTypes violated for projects/cf-gcpai-sales-buddy-l-t4. Forwarding Rule projects/cf-gcpai-sales-buddy-l-t4/regions/europe-west4/forwardingRules/k8s2-tcp-mv3ejptg-anthos-identity-servic-gke-oidc-envo-eswdpmuk of type INTERNAL_TCP_UDP is not allowed.",
1900+
},
1901+
want: true,
1902+
},
1903+
{
1904+
desc: "status 412 but not constraint violation",
1905+
err: &googleapi.Error{
1906+
Code: http.StatusPreconditionFailed,
1907+
Message: "precondition failed for some other reason",
1908+
},
1909+
want: false,
1910+
},
1911+
}
1912+
for _, tC := range testCases {
1913+
t.Run(tC.desc, func(t *testing.T) {
1914+
if got := utils.IsConstraintViolationError(tC.err); got != tC.want {
1915+
t.Errorf("IsConstraintViolationError(%v) = %v, want %v", tC.err, got, tC.want)
1916+
}
1917+
})
1918+
}
1919+
}

0 commit comments

Comments
 (0)