Skip to content

Commit

Permalink
Make extractor handle standalone usecase (#1077)
Browse files Browse the repository at this point in the history
  • Loading branch information
0sewa0 authored Aug 23, 2022
1 parent a755492 commit b580613
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/controllers/csi/provisioner/processmoduleconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package csiprovisioner

import (
"encoding/json"
"io/ioutil"
"io"
"os"

"github.com/Dynatrace/dynatrace-operator/src/dtclient"
Expand Down Expand Up @@ -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")
Expand Down
5 changes: 1 addition & 4 deletions src/installer/url/config.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
3 changes: 2 additions & 1 deletion src/installer/url/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/installer/url/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{},
Expand Down
17 changes: 15 additions & 2 deletions src/installer/zip/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
10 changes: 6 additions & 4 deletions src/standalone/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand All @@ -54,14 +55,15 @@ func NewRunner(fs afero.Fs) (*Runner, error) {
Technologies: env.InstallerTech,
TargetVersion: url.VersionLatest,
Url: env.InstallerUrl,
PathResolver: metadata.PathResolver{RootDir: config.AgentBinDirMount},
},
)
}
log.Info("standalone runner created successfully")
return &Runner{
fs: fs,
env: env,
config: config,
config: secretConfig,
dtclient: client,
installer: oneAgentInstaller,
}, nil
Expand Down

0 comments on commit b580613

Please sign in to comment.