From 60c4065bb9e4dcab4a4842c192fda7b0c62f8726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8A=92=E6=83=85=E7=86=8A?= <2669184984@qq.com> Date: Fri, 6 Sep 2024 15:39:13 +0800 Subject: [PATCH] add service port (#5048) * add service port * fix bug * fix bug --- .../internal/controller/devbox_controller.go | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/controllers/devbox/internal/controller/devbox_controller.go b/controllers/devbox/internal/controller/devbox_controller.go index 0aa54b43318..b4aca506f3c 100644 --- a/controllers/devbox/internal/controller/devbox_controller.go +++ b/controllers/devbox/internal/controller/devbox_controller.go @@ -480,19 +480,42 @@ func (r *DevboxReconciler) generateDevboxPod(devbox *devboxv1alpha1.Devbox, runt } func (r *DevboxReconciler) syncService(ctx context.Context, devbox *devboxv1alpha1.Devbox, recLabels map[string]string) error { - expectServiceSpec := corev1.ServiceSpec{ - Selector: recLabels, - Type: corev1.ServiceTypeNodePort, - Ports: []corev1.ServicePort{ + var runtimeNamespace string + if devbox.Spec.RuntimeRef.Namespace != "" { + runtimeNamespace = devbox.Spec.RuntimeRef.Namespace + } else { + runtimeNamespace = devbox.Namespace + } + + runtimecr := &devboxv1alpha1.Runtime{} + if err := r.Get(ctx, client.ObjectKey{Namespace: runtimeNamespace, Name: devbox.Spec.RuntimeRef.Name}, runtimecr); err != nil { + return err + } + var servicePorts []corev1.ServicePort + for _, port := range runtimecr.Spec.Config.Ports { + servicePorts = append(servicePorts, corev1.ServicePort{ + Name: port.Name, + Port: port.ContainerPort, + TargetPort: intstr.FromInt32(port.ContainerPort), + Protocol: port.Protocol, + }) + } + if len(servicePorts) == 0 { + //use the default value + servicePorts = []corev1.ServicePort{ { Name: "tty", Port: 22, TargetPort: intstr.FromInt32(22), Protocol: corev1.ProtocolTCP, }, - }, + } + } + expectServiceSpec := corev1.ServiceSpec{ + Selector: recLabels, + Type: corev1.ServiceTypeNodePort, + Ports: servicePorts, } - service := &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: devbox.Name + "-svc",