Skip to content

Commit

Permalink
feat: use KongObjectRef in KongRoute (#142)
Browse files Browse the repository at this point in the history
Co-authored-by: Tao Yi <[email protected]>
  • Loading branch information
pmalek and randmonkey authored Nov 7, 2024
1 parent 05d5f58 commit 73ff963
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 67 deletions.
2 changes: 0 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ linters-settings:
disabled: false
arguments:
- "checkPrivateReceivers"
# TODO: enable this when ready to refactor exported types that stutter at call site.
- "disableStutteringCheck"
- name: context-as-argument
# TODO: re-add this rule after https://github.com/golangci/golangci-lint/issues/3280
# is resolved and released.
Expand Down
8 changes: 5 additions & 3 deletions api/configuration/v1alpha1/object_ref.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package v1alpha1

// TODO: https://github.com/Kong/kubernetes-configuration/issues/96
// Change other types to use the generic `KongObjectRef` and move it to a common package
// to prevent possible import cycles.

// KongObjectRef is a reference to another object representing a Kong entity with deterministic type.
//
// TODO: https://github.com/Kong/kubernetes-configuration/issues/96
// change other types to use the generic `KongObjectRef` and move it to a common package to prevent possible import cycles.
// +apireference:kgo:include
type KongObjectRef struct {
// Name is the name of the entity.
//
// NOTE: the `Required` validation rule does not reject empty strings so we use `MinLength` to reject empty string here.
// +kubebuilder:validation:MinLength=1
Name string `json:"name"`

// TODO: handle cross namespace references.
}
17 changes: 4 additions & 13 deletions api/configuration/v1alpha1/service_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,16 @@ const (
)

// ServiceRef is a reference to a KongService.
//
// +kubebuilder:validation:XValidation:rule="self.type == 'namespacedRef' ? has(self.namespacedRef) : true", message="when type is namespacedRef, namespacedRef must be set"
// +apireference:kgo:include
type ServiceRef struct {
// Type can be one of:
// - namespacedRef
//
// +kubebuilder:validation:Enum:=namespacedRef
Type string `json:"type,omitempty"`

// NamespacedRef is a reference to a KongService.
NamespacedRef *NamespacedServiceRef `json:"namespacedRef,omitempty"`
}

// NamespacedServiceRef is a namespaced reference to a KongService.
//
// NOTE: currently cross namespace references are not supported.
// +apireference:kgo:include
type NamespacedServiceRef struct {
// +kubebuilder:validation:Required
Name string `json:"name"`

// TODO: handle cross namespace references.
// https://github.com/Kong/kubernetes-configuration/issues/106
NamespacedRef *KongObjectRef `json:"namespacedRef,omitempty"`
}
17 changes: 1 addition & 16 deletions api/configuration/v1alpha1/zz_generated.deepcopy.go

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

4 changes: 4 additions & 0 deletions config/crd/bases/configuration.konghq.com_kongroutes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ spec:
description: NamespacedRef is a reference to a KongService.
properties:
name:
description: Name is the name of the entity.
minLength: 1
type: string
required:
- name
Expand All @@ -215,6 +217,8 @@ spec:
description: |-
Type can be one of:
- namespacedRef
enum:
- namespacedRef
type: string
type: object
x-kubernetes-validations:
Expand Down
5 changes: 1 addition & 4 deletions config/crd/bases/configuration.konghq.com_kongsnis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ spec:
which the KongSNI is attached.
properties:
name:
description: |-
Name is the name of the entity.
NOTE: the `Required` validation rule does not reject empty strings so we use `MinLength` to reject empty string here.
description: Name is the name of the entity.
minLength: 1
type: string
required:
Expand Down
25 changes: 4 additions & 21 deletions docs/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1228,19 +1228,18 @@ _Appears in:_
#### KongObjectRef


KongObjectRef is a reference to another object representing a Kong entity with deterministic type.<br /><br />
TODO: https://github.com/Kong/kubernetes-configuration/issues/96
change other types to use the generic `KongObjectRef` and move it to a common package to prevent possible import cycles.
KongObjectRef is a reference to another object representing a Kong entity with deterministic type.



| Field | Description |
| --- | --- |
| `name` _string_ | Name is the name of the entity.<br /><br /> NOTE: the `Required` validation rule does not reject empty strings so we use `MinLength` to reject empty string here. |
| `name` _string_ | Name is the name of the entity. |


_Appears in:_
- [KongSNISpec](#kongsnispec)
- [ServiceRef](#serviceref)

#### KongPluginBindingSpec

Expand Down Expand Up @@ -1596,22 +1595,6 @@ Namespace refers to a Kubernetes namespace. It must be a RFC 1123 label.
_Appears in:_
- [ControllerReference](#controllerreference)

#### NamespacedServiceRef


NamespacedServiceRef is a namespaced reference to a KongService.<br /><br />
NOTE: currently cross namespace references are not supported.



| Field | Description |
| --- | --- |
| `name` _string_ | |


_Appears in:_
- [ServiceRef](#serviceref)

#### ObjectName
_Underlying type:_ `string`

Expand Down Expand Up @@ -1687,7 +1670,7 @@ ServiceRef is a reference to a KongService.
| Field | Description |
| --- | --- |
| `type` _string_ | Type can be one of: - namespacedRef |
| `namespacedRef` _[NamespacedServiceRef](#namespacedserviceref)_ | NamespacedRef is a reference to a KongService. |
| `namespacedRef` _[KongObjectRef](#kongobjectref)_ | NamespacedRef is a reference to a KongService. |


_Appears in:_
Expand Down
49 changes: 41 additions & 8 deletions test/crdsvalidation/kongroute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestKongRoute(t *testing.T) {
},
ServiceRef: &configurationv1alpha1.ServiceRef{
Type: configurationv1alpha1.ServiceRefNamespacedRef,
NamespacedRef: &configurationv1alpha1.NamespacedServiceRef{
NamespacedRef: &configurationv1alpha1.KongObjectRef{
Name: "test-konnect-service",
},
},
Expand Down Expand Up @@ -226,7 +226,7 @@ func TestKongRoute(t *testing.T) {
Spec: configurationv1alpha1.KongRouteSpec{
ServiceRef: &configurationv1alpha1.ServiceRef{
Type: configurationv1alpha1.ServiceRefNamespacedRef,
NamespacedRef: &configurationv1alpha1.NamespacedServiceRef{Name: "svc"},
NamespacedRef: &configurationv1alpha1.KongObjectRef{Name: "svc"},
},
KongRouteAPISpec: configurationv1alpha1.KongRouteAPISpec{
Paths: []string{"/"},
Expand All @@ -241,7 +241,7 @@ func TestKongRoute(t *testing.T) {
Spec: configurationv1alpha1.KongRouteSpec{
ServiceRef: &configurationv1alpha1.ServiceRef{
Type: configurationv1alpha1.ServiceRefNamespacedRef,
NamespacedRef: &configurationv1alpha1.NamespacedServiceRef{Name: "svc"},
NamespacedRef: &configurationv1alpha1.KongObjectRef{Name: "svc"},
},
KongRouteAPISpec: configurationv1alpha1.KongRouteAPISpec{
Protocols: []sdkkonnectcomp.RouteProtocols{"http"},
Expand All @@ -257,7 +257,7 @@ func TestKongRoute(t *testing.T) {
Spec: configurationv1alpha1.KongRouteSpec{
ServiceRef: &configurationv1alpha1.ServiceRef{
Type: configurationv1alpha1.ServiceRefNamespacedRef,
NamespacedRef: &configurationv1alpha1.NamespacedServiceRef{Name: "svc"},
NamespacedRef: &configurationv1alpha1.KongObjectRef{Name: "svc"},
},
KongRouteAPISpec: configurationv1alpha1.KongRouteAPISpec{
Protocols: []sdkkonnectcomp.RouteProtocols{"http"},
Expand All @@ -278,7 +278,7 @@ func TestKongRoute(t *testing.T) {
Spec: configurationv1alpha1.KongRouteSpec{
ServiceRef: &configurationv1alpha1.ServiceRef{
Type: configurationv1alpha1.ServiceRefNamespacedRef,
NamespacedRef: &configurationv1alpha1.NamespacedServiceRef{
NamespacedRef: &configurationv1alpha1.KongObjectRef{
Name: "test-konnect-service",
},
},
Expand All @@ -288,6 +288,39 @@ func TestKongRoute(t *testing.T) {
},
},
},
{
Name: "NamespacedRef reference is invalid when empty name is provided",
TestObject: &configurationv1alpha1.KongRoute{
ObjectMeta: commonObjectMeta,
Spec: configurationv1alpha1.KongRouteSpec{
ServiceRef: &configurationv1alpha1.ServiceRef{
Type: configurationv1alpha1.ServiceRefNamespacedRef,
NamespacedRef: &configurationv1alpha1.KongObjectRef{
Name: "",
},
},
KongRouteAPISpec: configurationv1alpha1.KongRouteAPISpec{
Paths: []string{"/"},
},
},
},
ExpectedErrorMessage: lo.ToPtr("spec.serviceRef.namespacedRef.name in body should be at least 1 chars long"),
},
{
Name: "NamespacedRef reference is invalid when name is not provided",
TestObject: &configurationv1alpha1.KongRoute{
ObjectMeta: commonObjectMeta,
Spec: configurationv1alpha1.KongRouteSpec{
ServiceRef: &configurationv1alpha1.ServiceRef{
Type: configurationv1alpha1.ServiceRefNamespacedRef,
},
KongRouteAPISpec: configurationv1alpha1.KongRouteAPISpec{
Paths: []string{"/"},
},
},
},
ExpectedErrorMessage: lo.ToPtr("when type is namespacedRef, namespacedRef must be set"),
},
{
Name: "not providing namespacedRef when type is namespacedRef yields an error",
TestObject: &configurationv1alpha1.KongRoute{
Expand All @@ -310,7 +343,7 @@ func TestKongRoute(t *testing.T) {
Spec: configurationv1alpha1.KongRouteSpec{
ServiceRef: &configurationv1alpha1.ServiceRef{
Type: configurationv1alpha1.ServiceRefNamespacedRef,
NamespacedRef: &configurationv1alpha1.NamespacedServiceRef{
NamespacedRef: &configurationv1alpha1.KongObjectRef{
Name: "test-konnect-service",
},
},
Expand Down Expand Up @@ -341,7 +374,7 @@ func TestKongRoute(t *testing.T) {
Spec: configurationv1alpha1.KongRouteSpec{
ServiceRef: &configurationv1alpha1.ServiceRef{
Type: configurationv1alpha1.ServiceRefNamespacedRef,
NamespacedRef: &configurationv1alpha1.NamespacedServiceRef{
NamespacedRef: &configurationv1alpha1.KongObjectRef{
Name: "test-konnect-service",
},
},
Expand All @@ -363,7 +396,7 @@ func TestKongRoute(t *testing.T) {
Update: func(ks *configurationv1alpha1.KongRoute) {
ks.Spec.ServiceRef.Type = "otherRef"
},
ExpectedUpdateErrorMessage: lo.ToPtr("spec.serviceRef is immutable when an entity is already Programmed"),
ExpectedUpdateErrorMessage: lo.ToPtr("Unsupported value: \"otherRef\": supported values: \"namespacedRef\""),
},
}.Run(t)
})
Expand Down

0 comments on commit 73ff963

Please sign in to comment.