Skip to content

Commit

Permalink
add license monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
yxxchange authored and bxy4543 committed Jun 29, 2023
1 parent 8dfaa44 commit c76a674
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 59 deletions.
39 changes: 6 additions & 33 deletions controllers/cloud/internal/controller/license_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"

"github.com/go-logr/logr"
accountv1 "github.com/labring/sealos/controllers/account/api/v1"
cloudv1 "github.com/labring/sealos/controllers/cloud/api/v1"
"github.com/labring/sealos/controllers/cloud/internal/controller/util"
cloud "github.com/labring/sealos/controllers/cloud/internal/manager"
Expand Down Expand Up @@ -63,44 +62,18 @@ func (r *LicenseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
var license cloudv1.License
err := r.Client.Get(ctx, types.NamespacedName{Namespace: req.Namespace, Name: req.Name}, &license)
if err != nil {
r.logger.Error(err, "failed to get license cr", "namespace:", req.Namespace, "name:", req.Name)
r.logger.Error(err, "namespace:", req.Namespace, "name:", req.Name)
return ctrl.Result{}, err
}
payload, ok := crypto.IsLicenseValid(license)
if !ok {
err := errors.New("error license")
r.logger.Error(err, "license invalid", "namespace:", req.Namespace, "name:", req.Name)
pack := cloud.NewNotificationPackage(cloud.MessageForLicense, cloud.From, "Your license is invalid")
util.SubmitNotificationWithUser(ctx, r.Client, r.logger, req.Namespace, pack)
return ctrl.Result{}, err
}
var account accountv1.Account
err = r.Client.Get(ctx, types.NamespacedName{Namespace: "account-system", Name: license.Spec.UID}, &account)
if err != nil {
r.logger.Error(err, "failed to get account cr", "namespace:", req.Namespace, "name:", req.Name)
return ctrl.Result{}, err
}
account.Status.EncryptBalance, err := rechargeBalance(account.Status.EncryptBalance, payload["amt"])
if err != nil {
r.logger.Error(err, "Recharge Failed", "namespace:", req.Namespace, "name:", req.Name)
if pack, err := cloud.NewNotificationPackage(cloud.MessageForLicense, cloud.From, "Recharge Failed"); err != nil {
r.logger.Error(err, "NewNotificationPackage", "namespace:", req.Namespace, "name:", req.Name)
} else {
util.SubmitNotificationWithUser(ctx, r.Client, r.logger, req.Namespace, pack)
}
return ctrl.Result{}, err
}
err = r.Client.Update(ctx, &account)
if err != nil {
r.logger.Error(err, "Recharge Failed", "namespace:", req.Namespace, "name:", req.Name)
return ctrl.Result{}, err
}
if pack, _ := cloud.NewNotificationPackage(cloud.MessageForLicense, cloud.From, "Recharge Failed"); err != nil {
r.logger.Error(err, "NewNotificationPackage", "namespace:", req.Namespace, "name:", req.Name)
} else {
util.SubmitNotificationWithUser(ctx, r.Client, r.logger, req.Namespace, pack)
r.logger.Error(err, "namespace:", req.Namespace, "name:", req.Name)
util.SubmitNotificationWithUser(ctx, r.Client, r.logger, req.Name, req.Namespace, "your license is valid")

}
return ctrl.Result{}, err

return ctrl.Result{}, nil
}

// SetupWithManager sets up the controller with the Manager.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (r *LicenseMonitorReconciler) Reconcile(ctx context.Context, req ctrl.Reque
r.logger.Error(em.Concat(": "), "failed to submit new license when check license")
return ctrl.Result{}, em.Concat(": ")
}
//util.SubmitNotificationWithUserCategory(ctx, r.Client, r.logger, r.Users, cloud.AdmPrefix, LicenseMonitorRes.Description)
util.SubmitNotificationWithUserCategory(ctx, r.Client, r.logger, r.Users, cloud.AdmPrefix, LicenseMonitorRes.Description)
}
}
r.logger.Info("Success to complete the delivery process")
Expand Down
35 changes: 16 additions & 19 deletions controllers/cloud/internal/controller/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"errors"
"net/http"
"strconv"
"sync"
"time"

Expand Down Expand Up @@ -170,8 +171,7 @@ func RegisterAndStart(data RegisterAndStartData) *cloud.ErrorMgr {
if em != nil {
return cloud.LoadError("RegisterAndStart", em)
}
pack := cloud.NewNotificationPackage(cloud.MessgaeForRegistration, cloud.From, data.FreeLicense.Description)
SubmitNotificationWithUserCategory(data.ctx, data.client, data.logger, data.Users, cloud.AdmPrefix, pack)
SubmitNotificationWithUserCategory(data.ctx, data.client, data.logger, data.Users, cloud.AdmPrefix, data.FreeLicense.Description)
}
em := data.StartCloudModule()
if em != nil {
Expand Down Expand Up @@ -261,8 +261,13 @@ func (rd *RegisterAndStartData) startCloudClient() *cloud.ErrorMgr {
return nil
}

func SubmitNotificationWithUserCategory(ctx context.Context, client cl.Client, logger logr.Logger, users cloud.UserCategory, prefix string, pack cloud.NotificationPackage) {
notification := cloud.NotificationPackageToNotification(pack)
func SubmitNotificationWithUserCategory(ctx context.Context, client cl.Client, logger logr.Logger, users cloud.UserCategory, prefix string, message string) {
notification := ntf.Notification{}
notification.Name = prefix + strconv.Itoa(int(time.Now().Unix()))
notification.Spec.Message = message
notification.Spec.Title = "Registration successful, welcome!"
notification.Spec.From = "Sealos Cloud"
notification.Spec.Timestamp = time.Now().Unix()
var wg sync.WaitGroup
errchan := make(chan error)
for ns := range users[prefix].Iter() {
Expand All @@ -281,22 +286,14 @@ func SubmitNotificationWithUserCategory(ctx context.Context, client cl.Client, l
}
}

func SubmitNotificationWithUser(ctx context.Context, client cl.Client, logger logr.Logger, namespace string, pack cloud.NotificationPackage) {
notification := cloud.NotificationPackageToNotification(pack)
func SubmitNotificationWithUser(ctx context.Context, client cl.Client, logger logr.Logger, name string, namespace string, message string) {
notification := ntf.Notification{}
notification.Name = name + "-" + "license" + "-" + strconv.Itoa(int(time.Now().Unix()))
notification.Spec.Message = message
notification.Spec.Title = "Registration successful, welcome!"
notification.Spec.From = "Sealos Cloud"
notification.Spec.Timestamp = time.Now().Unix()
notificationTask := cloud.NewNotificationTask(ctx, client, namespace, []ntf.Notification{notification})
var wg sync.WaitGroup
errchan := make(chan error)
wg.Add(1)
go cloud.AsyncCloudTask(&wg, errchan, &notificationTask)
go func() {
wg.Wait()
close(errchan)
}()
for err := range errchan {
if err != nil {
logger.Error(err, "Failed to deliver notification success")
}
}
}

func SubmitLicense(ctx context.Context, client cl.Client, cluster corev1.Secret, expire int64) *cloud.ErrorMgr {
Expand Down
6 changes: 0 additions & 6 deletions controllers/cloud/internal/manager/cloud_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ const (
FALSE = "false"
)

const (
From = "Sealos Cloud"
MessgaeForRegistration = "Registration successful, welcome!"
MessageForLicense = "License Renewal Successful"
)

type PolicyAction string

type NotificationRequest struct {
Expand Down

0 comments on commit c76a674

Please sign in to comment.