This commit is contained in:
unknown
2025-05-11 20:18:33 +03:30
parent 61a9fbe9f2
commit 7bc33a8808
5 changed files with 119 additions and 19 deletions

View File

@@ -4,7 +4,9 @@ import (
"context"
"fmt"
"log"
"math/rand"
"sync"
"time"
"github.com/argoproj/argo-cd/v2/pkg/apiclient"
"github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
@@ -19,6 +21,7 @@ type ApplicationValues struct {
Cluster string `yaml:"cluster"`
RepoURL string `yaml:"repoURL"`
Server string `yaml:"server"`
UserID string `yaml:"userID"`
}
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()
@@ -56,13 +69,17 @@ func CreateApp(clustername string) {
log.Fatalf("Failed to create application client: %v", err)
}
// Generate unique cluster name with user prefix
uniqueClusterName := "test"
app := ApplicationValues{
Name: clustername,
Namespace: "vc-bhrz-n",
Name: uniqueClusterName,
Namespace: "ns-" + generateRandomString(4),
Path: "vcluster-0.21.1",
Cluster: "in-cluster",
Server: "https://kubernetes.default.svc",
RepoURL: "http://192.168.2.20:8015/root/application.git",
UserID: userID,
}
// Define the ArgoCD application
@@ -70,6 +87,10 @@ func CreateApp(clustername string) {
ObjectMeta: metav1.ObjectMeta{
Name: app.Name,
Namespace: "argocd",
Labels: map[string]string{
"user-id": userID,
"type": "vcluster",
},
Finalizers: []string{
"resources-finalizer.argocd.argoproj.io",
},
@@ -84,7 +105,32 @@ func CreateApp(clustername string) {
RepoURL: app.RepoURL,
TargetRevision: "HEAD",
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",
@@ -129,3 +175,28 @@ func SyncApp(appName string) {
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
}