@@ -392,6 +392,7 @@ func (r *Reconciler) ensureDataPlaneHasNetworkPolicy(
392392 ctx context.Context ,
393393 gateway * gwtypes.Gateway ,
394394 dataplane * operatorv1beta1.DataPlane ,
395+ controlplane * gwtypes.ControlPlane ,
395396) (createdOrUpdate bool , err error ) {
396397 networkPolicies , err := gatewayutils .ListNetworkPoliciesForGateway (ctx , r .Client , gateway )
397398 if err != nil {
@@ -406,8 +407,8 @@ func (r *Reconciler) ensureDataPlaneHasNetworkPolicy(
406407 return false , errors .New ("number of networkPolicies reduced" )
407408 }
408409
409- // generate the network policy that allows the KO pod to access the admin APIs of dataplane pods.
410- generatedPolicy , err := generateDataPlaneNetworkPolicy (r . Namespace , dataplane , r . PodLabels )
410+ // generate the network policy that allows the ControlPlane pod to access the admin APIs of dataplane pods.
411+ generatedPolicy , err := generateDataPlaneNetworkPolicy (dataplane , controlplane )
411412 if err != nil {
412413 return false , fmt .Errorf ("failed generating network policy for DataPlane %s: %w" , dataplane .Name , err )
413414 }
@@ -434,29 +435,20 @@ func (r *Reconciler) ensureDataPlaneHasNetworkPolicy(
434435 return true , r .Create (ctx , generatedPolicy )
435436}
436437
437- // generateDataPlaneNetworkPolicy generates the NetworkPolicy that allows the KO pod to access admin API of dataplane pods.
438- // the params `namespace` and `podLabels` are namespace and labels of the KO pod itself, and `dataplane` is the target dataplane.
438+ // generateDataPlaneNetworkPolicy generates the NetworkPolicy that allows the ControlPlane pod to access admin API of dataplane pods.
439+ // The ControlPlane (KIC) is the component that actually communicates with the DataPlane admin API, not the operator directly.
440+ // In hybrid mode (Konnect), controlplane may be nil since there's no local ControlPlane - in that case,
441+ // the admin API access restriction is omitted.
439442func generateDataPlaneNetworkPolicy (
440- namespace string ,
441443 dataplane * operatorv1beta1.DataPlane ,
442- podLabels map [ string ] string ,
444+ controlplane * gwtypes. ControlPlane ,
443445) (* networkingv1.NetworkPolicy , error ) {
444446 var (
445447 protocolTCP = corev1 .ProtocolTCP
446448 adminAPISSLPort = intstr .FromInt (consts .DataPlaneAdminAPIPort )
447449 proxyPort = intstr .FromInt (consts .DataPlaneProxyPort )
448450 proxySSLPort = intstr .FromInt (consts .DataPlaneProxySSLPort )
449451 metricsPort = intstr .FromInt (consts .DataPlaneMetricsPort )
450- // The label keys to match Kong operator pod.
451- // To not create new NetworkPolicy on upgrade of , we just keep the keys marking the application
452- // and remove the keys related to versions such as `version`,`pod-template-hash`,`helm.sh/chart`.
453- podLabelSelectorKeys = []string {
454- "app" ,
455- "app.kubernetes.io/component" ,
456- "app.kubernetes.io/instance" ,
457- "app.kubernetes.io/name" ,
458- "control-plane" ,
459- }
460452 )
461453
462454 // Check if KONG_PROXY_LISTEN and/or KONG_ADMIN_LISTEN are set in
@@ -490,35 +482,31 @@ func generateDataPlaneNetworkPolicy(
490482 }
491483 }
492484
493- // Construct the policy to allow the KO pod to access DataPlane admin APIs.
494- policyPeerForControllerPod := networkingv1.NetworkPolicyPeer {
495- NamespaceSelector : & metav1.LabelSelector {
496- MatchLabels : map [string ]string {
497- "kubernetes.io/metadata.name" : namespace ,
485+ // Construct the policy to allow the ControlPlane pod to access DataPlane admin APIs.
486+ // For hybrid mode (Konnect), there's no local ControlPlane, so we don't add admin API restrictions.
487+ var limitAdminAPIIngress networkingv1.NetworkPolicyIngressRule
488+ if controlplane != nil {
489+ policyPeerForControlPlanePod := networkingv1.NetworkPolicyPeer {
490+ PodSelector : & metav1.LabelSelector {
491+ MatchLabels : map [string ]string {
492+ "app" : controlplane .Name ,
493+ },
494+ },
495+ NamespaceSelector : & metav1.LabelSelector {
496+ MatchLabels : map [string ]string {
497+ "kubernetes.io/metadata.name" : dataplane .Namespace ,
498+ },
498499 },
499- },
500- }
501-
502- if len (podLabels ) > 0 {
503- matchPodLabels := map [string ]string {}
504- for _ , key := range podLabelSelectorKeys {
505- value , ok := podLabels [key ]
506- if ok {
507- matchPodLabels [key ] = value
508- }
509500 }
510- policyPeerForControllerPod .PodSelector = & metav1.LabelSelector {
511- MatchLabels : matchPodLabels ,
501+ limitAdminAPIIngress = networkingv1.NetworkPolicyIngressRule {
502+ Ports : []networkingv1.NetworkPolicyPort {
503+ {Protocol : & protocolTCP , Port : & adminAPISSLPort },
504+ },
505+ From : []networkingv1.NetworkPolicyPeer {
506+ policyPeerForControlPlanePod ,
507+ },
512508 }
513509 }
514- limitAdminAPIIngress := networkingv1.NetworkPolicyIngressRule {
515- Ports : []networkingv1.NetworkPolicyPort {
516- {Protocol : & protocolTCP , Port : & adminAPISSLPort },
517- },
518- From : []networkingv1.NetworkPolicyPeer {
519- policyPeerForControllerPod ,
520- },
521- }
522510
523511 allowProxyIngress := networkingv1.NetworkPolicyIngressRule {
524512 Ports : []networkingv1.NetworkPolicyPort {
@@ -533,6 +521,15 @@ func generateDataPlaneNetworkPolicy(
533521 },
534522 }
535523
524+ ingressRules := []networkingv1.NetworkPolicyIngressRule {
525+ allowProxyIngress ,
526+ allowMetricsIngress ,
527+ }
528+ // Only add admin API restriction when there's a local ControlPlane (not hybrid mode)
529+ if controlplane != nil {
530+ ingressRules = append ([]networkingv1.NetworkPolicyIngressRule {limitAdminAPIIngress }, ingressRules ... )
531+ }
532+
536533 return & networkingv1.NetworkPolicy {
537534 ObjectMeta : metav1.ObjectMeta {
538535 Namespace : dataplane .Namespace ,
@@ -547,11 +544,7 @@ func generateDataPlaneNetworkPolicy(
547544 PolicyTypes : []networkingv1.PolicyType {
548545 networkingv1 .PolicyTypeIngress ,
549546 },
550- Ingress : []networkingv1.NetworkPolicyIngressRule {
551- limitAdminAPIIngress ,
552- allowProxyIngress ,
553- allowMetricsIngress ,
554- },
547+ Ingress : ingressRules ,
555548 },
556549 }, nil
557550}
0 commit comments