Skip to content

Commit b43f479

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

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
@@ -32,7 +32,9 @@ import (
3232
"k8s.io/apimachinery/pkg/runtime"
3333
"k8s.io/apimachinery/pkg/util/intstr"
3434
coreapply "k8s.io/client-go/applyconfigurations/core/v1"
35+
metav1apply "k8s.io/client-go/applyconfigurations/meta/v1"
3536
v1 "k8s.io/client-go/applyconfigurations/meta/v1"
37+
networkingapply "k8s.io/client-go/applyconfigurations/networking/v1"
3638
rbacapply "k8s.io/client-go/applyconfigurations/rbac/v1"
3739
"k8s.io/client-go/kubernetes"
3840
ctrl "sigs.k8s.io/controller-runtime"
@@ -205,6 +207,11 @@ func (r *RayClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
205207
}
206208
}
207209

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+
208215
return ctrl.Result{}, nil
209216
}
210217

@@ -337,6 +344,41 @@ func desiredOAuthSecret(cluster *rayv1.RayCluster, r *RayClusterReconciler) *cor
337344
// Create a Kubernetes secret to store the cookie secret
338345
}
339346

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

0 commit comments

Comments
 (0)