Skip to content

Commit

Permalink
add service port (#5048)
Browse files Browse the repository at this point in the history
* add service port

* fix bug

* fix bug
  • Loading branch information
bearslyricattack authored Sep 6, 2024
1 parent 8ad51bc commit 60c4065
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions controllers/devbox/internal/controller/devbox_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 60c4065

Please sign in to comment.