From 393a7650c18727648b7bfd1841818f0d86ccace1 Mon Sep 17 00:00:00 2001 From: behrooz razzaghi Date: Fri, 23 May 2025 20:59:36 +0330 Subject: [PATCH] cluster list --- application | 2 +- argohandler/argohandler.go | 29 +++++++++++++++++++++++------ db/db.go | 2 +- handler/handler.go | 9 +++++++++ main.go | 1 + 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/application b/application index f93574d..eba337f 160000 --- a/application +++ b/application @@ -1 +1 @@ -Subproject commit f93574db8f2c35dac6e640f72cc50d9351449fde +Subproject commit eba337fb6889b547a43e511ed0f2383d6fdd0a50 diff --git a/argohandler/argohandler.go b/argohandler/argohandler.go index 236ece8..5a607bf 100644 --- a/argohandler/argohandler.go +++ b/argohandler/argohandler.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "math/rand" + "strings" "sync" "time" @@ -24,6 +25,16 @@ type ApplicationValues struct { UserID string `yaml:"userID"` } +type Clusters struct { + Name string `yaml:"name"` + ClusterID string `yaml:"clusterid"` + Status string `yaml:"status"` + Version string `yaml:version` + HealthCheck string `yaml:"healthcheck` + Alert string `yaml:"alert"` + EndPoint string `yaml:"endpoint"` +} + var ( client apiclient.Client once sync.Once @@ -176,12 +187,12 @@ func SyncApp(appName string) { } -func ListUserClusters(userID string) ([]string, error) { +func ListUserClusters(userID string) (error, []Clusters) { InitializeClient() _, appClient, err := client.NewApplicationClient() if err != nil { - return nil, fmt.Errorf("failed to create application client: %v", err) + return fmt.Errorf("failed to create application client: %v", err), nil } // List all applications with the user's label @@ -190,13 +201,19 @@ func ListUserClusters(userID string) ([]string, error) { Selector: &selector, }) if err != nil { - return nil, fmt.Errorf("failed to list applications: %v", err) + return fmt.Errorf("failed to list applications: %v", err), nil } - var clusterNames []string + clusters := []Clusters{} + for _, app := range apps.Items { - clusterNames = append(clusterNames, app.Name) + newCluster := Clusters{ + Name: app.Name, + Status: string(app.Status.Health.Status), + ClusterID: strings.Split(string(app.ObjectMeta.UID), "-")[0], + } + clusters = append(clusters, newCluster) } - return clusterNames, nil + return nil, clusters } diff --git a/db/db.go b/db/db.go index 0419304..753ffec 100644 --- a/db/db.go +++ b/db/db.go @@ -14,7 +14,7 @@ var ( ) func InitDB() { - clientOptions := options.Client().ApplyURI("mongodb://root:example@192.168.2.177:27017/") + clientOptions := options.Client().ApplyURI("mongodb://root:example@192.168.1.10:27017/") client, err := mongo.Connect(context.TODO(), clientOptions) if err != nil { log.Fatal(err) diff --git a/handler/handler.go b/handler/handler.go index 4817582..49c8d1a 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -65,6 +65,15 @@ func CreateClusterHandler(w http.ResponseWriter, r *http.Request) { } +func ListUserClusters(w http.ResponseWriter, r *http.Request) { + // var cluster Cluster + _, clusterList := argohandler.ListUserClusters("userid") + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(clusterList) + +} + // func RegsiterClusterRoute(r *mux.Router) { // r.HandleFunc("/createcluster", createClusterHandler).Methods("POST", "OPTIONS") // } diff --git a/main.go b/main.go index d5b09ee..7684707 100644 --- a/main.go +++ b/main.go @@ -126,6 +126,7 @@ func main() { router.HandleFunc("/register", registerHnadler) router.HandleFunc("/login", loginHandler) router.HandleFunc("/createcluster", handler.CreateClusterHandler) + router.HandleFunc("/clusters", handler.ListUserClusters) //handler.RegsiterClusterRoute(router) // Enable CORS c := cors.New(cors.Options{