Skip to content

[Feature] More configability for ingresses #3871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions ray-operator/apis/config/v1alpha1/configuration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1alpha1

import (
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/manager"

Expand Down Expand Up @@ -73,6 +74,17 @@ type Configuration struct {

// EnableMetrics indicates whether KubeRay operator should emit control plane metrics.
EnableMetrics bool `json:"enableMetrics,omitempty"`

// Host used for Ray Dashboard ingresses. The host will be the same for all `RayClusters` and they
// will be differentiated by their paths.
IngressHost string `json:"ingressHost,omitempty"`

// TLS configuration for the Ray Dashboard ingresses. Applies to all `RayClusters`.
IngressTLS []networkingv1.IngressTLS `json:"ingressTLS,omitempty"`

// Default annotations for the Ray Dashboard ingresses. Annotations on the `RayCluster` will override
// these on a case-by-case basis.
IngressAnnotations map[string]string `json:"ingressAnnotations,omitempty"`
}

func (config Configuration) GetDashboardClient(mgr manager.Manager) func() utils.RayDashboardClientInterface {
Expand Down
15 changes: 15 additions & 0 deletions ray-operator/apis/config/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 19 additions & 6 deletions ray-operator/controllers/ray/common/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const IngressClassAnnotationKey = "kubernetes.io/ingress.class"

// BuildIngressForHeadService Builds the ingress for head service dashboard.
// This is used to expose dashboard for external traffic.
func BuildIngressForHeadService(ctx context.Context, cluster rayv1.RayCluster) (*networkingv1.Ingress, error) {
func BuildIngressForHeadService(ctx context.Context, cluster rayv1.RayCluster, host string, ingressTLS []networkingv1.IngressTLS, defaultAnnotations map[string]string) (*networkingv1.Ingress, error) {
log := ctrl.LoggerFrom(ctx)

labels := map[string]string{
Expand All @@ -32,10 +32,16 @@ func BuildIngressForHeadService(ctx context.Context, cluster rayv1.RayCluster) (
excludeSet := map[string]struct{}{
IngressClassAnnotationKey: {},
}
annotation := map[string]string{}
annotations := map[string]string{}
for key, value := range defaultAnnotations {
if _, ok := excludeSet[key]; !ok {
annotations[key] = value
}
}
// cluster.Annotations takes precedence, so we add these after defaultAnnotations.
for key, value := range cluster.Annotations {
if _, ok := excludeSet[key]; !ok {
annotation[key] = value
annotations[key] = value
}
}

Expand Down Expand Up @@ -70,11 +76,13 @@ func BuildIngressForHeadService(ctx context.Context, cluster rayv1.RayCluster) (
Name: utils.GenerateIngressName(cluster.Name),
Namespace: cluster.Namespace,
Labels: labels,
Annotations: annotation,
Annotations: annotations,
},
Spec: networkingv1.IngressSpec{
TLS: ingressTLS,
Rules: []networkingv1.IngressRule{
{
Host: host,
IngressRuleValue: networkingv1.IngressRuleValue{
HTTP: &networkingv1.HTTPIngressRuleValue{
Paths: paths,
Expand All @@ -85,8 +93,13 @@ func BuildIngressForHeadService(ctx context.Context, cluster rayv1.RayCluster) (
},
}

// Get ingress class name from rayCluster annotations. this is a required field to use ingress.
if ingressClassName, ok := cluster.Annotations[IngressClassAnnotationKey]; !ok {
// First try to get ingress class name from rayCluster annotations.
ingressClassName, ok := cluster.Annotations[IngressClassAnnotationKey]
if !ok {
ingressClassName, ok = defaultAnnotations[IngressClassAnnotationKey]
}

if !ok {
log.Info("Ingress class annotation is not set for the cluster.", "clusterNamespace", cluster.Namespace, "clusterName", cluster.Name)
} else {
// TODO: in AWS EKS, set up IngressClassName will cause an error due to conflict with annotation.
Expand Down
51 changes: 49 additions & 2 deletions ray-operator/controllers/ray/common/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
Expand All @@ -19,6 +20,8 @@ var instanceWithIngressEnabled = &rayv1.RayCluster{
Namespace: "default",
Annotations: map[string]string{
IngressClassAnnotationKey: "nginx",
"annotation0": "value",
"annotation1": "value",
},
},
Spec: rayv1.RayClusterSpec{
Expand Down Expand Up @@ -74,15 +77,18 @@ var instanceWithIngressEnabledWithoutIngressClass = &rayv1.RayCluster{

// only throw warning message and rely on Kubernetes to assign default ingress class
func TestBuildIngressForHeadServiceWithoutIngressClass(t *testing.T) {
ingress, err := BuildIngressForHeadService(context.Background(), *instanceWithIngressEnabledWithoutIngressClass)
ingress, err := BuildIngressForHeadService(context.Background(), *instanceWithIngressEnabledWithoutIngressClass, "", []networkingv1.IngressTLS{}, map[string]string{})
assert.NotNil(t, ingress)
require.NoError(t, err)
}

func TestBuildIngressForHeadService(t *testing.T) {
ingress, err := BuildIngressForHeadService(context.Background(), *instanceWithIngressEnabled)
ingress, err := BuildIngressForHeadService(context.Background(), *instanceWithIngressEnabled, "", []networkingv1.IngressTLS{}, map[string]string{})
require.NoError(t, err)

// annotations count
assert.Len(t, ingress.Annotations, 2)

// check ingress.class annotation
assert.Equal(t, instanceWithIngressEnabled.Name, ingress.Labels[utils.RayClusterLabelKey])

Expand All @@ -107,4 +113,45 @@ func TestBuildIngressForHeadService(t *testing.T) {
for _, path := range paths {
assert.Equal(t, headSvcName, path.Backend.Service.Name)
}

// check host
assert.Equal(t, ingress.Spec.Rules[0].Host, "")

// tls count
assert.Len(t, ingress.Spec.TLS, 0)
}

func TestBuildIngressForHeadServiceWithControllerConfigs(t *testing.T) {
host := "ray.example.com"
tls := []networkingv1.IngressTLS{
{
Hosts: []string{host},
SecretName: "ray-tls-secret",
},
}
ingressClass := "different-ingress-class"
annotations := map[string]string{"annotation0": "value2", "annotation1": "value2", IngressClassAnnotationKey: ingressClass}
ingress, err := BuildIngressForHeadService(context.Background(), *instanceWithIngressEnabledWithoutIngressClass, host, tls, annotations)
require.NoError(t, err)

assert.Equal(t, *ingress.Spec.IngressClassName, ingressClass)
assert.Equal(t, ingress.Annotations, map[string]string{
"annotation0": "value2", "annotation1": "value2",
})
assert.Equal(t, ingress.Spec.Rules[0].Host, host)
assert.Equal(t, ingress.Spec.TLS, tls)
}

func TestBuildIngressForHeadServiceClusterSpecificAnnotationsTakePrecedence(t *testing.T) {
annotations := map[string]string{"annotation0": "value2", "annotation2": "value2", IngressClassAnnotationKey: "different-ingress-class"}
ingress, err := BuildIngressForHeadService(context.Background(), *instanceWithIngressEnabled, "", []networkingv1.IngressTLS{}, annotations)
require.NoError(t, err)

delete(annotations, IngressClassAnnotationKey)
assert.Equal(t, ingress.Annotations, map[string]string{
"annotation0": "value", // Overridden by cluster annotation
"annotation1": "value",
"annotation2": "value2",
})
assert.Equal(t, instanceWithIngressEnabled.Annotations[IngressClassAnnotationKey], *ingress.Spec.IngressClassName)
}
5 changes: 4 additions & 1 deletion ray-operator/controllers/ray/raycluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ type RayClusterReconcilerOptions struct {
HeadSidecarContainers []corev1.Container
WorkerSidecarContainers []corev1.Container
IsOpenShift bool
IngressHost string
IngressTLS []networkingv1.IngressTLS
IngressAnnotations map[string]string
}

// Reconcile reads that state of the cluster for a RayCluster object and makes changes based on it
Expand Down Expand Up @@ -478,7 +481,7 @@ func (r *RayClusterReconciler) reconcileIngressKubernetes(ctx context.Context, i
}

if len(headIngresses.Items) == 0 {
ingress, err := common.BuildIngressForHeadService(ctx, *instance)
ingress, err := common.BuildIngressForHeadService(ctx, *instance, r.options.IngressHost, r.options.IngressTLS, r.options.IngressAnnotations)
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions ray-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ func main() {
WorkerSidecarContainers: config.WorkerSidecarContainers,
IsOpenShift: utils.GetClusterType(),
RayClusterMetricsManager: rayClusterMetricsManager,
IngressHost: config.IngressHost,
IngressTLS: config.IngressTLS,
IngressAnnotations: config.IngressAnnotations,
}
exitOnError(ray.NewReconciler(ctx, mgr, rayClusterOptions, config).SetupWithManager(mgr, config.ReconcileConcurrency),
"unable to create controller", "controller", "RayCluster")
Expand Down
37 changes: 37 additions & 0 deletions ray-operator/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"

Expand Down Expand Up @@ -97,6 +98,42 @@ workerSidecarContainers:
},
expectErr: false,
},
{
name: "config with ingress options",
configData: `apiVersion: config.ray.io/v1alpha1
kind: Configuration
ingressHost: ray.example.com
ingressTLS:
- hosts:
- ray.example.com
secretName: ray-tls-secret
ingressAnnotations:
annotation0: value0
annotation1: value1
`,
expectedConfig: configapi.Configuration{
TypeMeta: metav1.TypeMeta{
Kind: "Configuration",
APIVersion: "config.ray.io/v1alpha1",
},
MetricsAddr: ":8080",
ProbeAddr: ":8082",
EnableLeaderElection: ptr.To(true),
ReconcileConcurrency: 1,
IngressHost: "ray.example.com",
IngressTLS: []networkingv1.IngressTLS{
{
Hosts: []string{"ray.example.com"},
SecretName: "ray-tls-secret",
},
},
IngressAnnotations: map[string]string{
"annotation0": "value0",
"annotation1": "value1",
},
},
expectErr: false,
},
{
name: "unknown filed ignored",
configData: `apiVersion: config.ray.io/v1alpha1
Expand Down