Skip to content

Commit

Permalink
feat(konnect): add namespace in KonnectNamespacedRef (#82)
Browse files Browse the repository at this point in the history
* add namespace in KonnectNamespacedRef

* add valdation rules to disable konnectNamespacedRef.namespace in namespaced resources

* update comments
  • Loading branch information
randmonkey authored Sep 20, 2024
1 parent 21b39dc commit f5436b2
Show file tree
Hide file tree
Showing 20 changed files with 156 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/configuration/v1/kongconsumer_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
// +kubebuilder:printcolumn:name="Programmed",type=string,JSONPath=`.status.conditions[?(@.type=="Programmed")].status`
// +kubebuilder:validation:XValidation:rule="has(self.username) || has(self.custom_id)", message="Need to provide either username or custom_id"
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.spec.controlPlaneRef) || has(self.spec.controlPlaneRef)", message="controlPlaneRef is required once set"
// +kubebuilder:validation:XValidation:rule="!has(self.spec.controlPlaneRef.konnectNamespacedRef) ? true : !has(self.spec.controlPlaneRef.konnectNamespacedRef.__namespace__)", message="spec.controlPlaneRef cannot specify namespace for namespaced resource"
// +kubebuilder:validation:XValidation:rule="(!has(self.status) || !self.status.conditions.exists(c, c.type == 'Programmed' && c.status == 'True')) ? true : oldSelf.spec.controlPlaneRef == self.spec.controlPlaneRef", message="spec.controlPlaneRef is immutable when an entity is already Programmed"

// KongConsumer is the Schema for the kongconsumers API.
Expand Down
1 change: 1 addition & 0 deletions api/configuration/v1alpha1/kong_ca_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Programmed",description="The Resource is Programmed on Konnect",type=string,JSONPath=`.status.conditions[?(@.type=='Programmed')].status`
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.spec.controlPlaneRef) || has(self.spec.controlPlaneRef)", message="controlPlaneRef is required once set"
// +kubebuilder:validation:XValidation:rule="!has(self.spec.controlPlaneRef.konnectNamespacedRef) ? true : !has(self.spec.controlPlaneRef.konnectNamespacedRef.__namespace__)", message="spec.controlPlaneRef cannot specify namespace for namespaced resource"
// +kubebuilder:validation:XValidation:rule="(!self.status.conditions.exists(c, c.type == 'Programmed' && c.status == 'True')) ? true : oldSelf.spec.controlPlaneRef == self.spec.controlPlaneRef", message="spec.controlPlaneRef is immutable when an entity is already Programmed"
type KongCACertificate struct {
metav1.TypeMeta `json:",inline"`
Expand Down
1 change: 1 addition & 0 deletions api/configuration/v1alpha1/kongservice_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
// +kubebuilder:printcolumn:name="Protocol",type=string,JSONPath=`.spec.procol`,description="Protocol of the service"
// +kubebuilder:printcolumn:name="Programmed",description="The Resource is Programmed on Konnect",type=string,JSONPath=`.status.conditions[?(@.type=='Programmed')].status`
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.spec.controlPlaneRef) || has(self.spec.controlPlaneRef)", message="controlPlaneRef is required once set"
// +kubebuilder:validation:XValidation:rule="!has(self.spec.controlPlaneRef.konnectNamespacedRef) ? true : !has(self.spec.controlPlaneRef.konnectNamespacedRef.__namespace__)", message="spec.controlPlaneRef cannot specify namespace for namespaced resource"
// +kubebuilder:validation:XValidation:rule="(!self.status.conditions.exists(c, c.type == 'Programmed' && c.status == 'True')) ? true : oldSelf.spec.controlPlaneRef == self.spec.controlPlaneRef", message="spec.controlPlaneRef is immutable when an entity is already Programmed"
type KongService struct {
metav1.TypeMeta `json:",inline"`
Expand Down
1 change: 1 addition & 0 deletions api/configuration/v1alpha1/kongupstream_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Programmed",description="The Resource is Programmed on Konnect",type=string,JSONPath=`.status.conditions[?(@.type=='Programmed')].status`
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.spec.controlPlaneRef) || has(self.spec.controlPlaneRef)", message="controlPlaneRef is required once set"
// +kubebuilder:validation:XValidation:rule="!has(self.spec.controlPlaneRef.konnectNamespacedRef) ? true : !has(self.spec.controlPlaneRef.konnectNamespacedRef.__namespace__)", message="spec.controlPlaneRef cannot specify namespace for namespaced resource"
// +kubebuilder:validation:XValidation:rule="(!self.status.conditions.exists(c, c.type == 'Programmed' && c.status == 'True')) ? true : oldSelf.spec.controlPlaneRef == self.spec.controlPlaneRef", message="spec.controlPlaneRef is immutable when an entity is already Programmed"
type KongUpstream struct {
metav1.TypeMeta `json:",inline"`
Expand Down
6 changes: 6 additions & 0 deletions api/configuration/v1alpha1/konnect_controlplaneref_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ type KonnectNamespacedRef struct {

// TODO: Implement cross namespace references:
// https://github.com/Kong/kubernetes-configuration/issues/36

// Namespace is the namespace where the Konnect Control Plane is in.
// Currently only cluster scoped resources (KongVault) are allowed to set `konnectNamespacedRef.namespace`.
//
// +optional
Namespace string `json:"namespace,omitempty"`
}
1 change: 1 addition & 0 deletions api/configuration/v1beta1/kongconsumergroup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
// +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp`,description="Age"
// +kubebuilder:printcolumn:name="Programmed",type=string,JSONPath=`.status.conditions[?(@.type=="Programmed")].status`
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.spec.controlPlaneRef) || has(self.spec.controlPlaneRef)", message="controlPlaneRef is required once set"
// +kubebuilder:validation:XValidation:rule="!has(self.spec.controlPlaneRef.konnectNamespacedRef) ? true : !has(self.spec.controlPlaneRef.konnectNamespacedRef.__namespace__)", message="spec.controlPlaneRef cannot specify namespace for namespaced resource"
// +kubebuilder:validation:XValidation:rule="(!has(self.status) || !self.status.conditions.exists(c, c.type == 'Programmed' && c.status == 'True')) ? true : oldSelf.spec.controlPlaneRef == self.spec.controlPlaneRef", message="spec.controlPlaneRef is immutable when an entity is already Programmed"

// KongConsumerGroup is the Schema for the kongconsumergroups API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ spec:
name:
description: Name is the name of the Konnect Control Plane.
type: string
namespace:
description: |-
Namespace is the namespace where the Konnect Control Plane is in.
Currently only cluster scoped resources (KongVault) are allowed to set `konnectNamespacedRef.namespace`.
type: string
required:
- name
type: object
Expand Down Expand Up @@ -194,6 +199,8 @@ spec:
x-kubernetes-validations:
- message: controlPlaneRef is required once set
rule: '!has(oldSelf.spec.controlPlaneRef) || has(self.spec.controlPlaneRef)'
- message: spec.controlPlaneRef cannot specify namespace for namespaced resource
rule: '!has(self.spec.controlPlaneRef.konnectNamespacedRef) ? true : !has(self.spec.controlPlaneRef.konnectNamespacedRef.__namespace__)'
- message: spec.controlPlaneRef is immutable when an entity is already Programmed
rule: '(!self.status.conditions.exists(c, c.type == ''Programmed'' && c.status
== ''True'')) ? true : oldSelf.spec.controlPlaneRef == self.spec.controlPlaneRef'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ spec:
name:
description: Name is the name of the Konnect Control Plane.
type: string
namespace:
description: |-
Namespace is the namespace where the Konnect Control Plane is in.
Currently only cluster scoped resources (KongVault) are allowed to set `konnectNamespacedRef.namespace`.
type: string
required:
- name
type: object
Expand Down Expand Up @@ -195,6 +200,8 @@ spec:
x-kubernetes-validations:
- message: controlPlaneRef is required once set
rule: '!has(oldSelf.spec.controlPlaneRef) || has(self.spec.controlPlaneRef)'
- message: spec.controlPlaneRef cannot specify namespace for namespaced resource
rule: '!has(self.spec.controlPlaneRef.konnectNamespacedRef) ? true : !has(self.spec.controlPlaneRef.konnectNamespacedRef.__namespace__)'
- message: spec.controlPlaneRef is immutable when an entity is already Programmed
rule: '(!has(self.status) || !self.status.conditions.exists(c, c.type ==
''Programmed'' && c.status == ''True'')) ? true : oldSelf.spec.controlPlaneRef
Expand Down
7 changes: 7 additions & 0 deletions config/crd/bases/configuration.konghq.com_kongconsumers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ spec:
name:
description: Name is the name of the Konnect Control Plane.
type: string
namespace:
description: |-
Namespace is the namespace where the Konnect Control Plane is in.
Currently only cluster scoped resources (KongVault) are allowed to set `konnectNamespacedRef.namespace`.
type: string
required:
- name
type: object
Expand Down Expand Up @@ -222,6 +227,8 @@ spec:
rule: has(self.username) || has(self.custom_id)
- message: controlPlaneRef is required once set
rule: '!has(oldSelf.spec.controlPlaneRef) || has(self.spec.controlPlaneRef)'
- message: spec.controlPlaneRef cannot specify namespace for namespaced resource
rule: '!has(self.spec.controlPlaneRef.konnectNamespacedRef) ? true : !has(self.spec.controlPlaneRef.konnectNamespacedRef.__namespace__)'
- message: spec.controlPlaneRef is immutable when an entity is already Programmed
rule: '(!has(self.status) || !self.status.conditions.exists(c, c.type ==
''Programmed'' && c.status == ''True'')) ? true : oldSelf.spec.controlPlaneRef
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ spec:
name:
description: Name is the name of the Konnect Control Plane.
type: string
namespace:
description: |-
Namespace is the namespace where the Konnect Control Plane is in.
Currently only cluster scoped resources (KongVault) are allowed to set `konnectNamespacedRef.namespace`.
type: string
required:
- name
type: object
Expand Down
7 changes: 7 additions & 0 deletions config/crd/bases/configuration.konghq.com_kongservices.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ spec:
name:
description: Name is the name of the Konnect Control Plane.
type: string
namespace:
description: |-
Namespace is the namespace where the Konnect Control Plane is in.
Currently only cluster scoped resources (KongVault) are allowed to set `konnectNamespacedRef.namespace`.
type: string
required:
- name
type: object
Expand Down Expand Up @@ -255,6 +260,8 @@ spec:
x-kubernetes-validations:
- message: controlPlaneRef is required once set
rule: '!has(oldSelf.spec.controlPlaneRef) || has(self.spec.controlPlaneRef)'
- message: spec.controlPlaneRef cannot specify namespace for namespaced resource
rule: '!has(self.spec.controlPlaneRef.konnectNamespacedRef) ? true : !has(self.spec.controlPlaneRef.konnectNamespacedRef.__namespace__)'
- message: spec.controlPlaneRef is immutable when an entity is already Programmed
rule: '(!self.status.conditions.exists(c, c.type == ''Programmed'' && c.status
== ''True'')) ? true : oldSelf.spec.controlPlaneRef == self.spec.controlPlaneRef'
Expand Down
7 changes: 7 additions & 0 deletions config/crd/bases/configuration.konghq.com_kongupstreams.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ spec:
name:
description: Name is the name of the Konnect Control Plane.
type: string
namespace:
description: |-
Namespace is the namespace where the Konnect Control Plane is in.
Currently only cluster scoped resources (KongVault) are allowed to set `konnectNamespacedRef.namespace`.
type: string
required:
- name
type: object
Expand Down Expand Up @@ -411,6 +416,8 @@ spec:
x-kubernetes-validations:
- message: controlPlaneRef is required once set
rule: '!has(oldSelf.spec.controlPlaneRef) || has(self.spec.controlPlaneRef)'
- message: spec.controlPlaneRef cannot specify namespace for namespaced resource
rule: '!has(self.spec.controlPlaneRef.konnectNamespacedRef) ? true : !has(self.spec.controlPlaneRef.konnectNamespacedRef.__namespace__)'
- message: spec.controlPlaneRef is immutable when an entity is already Programmed
rule: '(!self.status.conditions.exists(c, c.type == ''Programmed'' && c.status
== ''True'')) ? true : oldSelf.spec.controlPlaneRef == self.spec.controlPlaneRef'
Expand Down
5 changes: 5 additions & 0 deletions config/crd/bases/configuration.konghq.com_kongvaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ spec:
name:
description: Name is the name of the Konnect Control Plane.
type: string
namespace:
description: |-
Namespace is the namespace where the Konnect Control Plane is in.
Currently only cluster scoped resources (KongVault) are allowed to set `konnectNamespacedRef.namespace`.
type: string
required:
- name
type: object
Expand Down
1 change: 1 addition & 0 deletions docs/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,7 @@ KonnectNamespacedRef is the schema for the KonnectNamespacedRef type.
| Field | Description |
| --- | --- |
| `name` _string_ | Name is the name of the Konnect Control Plane. |
| `namespace` _string_ | Namespace is the namespace where the Konnect Control Plane is in. Currently only cluster scoped resources (KongVault) are allowed to set `konnectNamespacedRef.namespace`. |


_Appears in:_
Expand Down
1 change: 1 addition & 0 deletions test/crdsvalidation/kongconsumer/testcases/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var TestCases = []testCasesGroup{}

func init() {
TestCases = append(TestCases,
controlPlaneRef,
requiredFields,
updatesNotAllowedForStatus,
)
Expand Down
31 changes: 31 additions & 0 deletions test/crdsvalidation/kongconsumer/testcases/controlplaneref.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package testcases

import (
"github.com/samber/lo"

configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1"
configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
)

var controlPlaneRef = testCasesGroup{
Name: "fields of controlPlaneRef",
TestCases: []testCase{
{
Name: "cpRef cannot have namespace",
KongConsumer: configurationv1.KongConsumer{
ObjectMeta: commonObjectMeta,
Spec: configurationv1.KongConsumerSpec{
ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef,
KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{
Name: "test-konnect-control-plane",
Namespace: "another-namespace",
},
},
},
Username: "username-1",
},
ExpectedErrorMessage: lo.ToPtr("spec.controlPlaneRef cannot specify namespace for namespaced resource"),
},
},
}
1 change: 1 addition & 0 deletions test/crdsvalidation/kongconsumergroup/testcases/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var TestCases = []testCasesGroup{}
func init() {
TestCases = append(TestCases,
fields,
controlPlaneRef,
updatesNotAllowedForStatus,
)
}
Expand Down
30 changes: 30 additions & 0 deletions test/crdsvalidation/kongconsumergroup/testcases/controlplaneref.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package testcases

import (
"github.com/samber/lo"

configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
configurationv1beta1 "github.com/kong/kubernetes-configuration/api/configuration/v1beta1"
)

var controlPlaneRef = testCasesGroup{
Name: "fields of controlPlaneRef",
TestCases: []testCase{
{
Name: "cpRef cannot have namespace",
KongConsumerGroup: configurationv1beta1.KongConsumerGroup{
ObjectMeta: commonObjectMeta,
Spec: configurationv1beta1.KongConsumerGroupSpec{
ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef,
KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{
Name: "test-konnect-control-plane",
Namespace: "another-namespace",
},
},
},
},
ExpectedErrorMessage: lo.ToPtr("spec.controlPlaneRef cannot specify namespace for namespaced resource"),
},
},
}
19 changes: 19 additions & 0 deletions test/crdsvalidation/kongservice/testcases/controlplaneref.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ var cpRef = testCasesGroup{
},
ExpectedErrorMessage: lo.ToPtr("when type is konnectID, konnectID must be set"),
},
{
Name: "providing namespace in konnectNamespacedRef yields an error",
KongService: configurationv1alpha1.KongService{
ObjectMeta: commonObjectMeta,
Spec: configurationv1alpha1.KongServiceSpec{
ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef,
KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{
Name: "test-konnect-control-plane",
Namespace: "another-namespace",
},
},
KongServiceAPISpec: configurationv1alpha1.KongServiceAPISpec{
Host: "example.com",
},
},
},
ExpectedErrorMessage: lo.ToPtr("spec.controlPlaneRef cannot specify namespace for namespaced resource"),
},
{
Name: "konnectNamespacedRef reference name cannot be changed when an entity is Programmed",
KongService: configurationv1alpha1.KongService{
Expand Down
17 changes: 17 additions & 0 deletions test/crdsvalidation/kongupstream/testcases/controlplaneref.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ var cpRef = testCasesGroup{
},
ExpectedErrorMessage: lo.ToPtr("when type is konnectID, konnectID must be set"),
},
{
Name: "providing namespace in konnectNamespacedRef yields an error",
KongUpstream: configurationv1alpha1.KongUpstream{
ObjectMeta: commonObjectMeta,
Spec: configurationv1alpha1.KongUpstreamSpec{
ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef,
KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{
Name: "test-konnect-control-plane",
Namespace: "another-namespace",
},
},
KongUpstreamAPISpec: configurationv1alpha1.KongUpstreamAPISpec{},
},
},
ExpectedErrorMessage: lo.ToPtr("spec.controlPlaneRef cannot specify namespace for namespaced resource"),
},
{
Name: "konnectNamespacedRef reference name cannot be changed when an entity is Programmed",
KongUpstream: configurationv1alpha1.KongUpstream{
Expand Down

0 comments on commit f5436b2

Please sign in to comment.