Skip to content

Commit

Permalink
Pass context to kube.Exec and similar functions. #25703 (#249)
Browse files Browse the repository at this point in the history
* Bump go version
* Adjust code to follow Kanister changes
* Set appropriate Kanister revision
  • Loading branch information
e-sumin authored Jun 4, 2024
1 parent 4b8ba2f commit 3843452
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 126 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21-bullseye AS builder
FROM golang:1.22-bullseye AS builder

ENV GO111MODULE=on \
CGO_ENABLED=0 \
Expand Down
45 changes: 23 additions & 22 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
module github.com/kastenhq/kubestr

go 1.21.9
go 1.22

toolchain go1.22.2

replace github.com/graymeta/stow => github.com/kastenhq/stow v0.1.2-kasten

require (
github.com/briandowns/spinner v1.23.0
github.com/frankban/quicktest v1.14.6
github.com/golang/mock v1.6.0
github.com/kanisterio/kanister v0.0.0-20240327164323-68e436d87b52
github.com/kanisterio/kanister v0.0.0-20240604102935-89345dc73d6e
github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.8.0
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
k8s.io/api v0.29.3
k8s.io/apimachinery v0.29.3
k8s.io/client-go v0.29.3
k8s.io/api v0.29.5
k8s.io/apimachinery v0.29.5
k8s.io/client-go v0.29.5
)

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.10.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.11.1 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/MakeNowJust/heredoc v1.0.0 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
Expand All @@ -29,9 +31,9 @@ require (
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-openapi/errors v0.22.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand Down Expand Up @@ -79,28 +81,27 @@ require (
github.com/xlab/treeprint v1.2.0 // indirect
go.mongodb.org/mongo-driver v1.14.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.20.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.19.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.29.3 // indirect
k8s.io/cli-runtime v0.29.3 // indirect
k8s.io/code-generator v0.29.3 // indirect
k8s.io/component-base v0.29.3 // indirect
k8s.io/apiextensions-apiserver v0.29.5 // indirect
k8s.io/cli-runtime v0.29.5 // indirect
k8s.io/code-generator v0.29.5 // indirect
k8s.io/component-base v0.29.5 // indirect
k8s.io/gengo v0.0.0-20230829151522-9cce18d56c01 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/kubectl v0.29.3 // indirect
k8s.io/kubectl v0.29.5 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
Expand Down
157 changes: 70 additions & 87 deletions go.sum

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pkg/csi/csi_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,18 +475,18 @@ func (p *apiVersionFetch) GetCSISnapshotGroupVersion() (*metav1.GroupVersionForD

//go:generate go run github.com/golang/mock/mockgen -destination=mocks/mock_data_validator.go -package=mocks . DataValidator
type DataValidator interface {
FetchPodData(podName string, podNamespace string) (string, error)
FetchPodData(ctx context.Context, podName string, podNamespace string) (string, error)
}

type validateData struct {
kubeCli kubernetes.Interface
}

func (p *validateData) FetchPodData(podName string, podNamespace string) (string, error) {
func (p *validateData) FetchPodData(ctx context.Context, podName string, podNamespace string) (string, error) {
if p.kubeCli == nil {
return "", fmt.Errorf("kubeCli not initialized")
}
stdout, _, err := kankube.Exec(p.kubeCli, podNamespace, podName, "", []string{"sh", "-c", "cat /data/out.txt"}, nil)
stdout, _, err := kankube.Exec(ctx, p.kubeCli, podNamespace, podName, "", []string{"sh", "-c", "cat /data/out.txt"}, nil)
return stdout, err
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/csi/mocks/mock_data_validator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/csi/snapshot_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (s *snapshotRestoreSteps) CreateApplication(ctx context.Context, args *type
}

func (s *snapshotRestoreSteps) ValidateData(ctx context.Context, pod *v1.Pod, data string) error {
podData, err := s.dataValidatorOps.FetchPodData(pod.Name, pod.Namespace)
podData, err := s.dataValidatorOps.FetchPodData(ctx, pod.Name, pod.Namespace)
if err != nil {
return errors.Wrap(err, "Failed to fetch data from pod. Failure may be due to permissions issues. Try again with runAsUser=1000 option.")
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/csi/snapshot_restore_steps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ func (s *CSITestSuite) TestValidateData(c *C) {
data: "somedata",
prepare: func(f *fields) {
gomock.InOrder(
f.validatorOps.EXPECT().FetchPodData("pod", "ns").Return("somedata", nil),
f.validatorOps.EXPECT().FetchPodData(context.Background(), "pod", "ns").Return("somedata", nil),
)
},
errChecker: IsNil,
Expand All @@ -919,7 +919,7 @@ func (s *CSITestSuite) TestValidateData(c *C) {
data: "somedata",
prepare: func(f *fields) {
gomock.InOrder(
f.validatorOps.EXPECT().FetchPodData("pod", "ns").Return("someotherdata", nil),
f.validatorOps.EXPECT().FetchPodData(context.Background(), "pod", "ns").Return("someotherdata", nil),
)
},
errChecker: NotNil,
Expand All @@ -934,7 +934,7 @@ func (s *CSITestSuite) TestValidateData(c *C) {
data: "somedata",
prepare: func(f *fields) {
gomock.InOrder(
f.validatorOps.EXPECT().FetchPodData("pod", "ns").Return("", fmt.Errorf("error")),
f.validatorOps.EXPECT().FetchPodData(context.Background(), "pod", "ns").Return("", fmt.Errorf("error")),
)
},
errChecker: NotNil,
Expand Down
8 changes: 4 additions & 4 deletions pkg/fio/fio.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func (s *fioStepper) runFIOCommand(ctx context.Context, podName, containerName,
var err error
timestart := time.Now()
go func() {
stdout, stderr, err = s.kubeExecutor.exec(namespace, podName, containerName, command)
stdout, stderr, err = s.kubeExecutor.exec(ctx, namespace, podName, containerName, command)
if err != nil || stderr != "" {
if err == nil {
err = fmt.Errorf("stderr when running FIO")
Expand Down Expand Up @@ -393,13 +393,13 @@ func (p *podReadyChecker) waitForPodReady(ctx context.Context, namespace, name s
}

type kubeExecInterface interface {
exec(namespace, podName, containerName string, command []string) (string, string, error)
exec(ctx context.Context, namespace, podName, containerName string, command []string) (string, string, error)
}

type kubeExecutor struct {
cli kubernetes.Interface
}

func (k *kubeExecutor) exec(namespace, podName, containerName string, command []string) (string, string, error) {
return kankube.Exec(k.cli, namespace, podName, containerName, command, nil)
func (k *kubeExecutor) exec(ctx context.Context, namespace, podName, containerName string, command []string) (string, string, error) {
return kankube.Exec(ctx, k.cli, namespace, podName, containerName, command, nil)
}
2 changes: 1 addition & 1 deletion pkg/fio/fio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ type fakeKubeExecutor struct {
keInCommand []string
}

func (fk *fakeKubeExecutor) exec(namespace, podName, containerName string, command []string) (string, string, error) {
func (fk *fakeKubeExecutor) exec(_ context.Context, namespace, podName, containerName string, command []string) (string, string, error) {
fk.keInNS = namespace
fk.keInPodName = podName
fk.keInContainerName = containerName
Expand Down

0 comments on commit 3843452

Please sign in to comment.