cluster list

This commit is contained in:
behrooz razzaghi
2025-05-23 20:59:36 +03:30
parent 35bdfb1248
commit 393a7650c1
5 changed files with 35 additions and 8 deletions

View File

@@ -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
}

View File

@@ -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)

View File

@@ -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")
// }

View File

@@ -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{