@@ -31,7 +31,9 @@ import (
31
31
"k8s.io/apimachinery/pkg/runtime"
32
32
"k8s.io/apimachinery/pkg/util/intstr"
33
33
coreapply "k8s.io/client-go/applyconfigurations/core/v1"
34
+ metav1apply "k8s.io/client-go/applyconfigurations/meta/v1"
34
35
v1 "k8s.io/client-go/applyconfigurations/meta/v1"
36
+ networkingapply "k8s.io/client-go/applyconfigurations/networking/v1"
35
37
rbacapply "k8s.io/client-go/applyconfigurations/rbac/v1"
36
38
"k8s.io/client-go/kubernetes"
37
39
ctrl "sigs.k8s.io/controller-runtime"
@@ -197,6 +199,11 @@ func (r *RayClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
197
199
}
198
200
}
199
201
202
+ _ , err = r .kubeClient .NetworkingV1 ().NetworkPolicies (cluster .Namespace ).Apply (ctx , desiredNetworkPolicy (& cluster ), metav1.ApplyOptions {FieldManager : controllerName , Force : true })
203
+ if err != nil {
204
+ logger .Error (err , "Failed to update NetworkPolicy" )
205
+ }
206
+
200
207
return ctrl.Result {}, nil
201
208
}
202
209
@@ -314,6 +321,41 @@ func desiredOAuthSecret(cluster *rayv1.RayCluster, r *RayClusterReconciler) *cor
314
321
// Create a Kubernetes secret to store the cookie secret
315
322
}
316
323
324
+ func desiredNetworkPolicy (cluster * rayv1.RayCluster ) * networkingapply.NetworkPolicyApplyConfiguration {
325
+
326
+ return networkingapply .NetworkPolicy (cluster .Name , cluster .Namespace ).
327
+ WithLabels (map [string ]string {"ray.io/cluster-name" : cluster .Name }).
328
+ WithSpec (networkingapply .NetworkPolicySpec ().
329
+ WithPodSelector (metav1apply .LabelSelector ().WithMatchLabels (map [string ]string {"ray.io/cluster" : cluster .Name , "ray.io/node-type" : "head" })).
330
+ WithIngress (
331
+ networkingapply .NetworkPolicyIngressRule ().
332
+ WithPorts (
333
+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (6379 )),
334
+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (10001 )),
335
+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (8080 )),
336
+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (8265 )),
337
+ ).WithFrom (
338
+ networkingapply .NetworkPolicyPeer ().WithPodSelector (metav1apply .LabelSelector ()),
339
+ ),
340
+ networkingapply .NetworkPolicyIngressRule ().WithFrom (
341
+ networkingapply .NetworkPolicyPeer ().WithPodSelector (metav1apply .LabelSelector ().
342
+ WithMatchLabels (map [string ]string {"app.kubernetes.io/component" : "kuberay-operator" })).
343
+ WithNamespaceSelector (metav1apply .LabelSelector ().WithMatchLabels (map [string ]string {"opendatahub.io/generated-namespace" : "true" })),
344
+ ).WithPorts (
345
+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (8265 )),
346
+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (10001 )),
347
+ ),
348
+ networkingapply .NetworkPolicyIngressRule ().
349
+ WithPorts (
350
+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (8443 )),
351
+ ),
352
+ ),
353
+ ).
354
+ WithOwnerReferences (
355
+ v1 .OwnerReference ().WithUID (cluster .UID ).WithName (cluster .Name ).WithKind (cluster .Kind ).WithAPIVersion (cluster .APIVersion ),
356
+ )
357
+ }
358
+
317
359
// SetupWithManager sets up the controller with the Manager.
318
360
func (r * RayClusterReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
319
361
r .kubeClient = kubernetes .NewForConfigOrDie (mgr .GetConfig ())
0 commit comments