Skip to content

Commit

Permalink
(fleet) env cleanup (#31628)
Browse files Browse the repository at this point in the history
  • Loading branch information
arbll authored Dec 1, 2024
1 parent c6d0c1e commit 8ea4e93
Show file tree
Hide file tree
Showing 30 changed files with 126 additions and 132 deletions.
4 changes: 2 additions & 2 deletions cmd/installer/subcommands/installer/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (

"github.com/DataDog/datadog-agent/cmd/installer/command"
"github.com/DataDog/datadog-agent/pkg/fleet/bootstrapper"
"github.com/DataDog/datadog-agent/pkg/fleet/env"
"github.com/DataDog/datadog-agent/pkg/fleet/installer"
"github.com/DataDog/datadog-agent/pkg/fleet/installer/env"
"github.com/DataDog/datadog-agent/pkg/fleet/telemetry"
"github.com/DataDog/datadog-agent/pkg/version"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -226,7 +226,7 @@ func newTelemetry(env *env.Env) *telemetry.Telemetry {
if site == "" {
site = config.Site
}
t, err := telemetry.NewTelemetry(apiKey, site, "datadog-installer") // No sampling rules for commands
t, err := telemetry.NewTelemetry(env.HTTPClient(), apiKey, site, "datadog-installer") // No sampling rules for commands
if err != nil {
fmt.Printf("failed to initialize telemetry: %v\n", err)
return nil
Expand Down
17 changes: 11 additions & 6 deletions comp/updater/telemetry/telemetryimpl/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
package telemetryimpl

import (
"net/http"

"go.uber.org/fx"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"

"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/comp/updater/telemetry"
"github.com/DataDog/datadog-agent/pkg/fleet/env"
"github.com/DataDog/datadog-agent/pkg/config/utils"
fleettelemetry "github.com/DataDog/datadog-agent/pkg/fleet/telemetry"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
httputils "github.com/DataDog/datadog-agent/pkg/util/http"
)

type dependencies struct {
Expand All @@ -32,12 +35,14 @@ func Module() fxutil.Module {
}

func newTelemetry(deps dependencies) (telemetry.Component, error) {
env := env.FromConfig(deps.Config)
telemetry, err := fleettelemetry.NewTelemetry(env.APIKey, env.Site, "datadog-installer",
client := &http.Client{
Transport: httputils.CreateHTTPTransport(deps.Config),
}
telemetry, err := fleettelemetry.NewTelemetry(client, utils.SanitizeAPIKey(deps.Config.GetString("api_key")), deps.Config.GetString("site"), "datadog-installer-daemon",
fleettelemetry.WithSamplingRules(
tracer.NameServiceRule("cdn.*", "datadog-installer", 0.1),
tracer.NameServiceRule("*garbage_collect*", "datadog-installer", 0.05),
tracer.NameServiceRule("HTTPClient.*", "datadog-installer", 0.05),
tracer.NameServiceRule("cdn.*", "datadog-installer-daemon", 0.1),
tracer.NameServiceRule("*garbage_collect*", "datadog-installer-daemon", 0.05),
tracer.NameServiceRule("HTTPClient.*", "datadog-installer-daemon", 0.05),
),
)
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion comp/updater/updater/updaterimpl/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
package updaterimpl

import (
"context"
"errors"
"fmt"

"go.uber.org/fx"

"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/comp/core/hostname"
log "github.com/DataDog/datadog-agent/comp/core/log/def"
"github.com/DataDog/datadog-agent/comp/remote-config/rcservice"
updatercomp "github.com/DataDog/datadog-agent/comp/updater/updater"
Expand All @@ -36,6 +38,7 @@ func Module() fxutil.Module {
type dependencies struct {
fx.In

Hostname hostname.Component
Log log.Component
Config config.Component
RemoteConfig optional.Option[rcservice.Component]
Expand All @@ -46,7 +49,11 @@ func newUpdaterComponent(lc fx.Lifecycle, dependencies dependencies) (updatercom
if !ok {
return nil, errRemoteConfigRequired
}
daemon, err := daemon.NewDaemon(remoteConfig, dependencies.Config)
hostname, err := dependencies.Hostname.Get(context.Background())
if err != nil {
return nil, fmt.Errorf("could not get hostname: %w", err)
}
daemon, err := daemon.NewDaemon(hostname, remoteConfig, dependencies.Config)
if err != nil {
return nil, fmt.Errorf("could not create updater: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/fleet/bootstrapper/bootstrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"context"
"fmt"

"github.com/DataDog/datadog-agent/pkg/fleet/env"
"github.com/DataDog/datadog-agent/pkg/fleet/installer/env"
"github.com/DataDog/datadog-agent/pkg/fleet/internal/bootstrap"
"github.com/DataDog/datadog-agent/pkg/fleet/internal/exec"
"github.com/DataDog/datadog-agent/pkg/fleet/internal/oci"
Expand Down
23 changes: 20 additions & 3 deletions pkg/fleet/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
osexec "os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"time"

Expand All @@ -24,8 +25,9 @@ import (

"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/pkg/config/remote/client"
"github.com/DataDog/datadog-agent/pkg/fleet/env"
"github.com/DataDog/datadog-agent/pkg/config/utils"
"github.com/DataDog/datadog-agent/pkg/fleet/installer"
"github.com/DataDog/datadog-agent/pkg/fleet/installer/env"
installerErrors "github.com/DataDog/datadog-agent/pkg/fleet/installer/errors"
"github.com/DataDog/datadog-agent/pkg/fleet/installer/repository"
"github.com/DataDog/datadog-agent/pkg/fleet/internal/bootstrap"
Expand Down Expand Up @@ -83,7 +85,7 @@ func newInstaller(env *env.Env, installerBin string) installer.Installer {
}

// NewDaemon returns a new daemon.
func NewDaemon(rcFetcher client.ConfigFetcher, config config.Reader) (Daemon, error) {
func NewDaemon(hostname string, rcFetcher client.ConfigFetcher, config config.Reader) (Daemon, error) {
installerBin, err := os.Executable()
if err != nil {
return nil, fmt.Errorf("could not get installer executable path: %w", err)
Expand All @@ -96,7 +98,22 @@ func NewDaemon(rcFetcher client.ConfigFetcher, config config.Reader) (Daemon, er
if err != nil {
return nil, fmt.Errorf("could not create remote config client: %w", err)
}
env := env.FromConfig(config)
env := &env.Env{
APIKey: utils.SanitizeAPIKey(config.GetString("api_key")),
Site: config.GetString("site"),
RemoteUpdates: config.GetBool("remote_updates"),
RemotePolicies: config.GetBool("remote_policies"),
Mirror: config.GetString("installer.mirror"),
RegistryOverride: config.GetString("installer.registry.url"),
RegistryAuthOverride: config.GetString("installer.registry.auth"),
RegistryUsername: config.GetString("installer.registry.username"),
RegistryPassword: config.GetString("installer.registry.password"),
Tags: utils.GetConfiguredTags(config, false),
Hostname: hostname,
HTTPProxy: config.GetString("proxy.http"),
HTTPSProxy: config.GetString("proxy.https"),
NoProxy: strings.Join(config.GetStringSlice("proxy.no_proxy"), ","),
}
installer := newInstaller(env, installerBin)
cdn, err := cdn.New(env, filepath.Join(paths.RunPath, "rc_daemon"))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/fleet/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/DataDog/datadog-agent/pkg/fleet/env"
"github.com/DataDog/datadog-agent/pkg/fleet/installer/env"
"github.com/DataDog/datadog-agent/pkg/fleet/installer/repository"
"github.com/DataDog/datadog-agent/pkg/fleet/internal/cdn"
pbgo "github.com/DataDog/datadog-agent/pkg/proto/pbgo/core"
Expand Down
22 changes: 0 additions & 22 deletions pkg/fleet/env/http_client.go

This file was deleted.

33 changes: 0 additions & 33 deletions pkg/fleet/env/install_script.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/fleet/installer/default_packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"slices"
"strings"

"github.com/DataDog/datadog-agent/pkg/fleet/env"
"github.com/DataDog/datadog-agent/pkg/fleet/installer/env"
"github.com/DataDog/datadog-agent/pkg/fleet/internal/oci"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/fleet/installer/default_packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package installer
import (
"testing"

"github.com/DataDog/datadog-agent/pkg/fleet/env"
"github.com/DataDog/datadog-agent/pkg/fleet/installer/env"
"github.com/DataDog/datadog-agent/pkg/fleet/internal/oci"
"github.com/stretchr/testify/assert"
)
Expand Down
83 changes: 52 additions & 31 deletions pkg/fleet/env/env.go → pkg/fleet/installer/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
package env

import (
"context"
"fmt"
"net"
"net/http"
"net/url"
"os"
"slices"
"strings"
"time"

"github.com/DataDog/datadog-agent/pkg/config/model"
"github.com/DataDog/datadog-agent/pkg/config/utils"
"github.com/DataDog/datadog-agent/pkg/util/hostname"
"golang.org/x/net/http/httpproxy"
)

const (
Expand Down Expand Up @@ -49,6 +49,9 @@ const (
envHTTPSProxy = "HTTPS_PROXY"
envDDNoProxy = "DD_PROXY_NO_PROXY"
envNoProxy = "NO_PROXY"

// install script
envApmInstrumentationEnabled = "DD_APM_INSTRUMENTATION_ENABLED"
)

var defaultEnv = Env{
Expand Down Expand Up @@ -80,6 +83,22 @@ type ApmLibLanguage string
// ApmLibVersion is the version of the library defined in DD_APM_INSTRUMENTATION_LIBRARIES env var
type ApmLibVersion string

const (
// APMInstrumentationEnabledAll enables APM instrumentation for all containers.
APMInstrumentationEnabledAll = "all"
// APMInstrumentationEnabledDocker enables APM instrumentation for Docker containers.
APMInstrumentationEnabledDocker = "docker"
// APMInstrumentationEnabledHost enables APM instrumentation for the host.
APMInstrumentationEnabledHost = "host"
// APMInstrumentationNotSet is the default value when the environment variable is not set.
APMInstrumentationNotSet = "not_set"
)

// InstallScriptEnv contains the environment variables for the install script.
type InstallScriptEnv struct {
APMInstrumentationEnabled string
}

// Env contains the configuration for the installer.
type Env struct {
APIKey string
Expand Down Expand Up @@ -119,6 +138,32 @@ type Env struct {
NoProxy string
}

// HTTPClient returns an HTTP client with the proxy settings from the environment.
func (e *Env) HTTPClient() *http.Client {
proxyConfig := &httpproxy.Config{
HTTPProxy: e.HTTPProxy,
HTTPSProxy: e.HTTPSProxy,
NoProxy: e.NoProxy,
}
proxyFunc := func(r *http.Request) (*url.URL, error) {
return proxyConfig.ProxyFunc()(r.URL)
}
client := &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
Proxy: proxyFunc,
},
}
return client
}

// FromEnv returns an Env struct with values from the environment.
func FromEnv() *Env {
splitFunc := func(c rune) bool {
Expand Down Expand Up @@ -150,7 +195,9 @@ func FromEnv() *Env {
AgentMinorVersion: os.Getenv(envAgentMinorVersion),
AgentUserName: getEnvOrDefault(envAgentUserName, os.Getenv(envAgentUserNameCompat)),

InstallScript: installScriptEnvFromEnv(),
InstallScript: InstallScriptEnv{
APMInstrumentationEnabled: getEnvOrDefault(envApmInstrumentationEnabled, APMInstrumentationNotSet),
},

CDNEnabled: strings.ToLower(os.Getenv(envCDNEnabled)) == "true",
CDNLocalDirPath: getEnvOrDefault(envCDNLocalDirPath, ""),
Expand All @@ -167,32 +214,6 @@ func FromEnv() *Env {
}
}

// FromConfig returns an Env struct with values from the configuration.
func FromConfig(config model.Reader) *Env {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
hostname, err := hostname.Get(ctx)
if err != nil {
hostname = "unknown"
}
return &Env{
APIKey: utils.SanitizeAPIKey(config.GetString("api_key")),
Site: config.GetString("site"),
RemoteUpdates: config.GetBool("remote_updates"),
RemotePolicies: config.GetBool("remote_policies"),
Mirror: config.GetString("installer.mirror"),
RegistryOverride: config.GetString("installer.registry.url"),
RegistryAuthOverride: config.GetString("installer.registry.auth"),
RegistryUsername: config.GetString("installer.registry.username"),
RegistryPassword: config.GetString("installer.registry.password"),
Tags: utils.GetConfiguredTags(config, false),
Hostname: hostname,
HTTPProxy: config.GetString("proxy.http"),
HTTPSProxy: config.GetString("proxy.https"),
NoProxy: strings.Join(config.GetStringSlice("proxy.no_proxy"), ","),
}
}

// ToEnv returns a slice of environment variables from the Env struct.
func (e *Env) ToEnv() []string {
var env []string
Expand Down
File renamed without changes.
Loading

0 comments on commit 8ea4e93

Please sign in to comment.