Skip to content

Commit

Permalink
Removing pkgconfigsetup global func from helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
gabedos committed Oct 8, 2024
1 parent 60a9122 commit aabad0a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions pkg/clusteragent/admission/mutate/agent_sidecar/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

////////////////////////////////
Expand All @@ -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

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions pkg/clusteragent/admission/mutate/agent_sidecar/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit aabad0a

Please sign in to comment.