add get cluster config
This commit is contained in:
Binary file not shown.
Submodule application updated: eba337fb68...f93574db8f
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"main/db"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -12,7 +13,10 @@ import (
|
||||
"github.com/argoproj/argo-cd/v2/pkg/apiclient"
|
||||
"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"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
)
|
||||
|
||||
type ApplicationValues struct {
|
||||
@@ -35,6 +39,17 @@ type Clusters struct {
|
||||
EndPoint string `yaml:"endpoint"`
|
||||
}
|
||||
|
||||
type HostConfig struct {
|
||||
Name string `yaml:"name"`
|
||||
Kubeconfig string `yaml:"kubeconfig"`
|
||||
}
|
||||
|
||||
type SyncTask struct {
|
||||
AppName string
|
||||
Clustername string
|
||||
Namespace string
|
||||
}
|
||||
|
||||
var (
|
||||
client apiclient.Client
|
||||
once sync.Once
|
||||
@@ -82,10 +97,11 @@ func CreateApp(clustername string, ControlPlane string, PlatformVersion string,
|
||||
|
||||
// Generate unique cluster name with user prefix
|
||||
uniqueClusterName := clustername
|
||||
namespace := "ns-" + generateRandomString(4)
|
||||
|
||||
app := ApplicationValues{
|
||||
Name: uniqueClusterName,
|
||||
Namespace: "ns-" + generateRandomString(4),
|
||||
Namespace: namespace,
|
||||
Path: "vcluster-0.21.1",
|
||||
Cluster: "schoobus-onsite",
|
||||
Server: "https://kubernetes.default.svc",
|
||||
@@ -161,10 +177,53 @@ func CreateApp(clustername string, ControlPlane string, PlatformVersion string,
|
||||
|
||||
fmt.Printf("Application created: %s\n", createdApp.Name)
|
||||
|
||||
SyncApp(app.Name)
|
||||
SyncApp(app.Name, clustername, namespace)
|
||||
|
||||
}
|
||||
|
||||
func SyncApp(appName string) {
|
||||
func getConfig(cluster string, namesname string) {
|
||||
var hostcluster HostConfig
|
||||
|
||||
// "vc-"+cluster will be vcluster secret
|
||||
// kubectl get secret vc-test-prod -n ns-ekf7 -o yaml
|
||||
err := db.Host_cluster_details.FindOne(context.TODO(), bson.M{"name": "host-cluster-1"}).Decode(&hostcluster)
|
||||
if err != nil {
|
||||
fmt.Println("Can't get host config file")
|
||||
}
|
||||
|
||||
kubeconfigBytes := []byte(hostcluster.Kubeconfig)
|
||||
config, err := clientcmd.RESTConfigFromKubeConfig(kubeconfigBytes)
|
||||
if err != nil {
|
||||
log.Fatalf("Error loading kubeconfig from string %v", err)
|
||||
}
|
||||
|
||||
clientset, err := kubernetes.NewForConfig(config)
|
||||
|
||||
if err != nil {
|
||||
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{})
|
||||
if err != nil {
|
||||
fmt.Println("Error in getting cluster config")
|
||||
return
|
||||
}
|
||||
|
||||
configStrings := string(secret.Data["config"])
|
||||
fmt.Println(configStrings)
|
||||
|
||||
// for _, ns := range namespaces.Items {
|
||||
// fmt.Println("Namespace:", ns.Name)
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
func SyncApp(appName string, cluster string, namesname string) {
|
||||
|
||||
InitializeClient()
|
||||
|
||||
@@ -183,6 +242,43 @@ func SyncApp(appName string) {
|
||||
log.Fatalf("Failed to sync application: %v", err)
|
||||
}
|
||||
|
||||
var syncQueue chan SyncTask
|
||||
syncQueue = make(chan SyncTask, 100)
|
||||
|
||||
go func() {
|
||||
for task := range syncQueue {
|
||||
for {
|
||||
app, err := appClient.Get(context.TODO(), &application.ApplicationQuery{Name: &task.AppName})
|
||||
if err != nil {
|
||||
log.Printf("Error fetching app %s: %v", task.AppName, err)
|
||||
break
|
||||
}
|
||||
|
||||
status := app.Status.Sync.Status
|
||||
health := app.Status.Health.Status
|
||||
log.Printf("App %s: sync=%s, health=%s", task.AppName, status, health)
|
||||
|
||||
if status == "Synced" && health == "Healthy" {
|
||||
log.Printf("App %s synced successfully", task.AppName)
|
||||
getConfig(task.Clustername, task.Namespace)
|
||||
break
|
||||
}
|
||||
|
||||
if status == "Unknown" || health == "Degraded" {
|
||||
log.Printf("App %s sync might be stuck or failed", task.AppName)
|
||||
}
|
||||
|
||||
time.Sleep(3 * time.Second)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
syncQueue <- SyncTask{
|
||||
AppName: appName,
|
||||
Clustername: cluster,
|
||||
Namespace: namesname,
|
||||
}
|
||||
|
||||
fmt.Printf("Application synced successfully: %s\n", appName)
|
||||
|
||||
}
|
||||
|
||||
5
db/db.go
5
db/db.go
@@ -10,11 +10,11 @@ import (
|
||||
|
||||
var (
|
||||
Client *mongo.Client
|
||||
UserCollection, Vclusters_details *mongo.Collection
|
||||
UserCollection, Vclusters_details, Host_cluster_details *mongo.Collection
|
||||
)
|
||||
|
||||
func InitDB() {
|
||||
clientOptions := options.Client().ApplyURI("mongodb://root:example@192.168.1.10:27017/")
|
||||
clientOptions := options.Client().ApplyURI("mongodb://root:example@192.168.2.177:27017/")
|
||||
client, err := mongo.Connect(context.TODO(), clientOptions)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -22,4 +22,5 @@ func InitDB() {
|
||||
|
||||
UserCollection = client.Database("vcluster").Collection("users")
|
||||
Vclusters_details = client.Database("vcluster").Collection("vclusters_details")
|
||||
Host_cluster_details = client.Database("vcluster").Collection("hostdetail")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user