@@ -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"
@@ -154,6 +156,11 @@ func (r *RayClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
154
156
logger .Error (err , "Failed to update OAuth ClusterRoleBinding" )
155
157
}
156
158
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
+
157
164
return ctrl.Result {}, nil
158
165
}
159
166
@@ -267,6 +274,41 @@ func desiredOAuthSecret(cluster *rayv1.RayCluster, r *RayClusterReconciler) *cor
267
274
// Create a Kubernetes secret to store the cookie secret
268
275
}
269
276
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
+
270
312
// SetupWithManager sets up the controller with the Manager.
271
313
func (r * RayClusterReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
272
314
r .kubeClient = kubernetes .NewForConfigOrDie (mgr .GetConfig ())
0 commit comments