Skip to content

Commit 7d04ce1

Browse files
committed
fix: require gateway parentRefs for HTTPRoute exposure
1 parent 3a625a6 commit 7d04ce1

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

internal/controller/codercontrolplane_controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,10 @@ func (r *CoderControlPlaneReconciler) reconcileHTTPRoute(ctx context.Context, co
12181218
return false, fmt.Errorf("assertion failed: gateway host must not be empty")
12191219
}
12201220

1221+
if len(gatewayExpose.ParentRefs) == 0 {
1222+
return false, fmt.Errorf("assertion failed: gateway parentRefs must not be empty")
1223+
}
1224+
12211225
httpRoute := &gatewayv1.HTTPRoute{ObjectMeta: metav1.ObjectMeta{Name: coderControlPlane.Name, Namespace: coderControlPlane.Namespace}}
12221226
_, err := controllerutil.CreateOrUpdate(ctx, r.Client, httpRoute, func() error {
12231227
labels := controlPlaneLabels(coderControlPlane.Name)

internal/controller/codercontrolplane_controller_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3633,6 +3633,46 @@ func TestReconcile_HTTPRouteExposure(t *testing.T) {
36333633
}
36343634
}
36353635

3636+
func TestReconcile_HTTPRouteExposure_RequiresParentRefs(t *testing.T) {
3637+
ensureGatewaySchemeRegistered(t)
3638+
ctx := context.Background()
3639+
ensureHTTPRouteCRDInstalled(t)
3640+
3641+
cp := &coderv1alpha1.CoderControlPlane{
3642+
ObjectMeta: metav1.ObjectMeta{Name: "test-httproute-parentrefs-required", Namespace: "default"},
3643+
Spec: coderv1alpha1.CoderControlPlaneSpec{
3644+
Image: "test-httproute:latest",
3645+
Expose: &coderv1alpha1.ExposeSpec{
3646+
Gateway: &coderv1alpha1.GatewayExposeSpec{
3647+
Host: "missing-parentrefs.gateway.example.test",
3648+
},
3649+
},
3650+
},
3651+
}
3652+
if err := k8sClient.Create(ctx, cp); err != nil {
3653+
t.Fatalf("create control plane: %v", err)
3654+
}
3655+
t.Cleanup(func() {
3656+
_ = k8sClient.Delete(ctx, cp)
3657+
})
3658+
3659+
r := &controller.CoderControlPlaneReconciler{Client: k8sClient, Scheme: scheme}
3660+
namespacedName := types.NamespacedName{Name: cp.Name, Namespace: cp.Namespace}
3661+
_, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: namespacedName})
3662+
if err == nil {
3663+
t.Fatal("expected reconcile to fail when gateway parentRefs are missing")
3664+
}
3665+
if !strings.Contains(err.Error(), "gateway parentRefs must not be empty") {
3666+
t.Fatalf("expected missing parentRefs assertion error, got: %v", err)
3667+
}
3668+
3669+
httpRoute := &gatewayv1.HTTPRoute{}
3670+
err = k8sClient.Get(ctx, namespacedName, httpRoute)
3671+
if !apierrors.IsNotFound(err) {
3672+
t.Fatalf("expected httproute to be absent when parentRefs are missing, got: %v", err)
3673+
}
3674+
}
3675+
36363676
func TestReconcile_HTTPRouteExposure_CRDMissingIsGraceful(t *testing.T) {
36373677
ensureGatewaySchemeRegistered(t)
36383678
ctx := context.Background()
@@ -3647,6 +3687,9 @@ func TestReconcile_HTTPRouteExposure_CRDMissingIsGraceful(t *testing.T) {
36473687
Expose: &coderv1alpha1.ExposeSpec{
36483688
Gateway: &coderv1alpha1.GatewayExposeSpec{
36493689
Host: "missing-crd.gateway.example.test",
3690+
ParentRefs: []coderv1alpha1.GatewayParentRef{{
3691+
Name: "missing-crd-gateway",
3692+
}},
36503693
},
36513694
},
36523695
},

0 commit comments

Comments
 (0)