Compare commits

..

6 Commits

Author SHA1 Message Date
behrooz
59bd105247 register created cluster to argocd 2025-11-14 19:35:29 +03:30
behrooz
a23a1e28f4 register created cluster to argocd 2025-11-14 19:33:30 +03:30
Ybehrooz
2482e37e8c Merge branch 'master' of http://130.185.77.247:31300/gitea_admin/vclusterapi 2025-11-09 19:36:42 +03:30
Ybehrooz
eb42739472 add helm chart installation 2025-11-09 19:36:10 +03:30
c0d4a62d60 Update .gitmodules 2025-11-09 09:53:38 +00:00
behrooz
f5057233a0 add redis queue to install helmcharts in background 2025-11-08 19:43:13 +03:30
9 changed files with 306 additions and 50 deletions

2
.gitmodules vendored
View File

@@ -1,3 +1,3 @@
[submodule "application"] [submodule "application"]
path = application path = application
url = http://192.168.2.20:8015/root/application url = http://130.185.77.247:31300/gitea_admin/application

View File

@@ -13,6 +13,8 @@ import (
"github.com/argoproj/argo-cd/v2/pkg/apiclient" "github.com/argoproj/argo-cd/v2/pkg/apiclient"
"github.com/argoproj/argo-cd/v2/pkg/apiclient/application" "github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
clusterapi "github.com/argoproj/argo-cd/v2/pkg/apiclient/cluster"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
argoprojv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" argoprojv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
@@ -61,8 +63,8 @@ var (
func InitializeClient() { func InitializeClient() {
once.Do(func() { once.Do(func() {
argocdServer := "130.185.77.247:30966" argocdServer := "argocd.bugx.ir"
argocdToken := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhcmdvY2QiLCJzdWIiOiJhZG1pbjphcGlLZXkiLCJuYmYiOjE3NjAwMjE1NjAsImlhdCI6MTc2MDAyMTU2MCwianRpIjoiZTdjMzMyNWQtZDU0Yy00M2Q0LWIzOGYtYjc3NGQ4OTcxZGZmIn0.35lU-UOwl3XxjvqEfnEXIhaVoug90-J2WWj5x0OihC0" argocdToken := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhcmdvY2QiLCJzdWIiOiJhZG1pbjphcGlLZXkiLCJuYmYiOjE3NjMxMzQ3NzEsImlhdCI6MTc2MzEzNDc3MSwianRpIjoiNDVkMmViYmYtMDYzNC00MTFlLTk4ODctMjMzMmJjMGFjNDkxIn0.UgQ7oIWzrsP7mCaN8Tfd7wrcFQ7Ew6wYFQHevMIGflw"
config := apiclient.ClientOptions{ config := apiclient.ClientOptions{
ServerAddr: argocdServer, ServerAddr: argocdServer,
@@ -108,7 +110,7 @@ func CreateApp(objectID string, clustername string, ControlPlane string, Platfor
Path: "vcluster-0.28.0", Path: "vcluster-0.28.0",
Cluster: "in-cluster", Cluster: "in-cluster",
Server: "https://kubernetes.default.svc", Server: "https://kubernetes.default.svc",
RepoURL: "http://130.185.77.247:31300/gitea_admin/application.git", RepoURL: "https://git.bugx.ir/gitea_admin/application.git",
UserID: userID, UserID: userID,
} }
@@ -276,11 +278,84 @@ func updateConfig(objectID string, configStrings string, namespace string) {
} }
_, err = db.Vclusters_details.UpdateOne(context.TODO(), filter, update) _, err = db.Vclusters_details.UpdateOne(context.TODO(), filter, update)
RegisterToArgo(configStrings, namespace)
if err != nil { if err != nil {
fmt.Println("update cluster config error: ", err) fmt.Println("update cluster config error: ", err)
} }
} }
func RegisterToArgo(kubeConfigString string, namespace string) {
// ArgoCD server
argocdServer := "argocd.bugx.ir"
argocdToken := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhcmdvY2QiLCJzdWIiOiJhZG1pbjphcGlLZXkiLCJuYmYiOjE3NjMxMzQ3NzEsImlhdCI6MTc2MzEzNDc3MSwianRpIjoiNDVkMmViYmYtMDYzNC00MTFlLTk4ODctMjMzMmJjMGFjNDkxIn0.UgQ7oIWzrsP7mCaN8Tfd7wrcFQ7Ew6wYFQHevMIGflw"
// Context name that must exist in kubeconfig
clusterContext := "kubernetes-super-admin@kubernetes"
decoded, err := base64.StdEncoding.DecodeString(kubeConfigString)
if err != nil {
panic(err)
}
// ---- Load kubeconfig of the target cluster ----
cfg, err := clientcmd.Load([]byte(decoded))
if err != nil {
panic(err)
}
ctx := cfg.Contexts[clusterContext]
if ctx == nil {
panic("cluster context not found in kubeconfig")
}
// Real cluster data
cluster := cfg.Clusters[ctx.Cluster]
user := cfg.AuthInfos[ctx.AuthInfo]
// ---- Create ArgoCD API client ----
clientOpts := apiclient.ClientOptions{
ServerAddr: argocdServer,
AuthToken: argocdToken,
Insecure: false,
}
argocdClient, err := apiclient.NewClient(&clientOpts)
if err != nil {
panic(err)
}
closer, clusterIf, err := argocdClient.NewClusterClient()
if err != nil {
panic(err)
}
defer closer.Close()
// ---- Build the correct request type ----
req := &clusterapi.ClusterCreateRequest{
Cluster: &v1alpha1.Cluster{
Name: namespace + clusterContext,
Server: cluster.Server,
Config: v1alpha1.ClusterConfig{
BearerToken: user.Token,
TLSClientConfig: v1alpha1.TLSClientConfig{
CAData: cluster.CertificateAuthorityData,
Insecure: false,
},
},
},
}
// ---- Register cluster to ArgoCD ----
createdCluster, err := clusterIf.Create(context.Background(), req)
if err != nil {
panic(err)
}
fmt.Println("Cluster registered:", createdCluster.Name)
}
func SyncApp(objectID string, appName string, cluster string, namesname string) { func SyncApp(objectID string, appName string, cluster string, namesname string) {
InitializeClient() InitializeClient()

6
go.mod
View File

@@ -6,12 +6,15 @@ require (
github.com/argoproj/argo-cd/v2 v2.13.3 github.com/argoproj/argo-cd/v2 v2.13.3
github.com/golang-jwt/jwt/v4 v4.5.1 github.com/golang-jwt/jwt/v4 v4.5.1
github.com/gorilla/mux v1.7.3 github.com/gorilla/mux v1.7.3
github.com/hibiken/asynq v0.25.1
github.com/rs/cors v1.11.0 github.com/rs/cors v1.11.0
golang.org/x/crypto v0.32.0 golang.org/x/crypto v0.32.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.31.0 k8s.io/api v0.31.0
k8s.io/apimachinery v0.31.0 k8s.io/apimachinery v0.31.0
k8s.io/client-go v0.31.0 k8s.io/client-go v0.31.0
k8s.io/kubectl v0.31.2 k8s.io/kubectl v0.31.2
sigs.k8s.io/yaml v1.4.0
) )
require ( require (
@@ -59,6 +62,7 @@ require (
github.com/redis/go-redis/v9 v9.7.0 // indirect github.com/redis/go-redis/v9 v9.7.0 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/spf13/cobra v1.8.1 // indirect github.com/spf13/cobra v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect
github.com/x448/float16 v0.8.4 // indirect github.com/x448/float16 v0.8.4 // indirect
@@ -87,7 +91,6 @@ require (
google.golang.org/grpc v1.69.4 // indirect google.golang.org/grpc v1.69.4 // indirect
google.golang.org/protobuf v1.36.3 // indirect google.golang.org/protobuf v1.36.3 // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.31.2 // indirect k8s.io/apiextensions-apiserver v0.31.2 // indirect
k8s.io/apiserver v0.31.0 // indirect k8s.io/apiserver v0.31.0 // indirect
@@ -96,7 +99,6 @@ require (
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
oras.land/oras-go/v2 v2.5.0 // indirect oras.land/oras-go/v2 v2.5.0 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.4-0.20241211184406-7bf59b3d70ee // indirect sigs.k8s.io/structured-merge-diff/v4 v4.4.4-0.20241211184406-7bf59b3d70ee // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
) )
require ( require (

6
go.sum
View File

@@ -112,6 +112,8 @@ github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4Nij
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M= github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
@@ -243,6 +245,8 @@ github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB1
github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M= github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU= github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU=
github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk= github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk=
github.com/hibiken/asynq v0.25.1 h1:phj028N0nm15n8O2ims+IvJ2gz4k2auvermngh9JhTw=
github.com/hibiken/asynq v0.25.1/go.mod h1:pazWNOLBu0FEynQRBvHA26qdIKRSmfdIfUm4HdsLmXg=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20240312041847-bd984b5ce465/go.mod h1:gx7rwoVhcfuVKG5uya9Hs3Sxj7EIvldVofAWIUtGouw= github.com/ianlancetaylor/demangle v0.0.0-20240312041847-bd984b5ce465/go.mod h1:gx7rwoVhcfuVKG5uya9Hs3Sxj7EIvldVofAWIUtGouw=
@@ -428,6 +432,8 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/skeema/knownhosts v1.3.0 h1:AM+y0rI04VksttfwjkSTNQorvGqmwATnvnAHpSgc0LY= github.com/skeema/knownhosts v1.3.0 h1:AM+y0rI04VksttfwjkSTNQorvGqmwATnvnAHpSgc0LY=
github.com/skeema/knownhosts v1.3.0/go.mod h1:sPINvnADmT/qYH1kfv+ePMmOBTH6Tbl7b5LvTDjFK7M= github.com/skeema/knownhosts v1.3.0/go.mod h1:sPINvnADmT/qYH1kfv+ePMmOBTH6Tbl7b5LvTDjFK7M=
github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w=
github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=

View File

@@ -11,14 +11,14 @@ import (
"main/argohandler" "main/argohandler"
"main/db" "main/db"
"main/helpers" "main/helpers"
"main/jobs"
"main/models" "main/models"
"net/http" "net/http"
"os"
"os/exec"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"github.com/hibiken/asynq"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
appsv1 "k8s.io/api/apps/v1" appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1" batchv1 "k8s.io/api/batch/v1"
@@ -3562,6 +3562,7 @@ func Helm_install(w http.ResponseWriter, r *http.Request) {
Namespace string `json:"Namespace"` Namespace string `json:"Namespace"`
Release string `json:"Release"` Release string `json:"Release"`
Repo string `json:"Repo"` Repo string `json:"Repo"`
Version string `json:"Version"`
} }
if err := json.NewDecoder(r.Body).Decode(&req); err != nil { if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid JSON body", http.StatusBadRequest) http.Error(w, "Invalid JSON body", http.StatusBadRequest)
@@ -3582,46 +3583,24 @@ func Helm_install(w http.ResponseWriter, r *http.Request) {
return return
} }
// Write kubeconfig to temp file var redisClient = asynq.NewClient(asynq.RedisClientOpt{Addr: "130.185.77.247:30828", Password: "xwy8ahx46F"})
tmpFile, err := os.CreateTemp("", "kubeconfig-*.yaml") 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 { if err != nil {
http.Error(w, "Failed to create temp file: "+err.Error(), http.StatusInternalServerError) fmt.Printf("Error in connecting redis")
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("This is issued task %v", info.ID)
go startWorkerHelmInstaller()
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{ json.NewEncoder(w).Encode(map[string]string{
@@ -3629,6 +3608,6 @@ func Helm_install(w http.ResponseWriter, r *http.Request) {
"release": req.Release, "release": req.Release,
"namespace": req.Namespace, "namespace": req.Namespace,
"chart": req.Chart, "chart": req.Chart,
"output": string(output), //"output": string(output),
}) })
} }

22
handler/worker.go Normal file
View File

@@ -0,0 +1,22 @@
package handler
import (
"log"
"main/jobs"
"github.com/hibiken/asynq"
)
func startWorkerHelmInstaller() {
srv := asynq.NewServer(
asynq.RedisClientOpt{Addr: "130.185.77.247:30828", Password: "xwy8ahx46F"},
asynq.Config{Concurrency: 5},
)
mux := asynq.NewServeMux()
mux.HandleFunc(jobs.TypeInstallChart, jobs.HandleInstallCahrt)
if err := srv.Run(mux); err != nil {
log.Fatalf("Could not run worker: %v", err)
}
}

174
jobs/helminstall.go Normal file
View File

@@ -0,0 +1,174 @@
package jobs
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"time"
"github.com/hibiken/asynq"
)
const (
TypeInstallChart = "chart:install"
)
type InstallChartPayload struct {
ChartName string
Version string
Namespace string
UserID string
Release string
Repo string
KubeConfig string
}
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,
Release: release,
KubeConfig: kubeconfig,
})
return asynq.NewTask(TypeInstallChart, payload)
}
func HandleInstallCahrt(ctx context.Context, t *asynq.Task) error {
var payload InstallChartPayload
if err := json.Unmarshal(t.Payload(), &payload); err != nil {
return fmt.Errorf("Faild to parse payload: %w", err)
}
// 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)
log.Printf("[Job] Creating resources ...")
time.Sleep(2 * time.Second)
log.Printf("[Job] Wating for pods ...")
time.Sleep(2 * time.Second)
log.Printf("[Job] Finilizing installation ...")
log.Printf("[Job] chart %s installed successfully", payload.ChartName)
return nil
}

View File

@@ -59,7 +59,6 @@ func registerHnadler(w http.ResponseWriter, r *http.Request) {
var existUser User var existUser User
_ = db.UserCollection.FindOne(context.TODO(), bson.M{"email": user.Email}).Decode(&existUser) _ = db.UserCollection.FindOne(context.TODO(), bson.M{"email": user.Email}).Decode(&existUser)
if existUser.Email == user.Email { if existUser.Email == user.Email {
http.Error(w, `{"message": "User already registered"}`, http.StatusUnauthorized) http.Error(w, `{"message": "User already registered"}`, http.StatusUnauthorized)
return return
@@ -122,7 +121,6 @@ func main() {
db.InitDB() db.InitDB()
router := mux.NewRouter() router := mux.NewRouter()
router.HandleFunc("/register", registerHnadler) router.HandleFunc("/register", registerHnadler)
router.HandleFunc("/login", loginHandler) router.HandleFunc("/login", loginHandler)
router.HandleFunc("/createcluster", handler.CreateClusterHandler) router.HandleFunc("/createcluster", handler.CreateClusterHandler)