Skip to content

Commit 706cc56

Browse files
fix: leave the namespace to the environment in a restricted render
The base kustomization always listed namespace.yaml, so a restricted render (what codefly deploy gitops render produces) carried a cluster-scoped Namespace. The CLI refuses one outside an AppProject contract, so every composition rendering a python-fastapi service without --app-project failed, while the same composition rendered go-grpc services, which already omit it when .Restricted. Template the base kustomization the way go-grpc does: the Namespace is created only by the ephemeral local apply. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 2b1e83d commit 706cc56

3 files changed

Lines changed: 56 additions & 4 deletions

File tree

‎deployment_test.go‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package main
22

33
import (
4+
"context"
45
"os"
56
"path/filepath"
67
"strings"
78
"testing"
89

910
"github.com/codefly-dev/core/agents/services"
1011
agenttesting "github.com/codefly-dev/core/agents/testing"
12+
basev0 "github.com/codefly-dev/core/generated/go/codefly/base/v0"
1113
builderv0 "github.com/codefly-dev/core/generated/go/codefly/services/builder/v0"
14+
"github.com/codefly-dev/core/resources"
15+
"github.com/codefly-dev/core/wool"
1216
"github.com/stretchr/testify/require"
1317
"gopkg.in/yaml.v3"
1418
)
@@ -284,3 +288,48 @@ func TestPreparePodOverlayNormalizesMounts(t *testing.T) {
284288
func TestPreparePodOverlayAcceptsNoOverlay(t *testing.T) {
285289
require.NoError(t, preparePodOverlay(nil))
286290
}
291+
292+
// renderProfile renders the deployment templates for one output profile through
293+
// the same core entry point Deploy uses.
294+
func renderProfile(t *testing.T, profile builderv0.KubernetesOutputProfile) string {
295+
t.Helper()
296+
ctx := context.Background()
297+
identity := &resources.ServiceIdentity{Workspace: "workspace", Module: "module", Name: "example-service", Version: "1.2.3"}
298+
base := &services.Base{
299+
Wool: wool.Get(ctx),
300+
Identity: identity,
301+
Information: &services.Information{Service: resources.ToServiceWithCase(identity), Module: resources.ToModuleWithCase(identity)},
302+
}
303+
if services.IsRestrictedOutputProfile(profile) {
304+
base.SetDockerImage(&resources.DockerImage{Name: "example/service", Digest: "sha256:" + strings.Repeat("a", 64)})
305+
} else {
306+
base.SetDockerImage(resources.NewDockerImage("example/service:1.2.3"))
307+
}
308+
builder := &services.BuilderWrapper{Base: base}
309+
base.Builder = builder
310+
destination := t.TempDir()
311+
deployment := &builderv0.KubernetesDeployment{Namespace: "codefly-test", Destination: destination, Profile: profile}
312+
params := services.DeploymentParameters{
313+
ConfigMap: services.EnvironmentMap{"CODEFLY_TEST_VALUE": "value"},
314+
Parameters: deploymentTestParameters(t, Parameters{}),
315+
}
316+
require.NoError(t, builder.KustomizeDeploy(ctx, &basev0.Environment{Name: "test"}, deployment, deploymentFS, params))
317+
return destination
318+
}
319+
320+
// A restricted render is what `codefly deploy gitops render` produces, and it
321+
// refuses a cluster-scoped object outside an AppProject contract. The Namespace
322+
// belongs to the environment that owns it, as in the go-grpc agent; only the
323+
// ephemeral local apply creates one.
324+
func TestRestrictedDeploymentLeavesTheNamespaceToTheEnvironment(t *testing.T) {
325+
restricted := renderProfile(t, builderv0.KubernetesOutputProfile_KUBERNETES_OUTPUT_PROFILE_RESTRICTED_PORTABLE_V1)
326+
kustomization, err := os.ReadFile(filepath.Join(restricted, "base", "kustomization.yaml"))
327+
require.NoError(t, err)
328+
require.NotContains(t, string(kustomization), "namespace.yaml")
329+
require.Contains(t, string(kustomization), "deployment.yaml")
330+
331+
local := renderProfile(t, builderv0.KubernetesOutputProfile_KUBERNETES_OUTPUT_PROFILE_EPHEMERAL_LOCAL_APPLY_V1)
332+
kustomization, err = os.ReadFile(filepath.Join(local, "base", "kustomization.yaml"))
333+
require.NoError(t, err)
334+
require.Contains(t, string(kustomization), "namespace.yaml")
335+
}

‎templates/deployment/kustomize/base/kustomization.yaml‎

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
resources:
2+
{{- if not .Restricted }}
3+
# A restricted render leaves the Namespace to the environment that owns it.
4+
- namespace.yaml
5+
{{- end }}
6+
- deployment.yaml
7+
- service.yaml

0 commit comments

Comments
 (0)