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

feat: add devbox command #5971

Merged
merged 28 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0d9d3af
feat: add initial `testkube devbox` command
rangoo94 Oct 24, 2024
c8a82a5
feat: clean up development tool a bit
rangoo94 Oct 25, 2024
a535434
fix: GZip the binaries before sending
rangoo94 Oct 25, 2024
3065b6e
fix: small issues with devbox, add dashboard link, add README
rangoo94 Oct 28, 2024
965b784
feat: parallelize devbox better
rangoo94 Oct 28, 2024
8a905e5
chore: add links for CRD Sync workflows and templates
rangoo94 Oct 28, 2024
1c446e4
chore: adjust devbox messages
rangoo94 Oct 28, 2024
4fefdca
chore: reduce size of binaries for devbox
rangoo94 Oct 28, 2024
3b1cc3d
fix: reduce Init Process size from 35MB to 5MB
rangoo94 Oct 28, 2024
bd2cf80
chore: round time in devbox
rangoo94 Oct 28, 2024
3a51866
fix: generate properly slug for devbox environment
rangoo94 Oct 28, 2024
85e5479
fixup lint
rangoo94 Oct 28, 2024
f4f8453
chore: add option to open dashboard
rangoo94 Oct 28, 2024
f084f70
fix: delete debug
rangoo94 Oct 28, 2024
a879137
chore: avoid logs for canceled operation
rangoo94 Oct 28, 2024
90bf0a7
chore: increase timeout for build bucket
rangoo94 Oct 28, 2024
c953c53
fix: restarting pod
rangoo94 Oct 28, 2024
845fa81
fix: restarting pod
rangoo94 Oct 28, 2024
d06fc1b
chore: clean messages and add transfer size
rangoo94 Oct 29, 2024
9d91e7b
feat: add basic binary storage to avoid transferring too much data in…
rangoo94 Oct 30, 2024
0832b7b
feat: make BinaryPatch more stable
rangoo94 Oct 30, 2024
e489e79
fixup delete unused code
rangoo94 Oct 30, 2024
a86ac21
fix: make binary patch more stable
rangoo94 Oct 30, 2024
cf4e378
chore: adjust a bit binary patch constants
rangoo94 Oct 30, 2024
d1ee6d4
chore: reuse buffer better
rangoo94 Oct 30, 2024
4a32325
fix: corner cases where binary patch was stuck
rangoo94 Oct 31, 2024
4084253
fixup lint
rangoo94 Oct 31, 2024
fff985d
fix: avoid unnecessary container for agent in devbox
rangoo94 Oct 31, 2024
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
3 changes: 3 additions & 0 deletions cmd/kubectl-testkube/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common/validator"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/pro"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/config"
"github.com/kubeshop/testkube/cmd/tcl/kubectl-testkube/devbox"
"github.com/kubeshop/testkube/pkg/telemetry"
"github.com/kubeshop/testkube/pkg/ui"
)
Expand Down Expand Up @@ -65,6 +66,8 @@ func init() {
RootCmd.AddCommand(NewDockerCmd())
RootCmd.AddCommand(pro.NewLoginCmd())

RootCmd.AddCommand(devbox.NewDevBoxCommand())

RootCmd.SetHelpCommand(NewHelpCmd())
}

Expand Down
190 changes: 190 additions & 0 deletions cmd/tcl/devbox-mutating-webhook/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
// Copyright 2024 Testkube.
//
// Licensed as a Testkube Pro file under the Testkube Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/kubeshop/testkube/blob/main/licenses/TCL.txt

package main

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"os/signal"
"strings"
"syscall"

"github.com/wI2L/jsondiff"
admissionv1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"

"github.com/kubeshop/testkube/internal/common"
"github.com/kubeshop/testkube/pkg/testworkflows/testworkflowprocessor/constants"
)

func main() {
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})

http.HandleFunc("/mutate", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if r.Header.Get("Content-Type") != "application/json" {
http.Error(w, "invalid content type", http.StatusBadRequest)
return
}

initImage := os.Args[1]
toolkitImage := os.Args[2]

buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
body := buf.Bytes()

if len(body) == 0 {
http.Error(w, "invalid request", http.StatusBadRequest)
return
}

var review admissionv1.AdmissionReview
if err := json.Unmarshal(body, &review); err != nil {
http.Error(w, fmt.Sprintf("invalid request: %s", err), http.StatusBadRequest)
return
}

if review.Request == nil {
http.Error(w, "invalid request: empty", http.StatusBadRequest)
return
}
if review.Request.Kind.Kind != "Pod" {
http.Error(w, fmt.Sprintf("invalid resource: %s", review.Request.Kind.Kind), http.StatusBadRequest)
return
}

pod := corev1.Pod{}
if err := json.Unmarshal(review.Request.Object.Raw, &pod); err != nil {
http.Error(w, fmt.Sprintf("invalid pod provided: %s", err), http.StatusBadRequest)
return
}
originalPod := pod.DeepCopy()

// Apply changes
if pod.Labels[constants.ResourceIdLabelName] != "" {
usesToolkit := false
for _, c := range append(pod.Spec.InitContainers, pod.Spec.Containers...) {
if c.Image == toolkitImage {
usesToolkit = true
}
}

pod.Spec.Volumes = append(pod.Spec.Volumes, corev1.Volume{
Name: "devbox",
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
})

script := `
set -e
/usr/bin/mc config host add minio "http://devbox-storage:9000" "minioadmin" "minioadmin"
/usr/bin/mc cp --disable-multipart minio/devbox/bin/init /.tk-devbox/init
chmod 777 /.tk-devbox/init
chmod +x /.tk-devbox/init
ls -lah /.tk-devbox`
if usesToolkit {
script = `
set -e
/usr/bin/mc config host add minio "http://devbox-storage:9000" "minioadmin" "minioadmin"
/usr/bin/mc cp --disable-multipart minio/devbox/bin/init /.tk-devbox/init
/usr/bin/mc cp --disable-multipart minio/devbox/bin/toolkit /.tk-devbox/toolkit
chmod 777 /.tk-devbox/init
chmod 777 /.tk-devbox/toolkit
chmod +x /.tk-devbox/init
chmod +x /.tk-devbox/toolkit
ls -lah /.tk-devbox`
}

pod.Spec.InitContainers = append([]corev1.Container{{
Name: "devbox-init",
Image: "minio/mc:latest",
ImagePullPolicy: corev1.PullIfNotPresent,
Command: []string{"/bin/sh", "-c"},
Args: []string{script},
}}, pod.Spec.InitContainers...)

// TODO: Handle it better, to not be ambiguous
pod.Annotations[constants.SpecAnnotationName] = strings.ReplaceAll(pod.Annotations[constants.SpecAnnotationName], "\"/toolkit\"", "\"/.tk-devbox/toolkit\"")
pod.Annotations[constants.SpecAnnotationName] = strings.ReplaceAll(pod.Annotations[constants.SpecAnnotationName], "\"/.tktw/toolkit\"", "\"/.tk-devbox/toolkit\"")

for i := range pod.Spec.InitContainers {
if (pod.Spec.InitContainers[i].Image == toolkitImage || pod.Spec.InitContainers[i].Image == initImage) && pod.Spec.InitContainers[i].Command[0] == "/init" {
pod.Spec.InitContainers[i].Command[0] = "/.tk-devbox/init"
}
if pod.Spec.InitContainers[i].Command[0] == "/.tktw/init" {
pod.Spec.InitContainers[i].Command[0] = "/.tk-devbox/init"
}
}
for i := range pod.Spec.Containers {
if (pod.Spec.Containers[i].Image == toolkitImage || pod.Spec.Containers[i].Image == initImage) && pod.Spec.Containers[i].Command[0] == "/init" {
pod.Spec.Containers[i].Command[0] = "/.tk-devbox/init"
}
if pod.Spec.Containers[i].Command[0] == "/.tktw/init" {
pod.Spec.Containers[i].Command[0] = "/.tk-devbox/init"
}
}

for i := range pod.Spec.InitContainers {
pod.Spec.InitContainers[i].VolumeMounts = append(pod.Spec.InitContainers[i].VolumeMounts, corev1.VolumeMount{
Name: "devbox",
MountPath: "/.tk-devbox",
})
}
for i := range pod.Spec.Containers {
pod.Spec.Containers[i].VolumeMounts = append(pod.Spec.Containers[i].VolumeMounts, corev1.VolumeMount{
Name: "devbox",
MountPath: "/.tk-devbox",
})
}
}

patch, err := jsondiff.Compare(originalPod, pod)
if err != nil {
http.Error(w, fmt.Sprintf("failed to build patch for changes: %s", err), http.StatusInternalServerError)
return
}

serializedPatch, err := json.Marshal(patch)
if err != nil {
http.Error(w, fmt.Sprintf("failed to serialize patch for changes: %s", err), http.StatusInternalServerError)
return
}

review.Response = &admissionv1.AdmissionResponse{
UID: review.Request.UID,
Allowed: true,
PatchType: common.Ptr(admissionv1.PatchTypeJSONPatch),
Patch: serializedPatch,
}

serializedResponse, err := json.Marshal(review)
if err != nil {
http.Error(w, fmt.Sprintf("cannot marshal result: %s", err), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "%s", serializedResponse)
})

stopSignal := make(chan os.Signal, 1)
signal.Notify(stopSignal, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-stopSignal
os.Exit(0)
}()

fmt.Println("Starting server...")

panic(http.ListenAndServeTLS(":8443", "/certs/tls.crt", "/certs/tls.key", nil))
}
Loading
Loading