Skip to content

Commit e0e8bc7

Browse files
committed
add additional function which creates network policy
Signed-off-by: Kevin <kpostlet@redhat.com>
1 parent faff28a commit e0e8bc7

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

pkg/controllers/raycluster_controller.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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"
@@ -197,6 +199,11 @@ func (r *RayClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
197199
}
198200
}
199201

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+
200207
return ctrl.Result{}, nil
201208
}
202209

@@ -314,6 +321,41 @@ func desiredOAuthSecret(cluster *rayv1.RayCluster, r *RayClusterReconciler) *cor
314321
// Create a Kubernetes secret to store the cookie secret
315322
}
316323

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+
317359
// SetupWithManager sets up the controller with the Manager.
318360
func (r *RayClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
319361
r.kubeClient = kubernetes.NewForConfigOrDie(mgr.GetConfig())

0 commit comments

Comments
 (0)