add helm chart installation

This commit is contained in:
Ybehrooz
2025-11-09 19:36:10 +03:30
parent f5057233a0
commit eb42739472
6 changed files with 154 additions and 74 deletions

View File

@@ -11,14 +11,14 @@ import (
"main/argohandler"
"main/db"
"main/helpers"
"main/jobs"
"main/models"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/hibiken/asynq"
"gopkg.in/yaml.v2"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
@@ -3562,6 +3562,7 @@ func Helm_install(w http.ResponseWriter, r *http.Request) {
Namespace string `json:"Namespace"`
Release string `json:"Release"`
Repo string `json:"Repo"`
Version string `json:"Version"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
@@ -3582,46 +3583,24 @@ func Helm_install(w http.ResponseWriter, r *http.Request) {
return
}
// Write kubeconfig to temp file
tmpFile, err := os.CreateTemp("", "kubeconfig-*.yaml")
var redisClient = asynq.NewClient(asynq.RedisClientOpt{Addr: "130.185.77.247:30828", Password: "xwy8ahx46F"})
chart := jobs.InstallChartPayload{
ChartName: req.Chart,
Release: req.Release,
Version: req.Version,
Namespace: req.Namespace,
UserID: "razzaghi",
}
if _, err := json.Marshal(chart); err != nil {
fmt.Printf("Could not json ")
}
task := jobs.NewInstallCahrtTask(chart.ChartName, chart.Version, chart.Namespace, chart.UserID, chart.Release, kubeconfig)
info, err := redisClient.Enqueue(task)
if err != nil {
http.Error(w, "Failed to create temp file: "+err.Error(), http.StatusInternalServerError)
return
}
defer os.Remove(tmpFile.Name())
if _, err := tmpFile.WriteString(kubeconfig); err != nil {
http.Error(w, "Failed to write kubeconfig: "+err.Error(), http.StatusInternalServerError)
return
}
tmpFile.Close()
// Add repo if not exists
cmd := exec.Command("helm", "repo", "add", "temp-repo", req.Repo)
cmd.Env = append(os.Environ(), "KUBECONFIG="+tmpFile.Name())
output, err := cmd.CombinedOutput()
if err != nil && !strings.Contains(string(output), "already exists") {
http.Error(w, "Failed to add helm repo: "+string(output), http.StatusInternalServerError)
return
}
// Update repo
cmd = exec.Command("helm", "repo", "update")
cmd.Env = append(os.Environ(), "KUBECONFIG="+tmpFile.Name())
output, err = cmd.CombinedOutput()
if err != nil {
http.Error(w, "Failed to update helm repo: "+string(output), http.StatusInternalServerError)
return
}
// Install chart
cmd = exec.Command("helm", "install", req.Release, req.Chart, "--namespace", req.Namespace, "--create-namespace")
cmd.Env = append(os.Environ(), "KUBECONFIG="+tmpFile.Name())
output, err = cmd.CombinedOutput()
if err != nil {
http.Error(w, "Failed to install helm chart: "+string(output), http.StatusInternalServerError)
return
fmt.Printf("Error in connecting redis")
}
fmt.Printf("This is issued task %v", info.ID)
go startWorkerHelmInstaller()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{
@@ -3629,6 +3608,6 @@ func Helm_install(w http.ResponseWriter, r *http.Request) {
"release": req.Release,
"namespace": req.Namespace,
"chart": req.Chart,
"output": string(output),
//"output": string(output),
})
}