@@ -32,7 +32,9 @@ import (
32
32
"k8s.io/apimachinery/pkg/runtime"
33
33
"k8s.io/apimachinery/pkg/util/intstr"
34
34
coreapply "k8s.io/client-go/applyconfigurations/core/v1"
35
+ metav1apply "k8s.io/client-go/applyconfigurations/meta/v1"
35
36
v1 "k8s.io/client-go/applyconfigurations/meta/v1"
37
+ networkingapply "k8s.io/client-go/applyconfigurations/networking/v1"
36
38
rbacapply "k8s.io/client-go/applyconfigurations/rbac/v1"
37
39
"k8s.io/client-go/kubernetes"
38
40
ctrl "sigs.k8s.io/controller-runtime"
@@ -205,6 +207,11 @@ func (r *RayClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
205
207
}
206
208
}
207
209
210
+ _ , err = r .kubeClient .NetworkingV1 ().NetworkPolicies (cluster .Namespace ).Apply (ctx , desiredNetworkPolicy (& cluster ), metav1.ApplyOptions {FieldManager : controllerName , Force : true })
211
+ if err != nil {
212
+ logger .Error (err , "Failed to update NetworkPolicy" )
213
+ }
214
+
208
215
return ctrl.Result {}, nil
209
216
}
210
217
@@ -337,6 +344,41 @@ func desiredOAuthSecret(cluster *rayv1.RayCluster, r *RayClusterReconciler) *cor
337
344
// Create a Kubernetes secret to store the cookie secret
338
345
}
339
346
347
+ func desiredNetworkPolicy (cluster * rayv1.RayCluster ) * networkingapply.NetworkPolicyApplyConfiguration {
348
+
349
+ return networkingapply .NetworkPolicy (cluster .Name , cluster .Namespace ).
350
+ WithLabels (map [string ]string {"ray.io/cluster-name" : cluster .Name }).
351
+ WithSpec (networkingapply .NetworkPolicySpec ().
352
+ WithPodSelector (metav1apply .LabelSelector ().WithMatchLabels (map [string ]string {"ray.io/cluster" : cluster .Name , "ray.io/node-type" : "head" })).
353
+ WithIngress (
354
+ networkingapply .NetworkPolicyIngressRule ().
355
+ WithPorts (
356
+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (6379 )),
357
+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (10001 )),
358
+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (8080 )),
359
+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (8265 )),
360
+ ).WithFrom (
361
+ networkingapply .NetworkPolicyPeer ().WithPodSelector (metav1apply .LabelSelector ()),
362
+ ),
363
+ networkingapply .NetworkPolicyIngressRule ().WithFrom (
364
+ networkingapply .NetworkPolicyPeer ().WithPodSelector (metav1apply .LabelSelector ().
365
+ WithMatchLabels (map [string ]string {"app.kubernetes.io/component" : "kuberay-operator" })).
366
+ WithNamespaceSelector (metav1apply .LabelSelector ().WithMatchLabels (map [string ]string {"opendatahub.io/generated-namespace" : "true" })),
367
+ ).WithPorts (
368
+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (8265 )),
369
+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (10001 )),
370
+ ),
371
+ networkingapply .NetworkPolicyIngressRule ().
372
+ WithPorts (
373
+ networkingapply .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (8443 )),
374
+ ),
375
+ ),
376
+ ).
377
+ WithOwnerReferences (
378
+ v1 .OwnerReference ().WithUID (cluster .UID ).WithName (cluster .Name ).WithKind (cluster .Kind ).WithAPIVersion (cluster .APIVersion ),
379
+ )
380
+ }
381
+
340
382
// SetupWithManager sets up the controller with the Manager.
341
383
func (r * RayClusterReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
342
384
r .kubeClient = kubernetes .NewForConfigOrDie (mgr .GetConfig ())
0 commit comments