Skip to content

Commit

Permalink
change command from string to []string
Browse files Browse the repository at this point in the history
  • Loading branch information
maorfr committed Sep 8, 2018
1 parent cf97426 commit fa87ea5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
33 changes: 17 additions & 16 deletions pkg/skbn/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func GetListOfFilesFromK8s(iClient interface{}, path, findType, findName string)
return nil, err
}
namespace, podName, containerName, findPath := initK8sVariables(pSplit)
command := fmt.Sprintf("find %s -type %s -name %s", findPath, findType, findName)
command := []string{"find", findPath, "-type", findType, "-name", findName}

attempts := 3
attempt := 0
Expand Down Expand Up @@ -111,7 +111,7 @@ func DownloadFromK8s(iClient interface{}, path string) ([]byte, error) {
return nil, err
}
namespace, podName, containerName, pathToCopy := initK8sVariables(pSplit)
command := fmt.Sprintf("cat %s", pathToCopy)
command := []string{"cat", pathToCopy}

attempts := 3
attempt := 0
Expand Down Expand Up @@ -154,7 +154,7 @@ func UploadToK8s(iClient interface{}, toPath, fromPath string, buffer []byte) er
for attempt < attempts {
attempt++
dir, _ := filepath.Split(pathToCopy)
command := fmt.Sprintf("mkdir -p %s", dir)
command := []string{"mkdir", "-p", dir}
_, stderr, err := Exec(client, namespace, podName, containerName, command, nil)

if len(stderr) != 0 {
Expand All @@ -173,24 +173,25 @@ func UploadToK8s(iClient interface{}, toPath, fromPath string, buffer []byte) er
}

chunkSize := 16777215
command = []string{"dd", "if=/dev/stdin", "of=" + pathToCopy}

command = fmt.Sprintf("dd if=/dev/stdin of=%s", pathToCopy)
// Implement upload using chunks
for i := 0; i < len(buffer); i += chunkSize {
end := i + chunkSize
if end > len(buffer) {
end = len(buffer)
}
stdin := bytes.NewReader(buffer[i:end])
_, _, err = Exec(client, namespace, podName, containerName, command, stdin)
command = fmt.Sprintf("dd if=/dev/stdin of=%s oflag=append conv=notrunc", pathToCopy)

// if len(stderr) != 0 {
// if attempt == attempts {
// return fmt.Errorf("STDERR: " + (string)(stderr))
// }
// utils.Sleep(attempt)
// continue
// }
_, stderr, err = Exec(client, namespace, podName, containerName, command, stdin)
command = []string{"dd", "if=/dev/stdin", "of=" + pathToCopy, "oflag=append", "conv=notrunc"}

if len(stderr) != 0 {
if attempt == attempts {
return fmt.Errorf("STDERR: " + (string)(stderr))
}
utils.Sleep(attempt)
continue
}
if err != nil {
if attempt == attempts {
return err
Expand All @@ -206,7 +207,7 @@ func UploadToK8s(iClient interface{}, toPath, fromPath string, buffer []byte) er
}

// Exec executes a command in a given container
func Exec(client K8sClient, namespace, podName, containerName, command string, stdin io.Reader) ([]byte, []byte, error) {
func Exec(client K8sClient, namespace, podName, containerName string, command []string, stdin io.Reader) ([]byte, []byte, error) {
clientset, config := client.ClientSet, client.Config

req := clientset.Core().RESTClient().Post().
Expand All @@ -221,7 +222,7 @@ func Exec(client K8sClient, namespace, podName, containerName, command string, s

parameterCodec := runtime.NewParameterCodec(scheme)
req.VersionedParams(&core_v1.PodExecOptions{
Command: strings.Fields(command),
Command: command,
Container: containerName,
Stdin: stdin != nil,
Stdout: true,
Expand Down
6 changes: 0 additions & 6 deletions pkg/skbn/skbn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"math"
"path/filepath"
"strings"

"github.com/maorfr/skbn/pkg/utils"
)
Expand Down Expand Up @@ -152,11 +151,6 @@ func PerformCopy(srcClient, dstClient interface{}, srcPrefix, dstPrefix string,
currentLinePadded := utils.LeftPad2Len(currentLine, 0, totalDigits)

go func(srcClient, dstClient interface{}, srcPrefix, fromPath, dstPrefix, toPath, currentLinePadded string, totalFiles int) {
if strings.Contains(fromPath, " ") {
bwg.Done()
log.Printf("file [%s/%d] skip: %s", currentLinePadded, totalFiles, fromPath)
return
}
buffer, err := Download(srcClient, srcPrefix, fromPath)
if err != nil {
bwg.Done()
Expand Down

0 comments on commit fa87ea5

Please sign in to comment.