Skip to content

Commit 0a271ab

Browse files
danegstaCopilot
andcommitted
Use target-specific environment casing for logical templates
Pass an explicit ignoreEnvironmentCase boolean: containers preserve case-distinct names on every host and host processes fold names only on Windows. Preserve last-value-wins normalization before evaluation and cover both policies, template errors, physical configuration, and lifecycle hashes. Co-authored-by: Copilot App <[email protected]>
1 parent 8b5fbbd commit 0a271ab

5 files changed

Lines changed: 143 additions & 27 deletions

File tree

‎controllers/logical_ports.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/microsoft/dcp/internal/networking"
2323
"github.com/microsoft/dcp/internal/templating"
2424
"github.com/microsoft/dcp/pkg/commonapi"
25+
"github.com/microsoft/dcp/pkg/osutil"
2526
)
2627

2728
type logicalPortRequest struct {
@@ -240,7 +241,8 @@ func (r *LogicalContainerReconciler) prepareLogicalContainerValues(
240241
}
241242
config := container.Spec.Container.DeepCopy()
242243
if config != nil {
243-
arguments, environment, evaluationErr := templating.EvaluateV2Values(ctx, r.NoCacheClient, container, servingPorts(ports), config.Command, config.Env)
244+
arguments, environment, evaluationErr := templating.EvaluateV2Values(
245+
ctx, r.NoCacheClient, container, servingPorts(ports), config.Command, config.Env, false)
244246
if evaluationErr != nil {
245247
log.Error(evaluationErr, "Failed to evaluate logical container values")
246248
data.reason, data.message = "TemplateEvaluationFailed", evaluationErr.Error()
@@ -277,7 +279,8 @@ func (r *LogicalProcessReconciler) prepareLogicalProcessValues(
277279
}
278280
config := resource.Spec.Process.DeepCopy()
279281
if config != nil {
280-
arguments, environment, evaluationErr := templating.EvaluateV2Values(ctx, r.NoCacheClient, resource, servingPorts(ports), config.Args, config.Env)
282+
arguments, environment, evaluationErr := templating.EvaluateV2Values(
283+
ctx, r.NoCacheClient, resource, servingPorts(ports), config.Args, config.Env, osutil.IsWindows())
281284
if evaluationErr != nil {
282285
log.Error(evaluationErr, "Failed to evaluate logical process values")
283286
data.reason, data.message = "TemplateEvaluationFailed", evaluationErr.Error()

‎internal/templating/v2_value_templates.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/microsoft/dcp/internal/networking"
2222
"github.com/microsoft/dcp/pkg/commonapi"
2323
"github.com/microsoft/dcp/pkg/maps"
24-
"github.com/microsoft/dcp/pkg/osutil"
2524
)
2625

2726
type errV2ServiceLookup struct {
@@ -46,10 +45,12 @@ type ServingPort struct {
4645
// EvaluateV2Values evaluates declared arguments and environment against one service-address snapshot.
4746
// Serving helpers use logical port names; consumer helpers accept same-namespace service references
4847
// and an optional "IPv4" or "IPv6" selector. Address helpers bracket IPv6 literals for URL/authority
49-
// interpolation. Inputs are never modified.
48+
// interpolation. ignoreEnvironmentCase controls case folding of environment names before evaluation.
49+
// Inputs are never modified.
5050
func EvaluateV2Values(
5151
ctx context.Context, reader ctrl_client.Reader, owner commonapi.DcpModelObject,
5252
ports []ServingPort, arguments []string, environment []commonapi.EnvVar,
53+
ignoreEnvironmentCase bool,
5354
) ([]string, []commonapi.EnvVar, error) {
5455
selectedAddresses := make(map[string]apiv2.ServiceAddress)
5556
serviceSnapshots := make(map[types.NamespacedName]*apiv2.LogicalService)
@@ -141,7 +142,7 @@ func EvaluateV2Values(
141142
return output.String(), nil
142143
}
143144
environmentMode := maps.StringMapModeCaseSensitive
144-
if osutil.IsWindows() {
145+
if ignoreEnvironmentCase {
145146
environmentMode = maps.StringMapModeCaseInsensitive
146147
}
147148
lastDeclarations := maps.NewStringKeyMap[int](environmentMode)

‎internal/templating/v2_value_templates_test.go‎

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
apiv2 "github.com/microsoft/dcp/api/v2"
2323
"github.com/microsoft/dcp/internal/networking"
2424
"github.com/microsoft/dcp/pkg/commonapi"
25-
"github.com/microsoft/dcp/pkg/osutil"
2625
)
2726

2827
type countedServiceReader struct {
@@ -59,7 +58,7 @@ func TestEvaluateV2ValuesServiceAddresses(t *testing.T) {
5958
expectedArguments = append(expectedArguments, "127.0.0.1:5000")
6059
environment := []commonapi.EnvVar{{Name: "PORT", Value: `{{portForServing "http"}}`}, {Name: "EMPTY", Value: ""}}
6160
resolvedArgs, resolvedEnv, evaluationErr := EvaluateV2Values(context.Background(), reader, owner,
62-
[]ServingPort{{Name: "http", Address: "0.0.0.0", Port: 8080}}, arguments, environment)
61+
[]ServingPort{{Name: "http", Address: "0.0.0.0", Port: 8080}}, arguments, environment, false)
6362
require.NoError(t, evaluationErr)
6463
require.Equal(t, expectedArguments, resolvedArgs)
6564
require.Equal(t, []commonapi.EnvVar{{Name: "PORT", Value: "8080"}, {Name: "EMPTY", Value: ""}}, resolvedEnv)
@@ -93,7 +92,7 @@ func TestEvaluateV2ValuesErrors(t *testing.T) {
9392
{"unavailable family", `{{portFor "service" "IPv6"}}`, true},
9493
} {
9594
t.Run(testCase.name, func(t *testing.T) {
96-
_, _, evaluationErr := EvaluateV2Values(context.Background(), reader, owner, nil, []string{testCase.value}, nil)
95+
_, _, evaluationErr := EvaluateV2Values(context.Background(), reader, owner, nil, []string{testCase.value}, nil, false)
9796
require.Error(t, evaluationErr)
9897
require.Equal(t, testCase.transient, IsTransientTemplateError(evaluationErr))
9998
})
@@ -142,7 +141,8 @@ func TestEvaluateV2ValuesValidatesConsumerReferences(t *testing.T) {
142141
} else {
143142
environment = []commonapi.EnvVar{{Name: "VALUE", Value: expression}}
144143
}
145-
resolvedArgs, resolvedEnv, evaluationErr := EvaluateV2Values(context.Background(), reader, owner, nil, arguments, environment)
144+
resolvedArgs, resolvedEnv, evaluationErr := EvaluateV2Values(
145+
context.Background(), reader, owner, nil, arguments, environment, false)
146146
if testCase.invalid {
147147
require.ErrorContains(t, evaluationErr, "invalid service reference")
148148
require.False(t, IsTransientTemplateError(evaluationErr))
@@ -176,36 +176,68 @@ func TestEvaluateV2ValuesIgnoresOverriddenEnvironment(t *testing.T) {
176176
t.Parallel()
177177
scheme := runtime.NewScheme()
178178
require.NoError(t, apiv2.AddToScheme(scheme))
179-
reader := &countedServiceReader{Reader: fake.NewClientBuilder().WithScheme(scheme).Build()}
180179
owner := &apiv2.LogicalProcess{ObjectMeta: metav1.ObjectMeta{Name: "process", Namespace: "test-namespace"}}
181180
environment := []commonapi.EnvVar{
182181
{Name: "PORT", Value: `{{portFor "missing"}}`},
183182
{Name: "OTHER", Value: "{{"},
184183
{Name: "PORT", Value: "8080"},
185184
{Name: "OTHER", Value: ""},
186185
}
187-
_, resolvedEnv, evaluationErr := EvaluateV2Values(context.Background(), reader, owner, nil, nil, environment)
188-
require.NoError(t, evaluationErr)
189-
require.Equal(t, []commonapi.EnvVar{{Name: "PORT", Value: "8080"}, {Name: "OTHER", Value: ""}}, resolvedEnv)
190-
require.Zero(t, reader.reads)
191-
require.Equal(t, `{{portFor "missing"}}`, environment[0].Value)
186+
for _, ignoreEnvironmentCase := range []bool{false, true} {
187+
t.Run(fmt.Sprint(ignoreEnvironmentCase), func(t *testing.T) {
188+
t.Parallel()
189+
reader := &countedServiceReader{Reader: fake.NewClientBuilder().WithScheme(scheme).Build()}
190+
_, resolvedEnv, evaluationErr := EvaluateV2Values(context.Background(), reader, owner, nil, nil, environment, ignoreEnvironmentCase)
191+
require.NoError(t, evaluationErr)
192+
require.Equal(t, []commonapi.EnvVar{{Name: "PORT", Value: "8080"}, {Name: "OTHER", Value: ""}}, resolvedEnv)
193+
require.Zero(t, reader.reads)
194+
require.Equal(t, `{{portFor "missing"}}`, environment[0].Value)
195+
})
196+
}
192197
}
193198

194-
// Verifies that EvaluateV2Values applies Windows case-insensitive environment overrides
195-
// without treating differently cased Unix variable names as equivalent.
199+
// Verifies that EvaluateV2Values uses the supplied environment comparison mode independently
200+
// of the host OS, preserving distinct names or ignoring overwritten templates as appropriate.
196201
func TestEvaluateV2ValuesEnvironmentCasing(t *testing.T) {
197202
t.Parallel()
198203
scheme := runtime.NewScheme()
199204
require.NoError(t, apiv2.AddToScheme(scheme))
200-
reader := fake.NewClientBuilder().WithScheme(scheme).Build()
201205
owner := &apiv2.LogicalProcess{ObjectMeta: metav1.ObjectMeta{Name: "process", Namespace: "test-namespace"}}
202-
environment := []commonapi.EnvVar{{Name: "VALUE", Value: "{{"}, {Name: "value", Value: "effective"}}
203-
_, resolvedEnv, evaluationErr := EvaluateV2Values(context.Background(), reader, owner, nil, nil, environment)
204-
if osutil.IsWindows() {
205-
require.NoError(t, evaluationErr)
206-
require.Equal(t, []commonapi.EnvVar{{Name: "value", Value: "effective"}}, resolvedEnv)
207-
} else {
208-
require.Error(t, evaluationErr)
206+
for _, ignoreEnvironmentCase := range []bool{false, true} {
207+
for _, testCase := range []struct {
208+
name string
209+
value string
210+
failure bool
211+
transient bool
212+
}{
213+
{name: "literal", value: "distinct"},
214+
{name: "malformed", value: "{{", failure: true},
215+
{name: "missing service", value: `{{portFor "missing"}}`, failure: true, transient: true},
216+
} {
217+
t.Run(fmt.Sprint(ignoreEnvironmentCase)+"/"+testCase.name, func(t *testing.T) {
218+
t.Parallel()
219+
reader := &countedServiceReader{Reader: fake.NewClientBuilder().WithScheme(scheme).Build()}
220+
environment := []commonapi.EnvVar{{Name: "VALUE", Value: testCase.value}, {Name: "value", Value: "effective"}}
221+
_, resolvedEnv, evaluationErr := EvaluateV2Values(context.Background(), reader, owner, nil, nil, environment, ignoreEnvironmentCase)
222+
if ignoreEnvironmentCase {
223+
require.NoError(t, evaluationErr)
224+
require.Equal(t, []commonapi.EnvVar{{Name: "value", Value: "effective"}}, resolvedEnv)
225+
require.Zero(t, reader.reads)
226+
} else if testCase.failure {
227+
require.Error(t, evaluationErr)
228+
require.Equal(t, testCase.transient, IsTransientTemplateError(evaluationErr))
229+
if testCase.transient {
230+
require.Equal(t, 1, reader.reads)
231+
} else {
232+
require.Zero(t, reader.reads)
233+
}
234+
} else {
235+
require.NoError(t, evaluationErr)
236+
require.Equal(t, environment, resolvedEnv)
237+
}
238+
require.Equal(t, testCase.value, environment[0].Value)
239+
})
240+
}
209241
}
210242
}
211243

@@ -224,7 +256,7 @@ func TestEvaluateV2ValuesIPv6Authorities(t *testing.T) {
224256
`http://{{addressForServing "http"}}:{{portForServing "http"}}`,
225257
}
226258
resolvedArgs, _, evaluationErr := EvaluateV2Values(context.Background(), reader, owner,
227-
[]ServingPort{{Name: "http", Address: "::", Port: 8080}}, arguments, nil)
259+
[]ServingPort{{Name: "http", Address: "::", Port: 8080}}, arguments, nil, false)
228260
require.NoError(t, evaluationErr)
229261
require.Equal(t, []string{"http://[::1]:5001", "http://[::]:8080"}, resolvedArgs)
230262
for _, argument := range resolvedArgs {

‎plan/v2-resource-plan.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ After the physical primitives are in place, add logical V2 resources that expres
216216
- Caller-managed endpoints may omit a workload owner. Service readiness describes DCP configuration and endpoint availability, not a network reachability probe.
217217
- Service preparation belongs in the existing logical pending/preparation handlers. Gate subsequent work until declared and template-referenced services are available; unresolved creation outcomes use an uncached read and otherwise wait for subsequent reconciliation. Preserve existing lifecycle and physical cleanup ordering.
218218
- Consumer templates validate service references using the same namespace/name rules as declared references before API lookup. Invalid references are terminal preparation errors; valid references to missing services remain retryable.
219-
- Normalize explicit environment declarations with V1's platform-aware last-value-wins rules before evaluating templates, so overwritten values cannot introduce dependencies or failures. Evaluate the effective environment and process arguments/container command arguments before computing lifecycle keys and creating physical runtime configuration. Hash and launch the same evaluated snapshot; preserve explicit key overrides and exclude implicit inherited values.
219+
- Normalize explicit environment declarations with last-value-wins rules before evaluating templates, so overwritten values cannot introduce dependencies or failures. Container environment names remain case-sensitive regardless of the controller host; host-process environment names are case-insensitive only on Windows. The evaluator takes an explicit `ignoreEnvironmentCase` boolean. Evaluate the effective environment and process arguments/container command arguments before computing lifecycle keys and creating physical runtime configuration. Hash and launch the same evaluated snapshot; preserve explicit key overrides and exclude implicit inherited values.
220220
- Retryable template dependencies leave preparation Pending without creating physical workload children. Fatal evaluation errors are terminal Failed. Stop bypasses evaluation only while preparation is nonterminal; a later stop request does not reopen terminal preparation or take control of a retained runtime.
221221
- Follow V1 lifecycle-key boundaries: process port/service declarations are not hashed; container keys include only resolved runtime port-publication settings, sorted by protocol and host port. Logical port names, service references/templates, and proxy address policy do not contribute independently of evaluated launch values.
222222
- Retain V1 helper names: `portForServing` and `addressForServing` select a named logical port; `portFor` and `addressFor` select a same-namespace service reference, using the existing IP-family preference with an optional `IPv4` or `IPv6` selector. Select matching address/port pairs consistently.

‎test/integration/v2_logical_environment_test.go‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
apiv2 "github.com/microsoft/dcp/api/v2"
1616
ctrl_testutil "github.com/microsoft/dcp/internal/testutil/ctrlutil"
1717
"github.com/microsoft/dcp/pkg/commonapi"
18+
"github.com/microsoft/dcp/pkg/osutil"
1819
"github.com/microsoft/dcp/pkg/testutil"
1920
)
2021

@@ -62,3 +63,82 @@ func TestV2LogicalValuesIgnoreOverriddenEnvironment(t *testing.T) {
6263
})
6364
}
6465
}
66+
67+
// Verifies that logical containers preserve case-distinct environment names on every host,
68+
// while logical processes apply host casing rules before evaluating overwritten templates.
69+
func TestV2LogicalValuesEnvironmentCasing(t *testing.T) {
70+
t.Parallel()
71+
for _, kind := range []string{"process", "container"} {
72+
for _, valueKind := range []string{"literal", "malformed"} {
73+
t.Run(kind+"/"+valueKind, func(t *testing.T) {
74+
t.Parallel()
75+
ctx, cancel := testutil.GetTestContext(t, defaultIntegrationTestTimeout)
76+
defer cancel()
77+
namespace := createActiveV2Namespace(t, ctx, "v2-environment-casing-"+kind+"-"+valueKind)
78+
name := types.NamespacedName{Name: "workload", Namespace: namespace.Name}
79+
value := `{{printf "upper"}}`
80+
if valueKind == "malformed" {
81+
value = "{{"
82+
}
83+
environment := []commonapi.EnvVar{{Name: "VALUE", Value: value}, {Name: "value", Value: "lower"}}
84+
expectedEnvironment := []commonapi.EnvVar{{Name: "VALUE", Value: "upper"}, {Name: "value", Value: "lower"}}
85+
ignoreUppercase := kind == "process" && osutil.IsWindows()
86+
if ignoreUppercase {
87+
expectedEnvironment = expectedEnvironment[1:]
88+
}
89+
expectedFailure := valueKind == "malformed" && !ignoreUppercase
90+
if kind == "process" {
91+
resource := &apiv2.LogicalProcess{ObjectMeta: metav1.ObjectMeta{Name: name.Name, Namespace: name.Namespace},
92+
Spec: apiv2.LogicalProcessSpec{Process: &apiv2.LogicalProcessConfig{
93+
ExecutablePath: namespace.Name, Env: environment,
94+
}}}
95+
require.NoError(t, client.Create(ctx, resource))
96+
observed := waitObjectAssumesState(t, ctx, name, func(current *apiv2.LogicalProcess) (bool, error) {
97+
return current.Status.Phase == apiv2.LogicalProcessPhaseRunning || current.Status.Phase == apiv2.LogicalProcessPhaseFailed, nil
98+
})
99+
if expectedFailure {
100+
require.Equal(t, apiv2.LogicalProcessPhaseFailed, observed.Status.Phase)
101+
requireReadyCondition(t, observed.Status.Conditions, metav1.ConditionFalse, "TemplateEvaluationFailed")
102+
require.Empty(t, observed.Status.PhysicalProcessRef)
103+
} else {
104+
require.Equal(t, apiv2.LogicalProcessPhaseRunning, observed.Status.Phase, "%+v", observed.Status.Conditions)
105+
physical := &apiv2.PhysicalProcess{}
106+
require.NoError(t, client.Get(ctx, types.NamespacedName{Name: observed.Status.PhysicalProcessRef, Namespace: name.Namespace}, physical))
107+
require.Equal(t, expectedEnvironment, physical.Spec.Process.Env)
108+
evaluated := observed.Spec.DeepCopy()
109+
evaluated.Process.Env = expectedEnvironment
110+
expectedKey, _, keyErr := evaluated.GetLifecycleKey()
111+
require.NoError(t, keyErr)
112+
require.Equal(t, expectedKey, observed.Status.LifecycleKey)
113+
}
114+
} else {
115+
resource := &apiv2.LogicalContainer{ObjectMeta: metav1.ObjectMeta{Name: name.Name, Namespace: name.Namespace},
116+
Spec: apiv2.LogicalContainerSpec{Container: &apiv2.LogicalContainerConfig{
117+
Image: &apiv2.PhysicalContainerImageConfig{Image: namespace.Name}, Env: environment,
118+
}}}
119+
require.NoError(t, client.Create(ctx, resource))
120+
observed := waitObjectAssumesState(t, ctx, name, func(current *apiv2.LogicalContainer) (bool, error) {
121+
return current.Status.Phase == apiv2.LogicalContainerPhaseRunning || current.Status.Phase == apiv2.LogicalContainerPhaseFailed, nil
122+
})
123+
if expectedFailure {
124+
require.Equal(t, apiv2.LogicalContainerPhaseFailed, observed.Status.Phase)
125+
requireReadyCondition(t, observed.Status.Conditions, metav1.ConditionFalse, "TemplateEvaluationFailed")
126+
require.Empty(t, observed.Status.PhysicalContainerRef)
127+
} else {
128+
require.Equal(t, apiv2.LogicalContainerPhaseRunning, observed.Status.Phase, "%+v", observed.Status.Conditions)
129+
physical := &apiv2.PhysicalContainer{}
130+
require.NoError(t, client.Get(ctx, types.NamespacedName{Name: observed.Status.PhysicalContainerRef, Namespace: name.Namespace}, physical))
131+
require.Equal(t, expectedEnvironment, physical.Spec.Container.Env)
132+
evaluated := observed.Spec.DeepCopy()
133+
evaluated.Container.Env = expectedEnvironment
134+
expectedKey, _, keyErr := evaluated.GetLifecycleKey(physical.Spec.Container.Ports)
135+
require.NoError(t, keyErr)
136+
require.Equal(t, expectedKey, observed.Status.LifecycleKey)
137+
}
138+
}
139+
require.NoError(t, client.Delete(ctx, namespace))
140+
ctrl_testutil.WaitObjectDeleted[apiv2.Namespace](t, ctx, client, namespace)
141+
})
142+
}
143+
}
144+
}

0 commit comments

Comments
 (0)