From 8e0251fb4b6d4473122cd6442e0a6a0474fe3f65 Mon Sep 17 00:00:00 2001 From: Mattia Lavacca Date: Mon, 29 Jul 2024 12:28:03 +0200 Subject: [PATCH] feat: kpb refs refactoring Signed-off-by: Mattia Lavacca --- .../v1alpha1/kongpluginbinding_types.go | 36 +++-- .../v1alpha1/zz_generated.deepcopy.go | 103 +++++++------- ...uration.konghq.com_kongpluginbindings.yaml | 130 ++++++++++-------- docs/api-reference.md | 70 +++++----- 4 files changed, 181 insertions(+), 158 deletions(-) diff --git a/api/configuration/v1alpha1/kongpluginbinding_types.go b/api/configuration/v1alpha1/kongpluginbinding_types.go index c08113c..202d796 100644 --- a/api/configuration/v1alpha1/kongpluginbinding_types.go +++ b/api/configuration/v1alpha1/kongpluginbinding_types.go @@ -87,18 +87,15 @@ type KongPluginBindingSpec struct { // KongClusterPlugin can be replaced by kongPluginBindings with no Kong references. This way we'd be // more coherent with the Konnect approach. // https://github.com/Kong/kubernetes-configuration/issues/7 - Kong *KongReferences `json:"kong,omitempty"` - // TODO(mlavacca): let's defer this one to the future as we are not sure about the shape we want to give it. - // https://github.com/Kong/kubernetes-configuration/issues/8 - // EntityReference *GenericEntityRef `json:"genericEntityRef,omitempty"` -} - -type KongReferences struct { - RouteReference *EntityRef `json:"routeRef,omitempty"` - ServiceReference *EntityRef `json:"serviceRef,omitempty"` - ConsumerReference *EntityRef `json:"consumerRef,omitempty"` - ConsumerGroupReference *EntityRef `json:"consumerGroupRef,omitempty"` + // TODO(mlavacca): RouteReference allows references to KongRoute, Ingress, HTTPRoute, or GCPRoute resource. + // TODO(mlavacca): ServiceReference allows Service or KongService resource. + // TODO(mlavacca): In case the routeRef references a KongRoute, the ServiceRef should be unset or set to a KongService. + // The same applies the other way around. + RouteReference *TargetRefWithGroupKind `json:"routeRef,omitempty"` + ServiceReference *TargetRefWithGroupKind `json:"serviceRef,omitempty"` + ConsumerReference *TargetRef `json:"consumerRef,omitempty"` + ConsumerGroupReference *TargetRef `json:"consumerGroupRef,omitempty"` } type PluginRef struct { @@ -115,12 +112,27 @@ type PluginRef struct { Kind *string `json:"kind,omitempty"` } -type EntityRef struct { +type TargetRef struct { // Name is the name of the entity. // +kubebuilder:validation:Required Name string `json:"name"` } +// TODO(mlavacca): add xvalidation between group and kind +type TargetRefWithGroupKind struct { + // Name is the name of the entity. + // +kubebuilder:validation:Required + Name string `json:"name"` + + // +kubebuilder:validation:Required + // +kubebuilder:validation:Enum=KongService;KongRoute;Service;HTTPRoute;GCPRoute;Ingress + Kind string `json:"kind"` + + // +kubebuilder:validation:Required + // +kubebuilder:validation:Enum="";core;gateway.networking.k8s.io;networking.k8s.io + Group string `json:"group"` +} + // type GenericEntityRef struct { // // Name is the name of the generic entity. // // +kubebuilder:validation:Required diff --git a/api/configuration/v1alpha1/zz_generated.deepcopy.go b/api/configuration/v1alpha1/zz_generated.deepcopy.go index e801017..57ef064 100644 --- a/api/configuration/v1alpha1/zz_generated.deepcopy.go +++ b/api/configuration/v1alpha1/zz_generated.deepcopy.go @@ -87,21 +87,6 @@ func (in *ControllerReference) DeepCopy() *ControllerReference { return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *EntityRef) DeepCopyInto(out *EntityRef) { - *out = *in -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new EntityRef. -func (in *EntityRef) DeepCopy() *EntityRef { - if in == nil { - return nil - } - out := new(EntityRef) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *IngressClassParameters) DeepCopyInto(out *IngressClassParameters) { *out = *in @@ -462,10 +447,25 @@ func (in *KongPluginBindingList) DeepCopyObject() runtime.Object { func (in *KongPluginBindingSpec) DeepCopyInto(out *KongPluginBindingSpec) { *out = *in in.PluginReference.DeepCopyInto(&out.PluginReference) - if in.Kong != nil { - in, out := &in.Kong, &out.Kong - *out = new(KongReferences) - (*in).DeepCopyInto(*out) + if in.RouteReference != nil { + in, out := &in.RouteReference, &out.RouteReference + *out = new(TargetRefWithGroupKind) + **out = **in + } + if in.ServiceReference != nil { + in, out := &in.ServiceReference, &out.ServiceReference + *out = new(TargetRefWithGroupKind) + **out = **in + } + if in.ConsumerReference != nil { + in, out := &in.ConsumerReference, &out.ConsumerReference + *out = new(TargetRef) + **out = **in + } + if in.ConsumerGroupReference != nil { + in, out := &in.ConsumerGroupReference, &out.ConsumerGroupReference + *out = new(TargetRef) + **out = **in } } @@ -506,41 +506,6 @@ func (in *KongPluginBindingStatus) DeepCopy() *KongPluginBindingStatus { return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *KongReferences) DeepCopyInto(out *KongReferences) { - *out = *in - if in.RouteReference != nil { - in, out := &in.RouteReference, &out.RouteReference - *out = new(EntityRef) - **out = **in - } - if in.ServiceReference != nil { - in, out := &in.ServiceReference, &out.ServiceReference - *out = new(EntityRef) - **out = **in - } - if in.ConsumerReference != nil { - in, out := &in.ConsumerReference, &out.ConsumerReference - *out = new(EntityRef) - **out = **in - } - if in.ConsumerGroupReference != nil { - in, out := &in.ConsumerGroupReference, &out.ConsumerGroupReference - *out = new(EntityRef) - **out = **in - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KongReferences. -func (in *KongReferences) DeepCopy() *KongReferences { - if in == nil { - return nil - } - out := new(KongReferences) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *KongRoute) DeepCopyInto(out *KongRoute) { *out = *in @@ -1132,3 +1097,33 @@ func (in *ServiceRef) DeepCopy() *ServiceRef { in.DeepCopyInto(out) return out } + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TargetRef) DeepCopyInto(out *TargetRef) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TargetRef. +func (in *TargetRef) DeepCopy() *TargetRef { + if in == nil { + return nil + } + out := new(TargetRef) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TargetRefWithGroupKind) DeepCopyInto(out *TargetRefWithGroupKind) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TargetRefWithGroupKind. +func (in *TargetRefWithGroupKind) DeepCopy() *TargetRefWithGroupKind { + if in == nil { + return nil + } + out := new(TargetRefWithGroupKind) + in.DeepCopyInto(out) + return out +} diff --git a/config/crd/bases/configuration.konghq.com_kongpluginbindings.yaml b/config/crd/bases/configuration.konghq.com_kongpluginbindings.yaml index 49c76bc..e71c80b 100644 --- a/config/crd/bases/configuration.konghq.com_kongpluginbindings.yaml +++ b/config/crd/bases/configuration.konghq.com_kongpluginbindings.yaml @@ -53,65 +53,21 @@ spec: spec: description: KongPluginBindingSpec defines specification of a KongPluginBinding. properties: - kong: - description: |- - Kong contains the Kong entity references. It is possible to set multiple combinations - of references, as described in https://docs.konghq.com/gateway/latest/key-concepts/plugins/#precedence - The complete set of allowed combinations and their order of precedence for plugins - configured to multiple entities is: - - - 1. Consumer + route + service - 2. Consumer group + service + route - 3. Consumer + route - 4. Consumer + service - 5. Consumer group + route - 6. Consumer group + service - 7. Route + service - 8. Consumer - 9. Consumer group - 10. Route - 11. Service - 12. Global - - - TODO(mlavacca): we need to figure out how to deal with global plugins. By means of this new API, - KongClusterPlugin can be replaced by kongPluginBindings with no Kong references. This way we'd be - more coherent with the Konnect approach. - https://github.com/Kong/kubernetes-configuration/issues/7 + consumerGroupRef: properties: - consumerGroupRef: - properties: - name: - description: Name is the name of the entity. - type: string - required: - - name - type: object - consumerRef: - properties: - name: - description: Name is the name of the entity. - type: string - required: - - name - type: object - routeRef: - properties: - name: - description: Name is the name of the entity. - type: string - required: - - name - type: object - serviceRef: - properties: - name: - description: Name is the name of the entity. - type: string - required: - - name - type: object + name: + description: Name is the name of the entity. + type: string + required: + - name + type: object + consumerRef: + properties: + name: + description: Name is the name of the entity. + type: string + required: + - name type: object pluginRef: description: PluginReference is a reference to the KongPlugin or KongClusterPlugin @@ -132,6 +88,64 @@ spec: required: - name type: object + routeRef: + description: |- + TODO(mlavacca): RouteReference allows references to KongRoute, Ingress, HTTPRoute, or GCPRoute resource. + TODO(mlavacca): ServiceReference allows Service or KongService resource. + TODO(mlavacca): In case the routeRef references a KongRoute, the ServiceRef should be unset or set to a KongService. + The same applies the other way around. + properties: + group: + enum: + - "" + - core + - gateway.networking.k8s.io + - networking.k8s.io + type: string + kind: + enum: + - KongService + - KongRoute + - Service + - HTTPRoute + - GCPRoute + - Ingress + type: string + name: + description: Name is the name of the entity. + type: string + required: + - group + - kind + - name + type: object + serviceRef: + description: 'TODO(mlavacca): add xvalidation between group and kind' + properties: + group: + enum: + - "" + - core + - gateway.networking.k8s.io + - networking.k8s.io + type: string + kind: + enum: + - KongService + - KongRoute + - Service + - HTTPRoute + - GCPRoute + - Ingress + type: string + name: + description: Name is the name of the entity. + type: string + required: + - group + - kind + - name + type: object required: - pluginRef type: object diff --git a/docs/api-reference.md b/docs/api-reference.md index 2472130..68c2ed0 100644 --- a/docs/api-reference.md +++ b/docs/api-reference.md @@ -492,21 +492,6 @@ _Appears in:_ _Appears in:_ - [KongLicenseControllerStatus](#konglicensecontrollerstatus) -#### EntityRef - - - - - - -| Field | Description | -| --- | --- | -| `name` _string_ | Name is the name of the entity. | - - -_Appears in:_ -- [KongReferences](#kongreferences) - #### Group _Underlying type:_ `string` @@ -614,7 +599,10 @@ KongPluginBindingSpec defines specification of a KongPluginBinding. | Field | Description | | --- | --- | | `pluginRef` _[PluginRef](#pluginref)_ | PluginReference is a reference to the KongPlugin or KongClusterPlugin resource. It is required | -| `kong` _[KongReferences](#kongreferences)_ | Kong contains the Kong entity references. It is possible to set multiple combinations of references, as described in https://docs.konghq.com/gateway/latest/key-concepts/plugins/#precedence The complete set of allowed combinations and their order of precedence for plugins configured to multiple entities is:

1. Consumer + route + service 2. Consumer group + service + route 3. Consumer + route 4. Consumer + service 5. Consumer group + route 6. Consumer group + service 7. Route + service 8. Consumer 9. Consumer group 10. Route 11. Service 12. Global

TODO(mlavacca): we need to figure out how to deal with global plugins. By means of this new API, KongClusterPlugin can be replaced by kongPluginBindings with no Kong references. This way we'd be more coherent with the Konnect approach. https://github.com/Kong/kubernetes-configuration/issues/7 | +| `routeRef` _[TargetRefWithGroupKind](#targetrefwithgroupkind)_ | TODO(mlavacca): RouteReference allows references to KongRoute, Ingress, HTTPRoute, or GCPRoute resource. TODO(mlavacca): ServiceReference allows Service or KongService resource. TODO(mlavacca): In case the routeRef references a KongRoute, the ServiceRef should be unset or set to a KongService. The same applies the other way around. | +| `serviceRef` _[TargetRefWithGroupKind](#targetrefwithgroupkind)_ | | +| `consumerRef` _[TargetRef](#targetref)_ | | +| `consumerGroupRef` _[TargetRef](#targetref)_ | | _Appears in:_ @@ -622,24 +610,6 @@ _Appears in:_ -#### KongReferences - - - - - - -| Field | Description | -| --- | --- | -| `routeRef` _[EntityRef](#entityref)_ | | -| `serviceRef` _[EntityRef](#entityref)_ | | -| `consumerRef` _[EntityRef](#entityref)_ | | -| `consumerGroupRef` _[EntityRef](#entityref)_ | | - - -_Appears in:_ -- [KongPluginBindingSpec](#kongpluginbindingspec) - #### KongRouteAPISpec @@ -897,6 +867,38 @@ _Appears in:_ _Appears in:_ - [KongRouteSpec](#kongroutespec) +#### TargetRef + + + + + + +| Field | Description | +| --- | --- | +| `name` _string_ | Name is the name of the entity. | + + +_Appears in:_ +- [KongPluginBindingSpec](#kongpluginbindingspec) + +#### TargetRefWithGroupKind + + +TODO(mlavacca): add xvalidation between group and kind + + + +| Field | Description | +| --- | --- | +| `name` _string_ | Name is the name of the entity. | +| `kind` _string_ | | +| `group` _string_ | | + + +_Appears in:_ +- [KongPluginBindingSpec](#kongpluginbindingspec) + ## configuration.konghq.com/v1beta1