From b5806134719bdefe830229d883f042e4173d63d5 Mon Sep 17 00:00:00 2001 From: Marcell Sevcsik <31651557+0sewa0@users.noreply.github.com> Date: Tue, 23 Aug 2022 12:36:04 +0200 Subject: [PATCH] Make extractor handle standalone usecase (#1077) --- .../csi/provisioner/processmoduleconfig.go | 4 ++-- src/installer/url/config.go | 5 +---- src/installer/url/installer.go | 3 ++- src/installer/url/installer_test.go | 3 ++- src/installer/zip/zip.go | 17 +++++++++++++++-- src/standalone/run.go | 10 ++++++---- 6 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/controllers/csi/provisioner/processmoduleconfig.go b/src/controllers/csi/provisioner/processmoduleconfig.go index eea0ded802..8c34fe230e 100644 --- a/src/controllers/csi/provisioner/processmoduleconfig.go +++ b/src/controllers/csi/provisioner/processmoduleconfig.go @@ -2,7 +2,7 @@ package csiprovisioner import ( "encoding/json" - "io/ioutil" + "io" "os" "github.com/Dynatrace/dynatrace-operator/src/dtclient" @@ -63,7 +63,7 @@ func (provisioner *OneAgentProvisioner) readProcessModuleConfigCache(tenantUUID return nil, err } - jsonBytes, err := ioutil.ReadAll(processModuleConfigCacheFile) + jsonBytes, err := io.ReadAll(processModuleConfigCacheFile) if err != nil { if err := processModuleConfigCacheFile.Close(); err != nil { log.Error(errors.WithStack(err), "error closing file after trying to read it") diff --git a/src/installer/url/config.go b/src/installer/url/config.go index 3534afc6bc..17d467d077 100644 --- a/src/installer/url/config.go +++ b/src/installer/url/config.go @@ -1,14 +1,11 @@ package url import ( - "path/filepath" - "github.com/Dynatrace/dynatrace-operator/src/logger" ) var ( - log = logger.NewDTLogger().WithName("oneagent-url-installer") - standaloneBinDir = filepath.Join("mnt", "bin") + log = logger.NewDTLogger().WithName("oneagent-url-installer") ) const ( diff --git a/src/installer/url/installer.go b/src/installer/url/installer.go index 965dcb9cb7..922b630441 100644 --- a/src/installer/url/installer.go +++ b/src/installer/url/installer.go @@ -3,6 +3,7 @@ package url import ( "os" + "github.com/Dynatrace/dynatrace-operator/src/config" "github.com/Dynatrace/dynatrace-operator/src/controllers/csi/metadata" "github.com/Dynatrace/dynatrace-operator/src/dtclient" "github.com/Dynatrace/dynatrace-operator/src/installer/symlink" @@ -92,7 +93,7 @@ func (installer UrlInstaller) installAgentFromUrl(targetDir string) error { } func (installer UrlInstaller) isAlreadyDownloaded(targetDir string) bool { - if standaloneBinDir == targetDir { + if config.AgentBinDirMount == targetDir { return false } _, err := installer.fs.Stat(targetDir) diff --git a/src/installer/url/installer_test.go b/src/installer/url/installer_test.go index 207bce2db7..e54a38865a 100644 --- a/src/installer/url/installer_test.go +++ b/src/installer/url/installer_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/Dynatrace/dynatrace-operator/src/arch" + "github.com/Dynatrace/dynatrace-operator/src/config" "github.com/Dynatrace/dynatrace-operator/src/controllers/csi/metadata" "github.com/Dynatrace/dynatrace-operator/src/dtclient" "github.com/Dynatrace/dynatrace-operator/src/installer/zip" @@ -204,7 +205,7 @@ func TestIsAlreadyDownloaded(t *testing.T) { }) t.Run(`false if standalone`, func(t *testing.T) { fs := afero.NewMemMapFs() - targetDir := standaloneBinDir + targetDir := config.AgentBinDirMount installer := &UrlInstaller{ fs: fs, props: &Properties{}, diff --git a/src/installer/zip/zip.go b/src/installer/zip/zip.go index ab91b69153..6a342aaa1d 100644 --- a/src/installer/zip/zip.go +++ b/src/installer/zip/zip.go @@ -7,6 +7,7 @@ import ( "path/filepath" "strings" + "github.com/Dynatrace/dynatrace-operator/src/config" "github.com/Dynatrace/dynatrace-operator/src/installer/common" "github.com/klauspost/compress/zip" "github.com/pkg/errors" @@ -32,12 +33,24 @@ func (extractor OneAgentExtractor) ExtractZip(sourceFile afero.File, targetDir s return errors.WithStack(err) } - err = extractFilesFromZip(fs, extractor.pathResolver.AgentTempUnzipDir(), reader) + extractDest := extractor.pathResolver.AgentTempUnzipDir() + if extractor.pathResolver.RootDir == config.AgentBinDirMount { + extractDest = targetDir + } + + err = extractFilesFromZip(fs, extractDest, reader) if err != nil { log.Info("failed to extract files from zip", "err", err) return err } - return extractor.moveToTargetDir(targetDir) + if extractDest != targetDir { + err := extractor.moveToTargetDir(targetDir) + if err != nil { + log.Info("failed to move file to final destination", "err", err) + return err + } + } + return nil } func extractFilesFromZip(fs afero.Fs, targetDir string, reader *zip.Reader) error { diff --git a/src/standalone/run.go b/src/standalone/run.go index af90f77aa9..09fca503b9 100644 --- a/src/standalone/run.go +++ b/src/standalone/run.go @@ -6,6 +6,7 @@ import ( "github.com/Dynatrace/dynatrace-operator/src/arch" "github.com/Dynatrace/dynatrace-operator/src/config" + "github.com/Dynatrace/dynatrace-operator/src/controllers/csi/metadata" "github.com/Dynatrace/dynatrace-operator/src/dtclient" "github.com/Dynatrace/dynatrace-operator/src/installer" "github.com/Dynatrace/dynatrace-operator/src/installer/url" @@ -29,16 +30,16 @@ func NewRunner(fs afero.Fs) (*Runner, error) { return nil, err } - var config *SecretConfig + var secretConfig *SecretConfig var client dtclient.Client var oneAgentInstaller *url.UrlInstaller if env.OneAgentInjected { - config, err = newSecretConfigViaFs(fs) + secretConfig, err = newSecretConfigViaFs(fs) if err != nil { return nil, err } - client, err = newDTClientBuilder(config).createClient() + client, err = newDTClientBuilder(secretConfig).createClient() if err != nil { return nil, err } @@ -54,6 +55,7 @@ func NewRunner(fs afero.Fs) (*Runner, error) { Technologies: env.InstallerTech, TargetVersion: url.VersionLatest, Url: env.InstallerUrl, + PathResolver: metadata.PathResolver{RootDir: config.AgentBinDirMount}, }, ) } @@ -61,7 +63,7 @@ func NewRunner(fs afero.Fs) (*Runner, error) { return &Runner{ fs: fs, env: env, - config: config, + config: secretConfig, dtclient: client, installer: oneAgentInstaller, }, nil