add config file
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -115,8 +118,10 @@ func CreateApp(clustername string, ControlPlane string, PlatformVersion string,
|
||||
Name: app.Name,
|
||||
Namespace: "argocd",
|
||||
Labels: map[string]string{
|
||||
"user-id": userID,
|
||||
"type": "vcluster",
|
||||
"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)
|
||||
|
||||
Reference in New Issue
Block a user