add config file

This commit is contained in:
Ybehrooz
2025-05-28 14:25:39 +03:30
parent 419ba18705
commit 92f6b5a9fd
3 changed files with 63 additions and 19 deletions

View File

@@ -2,6 +2,7 @@ package argohandler
import (
"context"
"encoding/base64"
"fmt"
"log"
"main/db"
@@ -14,6 +15,7 @@ import (
"github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
argoprojv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
@@ -48,6 +50,7 @@ type SyncTask struct {
AppName string
Clustername string
Namespace string
ObjectID string
}
var (
@@ -85,7 +88,7 @@ func generateRandomString(length int) string {
return string(b)
}
func CreateApp(clustername string, ControlPlane string, PlatformVersion string, Cpu string, Memory string, userID string) {
func CreateApp(objectID string, clustername string, ControlPlane string, PlatformVersion string, Cpu string, Memory string, userID string) {
InitializeClient()
@@ -117,6 +120,8 @@ func CreateApp(clustername string, ControlPlane string, PlatformVersion string,
Labels: map[string]string{
"user-id": userID,
"type": "vcluster",
"clustername": uniqueClusterName,
"namespace": namespace,
},
Finalizers: []string{
"resources-finalizer.argocd.argoproj.io",
@@ -177,11 +182,11 @@ func CreateApp(clustername string, ControlPlane string, PlatformVersion string,
fmt.Printf("Application created: %s\n", createdApp.Name)
SyncApp(app.Name, clustername, namespace)
SyncApp(objectID, app.Name, clustername, namespace)
}
func getConfig(cluster string, namesname string) {
func getConfig(objectID string, cluster string, namespace string) {
var hostcluster HostConfig
// "vc-"+cluster will be vcluster secret
@@ -203,27 +208,37 @@ func getConfig(cluster string, namesname string) {
log.Fatalf("Error creating clientSet: %v", err)
}
//namespaces, err := clientset.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
// if err != nil {
// log.Fatalf("Error listing namespaces: %v", err)
// }
secret, err := clientset.CoreV1().Secrets(namesname).Get(context.TODO(), "vc-"+cluster, metav1.GetOptions{})
secret, err := clientset.CoreV1().Secrets(namespace).Get(context.TODO(), "vc-"+cluster, metav1.GetOptions{})
if err != nil {
fmt.Println("Error in getting cluster config")
return
}
configStrings := string(secret.Data["config"])
fmt.Println(configStrings)
configStrings = base64.StdEncoding.EncodeToString([]byte(configStrings))
// for _, ns := range namespaces.Items {
// fmt.Println("Namespace:", ns.Name)
// }
updateConfig(objectID, configStrings, namespace)
}
func SyncApp(appName string, cluster string, namesname string) {
func updateConfig(objectID string, configStrings string, namespace string) {
id, _ := primitive.ObjectIDFromHex(objectID)
filter := bson.M{"_id": id}
update := bson.M{
"$set": bson.M{
"cluster_config": configStrings,
"namespace": namespace,
},
}
_, err = db.Vclusters_details.UpdateOne(context.TODO(), filter, update)
if err != nil {
fmt.Println("update cluster config error: ", err)
}
}
func SyncApp(objectID string, appName string, cluster string, namesname string) {
InitializeClient()
@@ -260,7 +275,7 @@ func SyncApp(appName string, cluster string, namesname string) {
if status == "Synced" && health == "Healthy" {
log.Printf("App %s synced successfully", task.AppName)
getConfig(task.Clustername, task.Namespace)
getConfig(task.ObjectID, task.Clustername, task.Namespace)
break
}
@@ -277,6 +292,7 @@ func SyncApp(appName string, cluster string, namesname string) {
AppName: appName,
Clustername: cluster,
Namespace: namesname,
ObjectID: objectID,
}
fmt.Printf("Application synced successfully: %s\n", appName)

View File

@@ -2,6 +2,7 @@ package handler
import (
"context"
"encoding/base64"
"encoding/json"
"main/argohandler"
"main/db"
@@ -22,6 +23,7 @@ type Cluster struct {
Memory string `json:"memory"`
CreatedAt string `json:"createdAt"`
UserID primitive.ObjectID `json:"userId"`
Cluster_config string `json:"clusterconfig"`
}
type Header struct {
@@ -51,12 +53,15 @@ func CreateClusterHandler(w http.ResponseWriter, r *http.Request) {
return
}
_, err := db.Vclusters_details.InsertOne(context.TODO(), cluster)
res, err := db.Vclusters_details.InsertOne(context.TODO(), cluster)
if err != nil {
http.Error(w, `{"message": "Could not create cluster"}`, http.StatusInternalServerError)
}
argohandler.CreateApp(cluster.Name, cluster.ControlPlane, cluster.PlatformVersion, cluster.Cpu, cluster.Memory, "userid")
objectID := res.InsertedID.(primitive.ObjectID)
idStr := objectID.Hex()
argohandler.CreateApp(idStr, cluster.Name, cluster.ControlPlane, cluster.PlatformVersion, cluster.Cpu, cluster.Memory, "userid")
response := map[string]string{"message": "Cluster created"}
@@ -74,6 +79,28 @@ func ListUserClusters(w http.ResponseWriter, r *http.Request) {
}
func Connect(w http.ResponseWriter, r *http.Request) {
clusterName := r.URL.Query().Get("Name")
if clusterName == "" {
http.Error(w, "Missing 'Name' parameter", http.StatusBadRequest)
return
}
var existsCluster Cluster
_ = db.Vclusters_details.FindOne(context.TODO(), bson.M{"name": clusterName}).Decode(&existsCluster)
decoded, err := base64.StdEncoding.DecodeString(existsCluster.Cluster_config)
if err != nil {
http.Error(w, "Failed to decode cluster config", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/x-yaml")
w.Write(decoded)
}
// func RegsiterClusterRoute(r *mux.Router) {
// r.HandleFunc("/createcluster", createClusterHandler).Methods("POST", "OPTIONS")
// }

View File

@@ -127,6 +127,7 @@ func main() {
router.HandleFunc("/login", loginHandler)
router.HandleFunc("/createcluster", handler.CreateClusterHandler)
router.HandleFunc("/clusters", handler.ListUserClusters)
router.HandleFunc("/connect", handler.Connect)
//handler.RegsiterClusterRoute(router)
// Enable CORS
c := cors.New(cors.Options{