Skip to content

Commit 624ccbe

Browse files
committed
fix: include custom service port in default access URL
1 parent c9ac36b commit 624ccbe

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

internal/controller/codercontrolplane_controller.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,12 +619,22 @@ func (r *CoderControlPlaneReconciler) reconcileDeployment(ctx context.Context, c
619619
}
620620
if configuredAccessURL == nil {
621621
scheme := "http"
622+
accessURLPort := coderControlPlane.Spec.Service.Port
623+
if accessURLPort == 0 {
624+
accessURLPort = defaultControlPlanePort
625+
}
622626
if tlsEnabled {
623627
scheme = "https"
628+
accessURLPort = 443
629+
}
630+
631+
accessURL := fmt.Sprintf("%s://%s.%s.svc.cluster.local", scheme, coderControlPlane.Name, coderControlPlane.Namespace)
632+
if (scheme == "http" && accessURLPort != 80) || (scheme == "https" && accessURLPort != 443) {
633+
accessURL = fmt.Sprintf("%s:%d", accessURL, accessURLPort)
624634
}
625635
env = append(env, corev1.EnvVar{
626636
Name: "CODER_ACCESS_URL",
627-
Value: fmt.Sprintf("%s://%s.%s.svc.cluster.local", scheme, coderControlPlane.Name, coderControlPlane.Namespace),
637+
Value: accessURL,
628638
})
629639
}
630640
}

internal/controller/codercontrolplane_controller_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2445,6 +2445,39 @@ func TestReconcile_DeploymentAlignment(t *testing.T) {
24452445
}
24462446
})
24472447

2448+
t.Run("DefaultAccessURLIncludesCustomServicePort", func(t *testing.T) {
2449+
cp := &coderv1alpha1.CoderControlPlane{
2450+
ObjectMeta: metav1.ObjectMeta{Name: "test-deployment-alignment-custom-service-port", Namespace: "default"},
2451+
Spec: coderv1alpha1.CoderControlPlaneSpec{
2452+
Image: "test-deployment-alignment:latest",
2453+
Service: coderv1alpha1.ServiceSpec{
2454+
Port: 8080,
2455+
},
2456+
},
2457+
}
2458+
if err := k8sClient.Create(ctx, cp); err != nil {
2459+
t.Fatalf("create control plane: %v", err)
2460+
}
2461+
t.Cleanup(func() {
2462+
_ = k8sClient.Delete(ctx, cp)
2463+
})
2464+
2465+
r := &controller.CoderControlPlaneReconciler{Client: k8sClient, Scheme: scheme}
2466+
if _, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{Name: cp.Name, Namespace: cp.Namespace}}); err != nil {
2467+
t.Fatalf("reconcile control plane: %v", err)
2468+
}
2469+
2470+
deployment := &appsv1.Deployment{}
2471+
if err := k8sClient.Get(ctx, types.NamespacedName{Name: cp.Name, Namespace: cp.Namespace}, deployment); err != nil {
2472+
t.Fatalf("get deployment: %v", err)
2473+
}
2474+
container := deployment.Spec.Template.Spec.Containers[0]
2475+
expectedAccessURL := "http://" + cp.Name + "." + cp.Namespace + ".svc.cluster.local:8080"
2476+
if got := mustFindEnvVar(t, container.Env, "CODER_ACCESS_URL").Value; got != expectedAccessURL {
2477+
t.Fatalf("expected default CODER_ACCESS_URL %q, got %q", expectedAccessURL, got)
2478+
}
2479+
})
2480+
24482481
t.Run("UserDefinedAccessURLTakesPrecedence", func(t *testing.T) {
24492482
cp := &coderv1alpha1.CoderControlPlane{
24502483
ObjectMeta: metav1.ObjectMeta{Name: "test-deployment-alignment-custom-access-url", Namespace: "default"},

0 commit comments

Comments
 (0)