Skip to content

Commit cd5396f

Browse files
committed
add: check for isRayDashboardOAuthEnabledWebhook before applying patch
1 parent 1e3bedc commit cd5396f

File tree

2 files changed

+76
-59
lines changed

2 files changed

+76
-59
lines changed

pkg/controllers/raycluster_webhook.go

+69-59
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
ctrl "sigs.k8s.io/controller-runtime"
2727
logf "sigs.k8s.io/controller-runtime/pkg/log"
2828
"sigs.k8s.io/controller-runtime/pkg/webhook"
29+
30+
"github.com/project-codeflare/codeflare-operator/pkg/config"
2931
)
3032

3133
// log is for logging in this package.
@@ -34,89 +36,97 @@ var rayclusterlog = logf.Log.WithName("raycluster-resource")
3436
func (r *RayClusterDefaulter) SetupWebhookWithManager(mgr ctrl.Manager) error {
3537
return ctrl.NewWebhookManagedBy(mgr).
3638
For(&rayv1.RayCluster{}).
37-
WithDefaulter(&RayClusterDefaulter{}).
39+
WithDefaulter(&RayClusterDefaulter{
40+
Config: r.Config,
41+
rayDashboardOauthEnabled: r.isRayDashboardOAuthEnabledWebhook(),
42+
}).
3843
Complete()
3944
}
4045

4146
//+kubebuilder:webhook:path=/mutate-ray-io-v1-raycluster,mutating=true,failurePolicy=fail,sideEffects=None,groups=ray.io,resources=rayclusters,verbs=create;update,versions=v1,name=mraycluster.kb.io,admissionReviewVersions=v1
4247

43-
type RayClusterDefaulter struct{}
48+
type RayClusterDefaulter struct {
49+
Config *config.KubeRayConfiguration
50+
rayDashboardOauthEnabled bool
51+
}
4452

4553
var _ webhook.CustomDefaulter = &RayClusterDefaulter{}
4654

4755
// Default implements webhook.Defaulter so a webhook will be registered for the type
4856
func (r *RayClusterDefaulter) Default(ctx context.Context, obj runtime.Object) error {
4957
raycluster := obj.(*rayv1.RayCluster)
5058

51-
rayclusterlog.Info("default", "name", raycluster.Name)
52-
// Check and add OAuth proxy if it does not exist.
53-
alreadyExists := false
54-
for _, container := range raycluster.Spec.HeadGroupSpec.Template.Spec.Containers {
55-
if container.Name == "oauth-proxy" {
56-
rayclusterlog.Info("OAuth sidecar already exists, no patch needed")
57-
alreadyExists = true
58-
break // exits the for loop
59+
if r.rayDashboardOauthEnabled {
60+
rayclusterlog.Info("default", "name", raycluster.Name)
61+
// Check and add OAuth proxy if it does not exist.
62+
alreadyExists := false
63+
for _, container := range raycluster.Spec.HeadGroupSpec.Template.Spec.Containers {
64+
if container.Name == "oauth-proxy" {
65+
rayclusterlog.Info("OAuth sidecar already exists, no patch needed")
66+
alreadyExists = true
67+
break // exits the for loop
68+
}
5969
}
60-
}
6170

62-
if !alreadyExists {
63-
rayclusterlog.Info("Adding OAuth sidecar container")
64-
// definition of the new container
65-
newOAuthSidecar := corev1.Container{
66-
Name: "oauth-proxy",
67-
Image: "registry.redhat.io/openshift4/ose-oauth-proxy@sha256:1ea6a01bf3e63cdcf125c6064cbd4a4a270deaf0f157b3eabb78f60556840366",
68-
Ports: []corev1.ContainerPort{
69-
{ContainerPort: 8443, Name: "oauth-proxy"},
70-
},
71-
Args: []string{
72-
"--https-address=:8443",
73-
"--provider=openshift",
74-
"--openshift-service-account=" + raycluster.Name + "-oauth-proxy",
75-
"--upstream=http://localhost:8265",
76-
"--tls-cert=/etc/tls/private/tls.crt",
77-
"--tls-key=/etc/tls/private/tls.key",
78-
"--cookie-secret=$(COOKIE_SECRET)",
79-
"--openshift-delegate-urls={\"/\":{\"resource\":\"pods\",\"namespace\":\"default\",\"verb\":\"get\"}}",
80-
},
81-
Env: []corev1.EnvVar{
82-
{
83-
Name: "COOKIE_SECRET",
84-
ValueFrom: &corev1.EnvVarSource{
85-
SecretKeyRef: &corev1.SecretKeySelector{
86-
LocalObjectReference: corev1.LocalObjectReference{
87-
Name: raycluster.Name + "-oauth-config",
71+
if !alreadyExists {
72+
rayclusterlog.Info("Adding OAuth sidecar container")
73+
// definition of the new container
74+
newOAuthSidecar := corev1.Container{
75+
Name: "oauth-proxy",
76+
Image: "registry.redhat.io/openshift4/ose-oauth-proxy@sha256:1ea6a01bf3e63cdcf125c6064cbd4a4a270deaf0f157b3eabb78f60556840366",
77+
Ports: []corev1.ContainerPort{
78+
{ContainerPort: 8443, Name: "oauth-proxy"},
79+
},
80+
Args: []string{
81+
"--https-address=:8443",
82+
"--provider=openshift",
83+
"--openshift-service-account=" + raycluster.Name + "-oauth-proxy",
84+
"--upstream=http://localhost:8265",
85+
"--tls-cert=/etc/tls/private/tls.crt",
86+
"--tls-key=/etc/tls/private/tls.key",
87+
"--cookie-secret=$(COOKIE_SECRET)",
88+
"--openshift-delegate-urls={\"/\":{\"resource\":\"pods\",\"namespace\":\"default\",\"verb\":\"get\"}}",
89+
},
90+
Env: []corev1.EnvVar{
91+
{
92+
Name: "COOKIE_SECRET",
93+
ValueFrom: &corev1.EnvVarSource{
94+
SecretKeyRef: &corev1.SecretKeySelector{
95+
LocalObjectReference: corev1.LocalObjectReference{
96+
Name: raycluster.Name + "-oauth-config",
97+
},
98+
Key: "cookie_secret",
8899
},
89-
Key: "cookie_secret",
90100
},
91101
},
92102
},
93-
},
94-
VolumeMounts: []corev1.VolumeMount{
95-
{
96-
Name: "proxy-tls-secret",
97-
MountPath: "/etc/tls/private",
98-
ReadOnly: true,
103+
VolumeMounts: []corev1.VolumeMount{
104+
{
105+
Name: "proxy-tls-secret",
106+
MountPath: "/etc/tls/private",
107+
ReadOnly: true,
108+
},
99109
},
100-
},
101-
}
110+
}
102111

103-
// Adding the new OAuth sidecar container
104-
raycluster.Spec.HeadGroupSpec.Template.Spec.Containers = append(raycluster.Spec.HeadGroupSpec.Template.Spec.Containers, newOAuthSidecar)
112+
// Adding the new OAuth sidecar container
113+
raycluster.Spec.HeadGroupSpec.Template.Spec.Containers = append(raycluster.Spec.HeadGroupSpec.Template.Spec.Containers, newOAuthSidecar)
105114

106-
tlsSecretVolume := corev1.Volume{
107-
Name: "proxy-tls-secret",
108-
VolumeSource: corev1.VolumeSource{
109-
Secret: &corev1.SecretVolumeSource{
110-
SecretName: raycluster.Name + "-proxy-tls-secret",
115+
tlsSecretVolume := corev1.Volume{
116+
Name: "proxy-tls-secret",
117+
VolumeSource: corev1.VolumeSource{
118+
Secret: &corev1.SecretVolumeSource{
119+
SecretName: raycluster.Name + "-proxy-tls-secret",
120+
},
111121
},
112-
},
113-
}
122+
}
114123

115-
raycluster.Spec.HeadGroupSpec.Template.Spec.Volumes = append(raycluster.Spec.HeadGroupSpec.Template.Spec.Volumes, tlsSecretVolume)
124+
raycluster.Spec.HeadGroupSpec.Template.Spec.Volumes = append(raycluster.Spec.HeadGroupSpec.Template.Spec.Volumes, tlsSecretVolume)
116125

117-
// Ensure the service account is set
118-
if raycluster.Spec.HeadGroupSpec.Template.Spec.ServiceAccountName == "" {
119-
raycluster.Spec.HeadGroupSpec.Template.Spec.ServiceAccountName = raycluster.Name + "-oauth-proxy"
126+
// Ensure the service account is set
127+
if raycluster.Spec.HeadGroupSpec.Template.Spec.ServiceAccountName == "" {
128+
raycluster.Spec.HeadGroupSpec.Template.Spec.ServiceAccountName = raycluster.Name + "-oauth-proxy"
129+
}
120130
}
121131
}
122132
return nil

pkg/controllers/support.go

+7
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,10 @@ func (r *RayClusterReconciler) isRayDashboardOAuthEnabled() bool {
155155
}
156156
return true
157157
}
158+
159+
func (r *RayClusterDefaulter) isRayDashboardOAuthEnabledWebhook() bool {
160+
if r.Config != nil && r.Config.RayDashboardOAuthEnabled != nil {
161+
return *r.Config.RayDashboardOAuthEnabled
162+
}
163+
return true
164+
}

0 commit comments

Comments
 (0)