Skip to content

Commit 5f4bb26

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

3 files changed

Lines changed: 56 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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,26 @@ func KonnectExtension(
10331033
return ke
10341034
}
10351035

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

test/integration/test_konnect_extension.go

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
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"
@@ -67,12 +65,14 @@ func TestKonnectExtension(t *testing.T) {
6765
)
6866
// Create a KonnectExtension attaching to the CP by its ID.
6967
t.Logf("Creating a KonnectExtension and waiting for Konnect control plane ref resolved")
70-
ke := deploy.KonnectExtensionWithAPIAuthRefAndCPID(
68+
ke := deploy.KonnectExtension(
7169
t, ctx, clientNamespaced,
72-
konnectv1alpha1.KonnectAPIAuthConfigurationRef{
73-
Name: authCfg.Name,
74-
},
75-
cp.GetKonnectID(),
70+
deploy.WithKonnectConfiguration[*konnectv1alpha1.KonnectExtension](konnectv1alpha1.KonnectConfiguration{
71+
APIAuthConfigurationRef: konnectv1alpha1.KonnectAPIAuthConfigurationRef{
72+
Name: authCfg.Name,
73+
},
74+
}),
75+
deploy.WithKonnectIDControlPlaneRef(cp),
7676
setKonnectExtensionDPCertSecretRef(t, s),
7777
)
7878

@@ -123,25 +123,10 @@ func checkKonnectExtensionConditions(t *assert.CollectT, ke *konnectv1alpha1.Kon
123123
err := GetClients().MgrClient.Get(GetCtx(), types.NamespacedName{Name: ke.Name, Namespace: ke.Namespace}, ke)
124124
require.NoError(t, err)
125125

126-
checkConditionTypes := []string{
126+
checkConditionTypes := []consts.ConditionType{
127127
konnectv1alpha1.ControlPlaneRefValidConditionType,
128128
konnectv1alpha1.DataPlaneCertificateProvisionedConditionType,
129129
konnectv1alpha1.KonnectExtensionReadyConditionType,
130130
}
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, ""
131+
return helpers.CheckAllConditionsTrue(ke, checkConditionTypes)
147132
}

0 commit comments

Comments
 (0)