From 940f2f62ee1a5ee85ae914b48ff90fd7157ff16d Mon Sep 17 00:00:00 2001 From: Daneyon Hansen Date: Tue, 7 Jan 2020 13:14:28 -0800 Subject: [PATCH] Refactors gateway api type to use label selectors --- api/v1alpha1/gateway_types.go | 48 ++++- api/v1alpha1/zz_generated.deepcopy.go | 41 +++- .../bases/networking.x.k8s.io_gateways.yaml | 177 +++++++++++++++--- 3 files changed, 231 insertions(+), 35 deletions(-) diff --git a/api/v1alpha1/gateway_types.go b/api/v1alpha1/gateway_types.go index e29d2d2e87..6bed1780cd 100644 --- a/api/v1alpha1/gateway_types.go +++ b/api/v1alpha1/gateway_types.go @@ -56,9 +56,15 @@ type GatewaySpec struct { // Listeners associated with this Gateway. Listeners define what addresses, // ports, protocols are bound on this Gateway. Listeners []Listener `json:"listeners"` - // Routes associated with this Gateway. Routes define - // protocol-specific routing to backends (e.g. Services). - Routes []core.TypedLocalObjectReference `json:"routes"` + // RouteSelector specifies routes to associate with the Gateway. Routes define + // protocol-specific routing to back-ends (e.g. Services). + // + // If undefined, the Gateway will be associated to all routes. + // + // Support: Core + // + // +optional + RouteSelector *RouteSelector `json:"routeSelector,omitempty"` } const ( @@ -180,7 +186,41 @@ type ListenerTLS struct { Options map[string]string `json:"options"` } -// GatewayStatus defines the observed state of Gateway. +// RouteSelector provides a mechanism to associate routes with the Gateway. +type RouteSelector struct { + // RouteRefs specifies what routes to associate with the Gateway. If defined, + // NamespaceSelector and LabelSelector are ignored and only the specified + // routes are associated with the Gateway. + // + // If undefined, all routes or routes matching NamespaceSelector and/or + // LabelSelector are associated with the Gateway. + // + // +optional + RouteRefs []core.ObjectReference `json:"routeRefs,omitempty"` + // NamespaceSelector specifies a set of namespace labels used for selecting + // routes to associate with the Gateway. + // + // If undefined, routes from all namespaces will be associated to the Gateway. + // If undefined and LabelSelector is defined, routes containing LabelSelector + // labels across all namespaces are associated to the Gateway. If NamespaceSelector + // and LabelSelector are defined, only routes matching the LabelSelector labels + // from NamespaceSelector namespaces are associated to the Gateway. + // + // +optional + NamespaceSelector *metav1.LabelSelector `json:"namespaceSelector,omitempty"` + // LabelSelector specifies route labels for associating routes to the Gateway. + // + // If undefined, the default is all routes are associated to the Gateway. If undefined + // and NamespaceSelector is defined, all routes within the NamespaceSelector namespaces + // are associated to the Gateway. If LabelSelector and NamespaceSelector are defined, + // only routes matching the LabelSelector labels from NamespaceSelector namespaces are + // associated to the Gateway. + // + // +optional + LabelSelector *metav1.LabelSelector `json:"labelSelector,omitempty"` +} + +// GatewayStatus defines the observed state of Gateway type GatewayStatus struct { // Conditions describe the current conditions of the Gateway. Conditions []GatewayCondition `json:"conditions"` diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 6f427f6152..8a4049e63f 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -21,6 +21,7 @@ package v1alpha1 import ( "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" ) @@ -239,12 +240,10 @@ func (in *GatewaySpec) DeepCopyInto(out *GatewaySpec) { (*in)[i].DeepCopyInto(&(*out)[i]) } } - if in.Routes != nil { - in, out := &in.Routes, &out.Routes - *out = make([]v1.TypedLocalObjectReference, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } + if in.RouteSelector != nil { + in, out := &in.RouteSelector, &out.RouteSelector + *out = new(RouteSelector) + (*in).DeepCopyInto(*out) } } @@ -701,6 +700,36 @@ func (in *ListenerTLS) DeepCopy() *ListenerTLS { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *RouteSelector) DeepCopyInto(out *RouteSelector) { + *out = *in + if in.RouteRefs != nil { + in, out := &in.RouteRefs, &out.RouteRefs + *out = make([]v1.ObjectReference, len(*in)) + copy(*out, *in) + } + if in.NamespaceSelector != nil { + in, out := &in.NamespaceSelector, &out.NamespaceSelector + *out = new(metav1.LabelSelector) + (*in).DeepCopyInto(*out) + } + if in.LabelSelector != nil { + in, out := &in.LabelSelector, &out.LabelSelector + *out = new(metav1.LabelSelector) + (*in).DeepCopyInto(*out) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RouteSelector. +func (in *RouteSelector) DeepCopy() *RouteSelector { + if in == nil { + return nil + } + out := new(RouteSelector) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *TcpRoute) DeepCopyInto(out *TcpRoute) { *out = *in diff --git a/config/crd/bases/networking.x.k8s.io_gateways.yaml b/config/crd/bases/networking.x.k8s.io_gateways.yaml index fde1c2d62e..67ca064037 100644 --- a/config/crd/bases/networking.x.k8s.io_gateways.yaml +++ b/config/crd/bases/networking.x.k8s.io_gateways.yaml @@ -153,34 +153,161 @@ spec: type: object type: object type: array - routes: - description: Routes associated with this Gateway. Routes define protocol-specific - routing to backends (e.g. Services). - items: - description: TypedLocalObjectReference contains enough information - to let you locate the typed referenced object inside the same namespace. - properties: - apiGroup: - description: APIGroup is the group for the resource being referenced. - If APIGroup is not specified, the specified Kind must be in - the core API group. For any other third-party types, APIGroup - is required. - type: string - kind: - description: Kind is the type of resource being referenced - type: string - name: - description: Name is the name of resource being referenced - type: string - required: - - kind - - name - type: object - type: array + routeSelector: + description: "RouteSelector specifies routes to associate with this + Gateway. Routes define protocol-specific routing to back-ends (e.g. + Services). \n If undefined, this Gateway will be associated to all + routes." + properties: + labelSelector: + description: "LabelSelector specifies route labels for associating + routes to this Gateway. \n If undefined, the default is all routes + are associated to this Gateway. If undefined and NamespaceSelector + is defined, all routes within the NamespaceSelector namespaces + are associated to this Gateway. If LabelSelector and NamespaceSelector + are defined, only routes matching the LabelSelector labels from + NamespaceSelector namespaces are associated to this Gateway." + properties: + matchExpressions: + description: matchExpressions is a list of label selector requirements. + The requirements are ANDed. + items: + description: A label selector requirement is a selector that + contains values, a key, and an operator that relates the + key and values. + properties: + key: + description: key is the label key that the selector applies + to. + type: string + operator: + description: operator represents a key's relationship + to a set of values. Valid operators are In, NotIn, Exists + and DoesNotExist. + type: string + values: + description: values is an array of string values. If the + operator is In or NotIn, the values array must be non-empty. + If the operator is Exists or DoesNotExist, the values + array must be empty. This array is replaced during a + strategic merge patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} pairs. A single + {key,value} in the matchLabels map is equivalent to an element + of matchExpressions, whose key field is "key", the operator + is "In", and the values array contains only "value". The requirements + are ANDed. + type: object + type: object + namespaceSelector: + description: "NamespaceSelector specifies a set of namespace labels + used for selecting routes to associate with this Gateway. \n If + undefined, routes from all namespaces will be associated to this + Gateway. If undefined and LabelSelector is defined, routes containing + LabelSelector labels across all namespaces are associated to this + Gateway. If NamespaceSelector and LabelSelector are defined, only + routes matching the LabelSelector labels from NamespaceSelector + namespaces are associated to this Gateway." + properties: + matchExpressions: + description: matchExpressions is a list of label selector requirements. + The requirements are ANDed. + items: + description: A label selector requirement is a selector that + contains values, a key, and an operator that relates the + key and values. + properties: + key: + description: key is the label key that the selector applies + to. + type: string + operator: + description: operator represents a key's relationship + to a set of values. Valid operators are In, NotIn, Exists + and DoesNotExist. + type: string + values: + description: values is an array of string values. If the + operator is In or NotIn, the values array must be non-empty. + If the operator is Exists or DoesNotExist, the values + array must be empty. This array is replaced during a + strategic merge patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} pairs. A single + {key,value} in the matchLabels map is equivalent to an element + of matchExpressions, whose key field is "key", the operator + is "In", and the values array contains only "value". The requirements + are ANDed. + type: object + type: object + routes: + description: "Routes specifies what routes to associate with this + Gateway. If defined, NamespaceSelector and LabelSelector are ignored + and only the specified routes are associated with this Gateway. + \n If undefined, all routes or routes matching NamespaceSelector + and/or LabelSelector are associated with this Gateway." + items: + description: ObjectReference contains enough information to let + you inspect or modify the referred object. + properties: + apiVersion: + description: API version of the referent. + type: string + fieldPath: + description: 'If referring to a piece of an object instead + of an entire object, this string should contain a valid + JSON/Go field access statement, such as desiredState.manifest.containers[2]. + For example, if the object reference is to a container within + a pod, this would take on a value like: "spec.containers{name}" + (where "name" refers to the name of the container that triggered + the event) or if no container name is specified "spec.containers[2]" + (container with index 2 in this pod). This syntax is chosen + only to have some well-defined way of referencing a part + of an object. TODO: this design is not final and this field + is subject to change in the future.' + type: string + kind: + description: 'Kind of the referent. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' + type: string + name: + description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names' + type: string + namespace: + description: 'Namespace of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/' + type: string + resourceVersion: + description: 'Specific resourceVersion to which this reference + is made, if any. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#concurrency-control-and-consistency' + type: string + uid: + description: 'UID of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids' + type: string + type: object + type: array + type: object required: - class - listeners - - routes type: object status: description: GatewayStatus defines the observed state of Gateway