@@ -31,7 +31,9 @@ import (
3131 "k8s.io/apimachinery/pkg/runtime"
3232 "k8s.io/apimachinery/pkg/util/intstr"
3333 coreapply "k8s.io/client-go/applyconfigurations/core/v1"
34+ metav1apply "k8s.io/client-go/applyconfigurations/meta/v1"
3435 v1 "k8s.io/client-go/applyconfigurations/meta/v1"
36+ networkingapply "k8s.io/client-go/applyconfigurations/networking/v1"
3537 rbacapply "k8s.io/client-go/applyconfigurations/rbac/v1"
3638 "k8s.io/client-go/kubernetes"
3739 ctrl "sigs.k8s.io/controller-runtime"
@@ -154,6 +156,11 @@ func (r *RayClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
154156 logger .Error (err , "Failed to update OAuth ClusterRoleBinding" )
155157 }
156158
159+ _ , err = r .kubeClient .NetworkingV1 ().NetworkPolicies (cluster .Namespace ).Apply (ctx , desiredNetworkPolicy (& cluster ), metav1.ApplyOptions {FieldManager : controllerName , Force : true })
160+ if err != nil {
161+ logger .Error (err , "Failed to update NetworkPolicy" )
162+ }
163+
157164 return ctrl.Result {}, nil
158165}
159166
@@ -267,6 +274,41 @@ func desiredOAuthSecret(cluster *rayv1.RayCluster, r *RayClusterReconciler) *cor
267274 // Create a Kubernetes secret to store the cookie secret
268275}
269276
277+ func desiredNetworkPolicy (cluster * rayv1.RayCluster ) * networkingapply.NetworkPolicyApplyConfiguration {
278+
279+ return networkingapply .NetworkPolicy (cluster .Name , cluster .Namespace ).
280+ WithLabels (map [string ]string {"ray.io/cluster-name" : cluster .Name }).
281+ WithSpec (networkingapply .NetworkPolicySpec ().
282+ WithPodSelector (metav1apply .LabelSelector ().WithMatchLabels (map [string ]string {"ray.io/cluster" : cluster .Name , "ray.io/node-type" : "head" })).
283+ WithIngress (
284+ networkingapply .NetworkPolicyIngressRule ().
285+ WithPorts (
286+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (6379 )),
287+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (10001 )),
288+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (8080 )),
289+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (8265 )),
290+ ).WithFrom (
291+ networkingapply .NetworkPolicyPeer ().WithPodSelector (metav1apply .LabelSelector ()),
292+ ),
293+ networkingapply .NetworkPolicyIngressRule ().WithFrom (
294+ networkingapply .NetworkPolicyPeer ().WithPodSelector (metav1apply .LabelSelector ().
295+ WithMatchLabels (map [string ]string {"app.kubernetes.io/component" : "kuberay-operator" })).
296+ WithNamespaceSelector (metav1apply .LabelSelector ().WithMatchLabels (map [string ]string {"opendatahub.io/generated-namespace" : "'true'" })),
297+ ).WithPorts (
298+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (8265 )),
299+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (10001 )),
300+ ),
301+ networkingapply .NetworkPolicyIngressRule ().
302+ WithPorts (
303+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (8443 )),
304+ ),
305+ ),
306+ ).
307+ WithOwnerReferences (
308+ v1 .OwnerReference ().WithUID (cluster .UID ).WithName (cluster .Name ).WithKind (cluster .Kind ).WithAPIVersion (cluster .APIVersion ),
309+ )
310+ }
311+
270312// SetupWithManager sets up the controller with the Manager.
271313func (r * RayClusterReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
272314 r .kubeClient = kubernetes .NewForConfigOrDie (mgr .GetConfig ())
0 commit comments