Skip to content

Commit 950d7a6

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

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed

go.mod

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ require (
66
github.com/onsi/ginkgo/v2 v2.11.0
77
github.com/onsi/gomega v1.27.10
88
github.com/openshift/api v0.0.0-20230213134911-7ba313770556
9-
github.com/openshift/client-go v0.0.0-20221019143426-16aed247da5c
10-
github.com/project-codeflare/codeflare-common v0.0.0-20240207083912-d7a229270a0a
11-
github.com/project-codeflare/instascale v0.4.0
12-
github.com/project-codeflare/multi-cluster-app-dispatcher v1.40.0
9+
github.com/project-codeflare/instascale v0.0.6
10+
github.com/project-codeflare/multi-cluster-app-dispatcher v1.33.0
1311
github.com/ray-project/kuberay/ray-operator v1.0.0
1412
go.uber.org/zap v1.26.0
1513
k8s.io/api v0.27.8
@@ -25,6 +23,12 @@ require (
2523
replace sigs.k8s.io/custom-metrics-apiserver => sigs.k8s.io/custom-metrics-apiserver v1.25.1-0.20230306170449-63d8c93851f3
2624

2725
require (
26+
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
27+
github.com/Azure/go-autorest/autorest v0.11.20 // indirect
28+
github.com/Azure/go-autorest/autorest/adal v0.9.13 // indirect
29+
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
30+
github.com/Azure/go-autorest/logger v0.2.1 // indirect
31+
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
2832
github.com/NYTimes/gziphandler v1.1.1 // indirect
2933
github.com/antlr/antlr4/runtime/Go/antlr v1.4.10 // indirect
3034
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a // indirect
@@ -41,6 +45,7 @@ require (
4145
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
4246
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
4347
github.com/felixge/httpsnoop v1.0.3 // indirect
48+
github.com/form3tech-oss/jwt-go v3.2.3+incompatible // indirect
4449
github.com/fsnotify/fsnotify v1.6.0 // indirect
4550
github.com/go-logr/logr v1.2.4 // indirect
4651
github.com/go-logr/stdr v1.2.2 // indirect

go.sum

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
3434
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
3535
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
3636
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
37+
github.com/Azure/go-autorest v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs=
38+
github.com/Azure/go-autorest v14.2.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
39+
github.com/Azure/go-autorest/autorest v0.11.20 h1:s8H1PbCZSqg/DH7JMlOz6YMig6htWLNPsjDdlLqCx3M=
40+
github.com/Azure/go-autorest/autorest v0.11.20/go.mod h1:o3tqFY+QR40VOlk+pV4d77mORO64jOXSgEnPQgLK6JY=
41+
github.com/Azure/go-autorest/autorest/adal v0.9.13 h1:Mp5hbtOePIzM8pJVRa3YLrWWmZtoxRXqUEzCfJt3+/Q=
42+
github.com/Azure/go-autorest/autorest/adal v0.9.13/go.mod h1:W/MM4U6nLxnIskrw4UwWzlHfGjwUS50aOsc/I3yuU8M=
43+
github.com/Azure/go-autorest/autorest/date v0.3.0 h1:7gUk1U5M/CQbp9WoqinNzJar+8KY+LPI6wiWrP/myHw=
44+
github.com/Azure/go-autorest/autorest/date v0.3.0/go.mod h1:BI0uouVdmngYNUzGWeSYnokU+TrmwEsOqdt8Y6sso74=
45+
github.com/Azure/go-autorest/autorest/mocks v0.4.1/go.mod h1:LTp+uSrOhSkaKrUy935gNZuuIPPVsHlr9DSOxSayd+k=
46+
github.com/Azure/go-autorest/logger v0.2.1 h1:IG7i4p/mDa2Ce4TRyAO8IHnVhAVF3RFU+ZtXWSmf4Tg=
47+
github.com/Azure/go-autorest/logger v0.2.1/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZmbF5NWuPV8+WeEW8=
48+
github.com/Azure/go-autorest/tracing v0.6.0 h1:TYi4+3m5t6K48TGI9AUdb+IzbnSxvnvUMfuitfgcfuo=
49+
github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
3750
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
3851
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3952
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
@@ -111,6 +124,9 @@ github.com/evanphx/json-patch/v5 v5.6.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2Vvl
111124
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
112125
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
113126
github.com/flowstack/go-jsonschema v0.1.1/go.mod h1:yL7fNggx1o8rm9RlgXv7hTBWxdBM0rVwpMwimd3F3N0=
127+
github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
128+
github.com/form3tech-oss/jwt-go v3.2.3+incompatible h1:7ZaBxOI7TMoYBfyA3cQHErNNyAWIKUMIwqxEtgHOs5c=
129+
github.com/form3tech-oss/jwt-go v3.2.3+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
114130
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
115131
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
116132
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
@@ -379,6 +395,7 @@ github.com/openshift-online/ocm-sdk-go v0.1.368 h1:qP+gkChV8WDwwpkUw1xUyjTXKdvrw
379395
github.com/openshift-online/ocm-sdk-go v0.1.368/go.mod h1:KYOw8kAKAHyPrJcQoVR82CneQ4ofC02Na4cXXaTq4Nw=
380396
github.com/openshift/api v0.0.0-20230213134911-7ba313770556 h1:7W2fOhJicyEff24VaF7ASNzPtYvr+iSCVft4SIBAzaE=
381397
github.com/openshift/api v0.0.0-20230213134911-7ba313770556/go.mod h1:aQ6LDasvHMvHZXqLHnX2GRmnfTWCF/iIwz8EMTTIE9A=
398+
github.com/openshift/client-go v0.0.0-20211209144617-7385dd6338e3 h1:SG1aqwleU6bGD0X4mhkTNupjVnByMYYuW4XbnCPavQU=
382399
github.com/openshift/client-go v0.0.0-20221019143426-16aed247da5c h1:CV76yFOTXmq9VciBR3Bve5ZWzSxdft7gaMVB3kS0rwg=
383400
github.com/openshift/client-go v0.0.0-20221019143426-16aed247da5c/go.mod h1:lFMO8mLHXWFzSdYvGNo8ivF9SfF6zInA8ZGw4phRnUE=
384401
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -389,8 +406,12 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
389406
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
390407
github.com/project-codeflare/codeflare-common v0.0.0-20240207083912-d7a229270a0a h1:Yk9J5qXjp+yfSRCzS0EElrhpTgfYJ+S+W/z84cmlmX4=
391408
github.com/project-codeflare/codeflare-common v0.0.0-20240207083912-d7a229270a0a/go.mod h1:2Ck9LC+6Xi4jTDSlCJoP00tCzSrxek0roLsjvUgL2gY=
409+
github.com/project-codeflare/instascale v0.0.6 h1:tMh9d9PyPfyrXtLWBFKugoTpEAiCPWZyMvINFGE/yhw=
410+
github.com/project-codeflare/instascale v0.0.6/go.mod h1:P5QrOMPtKpl4VlkC6bV8yUytt24QnU4leLseyHNeHDI=
392411
github.com/project-codeflare/instascale v0.4.0 h1:l/cb+x4FrJ2bN9wXjv1mCngy77tVw0CLMiqJovTAflo=
393412
github.com/project-codeflare/instascale v0.4.0/go.mod h1:CpduFXKeuqYW4Ph1CPOJV6dpAdpebOxhbU4CmccZWSo=
413+
github.com/project-codeflare/multi-cluster-app-dispatcher v1.33.0 h1:6a+MnxcFSlheC7RIPGg3s/QCt5+7dD8mJKwdpST7i70=
414+
github.com/project-codeflare/multi-cluster-app-dispatcher v1.33.0/go.mod h1:0J0BDSaIN5lvlmgw+32FcMqe8SflXHtHByUbHmPl4w8=
394415
github.com/project-codeflare/multi-cluster-app-dispatcher v1.40.0 h1:IkTmd/W/zxcsC5s4EbnW74PFpkQVEiTc/8rWWwFw0Ok=
395416
github.com/project-codeflare/multi-cluster-app-dispatcher v1.40.0/go.mod h1:XCZKkq8Mz2WySbV3NfVINNciy+as7Rq9Xs2megNFbdk=
396417
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
@@ -534,6 +555,7 @@ golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8U
534555
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
535556
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
536557
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
558+
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
537559
golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
538560
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
539561
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=

pkg/controllers/raycluster_controller.go

Lines changed: 51 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"
@@ -154,6 +156,11 @@ func (r *RayClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
154156
logger.Error(err, "Failed to update OAuth ClusterRoleBinding")
155157
}
156158

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+
157164
return ctrl.Result{}, nil
158165
}
159166

@@ -267,6 +274,50 @@ func desiredOAuthSecret(cluster *rayv1.RayCluster, r *RayClusterReconciler) *cor
267274
// Create a Kubernetes secret to store the cookie secret
268275
}
269276

277+
func getDSCInitializationResource(r *RayClusterReconciler) *odhv1.DSCInitialization {
278+
return &odhv1.DataScienceCluster{
279+
ObjectMeta: metav1.ObjectMeta{
280+
Name: "ray-operator",
281+
Namespace: "opendatahub",
282+
},
283+
}
284+
}
285+
286+
func desiredNetworkPolicy(cluster *rayv1.RayCluster) *networkingapply.NetworkPolicyApplyConfiguration {
287+
288+
return networkingapply.NetworkPolicy(cluster.Name, cluster.Namespace).
289+
WithLabels(map[string]string{"ray.io/cluster-name": cluster.Name}).
290+
WithSpec(networkingapply.NetworkPolicySpec().
291+
WithPodSelector(metav1apply.LabelSelector().WithMatchLabels(map[string]string{"ray.io/cluster": cluster.Name, "ray.io/node-type": "head"})).
292+
WithIngress(
293+
networkingapply.NetworkPolicyIngressRule().
294+
WithPorts(
295+
networkingapply.NetworkPolicyPort().WithProtocol(corev1.ProtocolTCP).WithPort(intstr.FromInt(6379)),
296+
networkingapply.NetworkPolicyPort().WithProtocol(corev1.ProtocolTCP).WithPort(intstr.FromInt(10001)),
297+
networkingapply.NetworkPolicyPort().WithProtocol(corev1.ProtocolTCP).WithPort(intstr.FromInt(8080)),
298+
networkingapply.NetworkPolicyPort().WithProtocol(corev1.ProtocolTCP).WithPort(intstr.FromInt(8265)),
299+
).WithFrom(
300+
networkingapply.NetworkPolicyPeer().WithPodSelector(metav1apply.LabelSelector()),
301+
),
302+
networkingapply.NetworkPolicyIngressRule().WithFrom(
303+
networkingapply.NetworkPolicyPeer().WithPodSelector(metav1apply.LabelSelector().
304+
WithMatchLabels(map[string]string{"app.kubernetes.io/component": "kuberay-operator"})).
305+
WithNamespaceSelector(metav1apply.LabelSelector().WithMatchLabels(map[string]string{"opendatahub.io/generated-namespace": "true"})),
306+
).WithPorts(
307+
networkingapply.NetworkPolicyPort().WithProtocol(corev1.ProtocolTCP).WithPort(intstr.FromInt(8265)),
308+
networkingapply.NetworkPolicyPort().WithProtocol(corev1.ProtocolTCP).WithPort(intstr.FromInt(10001)),
309+
),
310+
networkingapply.NetworkPolicyIngressRule().
311+
WithPorts(
312+
networkingapply.NetworkPolicyPort().WithProtocol(corev1.ProtocolTCP).WithPort(intstr.FromInt(8443)),
313+
),
314+
),
315+
).
316+
WithOwnerReferences(
317+
v1.OwnerReference().WithUID(cluster.UID).WithName(cluster.Name).WithKind(cluster.Kind).WithAPIVersion(cluster.APIVersion),
318+
)
319+
}
320+
270321
// SetupWithManager sets up the controller with the Manager.
271322
func (r *RayClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
272323
r.kubeClient = kubernetes.NewForConfigOrDie(mgr.GetConfig())

0 commit comments

Comments
 (0)