Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions internal/cmd/agent/deployer/summary/summarizers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/rancher/fleet/internal/cmd/agent/deployer/data"
"github.com/rancher/fleet/internal/cmd/agent/deployer/data/convert"
"github.com/rancher/fleet/internal/cmd/agent/deployer/kv"
"github.com/rancher/fleet/internal/config"
fleetv1 "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1/summary"

"github.com/sirupsen/logrus"
Expand All @@ -22,9 +23,8 @@ import (
)

const (
kindSep = ", Kind="
reason = "%REASON%"
checkGVKErrorMappingEnvVar = "CATTLE_WRANGLER_CHECK_GVK_ERROR_MAPPING"
kindSep = ", Kind="
reason = "%REASON%"
)

var (
Expand Down Expand Up @@ -152,7 +152,7 @@ func init() {
}

func initializeCheckErrors() {
gvkConfig := os.Getenv(checkGVKErrorMappingEnvVar)
gvkConfig := os.Getenv(config.EnvVarWranglerCheckGVKErrorMapping)
if gvkConfig != "" {
logrus.Debugf("GVK Error Mapping Provided")
gvkErrorMapping := ConditionTypeStatusErrorMapping{}
Expand Down
9 changes: 5 additions & 4 deletions internal/cmd/agent/deployer/summary/summarizers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/rancher/fleet/internal/cmd/agent/deployer/data"
"github.com/rancher/fleet/internal/config"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1/summary"
"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -173,7 +174,7 @@ func TestCheckErrors(t *testing.T) {
},
},
loadConditions: func() {
t.Setenv(checkGVKErrorMappingEnvVar, `
t.Setenv(config.EnvVarWranglerCheckGVKErrorMapping, `
[
{
"gvk": "sample.cattle.io/v1, Kind=Sample",
Expand Down Expand Up @@ -210,7 +211,7 @@ func TestCheckErrors(t *testing.T) {
},
},
loadConditions: func() {
t.Setenv(checkGVKErrorMappingEnvVar, `
t.Setenv(config.EnvVarWranglerCheckGVKErrorMapping, `
[
{
"gvk": "sample.cattle.io/v1, Kind=Sample",
Expand Down Expand Up @@ -294,7 +295,7 @@ func TestCheckErrors(t *testing.T) {
},
},
loadConditions: func() {
t.Setenv(checkGVKErrorMappingEnvVar, `
t.Setenv(config.EnvVarWranglerCheckGVKErrorMapping, `
[
{
"gvk": "sample.cattle.io/v1, Kind=Sample",
Expand Down Expand Up @@ -332,7 +333,7 @@ func TestCheckErrors(t *testing.T) {
},
},
loadConditions: func() {
t.Setenv(checkGVKErrorMappingEnvVar, `
t.Setenv(config.EnvVarWranglerCheckGVKErrorMapping, `
[
{
"gvk": "sample.cattle.io/v1, Kind=Sample",
Expand Down
16 changes: 16 additions & 0 deletions internal/cmd/controller/agentmanagement/agent/manifest.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package agent

import (
"os"
"path"
"strconv"
"strings"
Expand Down Expand Up @@ -304,6 +305,12 @@ func agentApp(namespace string, agentScope string, opts ManifestOptions) *appsv1
}

// additional env vars from cluster
if gvkVal := os.Getenv(config.EnvVarWranglerCheckGVKErrorMapping); gvkVal != "" && !envVarPresent(opts.AgentEnvVars, config.EnvVarWranglerCheckGVKErrorMapping) {
container.Env = append(container.Env, corev1.EnvVar{
Name: config.EnvVarWranglerCheckGVKErrorMapping,
Value: gvkVal,
})
}
if opts.AgentEnvVars != nil {
container.Env = append(container.Env, opts.AgentEnvVars...)
}
Expand All @@ -324,6 +331,15 @@ func agentApp(namespace string, agentScope string, opts ManifestOptions) *appsv1
return app
}

func envVarPresent(vars []corev1.EnvVar, name string) bool {
for _, v := range vars {
if v.Name == name {
return true
}
}
return false
}

func serviceAccount(namespace, name string) *corev1.ServiceAccount {
return &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Expand Down
80 changes: 80 additions & 0 deletions internal/cmd/controller/agentmanagement/agent/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/rancher/fleet/internal/cmd"
"github.com/rancher/fleet/internal/cmd/controller/agentmanagement/agent"
"github.com/rancher/fleet/internal/config"
)

const namespace = "fleet-system"
Expand Down Expand Up @@ -336,3 +337,82 @@ func TestPriorityClassName(t *testing.T) {
})
}
}

func findEnvVar(containers []corev1.Container, name string) (corev1.EnvVar, bool) {
for _, c := range containers {
for _, e := range c.Env {
if e.Name == name {
return e, true
}
}
}
return corev1.EnvVar{}, false
}

func TestManifestCheckGVKErrorMappingEnvVar(t *testing.T) {
baseOpts := agent.ManifestOptions{
LeaderElectionOptions: leaderOpts,
}

singleMapping := `[{"gvk":"sample.cattle.io/v1, Kind=Sample","conditionMappings":[{"type":"Failed","status":["True"]}]}]`
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"]}]}]`

for _, tc := range []struct {
name string
envValue string
agentEnvVars []corev1.EnvVar
expectFound bool
expectedValue string
}{
{
name: "env var not set",
envValue: "",
expectFound: false,
},
{
name: "single GVK mapping",
envValue: singleMapping,
expectFound: true,
expectedValue: singleMapping,
},
{
name: "multiple GVK mappings",
envValue: multiMapping,
expectFound: true,
expectedValue: multiMapping,
},
{
name: "env var already in AgentEnvVars is not duplicated",
envValue: singleMapping,
agentEnvVars: []corev1.EnvVar{
{Name: config.EnvVarWranglerCheckGVKErrorMapping, Value: multiMapping},
},
expectFound: true,
expectedValue: multiMapping,
},
} {
t.Run(tc.name, func(t *testing.T) {
t.Setenv(config.EnvVarWranglerCheckGVKErrorMapping, tc.envValue)

opts := baseOpts
opts.AgentEnvVars = tc.agentEnvVars

d := getAgentFromManifests("test-scope", opts)
if d == nil {
t.Fatal("no deployment returned from manifests")
}

envVar, found := findEnvVar(d.Spec.Template.Spec.Containers, config.EnvVarWranglerCheckGVKErrorMapping)
if found != tc.expectFound {
if tc.expectFound {
t.Fatalf("env var %s not found in agent container", config.EnvVarWranglerCheckGVKErrorMapping)
} else {
t.Fatalf("env var %s unexpectedly found in agent container with value %q", config.EnvVarWranglerCheckGVKErrorMapping, envVar.Value)
}
}
if tc.expectFound && envVar.Value != tc.expectedValue {
t.Fatalf("expected %s=%q, got %q", config.EnvVarWranglerCheckGVKErrorMapping, tc.expectedValue, envVar.Value)
}
})
}
Comment thread
0xavi0 marked this conversation as resolved.
}
3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const (
// server.
APIServerCAKey = "apiServerCA"

// EnvVarWranglerCheckGVKErrorMapping is the env var name used to configure Wrangler's GVK error mapping.
EnvVarWranglerCheckGVKErrorMapping = "CATTLE_WRANGLER_CHECK_GVK_ERROR_MAPPING"

// Default secret name for git credentials, used as a fallback if no secret is referenced by an app.
DefaultGitCredentialsSecretName = "gitcredential" //nolint:gosec // this is a resource name

Expand Down