Skip to content

Commit

Permalink
Add validation for Kubernetes object name fields and ProtocolType
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Young <ynick@vmware.com>
  • Loading branch information
Nick Young committed Sep 17, 2021
1 parent c5c4e07 commit c5b7b60
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 26 deletions.
9 changes: 5 additions & 4 deletions apis/v1alpha2/gateway_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ type GatewayList struct {
type GatewaySpec struct {
// GatewayClassName used for this Gateway. This is the name of a
// GatewayClass resource.
//
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=253
GatewayClassName string `json:"gatewayClassName"`
GatewayClassName ObjectName `json:"gatewayClassName"`

// Listeners associated with this Gateway. Listeners define
// logical endpoints that are bound on this Gateway's addresses.
Expand Down Expand Up @@ -247,6 +244,10 @@ type Listener struct {
// `mycompany.com/my-custom-protocol`. Un-prefixed names are reserved for core
// protocols. Any protocol defined by implementations will fall under custom
// conformance.
//
// +kubebuilder:validation:MinLength:=1
// +kubebuilder:validation:MinLength:=255
// +kubebuilder:validation:Pattern:=`^[a-zA-Z0-9]([A-Z-a-z0-9\/]*[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Z-a-z0-9\/]*[A-Za-z0-9])?)*$`
type ProtocolType string

const (
Expand Down
15 changes: 3 additions & 12 deletions apis/v1alpha2/object_reference_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ type LocalObjectReference struct {
Kind Kind `json:"kind"`

// Name is the name of the referent.
//
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=253
Name string `json:"name"`
Name ObjectName `json:"name"`
}

// SecretObjectReference identifies an API object including its namespace, defaulting to Secret.
Expand All @@ -49,10 +46,7 @@ type SecretObjectReference struct {
Kind *Kind `json:"kind"`

// Name is the name of the referent.
//
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=253
Name string `json:"name"`
Name ObjectName `json:"name"`

// Namespace is the namespace of the backend. When unspecified, the local
// namespace is inferred.
Expand Down Expand Up @@ -91,10 +85,7 @@ type BackendObjectReference struct {
Kind *Kind `json:"kind,omitempty"`

// Name is the name of the referent.
//
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=253
Name string `json:"name"`
Name ObjectName `json:"name"`

// Namespace is the namespace of the backend. When unspecified, the local
// namespace is inferred.
Expand Down
5 changes: 1 addition & 4 deletions apis/v1alpha2/policy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ type PolicyTargetReference struct {
Kind Kind `json:"kind"`

// Name is the name of the target resource.
//
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=253
Name string `json:"name"`
Name ObjectName `json:"name"`

// Namespace is the namespace of the referent. When unspecified, the local
// namespace is inferred. Even when policy targets a resource in a different
Expand Down
16 changes: 12 additions & 4 deletions apis/v1alpha2/shared_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ type ParentRef struct {
// Name is the name of the referent.
//
// Support: Core
//
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=253
Name string `json:"name"`
Name ObjectName `json:"name"`

// SectionName is the name of a section within the target resource. In the
// following resources, SectionName is interpreted as the following:
Expand Down Expand Up @@ -298,6 +295,17 @@ type Group string
// +kubebuilder:validation:Pattern=`^[a-zA-Z]([-a-zA-Z0-9]*[a-zA-Z0-9])?$`
type Kind string

// ObjectName refers to a Kubernetes object name.
// Object names can have a variety of forms, including RFC1123 subdomains,
// RFC 1123 labels, or RFC 1035 labels.
// Therefore the validation for this type is set to the least restritive out of
// those options, the RFC1123 subdomain.
//
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=253
// +kubebuilder:validation:Pattern=`^(\*\.)?[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`
type ObjectName string

// Namespace refers to a Kubernetes namespace. It must be a RFC 1123 label.
//
// This validation is based off of the corresponding Kubernetes validation:
Expand Down
4 changes: 2 additions & 2 deletions apis/v1alpha2/validation/httproute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
)

func TestValidateHTTPRoute(t *testing.T) {
testService := "test-service"
specialService := "special-service"
testService := gatewayv1a2.ObjectName("test-service")
specialService := gatewayv1a2.ObjectName("special-service")
tests := []struct {
name string
hRoute gatewayv1a2.HTTPRoute
Expand Down
4 changes: 4 additions & 0 deletions config/crd/v1alpha2/gateway.networking.k8s.io_gateways.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions config/crd/v1alpha2/gateway.networking.k8s.io_httproutes.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/crd/v1alpha2/gateway.networking.k8s.io_tcproutes.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/crd/v1alpha2/gateway.networking.k8s.io_tlsroutes.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/crd/v1alpha2/gateway.networking.k8s.io_udproutes.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c5b7b60

Please sign in to comment.