Skip to content

Commit

Permalink
use kube-system ns uid as cluster id.
Browse files Browse the repository at this point in the history
Signed-off-by: yy <[email protected]>
  • Loading branch information
lingdie committed Nov 7, 2023
1 parent bb5c816 commit 751b0ee
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
10 changes: 9 additions & 1 deletion controllers/license/cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
accountv1 "github.com/labring/sealos/controllers/account/api/v1"
licensev1 "github.com/labring/sealos/controllers/license/api/v1"
"github.com/labring/sealos/controllers/license/internal/controller"
utilid "github.com/labring/sealos/controllers/license/internal/util/clusterid"
"github.com/labring/sealos/controllers/license/internal/util/database"
//+kubebuilder:scaffold:imports
)
Expand Down Expand Up @@ -101,7 +102,14 @@ func main() {
}
defer db.Disconnect(context.Background())

if err = (&controller.LicenseReconciler{ClusterID: os.Getenv("CLUSTER_ID")}).SetupWithManager(mgr, db); err != nil {
clusterID, err := utilid.GetClusterID(context.Background(), mgr.GetClient())
if err != nil {
setupLog.Error(err, "unable to get cluster id")
os.Exit(1)
}
setupLog.Info("cluster id", "id", clusterID)

if err = (&controller.LicenseReconciler{ClusterID: clusterID}).SetupWithManager(mgr, db); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "License")
os.Exit(1)
}
Expand Down
37 changes: 15 additions & 22 deletions controllers/license/internal/util/clusterid/cluster_id.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
package clusterid

//import (
// "context"
// "os"
//
// "github.com/labring/sealos/controllers/license/internal/util/database"
//)
//
//func GetClusterID(ctx context.Context, db *database.DataBase) (string, error) {
// // TODO get cluster id from database
// //id, err := db.GetClusterID(ctx)
// //if errors.Is(err, mongo.ErrNoDocuments) {
// // // if cluster id is not set, set it
// // envID := os.Getenv("CLUSTER_ID")
// // if envID == "" {
// // return "", errors.New("cluster id not set")
// // }
// // if err := db.StoreClusterID(ctx, os.Getenv("CLUSTER_ID")); err != nil {
// // return db.GetClusterID(ctx)
// // }
// //}
// return os.Getenv("CLUSTER_ID"), nil
//}
import (
"context"

corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func GetClusterID(ctx context.Context, c client.Client) (string, error) {
ns := &corev1.Namespace{}
err := c.Get(ctx, client.ObjectKey{Name: "kube-system"}, ns)
if err != nil {
return "", err
}
return string(ns.UID), nil
}

0 comments on commit 751b0ee

Please sign in to comment.