Compare commits

...

2 Commits

Author SHA1 Message Date
behrooz
59bd105247 register created cluster to argocd 2025-11-14 19:35:29 +03:30
behrooz
a23a1e28f4 register created cluster to argocd 2025-11-14 19:33:30 +03:30
3 changed files with 79 additions and 6 deletions

View File

@@ -13,6 +13,8 @@ import (
"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"
clusterapi "github.com/argoproj/argo-cd/v2/pkg/apiclient/cluster"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
argoprojv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" argoprojv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"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"
@@ -61,8 +63,8 @@ var (
func InitializeClient() { func InitializeClient() {
once.Do(func() { once.Do(func() {
argocdServer := "130.185.77.247:30966" argocdServer := "argocd.bugx.ir"
argocdToken := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhcmdvY2QiLCJzdWIiOiJhZG1pbjphcGlLZXkiLCJuYmYiOjE3NjAwMjE1NjAsImlhdCI6MTc2MDAyMTU2MCwianRpIjoiZTdjMzMyNWQtZDU0Yy00M2Q0LWIzOGYtYjc3NGQ4OTcxZGZmIn0.35lU-UOwl3XxjvqEfnEXIhaVoug90-J2WWj5x0OihC0" argocdToken := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhcmdvY2QiLCJzdWIiOiJhZG1pbjphcGlLZXkiLCJuYmYiOjE3NjMxMzQ3NzEsImlhdCI6MTc2MzEzNDc3MSwianRpIjoiNDVkMmViYmYtMDYzNC00MTFlLTk4ODctMjMzMmJjMGFjNDkxIn0.UgQ7oIWzrsP7mCaN8Tfd7wrcFQ7Ew6wYFQHevMIGflw"
config := apiclient.ClientOptions{ config := apiclient.ClientOptions{
ServerAddr: argocdServer, ServerAddr: argocdServer,
@@ -108,7 +110,7 @@ func CreateApp(objectID string, clustername string, ControlPlane string, Platfor
Path: "vcluster-0.28.0", Path: "vcluster-0.28.0",
Cluster: "in-cluster", Cluster: "in-cluster",
Server: "https://kubernetes.default.svc", Server: "https://kubernetes.default.svc",
RepoURL: "http://130.185.77.247:31300/gitea_admin/application.git", RepoURL: "https://git.bugx.ir/gitea_admin/application.git",
UserID: userID, UserID: userID,
} }
@@ -276,11 +278,84 @@ func updateConfig(objectID string, configStrings string, namespace string) {
} }
_, err = db.Vclusters_details.UpdateOne(context.TODO(), filter, update) _, err = db.Vclusters_details.UpdateOne(context.TODO(), filter, update)
RegisterToArgo(configStrings, namespace)
if err != nil { if err != nil {
fmt.Println("update cluster config error: ", err) fmt.Println("update cluster config error: ", err)
} }
} }
func RegisterToArgo(kubeConfigString string, namespace string) {
// ArgoCD server
argocdServer := "argocd.bugx.ir"
argocdToken := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhcmdvY2QiLCJzdWIiOiJhZG1pbjphcGlLZXkiLCJuYmYiOjE3NjMxMzQ3NzEsImlhdCI6MTc2MzEzNDc3MSwianRpIjoiNDVkMmViYmYtMDYzNC00MTFlLTk4ODctMjMzMmJjMGFjNDkxIn0.UgQ7oIWzrsP7mCaN8Tfd7wrcFQ7Ew6wYFQHevMIGflw"
// Context name that must exist in kubeconfig
clusterContext := "kubernetes-super-admin@kubernetes"
decoded, err := base64.StdEncoding.DecodeString(kubeConfigString)
if err != nil {
panic(err)
}
// ---- Load kubeconfig of the target cluster ----
cfg, err := clientcmd.Load([]byte(decoded))
if err != nil {
panic(err)
}
ctx := cfg.Contexts[clusterContext]
if ctx == nil {
panic("cluster context not found in kubeconfig")
}
// Real cluster data
cluster := cfg.Clusters[ctx.Cluster]
user := cfg.AuthInfos[ctx.AuthInfo]
// ---- Create ArgoCD API client ----
clientOpts := apiclient.ClientOptions{
ServerAddr: argocdServer,
AuthToken: argocdToken,
Insecure: false,
}
argocdClient, err := apiclient.NewClient(&clientOpts)
if err != nil {
panic(err)
}
closer, clusterIf, err := argocdClient.NewClusterClient()
if err != nil {
panic(err)
}
defer closer.Close()
// ---- Build the correct request type ----
req := &clusterapi.ClusterCreateRequest{
Cluster: &v1alpha1.Cluster{
Name: namespace + clusterContext,
Server: cluster.Server,
Config: v1alpha1.ClusterConfig{
BearerToken: user.Token,
TLSClientConfig: v1alpha1.TLSClientConfig{
CAData: cluster.CertificateAuthorityData,
Insecure: false,
},
},
},
}
// ---- Register cluster to ArgoCD ----
createdCluster, err := clusterIf.Create(context.Background(), req)
if err != nil {
panic(err)
}
fmt.Println("Cluster registered:", createdCluster.Name)
}
func SyncApp(objectID string, appName string, cluster string, namesname string) { func SyncApp(objectID string, appName string, cluster string, namesname string) {
InitializeClient() InitializeClient()

View File

@@ -59,7 +59,6 @@ func registerHnadler(w http.ResponseWriter, r *http.Request) {
var existUser User var existUser User
_ = db.UserCollection.FindOne(context.TODO(), bson.M{"email": user.Email}).Decode(&existUser) _ = db.UserCollection.FindOne(context.TODO(), bson.M{"email": user.Email}).Decode(&existUser)
if existUser.Email == user.Email { if existUser.Email == user.Email {
http.Error(w, `{"message": "User already registered"}`, http.StatusUnauthorized) http.Error(w, `{"message": "User already registered"}`, http.StatusUnauthorized)
return return
@@ -121,7 +120,6 @@ func loginHandler(w http.ResponseWriter, r *http.Request) {
func main() { func main() {
db.InitDB() db.InitDB()
router := mux.NewRouter() router := mux.NewRouter()
router.HandleFunc("/register", registerHnadler) router.HandleFunc("/register", registerHnadler)
router.HandleFunc("/login", loginHandler) router.HandleFunc("/login", loginHandler)