Skip to content

Commit

Permalink
[installer] fix purge logging and telemetry (#24782)
Browse files Browse the repository at this point in the history
* * Inject logger and telemetry client in purge command
* Propagate context and errors trhough purge command

* fix tests
  • Loading branch information
paullegranddc authored and CelianR committed Apr 26, 2024
1 parent 1f920d5 commit 338e3f1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
34 changes: 26 additions & 8 deletions cmd/installer/subcommands/purge/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@
package purge

import (
"context"

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

"github.com/DataDog/datadog-agent/cmd/installer/command"
"github.com/DataDog/datadog-agent/comp/core"
"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/comp/core/log"
"github.com/DataDog/datadog-agent/comp/core/log/logimpl"
"github.com/DataDog/datadog-agent/comp/core/secrets"
"github.com/DataDog/datadog-agent/comp/core/sysprobeconfig/sysprobeconfigimpl"
"github.com/DataDog/datadog-agent/comp/updater/telemetry"
"github.com/DataDog/datadog-agent/comp/updater/telemetry/telemetryimpl"
"github.com/DataDog/datadog-agent/pkg/installer"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
)
Expand All @@ -30,7 +39,8 @@ func Commands(global *command.GlobalParams) []*cobra.Command {
Short: "Purge installer packages",
Long: ``,
RunE: func(_ *cobra.Command, _ []string) error {
return purgeFxWrapper(&cliParams{
ctx := context.Background()
return purgeFxWrapper(ctx, &cliParams{
GlobalParams: *global,
pkg: pkg,
})
Expand All @@ -40,21 +50,29 @@ func Commands(global *command.GlobalParams) []*cobra.Command {
return []*cobra.Command{purgeCmd}
}

func purgeFxWrapper(params *cliParams) error {
func purgeFxWrapper(ctx context.Context, params *cliParams) error {
return fxutil.OneShot(purge,
fx.Provide(func() context.Context { return ctx }),
fx.Supply(params),
fx.Supply(core.BundleParams{
LogParams: logimpl.ForOneShot("INSTALLER", "info", true),
ConfigParams: config.NewAgentParams(params.GlobalParams.ConfFilePath),
SecretParams: secrets.NewEnabledParams(),
SysprobeConfigParams: sysprobeconfigimpl.NewParams(),
LogParams: logimpl.ForOneShot("INSTALLER", "info", true),
}),
core.Bundle(),
telemetryimpl.Module(),
)
}

func purge(params *cliParams) error {
func purge(ctx context.Context, params *cliParams, _ log.Component, _ telemetry.Component) (err error) {
span, ctx := tracer.StartSpanFromContext(ctx, "cmd/purge")
defer func() { span.Finish(tracer.WithError(err)) }()

span.SetTag("params.pkg", params.pkg)

if params.pkg != "" {
installer.PurgePackage(params.pkg)
} else {
installer.Purge()
return installer.PurgePackage(ctx, params.pkg)
}
return nil
return installer.Purge(ctx)
}
26 changes: 14 additions & 12 deletions pkg/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,42 +122,43 @@ func Bootstrap(ctx context.Context, config config.Reader) error {
}

// Purge removes files installed by the installer
func Purge() {
purge(defaultLocksPath, defaultRepositoriesPath)
func Purge(ctx context.Context) error {
return purge(ctx, defaultLocksPath, defaultRepositoriesPath)
}

// PurgePackage removes an individual package
func PurgePackage(pkg string) {
purgePackage(pkg, defaultLocksPath, defaultRepositoriesPath)
func PurgePackage(ctx context.Context, pkg string) error {
return purgePackage(ctx, pkg, defaultLocksPath, defaultRepositoriesPath)
}

func purgePackage(pkg string, locksPath, repositoryPath string) {
var err error
span, ctx := tracer.StartSpanFromContext(context.Background(), "purge_pkg")
func purgePackage(ctx context.Context, pkg string, locksPath, repositoryPath string) (err error) {
span, ctx := tracer.StartSpanFromContext(ctx, "purge_pkg")
defer span.Finish(tracer.WithError(err))
span.SetTag("params.pkg", pkg)

switch pkg {
case "datadog-installer":
service.RemoveInstallerUnits(ctx)
case "datadog-agent":
service.RemoveAgentUnits(ctx)
case "datadog-apm-inject":
// todo(paullgdc): should we continue with removing the pkg directories if we failed here
// or should exit early?
if err = service.RemoveAPMInjector(ctx); err != nil {
log.Warnf("installer: could not remove APM injector: %v", err)
}
default:
log.Warnf("installer: unrecognized package purge")
return
return nil
}
if err = removePkgDirs(ctx, pkg, locksPath, repositoryPath); err != nil {
log.Warnf("installer: %v", err)
return err
}
return nil
}

func purge(locksPath, repositoryPath string) {
var err error
span, ctx := tracer.StartSpanFromContext(context.Background(), "purge")
func purge(ctx context.Context, locksPath, repositoryPath string) (err error) {
span, ctx := tracer.StartSpanFromContext(ctx, "purge")
defer span.Finish(tracer.WithError(err))

service.RemoveInstallerUnits(ctx)
Expand All @@ -173,6 +174,7 @@ func purge(locksPath, repositoryPath string) {

cleanDir(locksPath, os.RemoveAll)
cleanDir(repositoryPath, func(path string) error { return service.RemoveAll(ctx, path) })
return nil
}

func removePkgDirs(ctx context.Context, pkg string, locksPath, repositoryPath string) (err error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/installer/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func TestPurge(t *testing.T) {
assert.Nil(t, os.WriteFile(filepath.Join(locksPath, "not_empty"), []byte("morbier\n"), 0644))
assertDirNotEmpty(t, locksPath)
assertDirNotEmpty(t, rootPath)
purge(locksPath, rootPath)
purge(testCtx, locksPath, rootPath)
assertDirExistAndEmpty(t, locksPath)
assertDirExistAndEmpty(t, rootPath)
bootstrapAndAssert()
Expand Down

0 comments on commit 338e3f1

Please sign in to comment.