diff --git a/apis/v1alpha2/util/validation/gatewayclass.go b/apis/v1alpha2/util/validation/gatewayclass.go index 4aa11c4cd7..3169e7578e 100644 --- a/apis/v1alpha2/util/validation/gatewayclass.go +++ b/apis/v1alpha2/util/validation/gatewayclass.go @@ -22,13 +22,13 @@ import ( gatewayv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2" ) -// IsControllerNameValid checks that the provided controllerName complaints with the expected +var controllerNameRegex = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*\/[A-Za-z0-9\/\-._~%!$&'()*+,;=:]+$`) + +// IsControllerNameValid checks that the provided controllerName complies with the expected // format. It must be a non-empty domain prefixed path. func IsControllerNameValid(controllerName gatewayv1a2.GatewayController) bool { if controllerName == "" { return false } - - re := regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*\/[A-Za-z0-9\/\-._~%!$&'()*+,;=:]+$`) - return re.Match([]byte(controllerName)) + return controllerNameRegex.Match([]byte(controllerName)) } diff --git a/apis/v1alpha2/util/validation/gatewayclass_test.go b/apis/v1alpha2/util/validation/gatewayclass_test.go index abf81389c3..10c097e0fe 100644 --- a/apis/v1alpha2/util/validation/gatewayclass_test.go +++ b/apis/v1alpha2/util/validation/gatewayclass_test.go @@ -35,10 +35,20 @@ func TestIsControllerNameValid(t *testing.T) { isvalid: false, }, { - name: "invalid controller name", + name: "invalid controller name 1", controllerName: "example.com", isvalid: false, }, + { + name: "invalid controller name 2", + controllerName: "example*com", + isvalid: false, + }, + { + name: "invalid controller name 3", + controllerName: "example/@bar", + isvalid: false, + }, { name: "valid controller name", controllerName: "example.com/bar", diff --git a/apis/v1beta1/util/validation/gatewayclass.go b/apis/v1beta1/util/validation/gatewayclass.go index 46a03780a5..5ebad3095e 100644 --- a/apis/v1beta1/util/validation/gatewayclass.go +++ b/apis/v1beta1/util/validation/gatewayclass.go @@ -22,12 +22,13 @@ import ( gatewayv1b1 "sigs.k8s.io/gateway-api/apis/v1beta1" ) -// IsControllerNameValid checks that the provided controllerName complaints with the expected +var controllerNameRegex = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*\/[A-Za-z0-9\/\-._~%!$&'()*+,;=:]+$`) + +// IsControllerNameValid checks that the provided controllerName complies with the expected // format. It must be a non-empty domain prefixed path. func IsControllerNameValid(controllerName gatewayv1b1.GatewayController) bool { if controllerName == "" { return false } - re := regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*\/[A-Za-z0-9\/\-._~%!$&'()*+,;=:]+$`) - return re.Match([]byte(controllerName)) + return controllerNameRegex.Match([]byte(controllerName)) } diff --git a/apis/v1beta1/util/validation/gatewayclass_test.go b/apis/v1beta1/util/validation/gatewayclass_test.go index 73a3549062..c8fe0f6433 100644 --- a/apis/v1beta1/util/validation/gatewayclass_test.go +++ b/apis/v1beta1/util/validation/gatewayclass_test.go @@ -35,10 +35,20 @@ func TestIsControllerNameValid(t *testing.T) { isvalid: false, }, { - name: "invalid controller name", + name: "invalid controller name 1", controllerName: "example.com", isvalid: false, }, + { + name: "invalid controller name 2", + controllerName: "example*com", + isvalid: false, + }, + { + name: "invalid controller name 3", + controllerName: "example/@bar", + isvalid: false, + }, { name: "valid controller name", controllerName: "example.com/bar",