package argohandler import ( "context" "fmt" "log" "sync" "github.com/argoproj/argo-cd/v2/pkg/apiclient" "github.com/argoproj/argo-cd/v2/pkg/apiclient/application" argoprojv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) type ApplicationValues struct { Name string `yaml:"name"` Namespace string `yaml:"namespace"` Path string `yaml:"path"` Cluster string `yaml:"cluster"` RepoURL string `yaml:"repoURL"` Server string `yaml:"server"` } var ( client apiclient.Client once sync.Once err error ) func InitializeClient() { once.Do(func() { argocdServer := "192.168.1.10:31946" argocdToken := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhcmdvY2QiLCJzdWIiOiJhZG1pbjphcGlLZXkiLCJuYmYiOjE3Mzc5NzA4MTksImlhdCI6MTczNzk3MDgxOSwianRpIjoiYjgzODAxZDQtNWNjOC00NTNiLWE0NTYtNTBjNzZjODkyMzcwIn0.9QXrnVeeTnH18YXb9rE7UTWIk16O63a9O03au35SEwk" config := apiclient.ClientOptions{ ServerAddr: argocdServer, AuthToken: argocdToken, Insecure: true, } // Initialize the ArgoCD client client, err = apiclient.NewClient(&config) if err != nil { log.Fatalf("Failed to create ArgoCD client: %v", err) } }) } func CreateApp() { InitializeClient() // Create an application client _, appClient, err := client.NewApplicationClient() if err != nil { log.Fatalf("Failed to create application client: %v", err) } app := ApplicationValues{ Name: "vc-bhrz-n", 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", } // Define the ArgoCD application argoApp := &argoprojv1alpha1.Application{ ObjectMeta: metav1.ObjectMeta{ Name: app.Name, Namespace: "argocd", Finalizers: []string{ "resources-finalizer.argocd.argoproj.io", }, }, Spec: argoprojv1alpha1.ApplicationSpec{ Destination: argoprojv1alpha1.ApplicationDestination{ Namespace: app.Namespace, Name: app.Cluster, }, Source: &argoprojv1alpha1.ApplicationSource{ Path: app.Path, RepoURL: app.RepoURL, TargetRevision: "HEAD", Helm: &argoprojv1alpha1.ApplicationSourceHelm{ ValueFiles: []string{"vcluster.yaml"}, }, }, Project: "default", SyncPolicy: &argoprojv1alpha1.SyncPolicy{ SyncOptions: []string{"CreateNamespace=true"}, }, }, } // Create the application createdApp, err := appClient.Create(context.Background(), &application.ApplicationCreateRequest{ Application: argoApp, }) if err != nil { log.Fatalf("Failed to create application: %v", err) } fmt.Printf("Application created: %s\n", createdApp.Name) SyncApp(app.Name) } func SyncApp(appName string) { InitializeClient() // Create an application client _, appClient, err := client.NewApplicationClient() if err != nil { log.Fatalf("Failed to create application client: %v", err) } // Synchronize the application _, err = appClient.Sync(context.Background(), &application.ApplicationSyncRequest{ Name: &appName, }) if err != nil { log.Fatalf("Failed to sync application: %v", err) } fmt.Printf("Application synced successfully: %s\n", appName) }