Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement challenge.tar.gz unzip pipeline #77

Merged
merged 22 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/utils/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func dockerLogin(username string, password string) {
log.Println("Logged into Harbor successfully")
}

func CheckDockerfile(_DockerfilePath string) bool {
SomyaChawla0250 marked this conversation as resolved.
Show resolved Hide resolved
_, err := os.Stat(_DockerfilePath + "/Dockerfile")
return !os.IsNotExist(err)
}

func BuildDockerImage(_ChallengeName string, _DockerfilePath string) {
buf := new(bytes.Buffer)
if err := Tar(_DockerfilePath, buf); err != nil {
Expand All @@ -50,6 +55,10 @@ func BuildDockerImage(_ChallengeName string, _DockerfilePath string) {
return
}

log.Println(_ChallengeName)
ashpect marked this conversation as resolved.
Show resolved Hide resolved
log.Println("dfsdfsdfsdf")
log.Println(_DockerfilePath)

log.Println("Building Docker image, Please wait......")

imageBuildResponse, err := cli.ImageBuild(
Expand Down
307 changes: 230 additions & 77 deletions lib/utils/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"path/filepath"
"strings"
"compress/gzip"
"archive/tar"

g "github.com/sdslabs/katana/configs"
"github.com/sdslabs/katana/types"
Expand All @@ -22,6 +24,7 @@ import (
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/remotecommand"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
SomyaChawla0250 marked this conversation as resolved.
Show resolved Hide resolved
)

// GetKubeConfig returns a kubernetes REST config object
Expand Down Expand Up @@ -94,83 +97,6 @@ func GetMongoIP() string {
return service.Spec.ClusterIP
}

func CopyIntoPod(podName string, containerName string, pathInPod string, localFilePath string, ns ...string) error {
config, err := GetKubeConfig()
if err != nil {
return err
}

client, err := GetKubeClient()
if err != nil {
return err
}

localFile, err := os.Open(localFilePath)
if err != nil {
log.Printf("Error opening local file: %s\n", err)
}

namespace := "katana"
if len(ns) > 0 {
namespace = ns[0]
}

pod, err := client.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{})
if err != nil {
log.Printf("Error getting pod: %s\n", err)
}

// Find the container in the pod
var container *corev1.Container
for _, c := range pod.Spec.Containers {
if c.Name == containerName {
container = &c
break
}
}

if container == nil {
log.Printf("Container not found in pod\n")
}
// Create a stream to the container
req := client.CoreV1().RESTClient().Post().
Resource("pods").
Name(podName).
Namespace(namespace).
SubResource("exec").
Param("container", containerName)

req.VersionedParams(&corev1.PodExecOptions{
Container: containerName,
Command: []string{"bash", "-c", "cat > " + pathInPod},
Stdin: true,
Stdout: true,
Stderr: true,
TTY: false,
}, scheme.ParameterCodec)

exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
if err != nil {
log.Printf("Error creating executor: %s\n", err)
return err
}

// Stream the file
err = exec.Stream(remotecommand.StreamOptions{
Stdin: localFile,
Stdout: os.Stdout,
Stderr: os.Stderr,
Tty: false,
})
if err != nil {
log.Printf("Error streaming the file: %s\n", err)
return err
}

log.Println("File copied successfully")
return nil
}

func GetKatanaLoadbalancer() string {
client, err := GetKubeClient()
if err != nil {
Expand Down Expand Up @@ -428,3 +354,230 @@ func GetNodes(clientset *kubernetes.Clientset) ([]corev1.Node, error) {

return nodes.Items, nil
}

func CopyTarIntoPod(podName string, containerName string, destPath string, srcPath string, ns ...string) error {
config, err := GetKubeConfig()
if err != nil {
return err
}

client, err := GetKubeClient()
if err != nil {
return err
}

namespace := "katana"
if len(ns) > 0 {
namespace = ns[0]
}

reader, writer := io.Pipe()

var cmdArr []string

cmdArr = []string{"tar", "-zxf", "-"}
SomyaChawla0250 marked this conversation as resolved.
Show resolved Hide resolved
if len(destPath) > 0 {
cmdArr = append(cmdArr, "-C", destPath)
}

go func() {
defer writer.Close()
err := Tar(srcPath, writer)
cmdutil.CheckErr(err)
}()

pod, err := client.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{})
if err != nil {
log.Printf("Error getting pod: %s\n", err)
}

// Find the container in the pod
var container *corev1.Container
for _, c := range pod.Spec.Containers {
if c.Name == containerName {
container = &c
break
}
}

if container == nil {
log.Printf("Container not found in pod\n")
}
// Create a stream to the container
req := client.CoreV1().RESTClient().Post().
Resource("pods").
Name(podName).
Namespace(namespace).
SubResource("exec").
Param("container", containerName)

req.VersionedParams(&corev1.PodExecOptions{
Container: containerName,
Command: cmdArr,
Stdin: true,
Stdout: true,
Stderr: true,
TTY: false,
}, scheme.ParameterCodec)

exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
if err != nil {
log.Printf("Error creating executor: %s\n", err)
return err
}

// Stream the file
err = exec.Stream(remotecommand.StreamOptions{
Stdin: reader,
Stdout: os.Stdout,
Stderr: os.Stderr,
Tty: false,
})
if err != nil {
log.Printf("Error streaming the file: %s\n", err)
return err
}

log.Println("File copied successfully")
return nil
}


func CopyTarIntoPodNew(podName string, containerName string, destPath string, srcPath string, ns ...string) error {
config, err := GetKubeConfig()
if err != nil {
return err
}

client, err := GetKubeClient()
if err != nil {
return err
}

namespace := "katana"
if len(ns) > 0 {
namespace = ns[0]
}

reader, writer := io.Pipe()

var cmdArr []string


cmdArr = []string{"tar", "-zxf", "-"}
if len(destPath) > 0 {
cmdArr = append(cmdArr, "-C", destPath)
}

go func() {
defer writer.Close()
err := cpMakeTar(srcPath, writer)
cmdutil.CheckErr(err)
}()


pod, err := client.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{})
if err != nil {
log.Printf("Error getting pod: %s\n", err)
}

// Find the container in the pod
var container *corev1.Container
for _, c := range pod.Spec.Containers {
if c.Name == containerName {
container = &c
break
}
}

if container == nil {
log.Printf("Container not found in pod\n")
}
// Create a stream to the container
req := client.CoreV1().RESTClient().Post().
Resource("pods").
Name(podName).
Namespace(namespace).
SubResource("exec").
Param("container", containerName)

req.VersionedParams(&corev1.PodExecOptions{
Container: containerName,
Command: cmdArr,
Stdin: true,
Stdout: true,
Stderr: true,
TTY: false,
}, scheme.ParameterCodec)

exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
if err != nil {
log.Printf("Error creating executor: %s\n", err)
return err
}

// Stream the file
err = exec.Stream(remotecommand.StreamOptions{
Stdin: reader,
Stdout: os.Stdout,
Stderr: os.Stderr,
Tty: false,
})
if err != nil {
log.Printf("Error streaming the file: %s\n", err)
return err
}

log.Println("File copied successfully")
return nil
}


func cpMakeTar(src string, writers ...io.Writer) error {

if _, err := os.Stat(src); err != nil {
return fmt.Errorf("unable to tar files - %v", err.Error())
}

mw := io.MultiWriter(writers...)

gzw := gzip.NewWriter(mw)
defer gzw.Close()

tw := tar.NewWriter(gzw)
defer tw.Close()

return filepath.Walk(src, func(file string, fi os.FileInfo, err error) error {

if err != nil {
return err
}

if !fi.Mode().IsRegular() {
return nil
}

header, err := tar.FileInfoHeader(fi, fi.Name())
if err != nil {
return err
}

header.Name = strings.TrimPrefix(file, src+string(filepath.Separator))
if err := tw.WriteHeader(header); err != nil {
return err
}

f, err := os.Open(file)
if err != nil {
return err
}

if _, err := io.Copy(tw, f); err != nil {
return err
}

f.Close()

return nil
})
}
Loading