From 0e1f55b5cb8b4f6442a4762412253c1823c49abc Mon Sep 17 00:00:00 2001 From: Jacob Colvin Date: Wed, 1 Jan 2025 12:46:52 -0500 Subject: [PATCH] Remove dead code --- pkg/argoutil/config/reader.go | 20 ------- pkg/argoutil/exec/exec.go | 62 +++++----------------- pkg/argoutil/executil/exec.go | 1 - pkg/argoutil/helm/client.go | 34 ------------ pkg/argoutil/helm/helm.go | 19 ++++++- pkg/argoutil/io/bytereadseeker.go | 38 -------------- pkg/argoutil/io/bytereadseeker_test.go | 72 -------------------------- pkg/argoutil/rand/rand.go | 15 ------ pkg/argoutil/rand/rand_test.go | 16 ------ 9 files changed, 29 insertions(+), 248 deletions(-) delete mode 100755 pkg/argoutil/config/reader.go delete mode 100755 pkg/argoutil/io/bytereadseeker.go delete mode 100755 pkg/argoutil/io/bytereadseeker_test.go delete mode 100755 pkg/argoutil/rand/rand.go delete mode 100755 pkg/argoutil/rand/rand_test.go diff --git a/pkg/argoutil/config/reader.go b/pkg/argoutil/config/reader.go deleted file mode 100755 index 187a45c..0000000 --- a/pkg/argoutil/config/reader.go +++ /dev/null @@ -1,20 +0,0 @@ -package config - -import ( - "io" - "net/http" -) - -// ReadRemoteFile issues a GET request to retrieve the contents of the specified URL as a byte array. -// The caller is responsible for checking error return values. -func ReadRemoteFile(url string) ([]byte, error) { - var data []byte - resp, err := http.Get(url) - if err == nil { - defer func() { - _ = resp.Body.Close() - }() - data, err = io.ReadAll(resp.Body) - } - return data, err -} diff --git a/pkg/argoutil/exec/exec.go b/pkg/argoutil/exec/exec.go index 69c4848..5c39253 100755 --- a/pkg/argoutil/exec/exec.go +++ b/pkg/argoutil/exec/exec.go @@ -2,17 +2,15 @@ package exec import ( "bytes" + "crypto/rand" + "encoding/hex" "errors" "fmt" "log/slog" - "os" "os/exec" - "runtime" "strings" "syscall" "time" - - "github.com/MacroPower/kclx/pkg/argoutil/rand" ) var ( @@ -81,10 +79,19 @@ func Redact(items []string) func(text string) string { } } +// randString returns a cryptographically-secure pseudo-random alpha-numeric string of a given length +func randString(n int) (string, error) { + bytes := make([]byte, n/2+1) // we need one extra letter to discard + if _, err := rand.Read(bytes); err != nil { + return "", err + } + return hex.EncodeToString(bytes)[0:n], nil +} + // RunCommandExt is a convenience function to run/log a command and return/log stderr in an error upon // failure. func RunCommandExt(cmd *exec.Cmd, opts CmdOpts) (string, error) { - execId, err := rand.RandString(5) + execId, err := randString(5) if err != nil { return "", err } @@ -172,48 +179,3 @@ func RunCommandExt(cmd *exec.Cmd, opts CmdOpts) (string, error) { func RunCommand(name string, opts CmdOpts, arg ...string) (string, error) { return RunCommandExt(exec.Command(name, arg...), opts) } - -// WaitPIDOpts are options to WaitPID -type WaitPIDOpts struct { - PollInterval time.Duration - Timeout time.Duration -} - -// WaitPID waits for a non-child process id to exit -func WaitPID(pid int, opts ...WaitPIDOpts) error { - if runtime.GOOS != "linux" { - return fmt.Errorf("platform '%s' unsupported", runtime.GOOS) - } - var timeout time.Duration - pollInterval := time.Second - if len(opts) > 0 { - if opts[0].PollInterval != 0 { - pollInterval = opts[0].PollInterval - } - if opts[0].Timeout != 0 { - timeout = opts[0].Timeout - } - } - path := fmt.Sprintf("/proc/%d", pid) - - ticker := time.NewTicker(pollInterval) - defer ticker.Stop() - var timoutCh <-chan time.Time - if timeout != 0 { - timoutCh = time.NewTimer(timeout).C - } - for { - select { - case <-ticker.C: - _, err := os.Stat(path) - if err != nil { - if os.IsNotExist(err) { - return nil - } - return err - } - case <-timoutCh: - return ErrWaitPIDTimeout - } - } -} diff --git a/pkg/argoutil/executil/exec.go b/pkg/argoutil/executil/exec.go index f403cca..ee1e7ae 100755 --- a/pkg/argoutil/executil/exec.go +++ b/pkg/argoutil/executil/exec.go @@ -10,7 +10,6 @@ import ( "unicode" argoexec "github.com/MacroPower/kclx/pkg/argoutil/exec" - "github.com/MacroPower/kclx/pkg/argoutil/log" "github.com/MacroPower/kclx/pkg/tracing" ) diff --git a/pkg/argoutil/helm/client.go b/pkg/argoutil/helm/client.go index c125b05..9802877 100755 --- a/pkg/argoutil/helm/client.go +++ b/pkg/argoutil/helm/client.go @@ -46,11 +46,6 @@ type Creds struct { InsecureSkipVerify bool } -type indexCache interface { - SetHelmIndex(repo string, indexData []byte) error - GetHelmIndex(repo string, indexData *[]byte) error -} - type Client interface { CleanChartCache(chart string, version string, project string) error ExtractChart(chart string, version string, project string, passCredentials bool, manifestMaxExtractedSize int64, disableManifestMaxExtractedSize bool) (string, argoio.Closer, error) @@ -61,12 +56,6 @@ type Client interface { type ClientOpts func(c *nativeHelmChart) -func WithIndexCache(indexCache indexCache) ClientOpts { - return func(c *nativeHelmChart) { - c.indexCache = indexCache - } -} - func WithChartPaths(chartPaths argoio.TempPaths) ClientOpts { return func(c *nativeHelmChart) { c.chartCachePaths = chartPaths @@ -101,7 +90,6 @@ type nativeHelmChart struct { creds Creds repoLock sync.KeyLock enableOci bool - indexCache indexCache proxy string noProxy string } @@ -237,11 +225,6 @@ func (c *nativeHelmChart) GetIndex(noCache bool, maxIndexSize int64) (*Index, er defer indexLock.Unlock(c.repoURL) var data []byte - // if !noCache && c.indexCache != nil { - // if err := c.indexCache.GetHelmIndex(c.repoURL, &data); err != nil && !errors.Is(err, cache.ErrCacheMiss) { - // log.Warnf("Failed to load index cache for repo: %s: %v", c.repoURL, err) - // } - // } if len(data) == 0 { start := time.Now() @@ -251,12 +234,6 @@ func (c *nativeHelmChart) GetIndex(noCache bool, maxIndexSize int64) (*Index, er return nil, fmt.Errorf("error loading repo index: %w", err) } slog.Info("got index data", "seconds", time.Since(start).Seconds()) - - // if c.indexCache != nil { - // if err := c.indexCache.SetHelmIndex(c.repoURL, data); err != nil { - // log.Warnf("Failed to store index cache for repo: %s: %v", c.repoURL, err) - // } - // } } index := &Index{} @@ -415,11 +392,6 @@ func (c *nativeHelmChart) GetTags(chart string, noCache bool) (*TagsList, error) defer indexLock.Unlock(tagsURL) var data []byte - // if !noCache && c.indexCache != nil { - // if err := c.indexCache.GetHelmIndex(tagsURL, &data); err != nil && !errors.Is(err, cache.ErrCacheMiss) { - // log.Warnf("Failed to load index cache for repo: %s: %v", tagsURL, err) - // } - // } tags := &TagsList{} if len(data) == 0 { @@ -462,12 +434,6 @@ func (c *nativeHelmChart) GetTags(chart string, noCache bool) (*TagsList, error) return nil, fmt.Errorf("failed to get tags: %w", err) } slog.Info("got tags", "seconds", time.Since(start).Seconds(), "chart", chart, "repo", c.repoURL) - - // if c.indexCache != nil { - // if err := c.indexCache.SetHelmIndex(tagsURL, data); err != nil { - // log.Warnf("Failed to store tags list cache for repo: %s: %v", tagsURL, err) - // } - // } } else { err := json.Unmarshal(data, tags) if err != nil { diff --git a/pkg/argoutil/helm/helm.go b/pkg/argoutil/helm/helm.go index f5fd149..4d45b4a 100755 --- a/pkg/argoutil/helm/helm.go +++ b/pkg/argoutil/helm/helm.go @@ -3,7 +3,9 @@ package helm import ( "errors" "fmt" + "io" "log/slog" + "net/http" "net/url" "os" "os/exec" @@ -12,7 +14,6 @@ import ( "sigs.k8s.io/yaml" - "github.com/MacroPower/kclx/pkg/argoutil/config" "github.com/MacroPower/kclx/pkg/argoutil/executil" pathutil "github.com/MacroPower/kclx/pkg/argoutil/io/path" ) @@ -148,7 +149,7 @@ func (h *helm) GetParameters(valuesFiles []pathutil.ResolvedFilePath, appPath, r var fileValues []byte parsedURL, err := url.ParseRequestURI(file) if err == nil && (parsedURL.Scheme == "http" || parsedURL.Scheme == "https") { - fileValues, err = config.ReadRemoteFile(file) + fileValues, err = readRemoteFile(file) } else { _, fileReadErr := os.Stat(file) if os.IsNotExist(fileReadErr) { @@ -194,3 +195,17 @@ func flatVals(input interface{}, output map[string]string, prefixes ...string) { output[strings.Join(prefixes, ".")] = fmt.Sprintf("%v", i) } } + +// readRemoteFile issues a GET request to retrieve the contents of the specified URL as a byte array. +// The caller is responsible for checking error return values. +func readRemoteFile(url string) ([]byte, error) { + var data []byte + resp, err := http.Get(url) + if err == nil { + defer func() { + _ = resp.Body.Close() + }() + data, err = io.ReadAll(resp.Body) + } + return data, err +} diff --git a/pkg/argoutil/io/bytereadseeker.go b/pkg/argoutil/io/bytereadseeker.go deleted file mode 100755 index 43c246c..0000000 --- a/pkg/argoutil/io/bytereadseeker.go +++ /dev/null @@ -1,38 +0,0 @@ -package io - -import ( - "io" - "io/fs" -) - -func NewByteReadSeeker(data []byte) *byteReadSeeker { - return &byteReadSeeker{data: data} -} - -type byteReadSeeker struct { - data []byte - offset int64 -} - -func (f *byteReadSeeker) Read(b []byte) (int, error) { - if f.offset >= int64(len(f.data)) { - return 0, io.EOF - } - n := copy(b, f.data[f.offset:]) - f.offset += int64(n) - return n, nil -} - -func (f *byteReadSeeker) Seek(offset int64, whence int) (int64, error) { - switch whence { - case 1: - offset += f.offset - case 2: - offset += int64(len(f.data)) - } - if offset < 0 || offset > int64(len(f.data)) { - return 0, &fs.PathError{Op: "seek", Err: fs.ErrInvalid} - } - f.offset = offset - return offset, nil -} diff --git a/pkg/argoutil/io/bytereadseeker_test.go b/pkg/argoutil/io/bytereadseeker_test.go deleted file mode 100755 index cfaa1e9..0000000 --- a/pkg/argoutil/io/bytereadseeker_test.go +++ /dev/null @@ -1,72 +0,0 @@ -package io - -import ( - "io" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func TestByteReadSeeker_Read(t *testing.T) { - inString := "hello world" - reader := NewByteReadSeeker([]byte(inString)) - bytes := make([]byte, 11) - n, err := reader.Read(bytes) - require.NoError(t, err) - assert.Equal(t, len(inString), n) - assert.Equal(t, inString, string(bytes)) - _, err = reader.Read(bytes) - require.ErrorIs(t, err, io.EOF) -} - -func TestByteReadSeeker_Seek_Start(t *testing.T) { - inString := "hello world" - reader := NewByteReadSeeker([]byte(inString)) - offset, err := reader.Seek(6, io.SeekStart) - require.NoError(t, err) - assert.Equal(t, int64(6), offset) - bytes := make([]byte, 5) - n, err := reader.Read(bytes) - require.NoError(t, err) - assert.Equal(t, 5, n) - assert.Equal(t, "world", string(bytes)) -} - -func TestByteReadSeeker_Seek_Current(t *testing.T) { - inString := "hello world" - reader := NewByteReadSeeker([]byte(inString)) - offset, err := reader.Seek(3, io.SeekCurrent) - require.NoError(t, err) - assert.Equal(t, int64(3), offset) - offset, err = reader.Seek(3, io.SeekCurrent) - require.NoError(t, err) - assert.Equal(t, int64(6), offset) - bytes := make([]byte, 5) - n, err := reader.Read(bytes) - require.NoError(t, err) - assert.Equal(t, 5, n) - assert.Equal(t, "world", string(bytes)) -} - -func TestByteReadSeeker_Seek_End(t *testing.T) { - inString := "hello world" - reader := NewByteReadSeeker([]byte(inString)) - offset, err := reader.Seek(-5, io.SeekEnd) - require.NoError(t, err) - assert.Equal(t, int64(6), offset) - bytes := make([]byte, 5) - n, err := reader.Read(bytes) - require.NoError(t, err) - assert.Equal(t, 5, n) - assert.Equal(t, "world", string(bytes)) -} - -func TestByteReadSeeker_Seek_OutOfBounds(t *testing.T) { - inString := "hello world" - reader := NewByteReadSeeker([]byte(inString)) - _, err := reader.Seek(12, io.SeekStart) - require.Error(t, err) - _, err = reader.Seek(-1, io.SeekStart) - require.Error(t, err) -} diff --git a/pkg/argoutil/rand/rand.go b/pkg/argoutil/rand/rand.go deleted file mode 100755 index 453c115..0000000 --- a/pkg/argoutil/rand/rand.go +++ /dev/null @@ -1,15 +0,0 @@ -package rand - -import ( - "crypto/rand" - "encoding/hex" -) - -// RandString returns a cryptographically-secure pseudo-random alpha-numeric string of a given length -func RandString(n int) (string, error) { - bytes := make([]byte, n/2+1) // we need one extra letter to discard - if _, err := rand.Read(bytes); err != nil { - return "", err - } - return hex.EncodeToString(bytes)[0:n], nil -} diff --git a/pkg/argoutil/rand/rand_test.go b/pkg/argoutil/rand/rand_test.go deleted file mode 100755 index 52e4052..0000000 --- a/pkg/argoutil/rand/rand_test.go +++ /dev/null @@ -1,16 +0,0 @@ -package rand - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestRandString(t *testing.T) { - ss, err := RandString(10) - assert.NoError(t, err) - assert.Len(t, ss, 10) - ss, err = RandString(5) - assert.NoError(t, err) - assert.Len(t, ss, 5) -}