changes
This commit is contained in:
15
.vscode/launch.json
vendored
Normal file
15
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Launch Package",
|
||||||
|
"type": "go",
|
||||||
|
"request": "launch",
|
||||||
|
"mode": "auto",
|
||||||
|
"program": "${workspaceFolder}"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -4,7 +4,9 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"math/rand"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/argoproj/argo-cd/v2/pkg/apiclient"
|
"github.com/argoproj/argo-cd/v2/pkg/apiclient"
|
||||||
"github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
|
"github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
|
||||||
@@ -19,6 +21,7 @@ type ApplicationValues struct {
|
|||||||
Cluster string `yaml:"cluster"`
|
Cluster string `yaml:"cluster"`
|
||||||
RepoURL string `yaml:"repoURL"`
|
RepoURL string `yaml:"repoURL"`
|
||||||
Server string `yaml:"server"`
|
Server string `yaml:"server"`
|
||||||
|
UserID string `yaml:"userID"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -46,7 +49,17 @@ func InitializeClient() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateApp(clustername string) {
|
func generateRandomString(length int) string {
|
||||||
|
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
|
||||||
|
seededRand := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
|
b := make([]byte, length)
|
||||||
|
for i := range b {
|
||||||
|
b[i] = charset[seededRand.Intn(len(charset))]
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateApp(clustername string, ControlPlane string, PlatformVersion string, Cpu string, Memory string, userID string) {
|
||||||
|
|
||||||
InitializeClient()
|
InitializeClient()
|
||||||
|
|
||||||
@@ -56,13 +69,17 @@ func CreateApp(clustername string) {
|
|||||||
log.Fatalf("Failed to create application client: %v", err)
|
log.Fatalf("Failed to create application client: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generate unique cluster name with user prefix
|
||||||
|
uniqueClusterName := "test"
|
||||||
|
|
||||||
app := ApplicationValues{
|
app := ApplicationValues{
|
||||||
Name: clustername,
|
Name: uniqueClusterName,
|
||||||
Namespace: "vc-bhrz-n",
|
Namespace: "ns-" + generateRandomString(4),
|
||||||
Path: "vcluster-0.21.1",
|
Path: "vcluster-0.21.1",
|
||||||
Cluster: "in-cluster",
|
Cluster: "in-cluster",
|
||||||
Server: "https://kubernetes.default.svc",
|
Server: "https://kubernetes.default.svc",
|
||||||
RepoURL: "http://192.168.2.20:8015/root/application.git",
|
RepoURL: "http://192.168.2.20:8015/root/application.git",
|
||||||
|
UserID: userID,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define the ArgoCD application
|
// Define the ArgoCD application
|
||||||
@@ -70,6 +87,10 @@ func CreateApp(clustername string) {
|
|||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: app.Name,
|
Name: app.Name,
|
||||||
Namespace: "argocd",
|
Namespace: "argocd",
|
||||||
|
Labels: map[string]string{
|
||||||
|
"user-id": userID,
|
||||||
|
"type": "vcluster",
|
||||||
|
},
|
||||||
Finalizers: []string{
|
Finalizers: []string{
|
||||||
"resources-finalizer.argocd.argoproj.io",
|
"resources-finalizer.argocd.argoproj.io",
|
||||||
},
|
},
|
||||||
@@ -84,7 +105,32 @@ func CreateApp(clustername string) {
|
|||||||
RepoURL: app.RepoURL,
|
RepoURL: app.RepoURL,
|
||||||
TargetRevision: "HEAD",
|
TargetRevision: "HEAD",
|
||||||
Helm: &argoprojv1alpha1.ApplicationSourceHelm{
|
Helm: &argoprojv1alpha1.ApplicationSourceHelm{
|
||||||
ValueFiles: []string{"vcluster.yaml"},
|
Parameters: []argoprojv1alpha1.HelmParameter{
|
||||||
|
{
|
||||||
|
Name: "controlPlane.advanced.defaultImageRegistry",
|
||||||
|
Value: "192.168.2.43:31898",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "controlPlane.distro.k8s.version",
|
||||||
|
Value: ControlPlane,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "controlPlane.distro.k8s.resources.limits.cpu",
|
||||||
|
Value: Cpu,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "controlPlane.distro.k8s.resources.limits.memory",
|
||||||
|
Value: Memory,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "controlPlane.distro.k8s.resources.requests.cpu",
|
||||||
|
Value: Cpu,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "controlPlane.distro.k8s.resources.requests.memory",
|
||||||
|
Value: Memory,
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Project: "default",
|
Project: "default",
|
||||||
@@ -129,3 +175,28 @@ func SyncApp(appName string) {
|
|||||||
fmt.Printf("Application synced successfully: %s\n", appName)
|
fmt.Printf("Application synced successfully: %s\n", appName)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ListUserClusters(userID string) ([]string, error) {
|
||||||
|
InitializeClient()
|
||||||
|
|
||||||
|
_, appClient, err := client.NewApplicationClient()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create application client: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// List all applications with the user's label
|
||||||
|
selector := fmt.Sprintf("user-id=%s", userID)
|
||||||
|
apps, err := appClient.List(context.Background(), &application.ApplicationQuery{
|
||||||
|
Selector: &selector,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to list applications: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var clusterNames []string
|
||||||
|
for _, app := range apps.Items {
|
||||||
|
clusterNames = append(clusterNames, app.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return clusterNames, nil
|
||||||
|
}
|
||||||
|
|||||||
2
db/db.go
2
db/db.go
@@ -14,7 +14,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func InitDB() {
|
func InitDB() {
|
||||||
clientOptions := options.Client().ApplyURI("mongodb://root:example@192.168.1.10:27017/")
|
clientOptions := options.Client().ApplyURI("mongodb://root:example@172.20.158.234:27017/")
|
||||||
client, err := mongo.Connect(context.TODO(), clientOptions)
|
client, err := mongo.Connect(context.TODO(), clientOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|||||||
@@ -5,9 +5,10 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"main/argohandler"
|
"main/argohandler"
|
||||||
"main/db"
|
"main/db"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
// "github.com/gorilla/mux"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
)
|
)
|
||||||
@@ -24,11 +25,22 @@ type Cluster struct {
|
|||||||
UserID primitive.ObjectID `json:"userId"`
|
UserID primitive.ObjectID `json:"userId"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func createClusterHandler(w http.ResponseWriter, r *http.Request) {
|
type Header struct {
|
||||||
|
Authorization string `bson:"token"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateClusterHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
var cluster Cluster
|
var cluster Cluster
|
||||||
_ = json.NewDecoder(r.Body).Decode(&cluster)
|
_ = json.NewDecoder(r.Body).Decode(&cluster)
|
||||||
|
|
||||||
|
var header Header
|
||||||
|
header.Authorization = r.Header.Get("Authorization")
|
||||||
|
|
||||||
|
log.Fatal("--------------")
|
||||||
|
log.Fatal(r.Header.Get("Authorization"))
|
||||||
|
log.Fatal("--------------")
|
||||||
|
|
||||||
// vclusterCollection := db.Vclusters_details.FindOne(context.TODO(), bson.M{"name": Cluster.Name}).Decode(&existsCluster)
|
// 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 == "" {
|
if cluster.Name == "" || cluster.ControlPlane == "" || cluster.PlatformVersion == "" || cluster.Cpu == "" || cluster.Memory == "" {
|
||||||
@@ -49,7 +61,7 @@ func createClusterHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, `{"message": "Could not create cluster"}`, http.StatusInternalServerError)
|
http.Error(w, `{"message": "Could not create cluster"}`, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
argohandler.CreateApp(cluster.Name)
|
argohandler.CreateApp(cluster.Name, cluster.ControlPlane, cluster.PlatformVersion, cluster.Cpu, cluster.Memory, "userid")
|
||||||
|
|
||||||
response := map[string]string{"message": "Cluster created"}
|
response := map[string]string{"message": "Cluster created"}
|
||||||
|
|
||||||
@@ -58,6 +70,6 @@ func createClusterHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegsiterClusterRoute(r *mux.Router) {
|
// func RegsiterClusterRoute(r *mux.Router) {
|
||||||
r.HandleFunc("/createcluster", createClusterHandler).Methods("POST")
|
// r.HandleFunc("/createcluster", createClusterHandler).Methods("POST", "OPTIONS")
|
||||||
}
|
// }
|
||||||
|
|||||||
16
main.go
16
main.go
@@ -5,7 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
|
|
||||||
"main/db"
|
"main/db"
|
||||||
"main/handler"
|
"main/handler"
|
||||||
@@ -125,15 +125,17 @@ func main() {
|
|||||||
|
|
||||||
router.HandleFunc("/register", registerHnadler)
|
router.HandleFunc("/register", registerHnadler)
|
||||||
router.HandleFunc("/login", loginHandler)
|
router.HandleFunc("/login", loginHandler)
|
||||||
|
router.HandleFunc("/createcluster", handler.CreateClusterHandler)
|
||||||
handler.RegsiterClusterRoute(router)
|
//handler.RegsiterClusterRoute(router)
|
||||||
// Enable CORS
|
// Enable CORS
|
||||||
c := cors.New(cors.Options{
|
c := cors.New(cors.Options{
|
||||||
AllowedOrigins: []string{"http://localhost:4200", "*"}, // Angular app's origin
|
AllowedOrigins: []string{"*"}, // Allow all origins
|
||||||
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
|
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
||||||
AllowedHeaders: []string{"Content-Type"},
|
AllowedHeaders: []string{"Content-Type", "Authorization", "X-Requested-With", "Accept", "Origin"},
|
||||||
|
ExposedHeaders: []string{"Content-Length"},
|
||||||
AllowCredentials: true,
|
AllowCredentials: true,
|
||||||
|
Debug: true, // Enable debug logging
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(":8082", c.Handler((router))))
|
http.ListenAndServe("0.0.0.0:8082", c.Handler(router))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user