Skip to content

Commit caf57b0

Browse files
authored
Propagate CATTLE_WRANGLER_CHECK_GVK_ERROR_MAPPING to fleet-agent (#5085) (#5092)
* Propagate CATTLE_WRANGLER_CHECK_GVK_ERROR_MAPPING to fleet-agent The controller now forwards the CATTLE_WRANGLER_CHECK_GVK_ERROR_MAPPING env variable to the agent deployment, following the same pattern used by EXPERIMENTAL_COPY_RESOURCES_DOWNSTREAM. The env var name is defined as a single exported constant (EnvVarWranglerCheckGVKErrorMapping) in internal/config, replacing the package-private copy that existed in the summary package. Refers to: #5060 --------- Signed-off-by: Xavi Garcia <xavi.garcia@suse.com>
1 parent defbe21 commit caf57b0

5 files changed

Lines changed: 108 additions & 8 deletions

File tree

internal/cmd/agent/deployer/summary/summarizers.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/rancher/fleet/internal/cmd/agent/deployer/data"
1010
"github.com/rancher/fleet/internal/cmd/agent/deployer/data/convert"
1111
"github.com/rancher/fleet/internal/cmd/agent/deployer/kv"
12+
"github.com/rancher/fleet/internal/config"
1213
fleetv1 "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1/summary"
1314

1415
"github.com/sirupsen/logrus"
@@ -21,9 +22,8 @@ import (
2122
)
2223

2324
const (
24-
kindSep = ", Kind="
25-
reason = "%REASON%"
26-
checkGVKErrorMappingEnvVar = "CATTLE_WRANGLER_CHECK_GVK_ERROR_MAPPING"
25+
kindSep = ", Kind="
26+
reason = "%REASON%"
2727
)
2828

2929
var (
@@ -151,7 +151,7 @@ func init() {
151151
}
152152

153153
func initializeCheckErrors() {
154-
gvkConfig := os.Getenv(checkGVKErrorMappingEnvVar)
154+
gvkConfig := os.Getenv(config.EnvVarWranglerCheckGVKErrorMapping)
155155
if gvkConfig != "" {
156156
logrus.Debugf("GVK Error Mapping Provided")
157157
gvkErrorMapping := ConditionTypeStatusErrorMapping{}

internal/cmd/agent/deployer/summary/summarizers_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/rancher/fleet/internal/cmd/agent/deployer/data"
8+
"github.com/rancher/fleet/internal/config"
89
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1/summary"
910
"github.com/stretchr/testify/assert"
1011

@@ -174,7 +175,7 @@ func TestCheckErrors(t *testing.T) {
174175
},
175176
},
176177
loadConditions: func() {
177-
os.Setenv(checkGVKErrorMappingEnvVar, `
178+
os.Setenv(config.EnvVarWranglerCheckGVKErrorMapping, `
178179
[
179180
{
180181
"gvk": "sample.cattle.io/v1, Kind=Sample",
@@ -211,7 +212,7 @@ func TestCheckErrors(t *testing.T) {
211212
},
212213
},
213214
loadConditions: func() {
214-
os.Setenv(checkGVKErrorMappingEnvVar, `
215+
os.Setenv(config.EnvVarWranglerCheckGVKErrorMapping, `
215216
[
216217
{
217218
"gvk": "sample.cattle.io/v1, Kind=Sample",
@@ -295,7 +296,7 @@ func TestCheckErrors(t *testing.T) {
295296
},
296297
},
297298
loadConditions: func() {
298-
os.Setenv(checkGVKErrorMappingEnvVar, `
299+
os.Setenv(config.EnvVarWranglerCheckGVKErrorMapping, `
299300
[
300301
{
301302
"gvk": "sample.cattle.io/v1, Kind=Sample",
@@ -333,7 +334,7 @@ func TestCheckErrors(t *testing.T) {
333334
},
334335
},
335336
loadConditions: func() {
336-
os.Setenv(checkGVKErrorMappingEnvVar, `
337+
os.Setenv(config.EnvVarWranglerCheckGVKErrorMapping, `
337338
[
338339
{
339340
"gvk": "sample.cattle.io/v1, Kind=Sample",

internal/cmd/controller/agentmanagement/agent/manifest.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package agent
22

33
import (
44
"fmt"
5+
"os"
56
"path"
67
"strconv"
78
"strings"
@@ -305,6 +306,12 @@ func agentApp(namespace string, agentScope string, opts ManifestOptions) *appsv1
305306
}
306307

307308
// additional env vars from cluster
309+
if gvkVal := os.Getenv(config.EnvVarWranglerCheckGVKErrorMapping); gvkVal != "" && !envVarPresent(opts.AgentEnvVars, config.EnvVarWranglerCheckGVKErrorMapping) {
310+
container.Env = append(container.Env, corev1.EnvVar{
311+
Name: config.EnvVarWranglerCheckGVKErrorMapping,
312+
Value: gvkVal,
313+
})
314+
}
308315
if opts.AgentEnvVars != nil {
309316
container.Env = append(container.Env, opts.AgentEnvVars...)
310317
}
@@ -325,6 +332,15 @@ func agentApp(namespace string, agentScope string, opts ManifestOptions) *appsv1
325332
return app
326333
}
327334

335+
func envVarPresent(vars []corev1.EnvVar, name string) bool {
336+
for _, v := range vars {
337+
if v.Name == name {
338+
return true
339+
}
340+
}
341+
return false
342+
}
343+
328344
func serviceAccount(namespace, name string) *corev1.ServiceAccount {
329345
return &corev1.ServiceAccount{
330346
ObjectMeta: metav1.ObjectMeta{

internal/cmd/controller/agentmanagement/agent/manifest_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/rancher/fleet/internal/cmd"
1616
"github.com/rancher/fleet/internal/cmd/controller/agentmanagement/agent"
17+
"github.com/rancher/fleet/internal/config"
1718
)
1819

1920
const namespace = "fleet-system"
@@ -336,3 +337,82 @@ func TestPriorityClassName(t *testing.T) {
336337
})
337338
}
338339
}
340+
341+
func findEnvVar(containers []corev1.Container, name string) (corev1.EnvVar, bool) {
342+
for _, c := range containers {
343+
for _, e := range c.Env {
344+
if e.Name == name {
345+
return e, true
346+
}
347+
}
348+
}
349+
return corev1.EnvVar{}, false
350+
}
351+
352+
func TestManifestCheckGVKErrorMappingEnvVar(t *testing.T) {
353+
baseOpts := agent.ManifestOptions{
354+
LeaderElectionOptions: leaderOpts,
355+
}
356+
357+
singleMapping := `[{"gvk":"sample.cattle.io/v1, Kind=Sample","conditionMappings":[{"type":"Failed","status":["True"]}]}]`
358+
multiMapping := `[{"gvk":"sample.cattle.io/v1, Kind=Sample","conditionMappings":[{"type":"Failed","status":["True"]}]},{"gvk":"helm.cattle.io/v1, Kind=HelmChart","conditionMappings":[{"type":"Failed","status":["True"]}]}]`
359+
360+
for _, tc := range []struct {
361+
name string
362+
envValue string
363+
agentEnvVars []corev1.EnvVar
364+
expectFound bool
365+
expectedValue string
366+
}{
367+
{
368+
name: "env var not set",
369+
envValue: "",
370+
expectFound: false,
371+
},
372+
{
373+
name: "single GVK mapping",
374+
envValue: singleMapping,
375+
expectFound: true,
376+
expectedValue: singleMapping,
377+
},
378+
{
379+
name: "multiple GVK mappings",
380+
envValue: multiMapping,
381+
expectFound: true,
382+
expectedValue: multiMapping,
383+
},
384+
{
385+
name: "env var already in AgentEnvVars is not duplicated",
386+
envValue: singleMapping,
387+
agentEnvVars: []corev1.EnvVar{
388+
{Name: config.EnvVarWranglerCheckGVKErrorMapping, Value: multiMapping},
389+
},
390+
expectFound: true,
391+
expectedValue: multiMapping,
392+
},
393+
} {
394+
t.Run(tc.name, func(t *testing.T) {
395+
t.Setenv(config.EnvVarWranglerCheckGVKErrorMapping, tc.envValue)
396+
397+
opts := baseOpts
398+
opts.AgentEnvVars = tc.agentEnvVars
399+
400+
d := getAgentFromManifests("test-scope", opts)
401+
if d == nil {
402+
t.Fatal("no deployment returned from manifests")
403+
}
404+
405+
envVar, found := findEnvVar(d.Spec.Template.Spec.Containers, config.EnvVarWranglerCheckGVKErrorMapping)
406+
if found != tc.expectFound {
407+
if tc.expectFound {
408+
t.Fatalf("env var %s not found in agent container", config.EnvVarWranglerCheckGVKErrorMapping)
409+
} else {
410+
t.Fatalf("env var %s unexpectedly found in agent container with value %q", config.EnvVarWranglerCheckGVKErrorMapping, envVar.Value)
411+
}
412+
}
413+
if tc.expectFound && envVar.Value != tc.expectedValue {
414+
t.Fatalf("expected %s=%q, got %q", config.EnvVarWranglerCheckGVKErrorMapping, tc.expectedValue, envVar.Value)
415+
}
416+
})
417+
}
418+
}

internal/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ const (
4343
// server.
4444
APIServerCAKey = "apiServerCA"
4545

46+
// EnvVarWranglerCheckGVKErrorMapping is the env var name used to configure Wrangler's GVK error mapping.
47+
EnvVarWranglerCheckGVKErrorMapping = "CATTLE_WRANGLER_CHECK_GVK_ERROR_MAPPING"
48+
4649
// Default secret name for git credentials, used as a fallback if no secret is referenced by an app.
4750
DefaultGitCredentialsSecretName = "gitcredential" //nolint:gosec // this is a resource name
4851

0 commit comments

Comments
 (0)