Skip to content

Commit

Permalink
Refactor PodRunner (#5420)
Browse files Browse the repository at this point in the history
* Add podOptions to closure func

* Address review comments

* Remove podOptions from closure func

* Add check for PodRunner init
  • Loading branch information
DeepikaDixit authored and Ilya Kislenko committed Apr 17, 2019
1 parent dcd8cd3 commit 0b46e3d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
10 changes: 10 additions & 0 deletions pkg/kube/pod_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@ type PodRunner struct {
podOptions *PodOptions
}

func NewPodRunner(cli kubernetes.Interface, options *PodOptions) *PodRunner {
return &PodRunner{
cli: cli,
podOptions: options,
}
}

func (p *PodRunner) Run(ctx context.Context, fn func(context.Context, *v1.Pod) (map[string]interface{}, error)) (map[string]interface{}, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if p.cli == nil || p.podOptions == nil {
return nil, errors.New("Pod Runner not initialized")
}
pod, err := CreatePod(ctx, p.cli, p.podOptions)
if err != nil {
return nil, errors.Wrapf(err, "Failed to create pod")
Expand Down
7 changes: 2 additions & 5 deletions pkg/kube/pod_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@ func (s *PodRunnerTestSuite) TestPodRunnerContextCanceled(c *C) {
})
deleted := make(chan struct{})
cli.PrependReactor("delete", "pods", func(action testing.Action) (handled bool, ret runtime.Object, err error) {
c.Log("Pod Deleted due to Context Cancelled")
c.Log("Pod deleted due to Context Cancelled")
close(deleted)
return true, nil, nil
})
pr := PodRunner{
cli: cli,
podOptions: &PodOptions{},
}
pr := NewPodRunner(cli, &PodOptions{})
returned := make(chan struct{})
go func() {
_, err := pr.Run(ctx, makePodRunnerTestFunc(deleted))
Expand Down

0 comments on commit 0b46e3d

Please sign in to comment.