88 "github.com/stretchr/testify/assert"
99 "github.com/stretchr/testify/require"
1010 corev1 "k8s.io/api/core/v1"
11- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1211 "k8s.io/apimachinery/pkg/types"
1312 "sigs.k8s.io/controller-runtime/pkg/client"
1413
@@ -22,7 +21,7 @@ import (
2221 konnectv1alpha1 "github.com/kong/kubernetes-configuration/api/konnect/v1alpha1"
2322)
2423
25- func TestKonnectExtensionKonnectGatewayControlPlaneNamespacedRef (t * testing.T ) {
24+ func TestKonnectExtension (t * testing.T ) {
2625 ns , _ := helpers .SetupTestEnv (t , GetCtx (), GetEnv ())
2726
2827 // Let's generate a unique test ID that we can refer to in Konnect entities.
@@ -58,38 +57,17 @@ func TestKonnectExtensionKonnectGatewayControlPlaneNamespacedRef(t *testing.T) {
5857 assertKonnectEntityProgrammed (t , cp )
5958 }, testutils .ObjectUpdateTimeout , testutils .ObjectUpdateTick )
6059
61- t .Logf ("Creating a KonnectExtension" )
62- ke := deploy .KonnectExtensionRefencingKonnectGatewayControlPlane (
63- t , ctx ,
64- clientNamespaced ,
65- cp ,
66- )
67- t .Cleanup (deleteObjectAndWaitForDeletionFn (t , ke .DeepCopy ()))
68-
69- t .Logf ("Waiting for KonnectExtension %s/%s to have ControlPlaneRefValid contition set to True" , ke .Namespace , ke .Name )
70- require .EventuallyWithT (t , func (t * assert.CollectT ) {
71- err := GetClients ().MgrClient .Get (GetCtx (), types.NamespacedName {Name : ke .Name , Namespace : ke .Namespace }, ke )
72- require .NoError (t , err )
73- assert .Truef (t , lo .ContainsBy (
74- ke .Status .Conditions , func (cond metav1.Condition ) bool {
75- return cond .Type == konnectv1alpha1 .ControlPlaneRefValidConditionType &&
76- cond .Status == metav1 .ConditionTrue
77- },
78- ), "ControlPlaneRefValid has not been set to True, conditions: %+v" , ke .Status .Conditions )
79- }, testutils .ObjectUpdateTimeout , testutils .ObjectUpdateTick )
80-
81- // TODO: Create a DataPlane using this KonnectExtension:
82- // https://github.com/Kong/gateway-operator/issues/726
8360 // Create a secret used as dataplane certificate for the KonnectExtension.
8461 s := deploy .Secret (
8562 t , ctx , clientNamespaced ,
8663 // TODO: Fill real certificate data here after DP certifcates provisioning is done:
8764 // https://github.com/Kong/gateway-operator/issues/874
8865 map [string ][]byte {},
8966 )
90- // Create a KonnectExtension attaching to the CP by its ID.
91- t .Logf ("Creating a KonnectExtension and waiting for Konnect control plane ref resolved" )
92- ke = deploy .KonnectExtension (
67+
68+ // Tests on KonnectExtension with KonnectID control plane ref.
69+ t .Logf ("Creating a KonnectExtension with KonnectID typed control plane ref" )
70+ keWithKonnectIDCPRef := deploy .KonnectExtension (
9371 t , ctx , clientNamespaced ,
9472 deploy.WithKonnectConfiguration [* konnectv1alpha1.KonnectExtension ](konnectv1alpha1.KonnectConfiguration {
9573 APIAuthConfigurationRef : konnectv1alpha1.KonnectAPIAuthConfigurationRef {
@@ -100,30 +78,42 @@ func TestKonnectExtensionKonnectGatewayControlPlaneNamespacedRef(t *testing.T) {
10078 setKonnectExtensionDPCertSecretRef (t , s ),
10179 )
10280
103- t .Cleanup (deleteObjectAndWaitForDeletionFn (t , ke .DeepCopy ()))
81+ t .Cleanup (deleteObjectAndWaitForDeletionFn (t , keWithKonnectIDCPRef .DeepCopy ()))
10482
105- t .Logf ("Waiting for KonnectExtension %s/%s to have expected conditions set to True" , ke .Namespace , ke .Name )
83+ t .Logf ("Waiting for KonnectExtension %s/%s to have expected conditions set to True" , keWithKonnectIDCPRef .Namespace , keWithKonnectIDCPRef .Name )
10684 require .EventuallyWithT (t , func (t * assert.CollectT ) {
107- ok , msg := checkKonnectExtensionConditions (t , ke )
108- assert .Truef (t , ok , "condition check failed: %s, conditions: %+v" , msg , ke .Status .Conditions )
85+ ok , msg := checkKonnectExtensionConditions (t , keWithKonnectIDCPRef )
86+ assert .Truef (t , ok , "condition check failed: %s, conditions: %+v" , msg , keWithKonnectIDCPRef .Status .Conditions )
10987 }, testutils .ObjectUpdateTimeout , testutils .ObjectUpdateTick )
11088
111- t .Logf ("waiting for status.konnect and status.dataPlaneClientAuth to be set for KonnectExtension %s/%s" , ke .Namespace , ke .Name )
112- require .EventuallyWithT (t , func (t * assert.CollectT ) {
113- err := GetClients ().MgrClient .Get (GetCtx (), types.NamespacedName {Name : ke .Name , Namespace : ke .Namespace }, ke )
114- require .NoError (t , err )
115- // Check Konnect control plane ID
116- assert .NotNil (t , ke .Status .Konnect , "status.konnect should be present" )
117- assert .Equal (t , cp .GetKonnectID (), ke .Status .Konnect .ControlPlaneID , "Konnect control plane ID should be set in status" )
118- // Check dataplane client auth
119- assert .NotNil (t , ke .Status .DataPlaneClientAuth , "status.dataPlaneClientAuth should be present" )
120- assert .NotNil (t , ke .Status .DataPlaneClientAuth .CertificateSecretRef , "status.dataPlaneClientAuth.certiifcateSecretRef should be present" )
121- assert .Equal (t , s .Name , ke .Status .DataPlaneClientAuth .CertificateSecretRef .Name ,
122- "status.dataPlaneClientAuth.certiifcateSecretRef should have the expected secret name" )
89+ t .Logf ("waiting for status.konnect and status.dataPlaneClientAuth to be set for KonnectExtension %s/%s" , keWithKonnectIDCPRef .Namespace , keWithKonnectIDCPRef .Name )
90+ require .EventuallyWithT (t ,
91+ checkKonnectExtensionStatus (t , keWithKonnectIDCPRef , cp .GetKonnectID (), s .Name ),
92+ testutils .ObjectUpdateTimeout , testutils .ObjectUpdateTick )
93+
94+ // Tests on KonnectExtension with KonnectNamespacedRef control plane ref.
95+ // REVIEW: should we separate the KonnectExtensions with different control plane refs to different cases?
96+ t .Logf ("Creating a KonnectExtension with KonnectNamespacedRef typed control plane ref" )
97+ keWithNamespacedCPRef := deploy .KonnectExtension (
98+ t , ctx ,
99+ clientNamespaced ,
100+ setKonnectExtesionKonnectNamespacedRefControlPlaneRef (t , cp ),
101+ setKonnectExtensionDPCertSecretRef (t , s ),
102+ )
103+ t .Cleanup (deleteObjectAndWaitForDeletionFn (t , keWithNamespacedCPRef .DeepCopy ()))
123104
105+ t .Logf ("Waiting for KonnectExtension %s/%s to have expected conditions set to True" , keWithNamespacedCPRef .Namespace , keWithNamespacedCPRef .Name )
106+ require .EventuallyWithT (t , func (t * assert.CollectT ) {
107+ ok , msg := checkKonnectExtensionConditions (t , keWithNamespacedCPRef )
108+ assert .Truef (t , ok , "condition check failed: %s, conditions: %+v" , msg , keWithNamespacedCPRef .Status .Conditions )
124109 }, testutils .ObjectUpdateTimeout , testutils .ObjectUpdateTick )
125110
126- // TODO: Create DataPlanes using the KonnectExtension after DP certifcates provisioning is done:
111+ t .Logf ("waiting for status.konnect and status.dataPlaneClientAuth to be set for KonnectExtension %s/%s" , keWithNamespacedCPRef .Namespace , keWithNamespacedCPRef .Name )
112+ require .EventuallyWithT (t ,
113+ checkKonnectExtensionStatus (t , keWithNamespacedCPRef , cp .GetKonnectID (), s .Name ),
114+ testutils .ObjectUpdateTimeout , testutils .ObjectUpdateTick )
115+
116+ // TODO: Create DataPlanes using the KonnectExtensions after DP certifcates provisioning is done:
127117 // https://github.com/Kong/gateway-operator/issues/874
128118
129119}
@@ -141,7 +131,25 @@ func setKonnectExtensionKonnectIDControlPlaneRef(t *testing.T, cpID string) depl
141131 }
142132}
143133
144- func setKonnectExtensionDPCertSecretRef (t * testing.T , s * corev1.Secret ) func (client.Object ) {
134+ func setKonnectExtesionKonnectNamespacedRefControlPlaneRef (
135+ t * testing.T , cp * konnectv1alpha1.KonnectGatewayControlPlane ,
136+ ) deploy.ObjOption {
137+ return func (obj client.Object ) {
138+ ke , ok := obj .(* konnectv1alpha1.KonnectExtension )
139+ require .True (t , ok )
140+ // TODO: use `WithKonnectIDControlPlaneRef` after KonnectExtension support `SetControlPlaneRef`:
141+ // https://github.com/Kong/kubernetes-configuration/issues/328
142+ ke .Spec .KonnectControlPlane .ControlPlaneRef = commonv1alpha1.ControlPlaneRef {
143+ Type : commonv1alpha1 .ControlPlaneRefKonnectNamespacedRef ,
144+ KonnectNamespacedRef : & commonv1alpha1.KonnectNamespacedRef {
145+ Name : cp .Name ,
146+ Namespace : cp .Namespace ,
147+ },
148+ }
149+ }
150+ }
151+
152+ func setKonnectExtensionDPCertSecretRef (t * testing.T , s * corev1.Secret ) deploy.ObjOption {
145153 return func (obj client.Object ) {
146154 ke , ok := obj .(* konnectv1alpha1.KonnectExtension )
147155 require .True (t , ok )
@@ -167,3 +175,22 @@ func checkKonnectExtensionConditions(t *assert.CollectT, ke *konnectv1alpha1.Kon
167175 }
168176 return helpers .CheckAllConditionsTrue (ke , checkConditionTypes )
169177}
178+
179+ func checkKonnectExtensionStatus (t * testing.T ,
180+ ke * konnectv1alpha1.KonnectExtension ,
181+ expectedKonnectCPID string ,
182+ expectedDPCertificateSecretName string ,
183+ ) func (t * assert.CollectT ) {
184+ return func (t * assert.CollectT ) {
185+ err := GetClients ().MgrClient .Get (GetCtx (), types.NamespacedName {Name : ke .Name , Namespace : ke .Namespace }, ke )
186+ require .NoError (t , err )
187+ // Check Konnect control plane ID
188+ assert .NotNil (t , ke .Status .Konnect , "status.konnect should be present" )
189+ assert .Equal (t , expectedKonnectCPID , ke .Status .Konnect .ControlPlaneID , "Konnect control plane ID should be set in status" )
190+ // Check dataplane client auth
191+ assert .NotNil (t , ke .Status .DataPlaneClientAuth , "status.dataPlaneClientAuth should be present" )
192+ assert .NotNil (t , ke .Status .DataPlaneClientAuth .CertificateSecretRef , "status.dataPlaneClientAuth.certiifcateSecretRef should be present" )
193+ assert .Equal (t , expectedDPCertificateSecretName , ke .Status .DataPlaneClientAuth .CertificateSecretRef .Name ,
194+ "status.dataPlaneClientAuth.certiifcateSecretRef should have the expected secret name" )
195+ }
196+ }
0 commit comments