Skip to content

Commit d188aaf

Browse files
committed
refactor(api): route tenant kind check through validation.TenantKind
The convertHelmReleaseToApplication fall-through that populates Status.Namespace for tenant resources still compared r.kindName against the bare string "Tenant". The validation package already exposes TenantKind for exactly this reason: if the kind string ever diverges from the ApplicationDefinition source of truth, the constant and the call site drift together rather than silently desynchronizing. Add TestConvertHelmReleaseToApplication_TenantNamespaceKindGate to pin both sides of the gate: tenant kind populates Status.Namespace, non-tenant kind leaves it empty. Also rewrite the TestUpdate comment to describe the upsert invariant directly instead of referencing an issue number. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent ac1132e commit d188aaf

2 files changed

Lines changed: 68 additions & 4 deletions

File tree

‎pkg/registry/apps/application/rest.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ func (r *REST) convertHelmReleaseToApplication(ctx context.Context, hr *helmv2.H
11531153
app.SetConditions(conditions)
11541154

11551155
// Add namespace field for Tenant applications
1156-
if r.kindName == "Tenant" {
1156+
if r.kindName == validation.TenantKind {
11571157
app.Status.Namespace = r.computeTenantNamespace(hr.Namespace, app.Name)
11581158
externalIPsCount, err := r.countTenantExternalIPs(ctx, app.Status.Namespace)
11591159
if err != nil {

‎pkg/registry/apps/application/rest_validation_test.go‎

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"testing"
2424

2525
helmv2 "github.com/fluxcd/helm-controller/api/v2"
26+
corev1 "k8s.io/api/core/v1"
2627
apierrors "k8s.io/apimachinery/pkg/api/errors"
2728
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2829
"k8s.io/apimachinery/pkg/runtime"
@@ -32,6 +33,7 @@ import (
3233
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3334

3435
appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1"
36+
"github.com/cozystack/cozystack/pkg/apis/apps/validation"
3537
"github.com/cozystack/cozystack/pkg/config"
3638
)
3739

@@ -79,9 +81,10 @@ func TestValidateNameFormat(t *testing.T) {
7981
// Update → Create fall-through path. When a user runs `kubectl apply` and
8082
// the object does not yet exist, Kubernetes routes the request through
8183
// REST.Update with forceAllowCreate=true, which delegates to REST.Create
82-
// (rest.go:452). This test ensures tenant name validation fires on that
83-
// upsert path — otherwise a future refactor could silently regress the
84-
// fix for #2375 while unit tests of r.validateNameFormat alone keep passing.
84+
// (rest.go:452). Without this test, a future refactor could quietly reroute
85+
// that delegation and bypass the tenant name check — unit tests of the
86+
// pure r.validateNameFormat method would still pass while upsert-style
87+
// kubectl apply regressed back to accepting tenant names with dashes.
8588
func TestUpdate_ForceAllowCreate_RejectsTenantDashName(t *testing.T) {
8689
scheme := runtime.NewScheme()
8790
if err := helmv2.AddToScheme(scheme); err != nil {
@@ -154,6 +157,67 @@ func TestUpdate_ForceAllowCreate_RejectsTenantDashName(t *testing.T) {
154157
}
155158
}
156159

160+
// TestConvertHelmReleaseToApplication_TenantNamespaceKindGate pins the
161+
// behavior that convertHelmReleaseToApplication fills Status.Namespace only
162+
// when the kind is Tenant. This path is gated on r.kindName matching a
163+
// specific literal — the test uses the validation.TenantKind constant so
164+
// that if the source of truth for the tenant kind string is ever renamed,
165+
// the gate and the constant drift together (or the test fails).
166+
func TestConvertHelmReleaseToApplication_TenantNamespaceKindGate(t *testing.T) {
167+
scheme := runtime.NewScheme()
168+
if err := helmv2.AddToScheme(scheme); err != nil {
169+
t.Fatalf("register helmv2 scheme: %v", err)
170+
}
171+
if err := corev1.AddToScheme(scheme); err != nil {
172+
t.Fatalf("register corev1 scheme: %v", err)
173+
}
174+
175+
fakeClient := fake.NewClientBuilder().WithScheme(scheme).Build()
176+
177+
hr := &helmv2.HelmRelease{
178+
ObjectMeta: metav1.ObjectMeta{
179+
Name: "tenant-foo",
180+
Namespace: "tenant-root",
181+
},
182+
}
183+
184+
t.Run("tenant kind fills Status.Namespace", func(t *testing.T) {
185+
r := &REST{
186+
c: fakeClient,
187+
kindName: validation.TenantKind,
188+
releaseConfig: config.ReleaseConfig{
189+
Prefix: "tenant-",
190+
},
191+
}
192+
193+
app, err := r.convertHelmReleaseToApplication(context.Background(), hr)
194+
if err != nil {
195+
t.Fatalf("convertHelmReleaseToApplication: %v", err)
196+
}
197+
if app.Status.Namespace == "" {
198+
t.Errorf("expected Status.Namespace to be populated for tenant kind, got empty")
199+
}
200+
})
201+
202+
t.Run("non-tenant kind leaves Status.Namespace empty", func(t *testing.T) {
203+
r := &REST{
204+
c: fakeClient,
205+
kindName: "MySQL",
206+
releaseConfig: config.ReleaseConfig{
207+
Prefix: "mysql-",
208+
},
209+
}
210+
211+
app, err := r.convertHelmReleaseToApplication(context.Background(), hr)
212+
if err != nil {
213+
t.Fatalf("convertHelmReleaseToApplication: %v", err)
214+
}
215+
if app.Status.Namespace != "" {
216+
t.Errorf("expected Status.Namespace to be empty for non-tenant kind, got %q", app.Status.Namespace)
217+
}
218+
})
219+
}
220+
157221
func TestValidateNameLength(t *testing.T) {
158222
tests := []struct {
159223
name string

0 commit comments

Comments
 (0)