From aabad0a65917dcae2c93ff33e383b7969dd1992b Mon Sep 17 00:00:00 2001 From: Gabriel Dos Santos Date: Tue, 8 Oct 2024 19:20:41 +0000 Subject: [PATCH] Removing pkgconfigsetup global func from helper functions --- .../admission/mutate/agent_sidecar/agent_sidecar.go | 6 +++--- .../admission/mutate/agent_sidecar/profiles.go | 10 +++++----- .../admission/mutate/agent_sidecar/profiles_test.go | 4 ++-- .../admission/mutate/agent_sidecar/providers.go | 6 +++--- .../admission/mutate/agent_sidecar/providers_test.go | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/pkg/clusteragent/admission/mutate/agent_sidecar/agent_sidecar.go b/pkg/clusteragent/admission/mutate/agent_sidecar/agent_sidecar.go index 8036a709217cc..c8b2c6c4eb2eb 100644 --- a/pkg/clusteragent/admission/mutate/agent_sidecar/agent_sidecar.go +++ b/pkg/clusteragent/admission/mutate/agent_sidecar/agent_sidecar.go @@ -137,7 +137,7 @@ func (w *Webhook) injectAgentSidecar(pod *corev1.Pod, _ string, _ dynamic.Interf podUpdated = true } - updated, err := applyProviderOverrides(pod) + updated, err := applyProviderOverrides(pod, w.datadogConfig) if err != nil { log.Errorf("Failed to apply provider overrides: %v", err) return podUpdated, errors.New(metrics.InvalidInput) @@ -148,7 +148,7 @@ func (w *Webhook) injectAgentSidecar(pod *corev1.Pod, _ string, _ dynamic.Interf // highest override-priority. They only apply to the agent sidecar container. for i := range pod.Spec.Containers { if pod.Spec.Containers[i].Name == agentSidecarContainerName { - updated, err = applyProfileOverrides(&pod.Spec.Containers[i]) + updated, err = applyProfileOverrides(&pod.Spec.Containers[i], w.datadogConfig) if err != nil { log.Errorf("Failed to apply profile overrides: %v", err) return podUpdated, errors.New(metrics.InvalidInput) @@ -257,7 +257,7 @@ func labelSelectors(datadogConfig config.Component) (namespaceSelector, objectSe selectorsJSON := datadogConfig.GetString("admission_controller.agent_sidecar.selectors") // Get sidecar profiles - _, err := loadSidecarProfiles() + _, err := loadSidecarProfiles(datadogConfig) if err != nil { log.Errorf("encountered issue when loading sidecar profiles: %s", err) return nil, nil diff --git a/pkg/clusteragent/admission/mutate/agent_sidecar/profiles.go b/pkg/clusteragent/admission/mutate/agent_sidecar/profiles.go index c7367f62919d1..b5c05897c2632 100644 --- a/pkg/clusteragent/admission/mutate/agent_sidecar/profiles.go +++ b/pkg/clusteragent/admission/mutate/agent_sidecar/profiles.go @@ -13,7 +13,7 @@ import ( corev1 "k8s.io/api/core/v1" - pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup" + "github.com/DataDog/datadog-agent/comp/core/config" ) //////////////////////////////// @@ -32,9 +32,9 @@ type ProfileOverride struct { // loadSidecarProfiles returns the profile overrides provided by the user // It returns an error in case of miss-configuration or in case more than // one profile is configured -func loadSidecarProfiles() ([]ProfileOverride, error) { +func loadSidecarProfiles(datadogConfig config.Component) ([]ProfileOverride, error) { // Read and parse profiles - profilesJSON := pkgconfigsetup.Datadog().GetString("admission_controller.agent_sidecar.profiles") + profilesJSON := datadogConfig.GetString("admission_controller.agent_sidecar.profiles") var profiles []ProfileOverride @@ -52,12 +52,12 @@ func loadSidecarProfiles() ([]ProfileOverride, error) { // applyProfileOverrides applies the profile overrides to the container. It // returns a boolean that indicates if the container was mutated -func applyProfileOverrides(container *corev1.Container) (bool, error) { +func applyProfileOverrides(container *corev1.Container, datadogConfig config.Component) (bool, error) { if container == nil { return false, fmt.Errorf("can't apply profile overrides to nil containers") } - profiles, err := loadSidecarProfiles() + profiles, err := loadSidecarProfiles(datadogConfig) if err != nil { return false, err diff --git a/pkg/clusteragent/admission/mutate/agent_sidecar/profiles_test.go b/pkg/clusteragent/admission/mutate/agent_sidecar/profiles_test.go index 5414e60253654..fdbb17364eee0 100644 --- a/pkg/clusteragent/admission/mutate/agent_sidecar/profiles_test.go +++ b/pkg/clusteragent/admission/mutate/agent_sidecar/profiles_test.go @@ -117,7 +117,7 @@ func TestLoadSidecarProfiles(t *testing.T) { for _, test := range tests { t.Run(test.name, func(tt *testing.T) { mockConfig.SetWithoutSource("admission_controller.agent_sidecar.profiles", test.profilesJSON) - profiles, err := loadSidecarProfiles() + profiles, err := loadSidecarProfiles(mockConfig) if test.expectError { assert.Error(tt, err) @@ -361,7 +361,7 @@ func TestApplyProfileOverrides(t *testing.T) { for _, test := range tests { t.Run(test.name, func(tt *testing.T) { mockConfig.SetWithoutSource("admission_controller.agent_sidecar.profiles", test.profilesJSON) - mutated, err := applyProfileOverrides(test.baseContainer) + mutated, err := applyProfileOverrides(test.baseContainer, mockConfig) assert.Equal(tt, test.expectMutated, mutated) diff --git a/pkg/clusteragent/admission/mutate/agent_sidecar/providers.go b/pkg/clusteragent/admission/mutate/agent_sidecar/providers.go index 0f03cadbb8509..60e24f331c6a7 100644 --- a/pkg/clusteragent/admission/mutate/agent_sidecar/providers.go +++ b/pkg/clusteragent/admission/mutate/agent_sidecar/providers.go @@ -13,9 +13,9 @@ import ( corev1 "k8s.io/api/core/v1" + "github.com/DataDog/datadog-agent/comp/core/config" "github.com/DataDog/datadog-agent/pkg/clusteragent/admission/mutate/common" configWebhook "github.com/DataDog/datadog-agent/pkg/clusteragent/admission/mutate/config" - pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup" "github.com/DataDog/datadog-agent/pkg/util/pointer" ) @@ -55,8 +55,8 @@ func providerIsSupported(provider string) bool { // applyProviderOverrides applies the necessary overrides for the provider // configured. It returns a boolean that indicates if the pod was mutated. -func applyProviderOverrides(pod *corev1.Pod) (bool, error) { - provider := pkgconfigsetup.Datadog().GetString("admission_controller.agent_sidecar.provider") +func applyProviderOverrides(pod *corev1.Pod, datadogConfig config.Component) (bool, error) { + provider := datadogConfig.GetString("admission_controller.agent_sidecar.provider") if !providerIsSupported(provider) { return false, fmt.Errorf("unsupported provider: %v", provider) diff --git a/pkg/clusteragent/admission/mutate/agent_sidecar/providers_test.go b/pkg/clusteragent/admission/mutate/agent_sidecar/providers_test.go index 236d9d3257031..c3aaf1674cfae 100644 --- a/pkg/clusteragent/admission/mutate/agent_sidecar/providers_test.go +++ b/pkg/clusteragent/admission/mutate/agent_sidecar/providers_test.go @@ -416,7 +416,7 @@ func TestApplyProviderOverrides(t *testing.T) { for _, test := range tests { t.Run(test.name, func(tt *testing.T) { mockConfig.SetWithoutSource("admission_controller.agent_sidecar.provider", test.provider) - mutated, err := applyProviderOverrides(test.basePod) + mutated, err := applyProviderOverrides(test.basePod, mockConfig) if test.expectError { assert.Error(tt, err)