@@ -936,9 +936,9 @@ func TestGatewayDataPlaneNetworkPolicy(t *testing.T) {
936936 require .Equal (t , map [string ]string {"app" : dataplane .Name }, networkPolicy .Spec .PodSelector .MatchLabels )
937937
938938 t .Log ("verifying that the DataPlane's Pod Admin API is network restricted to ControlPlane Pods" )
939- var expectLimitedAdminAPI networkPolicyIngressRuleDecorator
940- expectLimitedAdminAPI .withProtocolPort (corev1 .ProtocolTCP , consts .DataPlaneAdminAPIPort )
941- expectLimitedAdminAPI .withPeerMatchLabels (
939+ var expectLimitedAdminAPIFromCP networkPolicyIngressRuleDecorator
940+ expectLimitedAdminAPIFromCP .withProtocolPort (corev1 .ProtocolTCP , consts .DataPlaneAdminAPIPort )
941+ expectLimitedAdminAPIFromCP .withPeerMatchLabels (
942942 map [string ]string {"app" : controlplane .Name },
943943 map [string ]string {"kubernetes.io/metadata.name" : dataplane .Namespace },
944944 )
@@ -953,9 +953,24 @@ func TestGatewayDataPlaneNetworkPolicy(t *testing.T) {
953953 expectAllowMetricsIngress .withProtocolPort (corev1 .ProtocolTCP , consts .DataPlaneMetricsPort )
954954
955955 t .Log ("verifying DataPlane's NetworkPolicies ingress rules correctness" )
956- require .Contains (t , networkPolicy .Spec .Ingress , expectLimitedAdminAPI .Rule )
957- require .Contains (t , networkPolicy .Spec .Ingress , expectAllowProxyIngress .Rule )
958- require .Contains (t , networkPolicy .Spec .Ingress , expectAllowMetricsIngress .Rule )
956+ // The NetworkPolicy should contain the ControlPlane admin API rule.
957+ // It may also contain an operator admin API rule (when running with operator pod labels available).
958+ require .Contains (t , networkPolicy .Spec .Ingress , expectLimitedAdminAPIFromCP .Rule ,
959+ "NetworkPolicy should contain ControlPlane admin API access rule" )
960+ require .Contains (t , networkPolicy .Spec .Ingress , expectAllowProxyIngress .Rule ,
961+ "NetworkPolicy should contain proxy ingress rule" )
962+ require .Contains (t , networkPolicy .Spec .Ingress , expectAllowMetricsIngress .Rule ,
963+ "NetworkPolicy should contain metrics ingress rule" )
964+
965+ // Verify we have the expected number of admin API rules (either 1 or 2 depending on whether operator labels are available)
966+ adminAPIRules := 0
967+ for _ , rule := range networkPolicy .Spec .Ingress {
968+ if len (rule .Ports ) == 1 && rule .Ports [0 ].Port != nil && rule .Ports [0 ].Port .IntVal == consts .DataPlaneAdminAPIPort {
969+ adminAPIRules ++
970+ }
971+ }
972+ require .GreaterOrEqual (t , adminAPIRules , 1 , "NetworkPolicy should have at least 1 admin API rule" )
973+ require .LessOrEqual (t , adminAPIRules , 2 , "NetworkPolicy should have at most 2 admin API rules (CP + operator)" )
959974
960975 t .Log ("deleting DataPlane's NetworkPolicies" )
961976 require .NoError (t ,
@@ -971,10 +986,23 @@ func TestGatewayDataPlaneNetworkPolicy(t *testing.T) {
971986 networkPolicy = networkpolicies [0 ]
972987 t .Logf ("NetworkPolicy generation %d" , networkPolicy .Generation )
973988
974- t .Log ("verifying DataPlane's NetworkPolicies ingress rules correctness" )
975- require .Contains (t , networkPolicy .Spec .Ingress , expectLimitedAdminAPI .Rule )
976- require .Contains (t , networkPolicy .Spec .Ingress , expectAllowProxyIngress .Rule )
977- require .Contains (t , networkPolicy .Spec .Ingress , expectAllowMetricsIngress .Rule )
989+ t .Log ("verifying DataPlane's NetworkPolicies ingress rules correctness after recreation" )
990+ require .Contains (t , networkPolicy .Spec .Ingress , expectLimitedAdminAPIFromCP .Rule ,
991+ "Recreated NetworkPolicy should contain ControlPlane admin API access rule" )
992+ require .Contains (t , networkPolicy .Spec .Ingress , expectAllowProxyIngress .Rule ,
993+ "Recreated NetworkPolicy should contain proxy ingress rule" )
994+ require .Contains (t , networkPolicy .Spec .Ingress , expectAllowMetricsIngress .Rule ,
995+ "Recreated NetworkPolicy should contain metrics ingress rule" )
996+
997+ // Verify admin API rules count again after recreation
998+ adminAPIRules = 0
999+ for _ , rule := range networkPolicy .Spec .Ingress {
1000+ if len (rule .Ports ) == 1 && rule .Ports [0 ].Port != nil && rule .Ports [0 ].Port .IntVal == consts .DataPlaneAdminAPIPort {
1001+ adminAPIRules ++
1002+ }
1003+ }
1004+ require .GreaterOrEqual (t , adminAPIRules , 1 , "Recreated NetworkPolicy should have at least 1 admin API rule" )
1005+ require .LessOrEqual (t , adminAPIRules , 2 , "Recreated NetworkPolicy should have at most 2 admin API rules (CP + operator)" )
9781006
9791007 t .Run ("verifying DataPlane's NetworkPolicies get updated after customizing kong proxy listen port through GatewayConfiguration" , func (t * testing.T ) {
9801008 // TODO: https://github.com/kong/kong-operator/issues/184
0 commit comments