Skip to content

Commit a68fe64

Browse files
committed
adress comments: extract common func
1 parent 1ae3c8e commit a68fe64

3 files changed

Lines changed: 73 additions & 25 deletions

File tree

test/helpers/conditions.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package helpers
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/samber/lo"
8+
9+
"github.com/kong/gateway-operator/pkg/consts"
10+
k8sutils "github.com/kong/gateway-operator/pkg/utils/kubernetes"
11+
)
12+
13+
// CheckAllConditionsTrue returns true if all the conditions with given type in `conditionTypes` are set to `True` in the given resource.
14+
// If it returns `false`, the second return value contains a message to tell what conditions are not `True`.
15+
func CheckAllConditionsTrue(resource k8sutils.ConditionsAware, conditionTypes []consts.ConditionType) (bool, string) {
16+
failedConditions := make([]string, 0, len(conditionTypes))
17+
lo.ForEach(conditionTypes, func(conditionType consts.ConditionType, _ int) {
18+
if !k8sutils.HasConditionTrue(conditionType, resource) {
19+
failedConditions = append(failedConditions, string(conditionType))
20+
}
21+
})
22+
if len(failedConditions) > 0 {
23+
return false, fmt.Sprintf("condition(s) %s not set to True", strings.Join(failedConditions, ", "))
24+
}
25+
return true, ""
26+
}

test/helpers/deploy/deploy_resources.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,29 @@ func KonnectExtension(
10331033
return ke
10341034
}
10351035

1036+
// ObjectSupportingKonnectConfiguration defines the interface of types supporting setting `KonnectConfiguration`.
1037+
type ObjectSupportingKonnectConfiguration interface {
1038+
*konnectv1alpha1.KonnectGatewayControlPlane |
1039+
*konnectv1alpha1.KonnectExtension |
1040+
*konnectv1alpha1.KonnectCloudGatewayNetwork
1041+
}
1042+
1043+
// WithKonnectConfiguration returns an option function that sets the `KonnectConfiguration` in the object.
1044+
func WithKonnectConfiguration[T ObjectSupportingKonnectConfiguration](
1045+
konnectConfiguration konnectv1alpha1.KonnectConfiguration,
1046+
) ObjOption {
1047+
return func(obj client.Object) {
1048+
switch o := any(obj).(type) {
1049+
case *konnectv1alpha1.KonnectGatewayControlPlane:
1050+
o.Spec.KonnectConfiguration = konnectConfiguration
1051+
case *konnectv1alpha1.KonnectExtension:
1052+
o.Spec.KonnectConfiguration = lo.ToPtr(konnectConfiguration)
1053+
case *konnectv1alpha1.KonnectCloudGatewayNetwork:
1054+
o.Spec.KonnectConfiguration = konnectConfiguration
1055+
}
1056+
}
1057+
}
1058+
10361059
// KonnectExtensionWithAPIAuthRefAndCPID deploys a KonnectExtension attaching to control plane
10371060
// by the CP's ID and the API auth configuration to the Konnect server where the CP is in.
10381061
func KonnectExtensionWithAPIAuthRefAndCPID(

test/integration/test_konnect_extension.go

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
package integration
22

33
import (
4-
"fmt"
5-
"strings"
64
"testing"
75

86
"github.com/google/uuid"
97
"github.com/samber/lo"
108
"github.com/stretchr/testify/assert"
119
"github.com/stretchr/testify/require"
1210
corev1 "k8s.io/api/core/v1"
13-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1411
"k8s.io/apimachinery/pkg/types"
1512
"sigs.k8s.io/controller-runtime/pkg/client"
1613

14+
"github.com/kong/gateway-operator/pkg/consts"
1715
testutils "github.com/kong/gateway-operator/pkg/utils/test"
1816
"github.com/kong/gateway-operator/test"
1917
"github.com/kong/gateway-operator/test/helpers"
2018
"github.com/kong/gateway-operator/test/helpers/deploy"
2119

20+
commonv1alpha1 "github.com/kong/kubernetes-configuration/api/common/v1alpha1"
2221
konnectv1alpha1 "github.com/kong/kubernetes-configuration/api/konnect/v1alpha1"
2322
)
2423

@@ -67,12 +66,14 @@ func TestKonnectExtension(t *testing.T) {
6766
)
6867
// Create a KonnectExtension attaching to the CP by its ID.
6968
t.Logf("Creating a KonnectExtension and waiting for Konnect control plane ref resolved")
70-
ke := deploy.KonnectExtensionWithAPIAuthRefAndCPID(
69+
ke := deploy.KonnectExtension(
7170
t, ctx, clientNamespaced,
72-
konnectv1alpha1.KonnectAPIAuthConfigurationRef{
73-
Name: authCfg.Name,
74-
},
75-
cp.GetKonnectID(),
71+
deploy.WithKonnectConfiguration[*konnectv1alpha1.KonnectExtension](konnectv1alpha1.KonnectConfiguration{
72+
APIAuthConfigurationRef: konnectv1alpha1.KonnectAPIAuthConfigurationRef{
73+
Name: authCfg.Name,
74+
},
75+
}),
76+
setKonnectExtensionKonnectIDControlPlaneRef(t, cp.GetKonnectID()),
7677
setKonnectExtensionDPCertSecretRef(t, s),
7778
)
7879

@@ -104,6 +105,19 @@ func TestKonnectExtension(t *testing.T) {
104105

105106
}
106107

108+
func setKonnectExtensionKonnectIDControlPlaneRef(t *testing.T, cpID string) deploy.ObjOption {
109+
return func(obj client.Object) {
110+
ke, ok := obj.(*konnectv1alpha1.KonnectExtension)
111+
require.True(t, ok)
112+
// TODO: use `WithKonnectIDControlPlaneRef` after KonnectExtension support `SetControlPlaneRef`:
113+
// https://github.com/Kong/kubernetes-configuration/issues/328
114+
ke.Spec.KonnectControlPlane.ControlPlaneRef = commonv1alpha1.ControlPlaneRef{
115+
Type: commonv1alpha1.ControlPlaneRefKonnectID,
116+
KonnectID: lo.ToPtr(cpID),
117+
}
118+
}
119+
}
120+
107121
func setKonnectExtensionDPCertSecretRef(t *testing.T, s *corev1.Secret) func(client.Object) {
108122
return func(obj client.Object) {
109123
ke, ok := obj.(*konnectv1alpha1.KonnectExtension)
@@ -123,25 +137,10 @@ func checkKonnectExtensionConditions(t *assert.CollectT, ke *konnectv1alpha1.Kon
123137
err := GetClients().MgrClient.Get(GetCtx(), types.NamespacedName{Name: ke.Name, Namespace: ke.Namespace}, ke)
124138
require.NoError(t, err)
125139

126-
checkConditionTypes := []string{
140+
checkConditionTypes := []consts.ConditionType{
127141
konnectv1alpha1.ControlPlaneRefValidConditionType,
128142
konnectv1alpha1.DataPlaneCertificateProvisionedConditionType,
129143
konnectv1alpha1.KonnectExtensionReadyConditionType,
130144
}
131-
failedConditionTypes := make([]string, 0, len(checkConditionTypes))
132-
conditionMap := lo.SliceToMap(ke.Status.Conditions, func(condition metav1.Condition) (string, metav1.ConditionStatus) {
133-
return condition.Type, condition.Status
134-
})
135-
136-
for _, conditionType := range checkConditionTypes {
137-
status, ok := conditionMap[conditionType]
138-
if !ok || status != metav1.ConditionTrue {
139-
failedConditionTypes = append(failedConditionTypes, conditionType)
140-
}
141-
}
142-
143-
if len(failedConditionTypes) > 0 {
144-
return false, fmt.Sprintf("condition(s) %s not set to True", strings.Join(failedConditionTypes, ", "))
145-
}
146-
return true, ""
145+
return helpers.CheckAllConditionsTrue(ke, checkConditionTypes)
147146
}

0 commit comments

Comments
 (0)