add helm chart installation
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/hibiken/asynq"
|
||||
@@ -15,18 +16,23 @@ const (
|
||||
)
|
||||
|
||||
type InstallChartPayload struct {
|
||||
ChartName string
|
||||
Version string
|
||||
Namespace string
|
||||
UserID string
|
||||
ChartName string
|
||||
Version string
|
||||
Namespace string
|
||||
UserID string
|
||||
Release string
|
||||
Repo string
|
||||
KubeConfig string
|
||||
}
|
||||
|
||||
func NewInstallCahrtTask(chartname, version, ns, userID string) *asynq.Task {
|
||||
func NewInstallCahrtTask(chartname, version, ns, userID string, release string, kubeconfig string) *asynq.Task {
|
||||
payload, _ := json.Marshal(InstallChartPayload{
|
||||
ChartName: chartname,
|
||||
Version: version,
|
||||
Namespace: ns,
|
||||
UserID: userID,
|
||||
ChartName: chartname,
|
||||
Version: version,
|
||||
Namespace: ns,
|
||||
UserID: userID,
|
||||
Release: release,
|
||||
KubeConfig: kubeconfig,
|
||||
})
|
||||
|
||||
return asynq.NewTask(TypeInstallChart, payload)
|
||||
@@ -38,7 +44,121 @@ func HandleInstallCahrt(ctx context.Context, t *asynq.Task) error {
|
||||
return fmt.Errorf("Faild to parse payload: %w", err)
|
||||
}
|
||||
|
||||
log.Printf("[Job] Installing chart %s in namespace %s", payload.ChartName, payload.Namespace)
|
||||
// Write kubeconfig to temp file
|
||||
tmpFile, err := os.CreateTemp("", "kubeconfig-*.yaml")
|
||||
if err != nil {
|
||||
//http.Error(w, "Failed to create temp file: "+err.Error(), http.StatusInternalServerError)
|
||||
return err
|
||||
}
|
||||
defer os.Remove(tmpFile.Name())
|
||||
|
||||
if _, err := tmpFile.WriteString(payload.KubeConfig); err != nil {
|
||||
//http.Error(w, "Failed to write kubeconfig: "+err.Error(), http.StatusInternalServerError)
|
||||
return err
|
||||
}
|
||||
tmpFile.Close()
|
||||
|
||||
// settings := cli.New()
|
||||
// settings.KubeConfig = tmpFile.Name()
|
||||
// repoName := payload.ChartName
|
||||
// repoURL := "http://130.185.77.247:31300/gitea_admin/application/src/branch/main/backing-services"
|
||||
// username := "gitea_admin"
|
||||
// password := "Tips123$"
|
||||
|
||||
// // Add the Helm repo
|
||||
// repoFile := settings.RepositoryConfig
|
||||
// repoCache := settings.RepositoryCache
|
||||
// entry := &repo.Entry{
|
||||
// Name: repoName,
|
||||
// URL: repoURL,
|
||||
// Username: username,
|
||||
// Password: password,
|
||||
// }
|
||||
// chartRepo, err := repo.NewChartRepository(entry, getter.All(settings))
|
||||
// if err != nil {
|
||||
// fmt.Printf("Faild to connect to repository, %s", err)
|
||||
// }
|
||||
// _, err = chartRepo.DownloadIndexFile()
|
||||
// if err != nil {
|
||||
// fmt.Println("Failed to connect to repo: %v", err)
|
||||
// }
|
||||
|
||||
// fmt.Println("✅ Connected to Helm repo successfully")
|
||||
|
||||
// // Save to repositories.yaml
|
||||
// file, err := repo.LoadFile(repoFile)
|
||||
// if os.IsNotExist(err) {
|
||||
// file = repo.NewFile()
|
||||
// }
|
||||
// file.Update(entry)
|
||||
// if err := file.WriteFile(repoFile, 0644); err != nil {
|
||||
// fmt.Printf("%s", err)
|
||||
// }
|
||||
|
||||
// // Create Helm install client
|
||||
// actionConfig := new(action.Configuration)
|
||||
// if err := actionConfig.Init(settings.RESTClientGetter(), "default", os.Getenv("HELM_DRIVER"), func(format string, v ...interface{}) {}); err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
|
||||
// install := action.NewInstall(actionConfig)
|
||||
// install.ReleaseName = "mysql"
|
||||
// install.Namespace = "default"
|
||||
// install.RepoURL = repoURL
|
||||
// install.ChartPathOptions.RepoURL = repoURL
|
||||
// install.ChartPathOptions.Username = username
|
||||
// install.ChartPathOptions.Password = password
|
||||
|
||||
// // Chart name
|
||||
// chartName := fmt.Sprintf("%s/mysql", repoName)
|
||||
|
||||
// // Load values (optional)
|
||||
// valOpts := &values.Options{}
|
||||
// vals, err := valOpts.MergeValues(getter.All(settings))
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
|
||||
// // Run the install
|
||||
// cp, err := install.ChartPathOptions.LocateChart(chartName, settings)
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
|
||||
// rel, err := install.RunWithContext(context.Background(), cp, vals)
|
||||
// if err != nil {
|
||||
// panic(fmt.Sprintf("Helm install failed: %v", err))
|
||||
// }
|
||||
|
||||
// fmt.Printf("✅ Helm chart '%s' installed successfully: %s\n", rel.Name, rel.Info.Status)
|
||||
// // 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
|
||||
// }
|
||||
|
||||
log.Printf("[Job] Installing chart %s Release %s in namespace %s", payload.ChartName, payload.Release, payload.Namespace)
|
||||
log.Printf("[Job] Validating Chart ... ")
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user