Skip to content

Commit 12b72cb

Browse files
Altered network policy to allow all trafic for head & worker pods (project-codeflare#544)
* Altered network policy to allow all trafic for head & worker pods * Added Worker Network Policy * Review changes * Re-added client port to NWP * Added Dashboard port & renaming function * Added empty label selector * Update pkg/controllers/raycluster_controller.go Co-authored-by: Antonin Stefanutti <astefanutti@users.noreply.github.com> --------- Co-authored-by: Antonin Stefanutti <astefanutti@users.noreply.github.com>
1 parent 1e76157 commit 12b72cb

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

pkg/controllers/raycluster_controller.go

+28-6
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,12 @@ func (r *RayClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
262262
kubeRayNamespaces = []string{dsci.Spec.ApplicationsNamespace}
263263
}
264264

265-
_, err = r.kubeClient.NetworkingV1().NetworkPolicies(cluster.Namespace).Apply(ctx, desiredNetworkPolicy(cluster, r.Config, kubeRayNamespaces), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
265+
_, err = r.kubeClient.NetworkingV1().NetworkPolicies(cluster.Namespace).Apply(ctx, desiredHeadNetworkPolicy(cluster, r.Config, kubeRayNamespaces), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
266+
if err != nil {
267+
logger.Error(err, "Failed to update NetworkPolicy")
268+
}
269+
270+
_, err = r.kubeClient.NetworkingV1().NetworkPolicies(cluster.Namespace).Apply(ctx, desiredWorkersNetworkPolicy(cluster), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
266271
if err != nil {
267272
logger.Error(err, "Failed to update NetworkPolicy")
268273
}
@@ -459,24 +464,41 @@ func generateCACertificate() ([]byte, []byte, error) {
459464

460465
return privateKeyPem, certPem, nil
461466
}
462-
463-
func desiredNetworkPolicy(cluster *rayv1.RayCluster, cfg *config.KubeRayConfiguration, kubeRayNamespaces []string) *networkingv1ac.NetworkPolicyApplyConfiguration {
467+
func desiredWorkersNetworkPolicy(cluster *rayv1.RayCluster) *networkingv1ac.NetworkPolicyApplyConfiguration {
468+
return networkingv1ac.NetworkPolicy(cluster.Name+"-workers", cluster.Namespace).
469+
WithLabels(map[string]string{"ray.io/cluster-name": cluster.Name}).
470+
WithSpec(networkingv1ac.NetworkPolicySpec().
471+
WithPodSelector(metav1ac.LabelSelector().WithMatchLabels(map[string]string{"ray.io/cluster": cluster.Name, "ray.io/node-type": "worker"})).
472+
WithIngress(
473+
networkingv1ac.NetworkPolicyIngressRule().
474+
WithFrom(
475+
networkingv1ac.NetworkPolicyPeer().WithPodSelector(metav1ac.LabelSelector().WithMatchLabels(map[string]string{"ray.io/cluster": cluster.Name})),
476+
),
477+
),
478+
).
479+
WithOwnerReferences(
480+
metav1ac.OwnerReference().WithUID(cluster.UID).WithName(cluster.Name).WithKind(cluster.Kind).WithAPIVersion(cluster.APIVersion),
481+
)
482+
}
483+
func desiredHeadNetworkPolicy(cluster *rayv1.RayCluster, cfg *config.KubeRayConfiguration, kubeRayNamespaces []string) *networkingv1ac.NetworkPolicyApplyConfiguration {
464484
allSecuredPorts := []*networkingv1ac.NetworkPolicyPortApplyConfiguration{
465485
networkingv1ac.NetworkPolicyPort().WithProtocol(corev1.ProtocolTCP).WithPort(intstr.FromInt(8443)),
466486
}
467487
if ptr.Deref(cfg.MTLSEnabled, true) {
468488
allSecuredPorts = append(allSecuredPorts, networkingv1ac.NetworkPolicyPort().WithProtocol(corev1.ProtocolTCP).WithPort(intstr.FromInt(10001)))
469489
}
470-
return networkingv1ac.NetworkPolicy(cluster.Name, cluster.Namespace).
490+
return networkingv1ac.NetworkPolicy(cluster.Name+"-head", cluster.Namespace).
471491
WithLabels(map[string]string{"ray.io/cluster-name": cluster.Name}).
472492
WithSpec(networkingv1ac.NetworkPolicySpec().
473493
WithPodSelector(metav1ac.LabelSelector().WithMatchLabels(map[string]string{"ray.io/cluster": cluster.Name, "ray.io/node-type": "head"})).
474494
WithIngress(
495+
networkingv1ac.NetworkPolicyIngressRule().
496+
WithFrom(
497+
networkingv1ac.NetworkPolicyPeer().WithPodSelector(metav1ac.LabelSelector().WithMatchLabels(map[string]string{"ray.io/cluster": cluster.Name})),
498+
),
475499
networkingv1ac.NetworkPolicyIngressRule().
476500
WithPorts(
477-
networkingv1ac.NetworkPolicyPort().WithProtocol(corev1.ProtocolTCP).WithPort(intstr.FromInt(6379)),
478501
networkingv1ac.NetworkPolicyPort().WithProtocol(corev1.ProtocolTCP).WithPort(intstr.FromInt(10001)),
479-
networkingv1ac.NetworkPolicyPort().WithProtocol(corev1.ProtocolTCP).WithPort(intstr.FromInt(8080)),
480502
networkingv1ac.NetworkPolicyPort().WithProtocol(corev1.ProtocolTCP).WithPort(intstr.FromInt(8265)),
481503
).WithFrom(
482504
networkingv1ac.NetworkPolicyPeer().WithPodSelector(metav1ac.LabelSelector()),

0 commit comments

Comments
 (0)