Skip to content

Commit b37991f

Browse files
committed
ut
Signed-off-by: zirain <zirain2009@gmail.com>
1 parent 7999151 commit b37991f

File tree

4 files changed

+153
-6
lines changed

4 files changed

+153
-6
lines changed

internal/provider/kubernetes/indexers.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -955,11 +955,9 @@ func secretBtlsIndexFunc(rawObj client.Object) []string {
955955
func clusterTrustBundleBtlsIndexFunc(rawObj client.Object) []string {
956956
btls := rawObj.(*gwapiv1a3.BackendTLSPolicy)
957957
var refs []string
958-
if btls.Spec.Validation.CACertificateRefs != nil {
959-
for _, caCertRef := range btls.Spec.Validation.CACertificateRefs {
960-
if string(caCertRef.Kind) == resource.KindClusterTrustBundle {
961-
refs = append(refs, string(caCertRef.Name))
962-
}
958+
for _, caCertRef := range btls.Spec.Validation.CACertificateRefs {
959+
if string(caCertRef.Kind) == resource.KindClusterTrustBundle {
960+
refs = append(refs, string(caCertRef.Name))
963961
}
964962
}
965963
return refs

internal/provider/kubernetes/predicates.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func (r *gatewayAPIReconciler) isBackendReferencingClusterTrustBundle(ctb *certi
245245
func (r *gatewayAPIReconciler) isBackendTLSPolicyReferencingClusterTrustBundle(ctb *certificatesv1a1.ClusterTrustBundle) bool {
246246
btlsList := &gwapiv1a3.BackendTLSPolicyList{}
247247
if err := r.client.List(context.Background(), btlsList, &client.ListOptions{
248-
FieldSelector: fields.OneTermEqualSelector(clusterTrustBundleBackendIndex, ctb.Name),
248+
FieldSelector: fields.OneTermEqualSelector(clusterTrustBundleBtlsIndex, ctb.Name),
249249
}); err != nil {
250250
r.log.Error(err, "unable to find associated BackendTLSPolicy")
251251
return false

internal/provider/kubernetes/predicates_test.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212

1313
"github.com/stretchr/testify/require"
14+
certificatesv1a1 "k8s.io/api/certificates/v1alpha1"
1415
corev1 "k8s.io/api/core/v1"
1516
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1617
"k8s.io/apimachinery/pkg/types"
@@ -20,6 +21,7 @@ import (
2021
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
2122
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
2223
gwapiv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
24+
gwapiv1a3 "sigs.k8s.io/gateway-api/apis/v1alpha3"
2325

2426
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
2527
"github.com/envoyproxy/gateway/internal/envoygateway"
@@ -1341,3 +1343,126 @@ func TestValidateHTTPRouteFilerForReconcile(t *testing.T) {
13411343
})
13421344
}
13431345
}
1346+
1347+
func TestValidateClusterTrustBundleForReconcile(t *testing.T) {
1348+
gc := test.GetGatewayClass("test-gc", egv1a1.GatewayControllerName, nil)
1349+
gtw := test.GetGateway(types.NamespacedName{Namespace: "default", Name: "scheduled-status-test"}, "test-gc", 8080)
1350+
ctb := test.GetClusterTrustBundle("fake-ctb")
1351+
backend := &egv1a1.Backend{
1352+
ObjectMeta: metav1.ObjectMeta{
1353+
Name: "backend-dynamic-resolver-clustertrustbundle",
1354+
Namespace: "default",
1355+
},
1356+
Spec: egv1a1.BackendSpec{
1357+
Type: ptr.To(egv1a1.BackendTypeDynamicResolver),
1358+
TLS: &egv1a1.BackendTLSSettings{
1359+
CACertificateRefs: []gwapiv1.LocalObjectReference{
1360+
{
1361+
Kind: gwapiv1.Kind("ClusterTrustBundle"),
1362+
Name: gwapiv1.ObjectName(ctb.Name),
1363+
},
1364+
},
1365+
},
1366+
},
1367+
}
1368+
btp := &gwapiv1a3.BackendTLSPolicy{
1369+
ObjectMeta: metav1.ObjectMeta{
1370+
Name: "backend-tls-policy-dynamic-resolver-clustertrustbundle",
1371+
Namespace: "default",
1372+
},
1373+
Spec: gwapiv1a3.BackendTLSPolicySpec{
1374+
Validation: gwapiv1a3.BackendTLSPolicyValidation{
1375+
CACertificateRefs: []gwapiv1.LocalObjectReference{
1376+
{
1377+
Kind: gwapiv1.Kind("ClusterTrustBundle"),
1378+
Name: gwapiv1.ObjectName(ctb.Name),
1379+
},
1380+
},
1381+
},
1382+
},
1383+
}
1384+
ctp := test.GetClientTrafficPolicy(
1385+
types.NamespacedName{Name: "fake-ctp", Namespace: "default"},
1386+
&egv1a1.ClientTLSSettings{
1387+
ClientValidation: &egv1a1.ClientValidationContext{
1388+
CACertificateRefs: []gwapiv1.SecretObjectReference{
1389+
{
1390+
Kind: ptr.To[gwapiv1.Kind]("ClusterTrustBundle"),
1391+
Name: gwapiv1.ObjectName(ctb.Name),
1392+
},
1393+
},
1394+
},
1395+
})
1396+
1397+
testCases := []struct {
1398+
name string
1399+
configs []client.Object
1400+
ctb *certificatesv1a1.ClusterTrustBundle
1401+
expect bool
1402+
}{
1403+
{
1404+
name: "referenced by Backend",
1405+
configs: []client.Object{
1406+
gc,
1407+
gtw,
1408+
backend,
1409+
},
1410+
ctb: ctb,
1411+
expect: true,
1412+
},
1413+
{
1414+
name: "referenced by BackendTLSPolicy",
1415+
configs: []client.Object{
1416+
gc,
1417+
gtw,
1418+
btp,
1419+
},
1420+
ctb: ctb,
1421+
expect: true,
1422+
},
1423+
{
1424+
name: "referenced by ClientTrafficPolicy",
1425+
configs: []client.Object{
1426+
gc,
1427+
gtw,
1428+
ctp,
1429+
},
1430+
ctb: ctb,
1431+
expect: true,
1432+
},
1433+
{
1434+
name: "ClusterTrustBundle not referenced",
1435+
configs: []client.Object{
1436+
gc,
1437+
gtw,
1438+
},
1439+
ctb: ctb,
1440+
expect: false,
1441+
},
1442+
}
1443+
1444+
// Create the reconciler.
1445+
logger := logging.DefaultLogger(os.Stdout, egv1a1.LogLevelInfo)
1446+
1447+
r := gatewayAPIReconciler{
1448+
classController: egv1a1.GatewayControllerName,
1449+
log: logger,
1450+
backendCRDExists: true,
1451+
bTLSPolicyCRDExists: true,
1452+
ctpCRDExists: true,
1453+
}
1454+
1455+
for _, tc := range testCases {
1456+
r.client = fakeclient.NewClientBuilder().
1457+
WithScheme(envoygateway.GetScheme()).
1458+
WithObjects(tc.configs...).
1459+
WithIndex(&egv1a1.Backend{}, clusterTrustBundleBackendIndex, clusterTrustBundleBackendIndexFunc).
1460+
WithIndex(&gwapiv1a3.BackendTLSPolicy{}, clusterTrustBundleBtlsIndex, clusterTrustBundleBtlsIndexFunc).
1461+
WithIndex(&egv1a1.ClientTrafficPolicy{}, clusterTrustBundlerCtpIndex, clusterTrustBundleCtpIndexFunc).
1462+
Build()
1463+
t.Run(tc.name, func(t *testing.T) {
1464+
res := r.validateClusterTrustBundleForReconcile(tc.ctb)
1465+
require.Equal(t, tc.expect, res)
1466+
})
1467+
}
1468+
}

internal/provider/kubernetes/test/utils.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package test
77

88
import (
99
appsv1 "k8s.io/api/apps/v1"
10+
certificatesv1a1 "k8s.io/api/certificates/v1alpha1"
1011
corev1 "k8s.io/api/core/v1"
1112
discoveryv1 "k8s.io/api/discovery/v1"
1213
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -417,3 +418,26 @@ func GetHTTPRouteFilter(nsName types.NamespacedName) *egv1a1.HTTPRouteFilter {
417418
},
418419
}
419420
}
421+
422+
func GetClusterTrustBundle(name string) *certificatesv1a1.ClusterTrustBundle {
423+
return &certificatesv1a1.ClusterTrustBundle{
424+
ObjectMeta: metav1.ObjectMeta{
425+
Name: name,
426+
},
427+
Spec: certificatesv1a1.ClusterTrustBundleSpec{
428+
TrustBundle: "fake-trust-bundle",
429+
},
430+
}
431+
}
432+
433+
func GetClientTrafficPolicy(nn types.NamespacedName, tls *egv1a1.ClientTLSSettings) *egv1a1.ClientTrafficPolicy {
434+
return &egv1a1.ClientTrafficPolicy{
435+
ObjectMeta: metav1.ObjectMeta{
436+
Name: nn.Name,
437+
Namespace: nn.Namespace,
438+
},
439+
Spec: egv1a1.ClientTrafficPolicySpec{
440+
TLS: tls,
441+
},
442+
}
443+
}

0 commit comments

Comments
 (0)