Skip to content

Commit

Permalink
modify PodWriter to provide pod info as params (#5561)
Browse files Browse the repository at this point in the history
  • Loading branch information
SupriyaKasten authored and Ilya Kislenko committed May 3, 2019
1 parent a960a0f commit 3ae0a28
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
36 changes: 16 additions & 20 deletions pkg/kube/pod_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,34 @@ import (

// PodWriter specifies Kubernetes Client and the other params needed for writing content to a file
type PodWriter struct {
cli kubernetes.Interface
namespace string
path string
podName string
containerName string
cli kubernetes.Interface
path string
content io.Reader
}

// NewPodWriter returns a new PodWriter given Kubernetes Client, Namespace, path of file, name of pod and container
func NewPodWriter(cli kubernetes.Interface, namespace, path, podName, containerName string) *PodWriter {
// NewPodWriter returns a new PodWriter given Kubernetes Client, path of file and content
func NewPodWriter(cli kubernetes.Interface, path string, content io.Reader) *PodWriter {
return &PodWriter{
cli: cli,
namespace: namespace,
path: filepath.Clean(path),
podName: podName,
containerName: containerName,
cli: cli,
path: filepath.Clean(path),
content: content,
}
}

// Write will create a new file(if not present) and write the provided content to the file
func (p *PodWriter) Write(ctx context.Context, content io.Reader) error {
func (p *PodWriter) Write(ctx context.Context, namespace, podName, containerName string) error {
cmd := []string{"sh", "-c", "cat - > " + p.path}
stdout, stderr, err := Exec(p.cli, p.namespace, p.podName, p.containerName, cmd, content)
format.Log(p.podName, p.containerName, stdout)
format.Log(p.podName, p.containerName, stderr)
stdout, stderr, err := Exec(p.cli, namespace, podName, containerName, cmd, p.content)
format.Log(podName, containerName, stdout)
format.Log(podName, containerName, stderr)
return errors.Wrap(err, "Failed to write contents to file")
}

// Remove will delete the file created by Write() func
func (p *PodWriter) Remove(ctx context.Context) error {
func (p *PodWriter) Remove(ctx context.Context, namespace, podName, containerName string) error {
cmd := []string{"sh", "-c", "rm " + p.path}
stdout, stderr, err := Exec(p.cli, p.namespace, p.podName, p.containerName, cmd, nil)
format.Log(p.podName, p.containerName, stdout)
format.Log(p.podName, p.containerName, stderr)
stdout, stderr, err := Exec(p.cli, namespace, podName, containerName, cmd, nil)
format.Log(podName, containerName, stdout)
format.Log(podName, containerName, stderr)
return errors.Wrap(err, "Failed to delete file")
}
6 changes: 3 additions & 3 deletions pkg/kube/pod_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ func (p *PodWriteSuite) TestPodWriter(c *C) {
c.Assert(p.pod.Status.Phase, Equals, v1.PodRunning)
c.Assert(len(p.pod.Status.ContainerStatuses) > 0, Equals, true)
for _, cs := range p.pod.Status.ContainerStatuses {
pw := NewPodWriter(p.cli, p.pod.Namespace, path, p.pod.Name, cs.Name)
err := pw.Write(context.Background(), bytes.NewBufferString("badabing"))
pw := NewPodWriter(p.cli, path, bytes.NewBufferString("badabing"))
err := pw.Write(context.Background(), p.pod.Namespace, p.pod.Name, cs.Name)
c.Assert(err, IsNil)
cmd := []string{"sh", "-c", "cat " + pw.path}
stdout, stderr, err := Exec(p.cli, p.pod.Namespace, p.pod.Name, cs.Name, cmd, nil)
c.Assert(err, IsNil)
c.Assert(stdout, Equals, "badabing")
c.Assert(stderr, Equals, "")
err = pw.Remove(context.Background())
err = pw.Remove(context.Background(), p.pod.Namespace, p.pod.Name, cs.Name)
c.Assert(err, IsNil)
}
}

0 comments on commit 3ae0a28

Please sign in to comment.