Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add system monitor #5168

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions controllers/account/controllers/billing_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,39 @@ type BillingReconciler struct {
func (r *BillingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
r.Logger.V(1).Info("Reconcile Billing: ", "req.NamespacedName", req.NamespacedName)
ns := &corev1.Namespace{}
if err := r.Get(ctx, req.NamespacedName, ns); err != nil {
err := r.Get(ctx, req.NamespacedName, ns)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}

if ns.DeletionTimestamp != nil {
r.Logger.V(1).Info("namespace is deleting", "namespace", ns)
return ctrl.Result{}, nil
}

owner := ns.Labels[v1.UserLabelOwnerKey]
nsList, err := getOwnNsList(r.Client, owner)
if err != nil {
r.Logger.Error(err, "get own namespace list failed")
return ctrl.Result{Requeue: true}, err
var (
owner string
nsList []string
)
if !strings.HasPrefix(ns.Name, "ns-") {
owner = SystemOwner
nsList = []string{ns.Name}
} else {
owner = ns.Labels[v1.UserLabelOwnerKey]
nsList, err = getOwnNsList(r.Client, owner)
if err != nil {
r.Logger.Error(err, "get own namespace list failed")
return ctrl.Result{Requeue: true}, err
}
}
r.Logger.V(1).Info("own namespace list", "own", owner, "nsList", nsList)
return r.reconcile(nsList, owner)
}

const SystemOwner = "sealos-system"

func (r *BillingReconciler) reconcile(nsList []string, owner string) (ctrl.Result, error) {
now := time.Now()
currentHourTime := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, time.Local).UTC()
queryTime := currentHourTime.Add(-1 * time.Hour)

// TODO r.处理Unsettle状态的账单

if exist, lastUpdateTime, _ := r.DBClient.GetBillingLastUpdateTime(owner, v12.Consumption); exist {
if lastUpdateTime.Equal(currentHourTime) || lastUpdateTime.After(currentHourTime) {
return ctrl.Result{Requeue: true, RequeueAfter: time.Until(currentHourTime.Add(1*time.Hour + 10*time.Minute))}, nil
Expand All @@ -120,7 +131,7 @@ func (r *BillingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
orderList = append(orderList, ids...)
consumAmount += amount
}
if consumAmount > 0 {
if consumAmount > 0 && owner != SystemOwner {
if err := r.rechargeBalance(owner, consumAmount); err != nil {
for i := range orderList {
if err := r.DBClient.UpdateBillingStatus(orderList[i], resources.Unsettled); err != nil {
Expand Down Expand Up @@ -168,9 +179,8 @@ func (r *BillingReconciler) SetupWithManager(mgr ctrl.Manager, rateOpts controll
}
return ctrl.NewControllerManagedBy(mgr).
For(&corev1.Namespace{}, builder.WithPredicates(predicate.Funcs{
CreateFunc: func(createEvent event.CreateEvent) bool {
own, ok := createEvent.Object.GetLabels()[v1.UserLabelOwnerKey]
return ok && getUsername(createEvent.Object.GetName()) == own
CreateFunc: func(_ event.CreateEvent) bool {
return true
},
UpdateFunc: func(_ event.UpdateEvent) bool {
return false
Expand All @@ -181,7 +191,7 @@ func (r *BillingReconciler) SetupWithManager(mgr ctrl.Manager, rateOpts controll
GenericFunc: func(_ event.GenericEvent) bool {
return false
},
})).
}), builder.OnlyMetadata).
WithOptions(rateOpts).
Complete(r)
}
Expand Down
18 changes: 5 additions & 13 deletions controllers/resources/controllers/monitor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ import (

"golang.org/x/sync/semaphore"

"k8s.io/apimachinery/pkg/selection"

"k8s.io/apimachinery/pkg/labels"

userv1 "github.com/labring/sealos/controllers/user/api/v1"

"github.com/labring/sealos/controllers/user/controllers/helper/config"

"github.com/minio/minio-go/v7"
Expand Down Expand Up @@ -195,13 +189,11 @@ func (r *MonitorReconciler) startPeriodicReconcile() {

func (r *MonitorReconciler) getNamespaceList() (*corev1.NamespaceList, error) {
namespaceList := &corev1.NamespaceList{}
req, err := labels.NewRequirement(userv1.UserLabelOwnerKey, selection.Exists, nil)
if err != nil {
return nil, fmt.Errorf("failed to create label requirement: %v", err)
}
return namespaceList, r.List(context.Background(), namespaceList, &client.ListOptions{
LabelSelector: labels.NewSelector().Add(*req),
})
//req, err := labels.NewRequirement(userv1.UserLabelOwnerKey, selection.Exists, nil)
//if err != nil {
// return nil, fmt.Errorf("failed to create label requirement: %v", err)
//}
return namespaceList, r.List(context.Background(), namespaceList)
}

func waitNextMinute() {
Expand Down
Loading