Skip to content

Commit

Permalink
Fix golangci-lint findings
Browse files Browse the repository at this point in the history
Replace deprecated functions.

Introduce context to switch to functions that now require a context.
  • Loading branch information
HeavyWombat committed May 8, 2024
1 parent 1316ff3 commit 5f946d3
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 67 deletions.
9 changes: 0 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
Expand All @@ -23,7 +22,6 @@ github.com/evanphx/json-patch v5.6.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLi
github.com/fatih/camelcase v1.0.0 h1:hxNvNX/xYBp0ovncs8WyWZrOrpBNub/JfaMvbURyft8=
github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
Expand Down Expand Up @@ -104,7 +102,6 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down Expand Up @@ -137,9 +134,7 @@ github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00/go.mod
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/ginkgo/v2 v2.17.3 h1:oJcvKpIb7/8uLpDDtnQuf18xVnwKp8DTD7DQ6gTd/MU=
github.com/onsi/ginkgo/v2 v2.17.3/go.mod h1:nP2DPOQoNsQmsVyv5rDA8JkXQoCs6goXIvr/PRJ1eCc=
github.com/onsi/gomega v1.33.1 h1:dsYjIxxSR755MDmKVsaFQTE22ChNBcuuTWgkUDSubOk=
Expand All @@ -152,18 +147,15 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
Expand Down Expand Up @@ -286,7 +278,6 @@ gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
Expand Down
10 changes: 5 additions & 5 deletions internal/cmd/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ package cmd

import (
"context"
"fmt"
"strings"
"time"

"github.com/spf13/cobra"

"github.com/gonvenience/bunt"
"github.com/gonvenience/wrap"
"github.com/homeport/havener/pkg/havener"

corev1 "k8s.io/api/core/v1"
Expand All @@ -55,9 +55,9 @@ var eventsCmd = &cobra.Command{
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
hvnr, err := havener.NewHavener(havener.KubeConfig(kubeConfig))
hvnr, err := havener.NewHavener(havener.WithContext(cmd.Context()), havener.WithKubeConfigPath(kubeConfig))

Check warning on line 58 in internal/cmd/events.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/events.go#L58

Added line #L58 was not covered by tests
if err != nil {
return wrap.Error(err, "unable to get access to cluster")
return fmt.Errorf("unable to get access to cluster: %w", err)

Check warning on line 60 in internal/cmd/events.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/events.go#L60

Added line #L60 was not covered by tests
}

return retrieveClusterEvents(hvnr)
Expand All @@ -73,7 +73,7 @@ func init() {
func retrieveClusterEvents(hvnr havener.Havener) error {
namespaces, err := havener.ListNamespaces(hvnr.Client())
if err != nil {
return wrap.Error(err, "failed to get a list of namespaces")
return fmt.Errorf("failed to get a list of namespaces: %w", err)

Check warning on line 76 in internal/cmd/events.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/events.go#L76

Added line #L76 was not covered by tests
}

notes := make(chan note)
Expand All @@ -88,7 +88,7 @@ func retrieveClusterEvents(hvnr havener.Havener) error {

watcher, err := hvnr.Client().CoreV1().Events(namespace).Watch(context.TODO(), metav1.ListOptions{})
if err != nil {
return wrap.Error(err, "failed to setup event watcher")
return fmt.Errorf("failed to setup event watcher: %w", err)

Check warning on line 91 in internal/cmd/events.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/events.go#L91

Added line #L91 was not covered by tests
}

go func() {
Expand Down
10 changes: 4 additions & 6 deletions internal/cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/spf13/cobra"

"github.com/gonvenience/wait"
"github.com/gonvenience/wrap"

"github.com/homeport/havener/pkg/havener"
)
Expand All @@ -54,9 +53,9 @@ The download includes all deployment YAMLs of the pods and the describe output.`
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
hvnr, err := havener.NewHavener(havener.KubeConfig(kubeConfig))
hvnr, err := havener.NewHavener(havener.WithContext(cmd.Context()), havener.WithKubeConfigPath(kubeConfig))

Check warning on line 56 in internal/cmd/logs.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/logs.go#L56

Added line #L56 was not covered by tests
if err != nil {
return wrap.Error(err, "unable to get access to cluster")
return fmt.Errorf("unable to get access to cluster: %w", err)

Check warning on line 58 in internal/cmd/logs.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/logs.go#L58

Added line #L58 was not covered by tests
}

return retrieveClusterLogs(hvnr)
Expand Down Expand Up @@ -101,14 +100,13 @@ func retrieveClusterLogs(hvnr havener.Havener) error {
case err := <-resultChan:
if err != nil {
pi.Stop()
return wrap.Error(err, "unable to retrieve logs from pods")
return fmt.Errorf("unable to retrieve logs from pods: %w", err)

Check warning on line 103 in internal/cmd/logs.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/logs.go#L103

Added line #L103 was not covered by tests
}

case <-time.After(timeout):
pi.Stop()
return wrap.Error(
return fmt.Errorf("unable to retrieve logs from pods: %w",

Check warning on line 108 in internal/cmd/logs.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/logs.go#L108

Added line #L108 was not covered by tests
fmt.Errorf("download did not finish within configured timeout"),
"unable to retrieve logs from pods",
)
}

Expand Down
15 changes: 6 additions & 9 deletions internal/cmd/nexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (

"github.com/gonvenience/bunt"
"github.com/gonvenience/term"
"github.com/gonvenience/wrap"
"github.com/homeport/havener/pkg/havener"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -81,9 +80,9 @@ all nodes automatically.
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
hvnr, err := havener.NewHavener(havener.KubeConfig(kubeConfig))
hvnr, err := havener.NewHavener(havener.WithContext(cmd.Context()), havener.WithKubeConfigPath(kubeConfig))

Check warning on line 83 in internal/cmd/nexec.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/nexec.go#L83

Added line #L83 was not covered by tests
if err != nil {
return wrap.Error(err, "unable to get access to cluster")
return fmt.Errorf("unable to get access to cluster: %w", err)

Check warning on line 85 in internal/cmd/nexec.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/nexec.go#L85

Added line #L85 was not covered by tests
}

return execInClusterNodes(hvnr, args)
Expand Down Expand Up @@ -241,17 +240,15 @@ func lookupNodesByName(client kubernetes.Interface, input string) ([]corev1.Node
func availableNodesError(client kubernetes.Interface, title string, fArgs ...interface{}) error {
nodes, err := havener.ListNodes(client)
if err != nil {
return wrap.Error(err, "failed to list all nodes in cluster")
return fmt.Errorf("failed to list all nodes in cluster: %w", err)

Check warning on line 243 in internal/cmd/nexec.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/nexec.go#L243

Added line #L243 was not covered by tests
}

if len(nodes) == 0 {
return fmt.Errorf("failed to find any node in cluster")
}

return wrap.Errorf(
bunt.Errorf("*list of available nodes:*\n%s\n\nor, use _all_ to target all nodes",
strings.Join(nodes, "\n"),
),
title, fArgs...,
return fmt.Errorf("%s: %w",
fmt.Sprintf(title, fArgs...),
bunt.Errorf("*list of available nodes:*\n%s\n\nor, use _all_ to target all nodes", strings.Join(nodes, "\n")),

Check warning on line 252 in internal/cmd/nexec.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/nexec.go#L250-L252

Added lines #L250 - L252 were not covered by tests
)
}
15 changes: 6 additions & 9 deletions internal/cmd/pexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/gonvenience/term"
"github.com/gonvenience/wrap"
"github.com/homeport/havener/pkg/havener"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -75,10 +74,10 @@ all pods in all namespaces automatically.
`,
SilenceUsage: true,
SilenceErrors: true,
RunE: func(_ *cobra.Command, args []string) error {
hvnr, err := havener.NewHavener(havener.KubeConfig(kubeConfig))
RunE: func(cmd *cobra.Command, args []string) error {
hvnr, err := havener.NewHavener(havener.WithContext(cmd.Context()), havener.WithKubeConfigPath(kubeConfig))

Check warning on line 78 in internal/cmd/pexec.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/pexec.go#L77-L78

Added lines #L77 - L78 were not covered by tests
if err != nil {
return wrap.Error(err, "unable to get access to cluster")
return fmt.Errorf("unable to get access to cluster: %w", err)

Check warning on line 80 in internal/cmd/pexec.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/pexec.go#L80

Added line #L80 was not covered by tests
}

return execInClusterPods(hvnr, args)
Expand Down Expand Up @@ -300,7 +299,7 @@ func lookupPodsByName(client kubernetes.Interface, input string) (map[*corev1.Po
func availablePodsError(client kubernetes.Interface, title string) error {
pods, err := havener.ListPods(client)
if err != nil {
return wrap.Error(err, "failed to list all pods in cluster")
return fmt.Errorf("failed to list all pods in cluster: %w", err)

Check warning on line 302 in internal/cmd/pexec.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/pexec.go#L302

Added line #L302 was not covered by tests
}
podList := []string{}
for _, pod := range pods {
Expand All @@ -313,10 +312,8 @@ func availablePodsError(client kubernetes.Interface, title string) error {
}
}

return wrap.Error(
fmt.Errorf("> Usage:\npod-exec [flags] <pod> <command>\n> List of available pods:\n%s",
strings.Join(podList, "\n"),
),
return fmt.Errorf("%s: %w",

Check warning on line 315 in internal/cmd/pexec.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/pexec.go#L315

Added line #L315 was not covered by tests
title,
fmt.Errorf("> Usage:\npod-exec [flags] <pod> <command>\n> List of available pods:\n%s", strings.Join(podList, "\n")),

Check warning on line 317 in internal/cmd/pexec.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/pexec.go#L317

Added line #L317 was not covered by tests
)
}
2 changes: 1 addition & 1 deletion internal/cmd/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ cluster as well as a list per node.
topCmdSettings.cycles = 1
}

hvnr, err := havener.NewHavener(havener.KubeConfig(kubeConfig))
hvnr, err := havener.NewHavener(havener.WithContext(cmd.Context()), havener.WithKubeConfigPath(kubeConfig))

Check warning on line 73 in internal/cmd/top.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/top.go#L73

Added line #L73 was not covered by tests
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var watchCmd = &cobra.Command{
Short: "Watch status of all pods in all namespaces",
Long: `Continuesly creates a list of all pods in all namespaces.`,
RunE: func(cmd *cobra.Command, args []string) error {
hvnr, err := havener.NewHavener(havener.KubeConfig(kubeConfig))
hvnr, err := havener.NewHavener(havener.WithContext(cmd.Context()), havener.WithKubeConfigPath(kubeConfig))

Check warning on line 51 in internal/cmd/watch.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/watch.go#L51

Added line #L51 was not covered by tests
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/havener/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"

"github.com/gonvenience/wrap"
"gopkg.in/yaml.v3"
)

Expand All @@ -47,7 +46,7 @@ import (
func KubeConfigDefault() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", wrap.Error(err, "unable to get home directory")
return "", fmt.Errorf("unable to get home directory: %w", err)

Check warning on line 49 in pkg/havener/common.go

View check run for this annotation

Codecov / codecov/patch

pkg/havener/common.go#L49

Added line #L49 was not covered by tests
}

return filepath.Join(home, ".kube", "config"), nil
Expand Down
25 changes: 21 additions & 4 deletions pkg/havener/havener.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ this kind of workload.
package havener

import (
"context"
"fmt"
"io"
"os"
"time"

"github.com/gonvenience/wrap"
"golang.org/x/sync/syncmap"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -73,6 +73,7 @@ func GracefulShutdown() {

// Hvnr is the internal handle to consolidate required cluster access variables
type Hvnr struct {
ctx context.Context
kubeConfigPath string
client kubernetes.Interface
restconfig *rest.Config
Expand Down Expand Up @@ -103,12 +104,23 @@ type Havener interface {
type Option func(*Hvnr)

// KubeConfig is an option to set a specific Kubernetes configuration path
//
// Deprecated: Use WithKubeConfigPath instead
func KubeConfig(kubeConfig string) Option {
return func(h *Hvnr) {
h.kubeConfigPath = kubeConfig
}
}

func WithKubeConfigPath(kubeConfig string) Option {
return func(h *Hvnr) { h.kubeConfigPath = kubeConfig }

Check warning on line 116 in pkg/havener/havener.go

View check run for this annotation

Codecov / codecov/patch

pkg/havener/havener.go#L115-L116

Added lines #L115 - L116 were not covered by tests
}

// WithContext is an option to set the context
func WithContext(ctx context.Context) Option {
return func(h *Hvnr) { h.ctx = ctx }

Check warning on line 121 in pkg/havener/havener.go

View check run for this annotation

Codecov / codecov/patch

pkg/havener/havener.go#L120-L121

Added lines #L120 - L121 were not covered by tests
}

// NewHavenerFromFields returns a new Havener handle using the provided
// input arguments (use for unit testing only)
func NewHavenerFromFields(client kubernetes.Interface, restconfig *rest.Config, clusterName string, kubeConfigPath string) *Hvnr {
Expand All @@ -127,6 +139,11 @@ func NewHavener(opts ...Option) (hvnr *Hvnr, err error) {
opt(hvnr)
}

// Set default backgroud context if nothing is set
if hvnr.ctx == nil {
hvnr.ctx = context.Background()

Check warning on line 144 in pkg/havener/havener.go

View check run for this annotation

Codecov / codecov/patch

pkg/havener/havener.go#L143-L144

Added lines #L143 - L144 were not covered by tests
}

// In case `KUBECONFIG` environment variable is set, this will take
// precedence over command line flag or default value
if value, ok := os.LookupEnv("KUBECONFIG"); ok {
Expand All @@ -137,18 +154,18 @@ func NewHavener(opts ...Option) (hvnr *Hvnr, err error) {
if hvnr.kubeConfigPath == "" {
hvnr.kubeConfigPath, err = KubeConfigDefault()
if err != nil {
return nil, wrap.Error(err, "failed to look-up default kube config")
return nil, fmt.Errorf("failed to look-up default kube config: %w", err)

Check warning on line 157 in pkg/havener/havener.go

View check run for this annotation

Codecov / codecov/patch

pkg/havener/havener.go#L157

Added line #L157 was not covered by tests
}
}

hvnr.client, hvnr.restconfig, err = outOfClusterAuthentication(hvnr.kubeConfigPath)
if err != nil {
return nil, wrap.Error(err, "unable to get access to cluster")
return nil, fmt.Errorf("unable to get access to cluster: %w", err)

Check warning on line 163 in pkg/havener/havener.go

View check run for this annotation

Codecov / codecov/patch

pkg/havener/havener.go#L163

Added line #L163 was not covered by tests
}

hvnr.clusterName, err = clusterName(hvnr.kubeConfigPath)
if err != nil {
return nil, wrap.Error(err, "unable to get cluster name")
return nil, fmt.Errorf("unable to get cluster name: %w", err)

Check warning on line 168 in pkg/havener/havener.go

View check run for this annotation

Codecov / codecov/patch

pkg/havener/havener.go#L168

Added line #L168 was not covered by tests
}

return hvnr, nil
Expand Down
13 changes: 6 additions & 7 deletions pkg/havener/kubexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (

"github.com/gonvenience/term"
"github.com/gonvenience/text"
"github.com/gonvenience/wrap"
terminal "golang.org/x/term"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -59,7 +58,7 @@ func (h *Hvnr) PodExec(pod *corev1.Pod, container string, command []string, stdi

executor, err := remotecommand.NewSPDYExecutor(h.restconfig, "POST", req.URL())
if err != nil {
return wrap.Error(err, "failed to initialize remote executor")
return fmt.Errorf("failed to initialize remote executor: %w", err)

Check warning on line 61 in pkg/havener/kubexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/havener/kubexec.go#L61

Added line #L61 was not covered by tests
}

var tsq *terminalSizeQueue
Expand All @@ -77,8 +76,8 @@ func (h *Hvnr) PodExec(pod *corev1.Pod, container string, command []string, stdi
}
}

if err = executor.Stream(remotecommand.StreamOptions{Stdin: stdin, Stdout: stdout, Stderr: stderr, Tty: tty, TerminalSizeQueue: tsq}); err != nil {
return wrap.Errorf(err, "failed to execute command on pod %s, container %s", pod.Name, container)
if err = executor.StreamWithContext(h.ctx, remotecommand.StreamOptions{Stdin: stdin, Stdout: stdout, Stderr: stderr, Tty: tty, TerminalSizeQueue: tsq}); err != nil {
return fmt.Errorf("failed to execute command on pod %s, container %s: %w", pod.Name, container, err)

Check warning on line 80 in pkg/havener/kubexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/havener/kubexec.go#L79-L80

Added lines #L79 - L80 were not covered by tests
}

logf(Verbose, "Successfully executed command.")
Expand Down Expand Up @@ -135,7 +134,7 @@ func (h *Hvnr) preparePodOnNode(node corev1.Node, namespace string, name string,
ImagePullPolicy: corev1.PullIfNotPresent,
Stdin: useStdin,
SecurityContext: &corev1.SecurityContext{
Privileged: pointer.BoolPtr(true),
Privileged: pointer.Bool(true),

Check warning on line 137 in pkg/havener/kubexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/havener/kubexec.go#L137

Added line #L137 was not covered by tests
},
},
},
Expand Down Expand Up @@ -205,11 +204,11 @@ func (h *Hvnr) waitForPodReadiness(namespace string, pod *corev1.Pod, timeoutSec
description = "Unable to provide further details regarding the state of the pod."
}

return wrap.Errorf(fmt.Errorf("status of pod at the moment of the timeout:\n\n%s", description),
"Giving up waiting for pod %s in namespace %s to become ready within %s",
return fmt.Errorf("Giving up waiting for pod %s in namespace %s to become ready within %s: %w",

Check warning on line 207 in pkg/havener/kubexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/havener/kubexec.go#L207

Added line #L207 was not covered by tests
pod.Name,
pod.Namespace,
text.Plural(timeoutSeconds, "second"),
fmt.Errorf("status of pod at the moment of the timeout:\n\n%s", description),

Check warning on line 211 in pkg/havener/kubexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/havener/kubexec.go#L211

Added line #L211 was not covered by tests
)
}
}
Expand Down
Loading

0 comments on commit 5f946d3

Please sign in to comment.