Skip to content

Commit

Permalink
keepin it DRY with a helper function 4 owner ref
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin <[email protected]>
  • Loading branch information
KPostOffice committed Aug 6, 2024
1 parent ca81c46 commit 23b8b3c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 39 deletions.
31 changes: 7 additions & 24 deletions pkg/controllers/raycluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,7 @@ func desiredServiceAccount(cluster *rayv1.RayCluster) *corev1ac.ServiceAccountAp
`{"kind":"OAuthRedirectReference","apiVersion":"v1",` +
`"reference":{"kind":"Route","name":"` + dashboardNameFromCluster(cluster) + `"}}`,
}).
WithOwnerReferences(
metav1ac.OwnerReference().WithUID(cluster.UID).WithName(cluster.Name).WithKind(cluster.Kind).WithAPIVersion(cluster.APIVersion).WithController(true),
)
WithOwnerReferences(OwnerRefFromCluster(cluster))
}

func dashboardNameFromCluster(cluster *rayv1.RayCluster) string {
Expand All @@ -361,9 +359,7 @@ func desiredClusterRoute(cluster *rayv1.RayCluster) *routev1ac.RouteApplyConfigu
WithTermination(routev1.TLSTerminationReencrypt),
),
).
WithOwnerReferences(
metav1ac.OwnerReference().WithUID(cluster.UID).WithName(cluster.Name).WithKind(cluster.Kind).WithAPIVersion(cluster.APIVersion).WithController(true),
)
WithOwnerReferences(OwnerRefFromCluster(cluster))
}

func oauthServiceNameFromCluster(cluster *rayv1.RayCluster) string {
Expand All @@ -389,9 +385,7 @@ func desiredOAuthService(cluster *rayv1.RayCluster) *corev1ac.ServiceApplyConfig
).
WithSelector(map[string]string{"ray.io/cluster": cluster.Name, "ray.io/node-type": "head"}),
).
WithOwnerReferences(
metav1ac.OwnerReference().WithUID(cluster.UID).WithName(cluster.Name).WithKind(cluster.Kind).WithAPIVersion(cluster.APIVersion).WithController(true),
)
WithOwnerReferences(OwnerRefFromCluster(cluster))
}

func oauthSecretNameFromCluster(cluster *rayv1.RayCluster) string {
Expand All @@ -408,9 +402,7 @@ func desiredOAuthSecret(cluster *rayv1.RayCluster, cookieSalt string) *corev1ac.
return corev1ac.Secret(oauthSecretNameFromCluster(cluster), cluster.Namespace).
WithLabels(map[string]string{RayClusterNameLabel: cluster.Name}).
WithStringData(map[string]string{"cookie_secret": cookieSecret}).
WithOwnerReferences(
metav1ac.OwnerReference().WithUID(cluster.UID).WithName(cluster.Name).WithKind(cluster.Kind).WithAPIVersion(cluster.APIVersion).WithController(true),
)
WithOwnerReferences(OwnerRefFromCluster(cluster))
}

func caSecretNameFromCluster(cluster *rayv1.RayCluster) string {
Expand All @@ -424,12 +416,7 @@ func desiredCASecret(cluster *rayv1.RayCluster, key, cert []byte) *corev1ac.Secr
CAPrivateKeyKey: key,
CACertKey: cert,
}).
WithOwnerReferences(metav1ac.OwnerReference().
WithUID(cluster.UID).
WithName(cluster.Name).
WithKind(cluster.Kind).
WithAPIVersion(cluster.APIVersion).
WithController(true))
WithOwnerReferences(OwnerRefFromCluster(cluster))
}

func generateCACertificate() ([]byte, []byte, error) {
Expand Down Expand Up @@ -487,9 +474,7 @@ func desiredWorkersNetworkPolicy(cluster *rayv1.RayCluster) *networkingv1ac.Netw
),
),
).
WithOwnerReferences(
metav1ac.OwnerReference().WithUID(cluster.UID).WithName(cluster.Name).WithKind(cluster.Kind).WithAPIVersion(cluster.APIVersion).WithController(true),
)
WithOwnerReferences(OwnerRefFromCluster(cluster))
}

func desiredHeadNetworkPolicy(cluster *rayv1.RayCluster, cfg *config.KubeRayConfiguration, kubeRayNamespaces []string) *networkingv1ac.NetworkPolicyApplyConfiguration {
Expand Down Expand Up @@ -545,9 +530,7 @@ func desiredHeadNetworkPolicy(cluster *rayv1.RayCluster, cfg *config.KubeRayConf
),
),
).
WithOwnerReferences(
metav1ac.OwnerReference().WithUID(cluster.UID).WithName(cluster.Name).WithKind(cluster.Kind).WithAPIVersion(cluster.APIVersion).WithController(true),
)
WithOwnerReferences(OwnerRefFromCluster(cluster))
}

func (r *RayClusterReconciler) deleteHeadPodIfMissingImagePullSecrets(ctx context.Context, cluster *rayv1.RayCluster) error {
Expand Down
27 changes: 12 additions & 15 deletions pkg/controllers/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ func desiredRayClientRoute(cluster *rayv1.RayCluster) *routeapply.RouteApplyConf
WithPort(routeapply.RoutePort().WithTargetPort(intstr.FromString("client"))).
WithTLS(routeapply.TLSConfig().WithTermination("passthrough")),
).
WithOwnerReferences(
v1.OwnerReference().WithUID(cluster.UID).WithName(cluster.Name).WithKind(cluster.Kind).WithAPIVersion(cluster.APIVersion).WithController(true),
)
WithOwnerReferences(OwnerRefFromCluster(cluster))
}

func desiredRayClientIngress(cluster *rayv1.RayCluster, ingressHost string) *networkingv1ac.IngressApplyConfiguration {
Expand All @@ -55,12 +53,7 @@ func desiredRayClientIngress(cluster *rayv1.RayCluster, ingressHost string) *net
"nginx.ingress.kubernetes.io/ssl-redirect": "true",
"nginx.ingress.kubernetes.io/ssl-passthrough": "true",
}).
WithOwnerReferences(v1.OwnerReference().
WithAPIVersion(cluster.APIVersion).
WithKind(cluster.Kind).
WithName(cluster.Name).
WithUID(cluster.UID).
WithController(true)).
WithOwnerReferences(OwnerRefFromCluster(cluster)).
WithSpec(networkingv1ac.IngressSpec().
WithIngressClassName("nginx").
WithRules(networkingv1ac.IngressRule().
Expand All @@ -86,12 +79,7 @@ func desiredRayClientIngress(cluster *rayv1.RayCluster, ingressHost string) *net
func desiredClusterIngress(cluster *rayv1.RayCluster, ingressHost string) *networkingv1ac.IngressApplyConfiguration {
return networkingv1ac.Ingress(dashboardNameFromCluster(cluster), cluster.Namespace).
WithLabels(map[string]string{RayClusterNameLabel: cluster.Name}).
WithOwnerReferences(v1.OwnerReference().
WithAPIVersion(cluster.APIVersion).
WithKind(cluster.Kind).
WithName(cluster.Name).
WithUID(cluster.UID).
WithController(true)).
WithOwnerReferences(OwnerRefFromCluster(cluster)).
WithSpec(networkingv1ac.IngressSpec().
WithRules(networkingv1ac.IngressRule().
WithHost(ingressHost). // Full Hostname
Expand Down Expand Up @@ -212,3 +200,12 @@ func (l logSink) WithName(name string) logr.LogSink {
func FilteredLogger(logger logr.Logger) logr.Logger {
return logger.WithSink(logSink{logger.GetSink()})
}

func OwnerRefFromCluster(cluster *rayv1.RayCluster) *v1.OwnerReferenceApplyConfiguration {
return v1.OwnerReference().
WithAPIVersion(cluster.APIVersion).
WithKind(cluster.Kind).
WithName(cluster.Name).
WithUID(cluster.UID).
WithController(true)
}

0 comments on commit 23b8b3c

Please sign in to comment.