From 61a9fbe9f2e0aa0804fda89a39524c0e36cbe14a Mon Sep 17 00:00:00 2001 From: behrooz razzaghi Date: Sat, 5 Apr 2025 16:58:47 +0330 Subject: [PATCH] fix registry address --- application | 2 +- argohandler/argohandler.go | 10 +++++----- db/db.go | 1 + docker-compose.yaml | 33 +-------------------------------- go.mod | 1 + go.sum | 2 ++ handler/handler.go | 14 +++++++++++--- main.go | 2 +- 8 files changed, 23 insertions(+), 42 deletions(-) diff --git a/application b/application index 587a331..eba337f 160000 --- a/application +++ b/application @@ -1 +1 @@ -Subproject commit 587a331bbeb199fa6c4c5b4b4a61b70f453ccc66 +Subproject commit eba337fb6889b547a43e511ed0f2383d6fdd0a50 diff --git a/argohandler/argohandler.go b/argohandler/argohandler.go index 1a52873..70d24cf 100644 --- a/argohandler/argohandler.go +++ b/argohandler/argohandler.go @@ -29,8 +29,8 @@ var ( func InitializeClient() { once.Do(func() { - argocdServer := "192.168.1.10:31946" - argocdToken := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhcmdvY2QiLCJzdWIiOiJhZG1pbjphcGlLZXkiLCJuYmYiOjE3Mzc5NzA4MTksImlhdCI6MTczNzk3MDgxOSwianRpIjoiYjgzODAxZDQtNWNjOC00NTNiLWE0NTYtNTBjNzZjODkyMzcwIn0.9QXrnVeeTnH18YXb9rE7UTWIk16O63a9O03au35SEwk" + argocdServer := "192.168.2.172:32200" + argocdToken := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhcmdvY2QiLCJzdWIiOiJhZG1pbjphcGlLZXkiLCJuYmYiOjE3NDM4NTAwNzIsImlhdCI6MTc0Mzg1MDA3MiwianRpIjoiNWZhNmQ5MDgtMzljNi00ZWQ4LWE5YzgtMzI4YzMzYjkyNzk4In0.ZvhJk4L5vBQldtJyReKYXCQCWF8j8gHLZlY8PninSFA" config := apiclient.ClientOptions{ ServerAddr: argocdServer, @@ -46,7 +46,7 @@ func InitializeClient() { }) } -func CreateApp() { +func CreateApp(clustername string) { InitializeClient() @@ -57,12 +57,12 @@ func CreateApp() { } app := ApplicationValues{ - Name: "vc-bhrz-n", + Name: clustername, Namespace: "vc-bhrz-n", Path: "vcluster-0.21.1", Cluster: "in-cluster", Server: "https://kubernetes.default.svc", - RepoURL: "http://192.168.1.10:3000/admin/applications", + RepoURL: "http://192.168.2.20:8015/root/application.git", } // Define the ArgoCD application diff --git a/db/db.go b/db/db.go index 8250782..753ffec 100644 --- a/db/db.go +++ b/db/db.go @@ -21,4 +21,5 @@ func InitDB() { } UserCollection = client.Database("vcluster").Collection("users") + Vclusters_details = client.Database("vcluster").Collection("vclusters_details") } diff --git a/docker-compose.yaml b/docker-compose.yaml index df98233..c48dc9f 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -19,35 +19,4 @@ services: environment: ME_CONFIG_MONGODB_ADMINUSERNAME: root ME_CONFIG_MONGODB_ADMINPASSWORD: example - ME_CONFIG_MONGODB_URL: mongodb://root:example@192.168.1.10:27017/ - - gitea: - image: gitea/gitea:latest - container_name: gitea - environment: - - USER_UID=1000 - - USER_GID=1000 - - DB_TYPE=postgres - - DB_HOST=db:5432 - - DB_NAME=gitea - - DB_USER=gitea - - DB_PASSWD=gitea_password - restart: always - volumes: - - ./gitea/data:/data - ports: - - "3000:3000" - - "222:22" - depends_on: - - db - - db: - image: postgres:15 - container_name: gitea_db - environment: - - POSTGRES_USER=gitea - - POSTGRES_PASSWORD=gitea_password - - POSTGRES_DB=gitea - restart: always - volumes: - - ./postgres/data:/var/lib/postgresql/data \ No newline at end of file + ME_CONFIG_MONGODB_URL: mongodb://root:example@192.168.1.10:27017/ \ No newline at end of file diff --git a/go.mod b/go.mod index 9055f80..c75ccf6 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/argoproj/argo-cd/v2 v2.13.3 github.com/golang-jwt/jwt/v4 v4.5.1 github.com/gorilla/mux v1.7.3 + github.com/rs/cors v1.11.0 golang.org/x/crypto v0.32.0 k8s.io/apimachinery v0.31.0 ) diff --git a/go.sum b/go.sum index 654f265..d7cf250 100644 --- a/go.sum +++ b/go.sum @@ -413,6 +413,8 @@ github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/f github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= +github.com/rs/cors v1.11.0 h1:0B9GE/r9Bc2UxRMMtymBkHTenPkHDv0CW4Y98GBY+po= +github.com/rs/cors v1.11.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= diff --git a/handler/handler.go b/handler/handler.go index 8b88d93..ba837ea 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -3,6 +3,7 @@ package handler import ( "context" "encoding/json" + "main/argohandler" "main/db" "net/http" @@ -25,24 +26,31 @@ type Cluster struct { func createClusterHandler(w http.ResponseWriter, r *http.Request) { - vclusterCollection := db.Client.Database("vcluster").Collection("vclusters_details") - var cluster Cluster _ = json.NewDecoder(r.Body).Decode(&cluster) + // vclusterCollection := db.Vclusters_details.FindOne(context.TODO(), bson.M{"name": Cluster.Name}).Decode(&existsCluster) + if cluster.Name == "" || cluster.ControlPlane == "" || cluster.PlatformVersion == "" || cluster.Cpu == "" || cluster.Memory == "" { http.Error(w, "Invalid input", http.StatusBadRequest) return } var existsCluster Cluster - _ = vclusterCollection.FindOne(context.TODO(), bson.M{"name": cluster.Name}).Decode(&existsCluster) + _ = db.Vclusters_details.FindOne(context.TODO(), bson.M{"name": cluster.Name}).Decode(&existsCluster) if existsCluster.Name == cluster.Name { http.Error(w, "Cluster name is duplicated", http.StatusBadRequest) return } + _, 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) + response := map[string]string{"message": "Cluster created"} w.Header().Set("Content-Type", "application/json") diff --git a/main.go b/main.go index cf8f972..a5c1068 100644 --- a/main.go +++ b/main.go @@ -135,5 +135,5 @@ func main() { AllowCredentials: true, }) - log.Fatal(http.ListenAndServe(":8080", c.Handler((router)))) + log.Fatal(http.ListenAndServe(":8082", c.Handler((router)))) }