Skip to content

Commit c9ac36b

Browse files
committed
fix: avoid duplicate TLS service ports on 443
1 parent 4942b8f commit c9ac36b

2 files changed

Lines changed: 57 additions & 3 deletions

File tree

internal/controller/codercontrolplane_controller.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,13 +788,20 @@ func (r *CoderControlPlaneReconciler) reconcileService(ctx context.Context, code
788788
servicePort = defaultControlPlanePort
789789
}
790790

791-
servicePorts := []corev1.ServicePort{{
791+
tlsEnabled := controlPlaneTLSEnabled(coderControlPlane)
792+
primaryServicePort := corev1.ServicePort{
792793
Name: "http",
793794
Port: servicePort,
794795
Protocol: corev1.ProtocolTCP,
795796
TargetPort: intstr.FromInt(int(controlPlaneTargetPort)),
796-
}}
797-
if controlPlaneTLSEnabled(coderControlPlane) {
797+
}
798+
if tlsEnabled && servicePort == 443 {
799+
primaryServicePort.Name = "https"
800+
primaryServicePort.TargetPort = intstr.FromInt(int(controlPlaneTLSTargetPort))
801+
}
802+
803+
servicePorts := []corev1.ServicePort{primaryServicePort}
804+
if tlsEnabled && servicePort != 443 {
798805
servicePorts = append(servicePorts, corev1.ServicePort{
799806
Name: "https",
800807
Port: 443,

internal/controller/codercontrolplane_controller_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2729,6 +2729,53 @@ func TestReconcile_TLSAlignment(t *testing.T) {
27292729
}
27302730
}
27312731

2732+
func TestReconcile_TLSWithServicePort443(t *testing.T) {
2733+
ensureGatewaySchemeRegistered(t)
2734+
ctx := context.Background()
2735+
2736+
cp := &coderv1alpha1.CoderControlPlane{
2737+
ObjectMeta: metav1.ObjectMeta{Name: "test-tls-service-port-443", Namespace: "default"},
2738+
Spec: coderv1alpha1.CoderControlPlaneSpec{
2739+
Image: "test-tls-443:latest",
2740+
Service: coderv1alpha1.ServiceSpec{
2741+
Port: 443,
2742+
},
2743+
TLS: coderv1alpha1.TLSSpec{
2744+
SecretNames: []string{"my-tls-443"},
2745+
},
2746+
},
2747+
}
2748+
if err := k8sClient.Create(ctx, cp); err != nil {
2749+
t.Fatalf("create control plane: %v", err)
2750+
}
2751+
t.Cleanup(func() {
2752+
_ = k8sClient.Delete(ctx, cp)
2753+
})
2754+
2755+
r := &controller.CoderControlPlaneReconciler{Client: k8sClient, Scheme: scheme}
2756+
if _, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{Name: cp.Name, Namespace: cp.Namespace}}); err != nil {
2757+
t.Fatalf("reconcile control plane: %v", err)
2758+
}
2759+
2760+
service := &corev1.Service{}
2761+
if err := k8sClient.Get(ctx, types.NamespacedName{Name: cp.Name, Namespace: cp.Namespace}, service); err != nil {
2762+
t.Fatalf("get service: %v", err)
2763+
}
2764+
if len(service.Spec.Ports) != 1 {
2765+
t.Fatalf("expected exactly one service port when service.port=443 and TLS is enabled, got %+v", service.Spec.Ports)
2766+
}
2767+
port := service.Spec.Ports[0]
2768+
if port.Name != "https" {
2769+
t.Fatalf("expected single service port to be named https, got %q", port.Name)
2770+
}
2771+
if port.Port != 443 {
2772+
t.Fatalf("expected single service port number 443, got %d", port.Port)
2773+
}
2774+
if port.TargetPort != intstr.FromInt(8443) {
2775+
t.Fatalf("expected single service port target 8443, got %+v", port.TargetPort)
2776+
}
2777+
}
2778+
27322779
func TestReconcile_PassThroughConfiguration(t *testing.T) {
27332780
ensureGatewaySchemeRegistered(t)
27342781
ctx := context.Background()

0 commit comments

Comments
 (0)