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: message notification #4350

Merged
merged 16 commits into from
Nov 29, 2023
Merged
2 changes: 1 addition & 1 deletion controllers/account/controllers/account_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type AccountReconciler struct {
Scheme *runtime.Scheme
Logger logr.Logger
AccountSystemNamespace string
DBClient database.Interface
DBClient database.Account
MongoDBURI string
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/account/controllers/billing_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type BillingReconciler struct {
Scheme *runtime.Scheme
logr.Logger
AccountSystemNamespace string
DBClient database.Interface
DBClient database.Account
Properties *resources.PropertyTypeLS
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type BillingInfoQueryReconciler struct {
client.Client
logr.Logger
Scheme *runtime.Scheme
DBClient database.Interface
DBClient database.Account
Properties *resources.PropertyTypeLS
propertiesQuery []accountv1.PropertyQuery
QueryFuncMap map[string]func(context.Context, ctrl.Request, *accountv1.BillingInfoQuery) (string, error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"strconv"
"time"

"github.com/labring/sealos/controllers/pkg/database/mongo"

"github.com/go-logr/logr"

accountv1 "github.com/labring/sealos/controllers/account/api/v1"
Expand Down Expand Up @@ -71,7 +73,7 @@ func (r *BillingRecordQueryReconciler) Reconcile(ctx context.Context, req ctrl.R
_ = log.FromContext(ctx)

dbCtx := context.Background()
dbClient, err := database.NewMongoDB(dbCtx, r.MongoDBURI)
dbClient, err := mongo.NewMongoInterface(dbCtx, r.MongoDBURI)
if err != nil {
r.Logger.Error(err, "connect mongo client failed")
return ctrl.Result{Requeue: true}, err
Expand Down
181 changes: 158 additions & 23 deletions controllers/account/controllers/debt_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,20 @@ package controllers
import (
"context"
"fmt"
"math"
"os"
"strconv"
"strings"
"time"

"github.com/alibabacloud-go/tea/tea"

"github.com/labring/sealos/controllers/pkg/database"

client2 "github.com/alibabacloud-go/dysmsapi-20170525/v3/client"

"github.com/labring/sealos/controllers/account/controllers/utils"

"github.com/go-logr/logr"

accountv1 "github.com/labring/sealos/controllers/account/api/v1"
Expand All @@ -41,16 +52,32 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

const DebtDetectionCycleEnv = "DebtDetectionCycleSeconds"
const (
DebtDetectionCycleEnv = "DebtDetectionCycleSeconds"

SMSAccessKeyIDEnv = "SMS_AK"
SMSAccessKeySecretEnv = "SMS_SK"
SMSEndpointEnv = "SMS_ENDPOINT"
SMSSignNameEnv = "SMS_SIGN_NAME"
SMSCodeMapEnv = "SMS_CODE_MAP"
)

// DebtReconciler reconciles a Debt object
type DebtReconciler struct {
client.Client
DBClient database.Auth
Scheme *runtime.Scheme
DebtDetectionCycle time.Duration
logr.Logger
accountSystemNamespace string
accountNamespace string
SmsConfig *SmsConfig
}

type SmsConfig struct {
Client *client2.Client
SmsSignName string
SmsCode map[int]string
}

var DebtConfig = accountv1.DefaultDebtConfig
Expand Down Expand Up @@ -151,7 +178,7 @@ func (r *DebtReconciler) reconcileDebtStatus(ctx context.Context, debt *accountv
return nil
}
update = SetDebtStatus(debt, accountv1.WarningPeriod)
if err := r.sendWarningNotice(ctx, userNamespaceList); err != nil {
if err := r.sendWarningNotice(ctx, debt.Spec.UserName, oweamount, userNamespaceList); err != nil {
r.Logger.Error(err, "send warning notice error")
}
case accountv1.WarningPeriod:
Expand All @@ -174,7 +201,7 @@ func (r *DebtReconciler) reconcileDebtStatus(ctx context.Context, debt *accountv
return nil
}
update = SetDebtStatus(debt, accountv1.ApproachingDeletionPeriod)
if err := r.sendApproachingDeletionNotice(ctx, userNamespaceList); err != nil {
if err := r.sendApproachingDeletionNotice(ctx, debt.Spec.UserName, oweamount, userNamespaceList); err != nil {
r.Logger.Error(err, "sendApproachingDeletionNotice error")
}

Expand All @@ -197,7 +224,7 @@ func (r *DebtReconciler) reconcileDebtStatus(ctx context.Context, debt *accountv
return nil
}
update = SetDebtStatus(debt, accountv1.ImminentDeletionPeriod)
if err := r.sendImminentDeletionNotice(ctx, userNamespaceList); err != nil {
if err := r.sendImminentDeletionNotice(ctx, debt.Spec.UserName, oweamount, userNamespaceList); err != nil {
r.Logger.Error(err, "sendImminentDeletionNotice error")
}
if err := r.SuspendUserResource(ctx, userNamespaceList); err != nil {
Expand Down Expand Up @@ -227,7 +254,7 @@ func (r *DebtReconciler) reconcileDebtStatus(ctx context.Context, debt *accountv
}
// TODO 暂时只暂停资源,后续会添加真正删除全部资源逻辑, 或直接删除namespace
update = SetDebtStatus(debt, accountv1.FinalDeletionPeriod)
if err := r.sendFinalDeletionNotice(ctx, userNamespaceList); err != nil {
if err := r.sendFinalDeletionNotice(ctx, debt.Spec.UserName, oweamount, userNamespaceList); err != nil {
r.Error(err, "sendFinalDeletionNotice error")
}
if err := r.SuspendUserResource(ctx, userNamespaceList); err != nil {
Expand Down Expand Up @@ -312,53 +339,115 @@ const (
FinalDeletionNotice
)

var NoticeTemplate = map[int]string{
const (
fromEn = "Debt-System"
fromZh = "欠费系统"
//languageEn = "en"
languageZh = "zh"
debtChoicePrefix = "debt-choice-"
)

var NoticeTemplateEN = map[int]string{
WarningNotice: "Your account balance is not enough to pay this month's bill, and services will be suspended for you. Please recharge in time to avoid affecting your normal use.",
ApproachingDeletionNotice: "Your account balance is not enough to pay this month's bill. The system will delete your resources after three days or after the arrears exceed the recharge amount. Please recharge in time to avoid affecting your normal use.",
ImminentDeletionNotice: "Your container instance resources have been suspended. If you are still in arrears for more than 7 days, the resources will be completely deleted and cannot be recovered. Please recharge in time to avoid affecting your normal use.",
FinalDeletionNotice: "The system has completely deleted all your resources, please recharge in time to avoid affecting your normal use.",
}

func (r *DebtReconciler) sendNotice(ctx context.Context, noticeType int, namespaces []string) error {
var TitleTemplateZH = map[int]string{
WarningNotice: "欠费告警",
ApproachingDeletionNotice: "资源暂停告警",
ImminentDeletionNotice: "资源释放告警",
FinalDeletionNotice: "资源已释放告警",
}

var TitleTemplateEN = map[int]string{
WarningNotice: "Debt Warning",
ApproachingDeletionNotice: "Resource Suspension Warning",
ImminentDeletionNotice: "Resource Release Warning",
FinalDeletionNotice: "Resource Release Warning",
}

var NoticeTemplateZH = map[int]string{
WarningNotice: "您的账户余额不足,系统将为您暂停服务,请及时充值,以免影响您的正常使用。",
ApproachingDeletionNotice: "您的账户余额不足,系统将在三天后或欠费超过充值金额后释放您的资源,请及时充值,以免影响您的正常使用。",
ImminentDeletionNotice: "您的容器实例资源已被暂停,若您仍欠费超过7天,系统将彻底释放资源,无法恢复,请及时充值,以免影响您的正常使用。",
FinalDeletionNotice: "系统已彻底释放您的所有资源,请及时充值,以免影响您的正常使用。",
}

func (r *DebtReconciler) sendSMSNotice(user string, oweAmount int64, noticeType int) error {
if r.SmsConfig == nil {
return nil
}
// TODO send sms
usr, err := r.DBClient.GetUser(user)
if err != nil {
return fmt.Errorf("failed to get user: %w", err)
}
if usr == nil || usr.Phone == "" {
r.Logger.Info("user not exist or user phone is empty, skip sms notification", "user", user)
return nil
}
oweamount := strconv.FormatInt(int64(math.Abs(math.Ceil(float64(oweAmount)/1_000_000))), 10)
return utils.SendSms(r.SmsConfig.Client, &client2.SendSmsRequest{
PhoneNumbers: tea.String(usr.Phone),
SignName: tea.String(r.SmsConfig.SmsSignName),
TemplateCode: tea.String(r.SmsConfig.SmsCode[noticeType]),
// |ownAmount/1_000_000|
TemplateParam: tea.String("{\"user_id\":\"" + user + "\",\"oweamount\":\"" + oweamount + "\"}"),
})
}

func (r *DebtReconciler) sendNotice(ctx context.Context, user string, oweAmount int64, noticeType int, namespaces []string) error {
now := time.Now().UTC().Unix()
ntfTmp := &v1.Notification{
ObjectMeta: metav1.ObjectMeta{
Name: "debt-notice" + strconv.Itoa(noticeType),
Name: debtChoicePrefix + strconv.Itoa(noticeType),
},
Spec: v1.NotificationSpec{
Title: "Debt Notice",
Message: NoticeTemplate[noticeType],
From: "Debt-System",
Importance: v1.High,
Timestamp: now,
}
ntfTmpSpc := v1.NotificationSpec{
Title: TitleTemplateEN[noticeType],
Message: NoticeTemplateEN[noticeType],
From: fromEn,
Importance: v1.High,
DesktopPopup: true,
Timestamp: now,
I18n: map[string]v1.I18n{
languageZh: {
Title: TitleTemplateZH[noticeType],
From: fromZh,
Message: NoticeTemplateZH[noticeType],
},
},
}
for i := range namespaces {
ntf := ntfTmp.DeepCopy()
ntfSpec := ntfTmpSpc.DeepCopy()
ntf.Namespace = namespaces[i]
if _, err := controllerutil.CreateOrUpdate(ctx, r.Client, ntf, func() error {
ntf.Spec = *ntfSpec
return nil
}); err != nil {
return err
}
}
return nil
return r.sendSMSNotice(user, oweAmount, noticeType)
}

func (r *DebtReconciler) sendWarningNotice(ctx context.Context, namespaces []string) error {
return r.sendNotice(ctx, WarningNotice, namespaces)
func (r *DebtReconciler) sendWarningNotice(ctx context.Context, user string, oweAmount int64, namespaces []string) error {
return r.sendNotice(ctx, user, oweAmount, WarningNotice, namespaces)
}

func (r *DebtReconciler) sendApproachingDeletionNotice(ctx context.Context, namespaces []string) error {
return r.sendNotice(ctx, ApproachingDeletionNotice, namespaces)
func (r *DebtReconciler) sendApproachingDeletionNotice(ctx context.Context, user string, oweAmount int64, namespaces []string) error {
return r.sendNotice(ctx, user, oweAmount, ApproachingDeletionNotice, namespaces)
}

func (r *DebtReconciler) sendImminentDeletionNotice(ctx context.Context, namespaces []string) error {
return r.sendNotice(ctx, ImminentDeletionNotice, namespaces)
func (r *DebtReconciler) sendImminentDeletionNotice(ctx context.Context, user string, oweAmount int64, namespaces []string) error {
return r.sendNotice(ctx, user, oweAmount, ImminentDeletionNotice, namespaces)
}

func (r *DebtReconciler) sendFinalDeletionNotice(ctx context.Context, namespaces []string) error {
return r.sendNotice(ctx, FinalDeletionNotice, namespaces)
func (r *DebtReconciler) sendFinalDeletionNotice(ctx context.Context, user string, oweAmount int64, namespaces []string) error {
return r.sendNotice(ctx, user, oweAmount, FinalDeletionNotice, namespaces)
}

func (r *DebtReconciler) SuspendUserResource(ctx context.Context, namespaces []string) error {
Expand All @@ -384,6 +473,45 @@ func (r *DebtReconciler) updateNamespaceStatus(ctx context.Context, status strin
return nil
}

// convert "1:code1,2:code2" to map[int]string
func splitSmsCodeMap(codeStr string) (map[int]string, error) {
codeMap := make(map[int]string)
for _, code := range strings.Split(codeStr, ",") {
split := strings.SplitN(code, ":", 2)
if len(split) != 2 {
return nil, fmt.Errorf("invalid sms code map: %s", codeStr)
}
codeInt, err := strconv.Atoi(split[0])
if err != nil {
return nil, fmt.Errorf("invalid sms code map: %s", codeStr)
}
codeMap[codeInt] = split[1]
}
return codeMap, nil
}

func setupSmsConfig() (*SmsConfig, error) {
if err := env.CheckEnvSetting([]string{SMSAccessKeyIDEnv, SMSAccessKeySecretEnv, SMSEndpointEnv, SMSSignNameEnv, SMSCodeMapEnv}); err != nil {
return nil, fmt.Errorf("check env setting error: %w", err)
}

smsCodeMap, err := splitSmsCodeMap(os.Getenv(SMSCodeMapEnv))
if err != nil {
return nil, fmt.Errorf("split sms code map error: %w", err)
}

smsClient, err := utils.CreateSMSClient(os.Getenv(SMSAccessKeyIDEnv), os.Getenv(SMSAccessKeySecretEnv), os.Getenv(SMSEndpointEnv))
if err != nil {
return nil, fmt.Errorf("create sms client error: %w", err)
}

return &SmsConfig{
Client: smsClient,
SmsSignName: os.Getenv(SMSSignNameEnv),
SmsCode: smsCodeMap,
}, nil
}

// SetupWithManager sets up the controller with the Manager.
func (r *DebtReconciler) SetupWithManager(mgr ctrl.Manager, rateOpts controller.Options) error {
r.Logger = ctrl.Log.WithName("DebtController")
Expand All @@ -393,6 +521,13 @@ func (r *DebtReconciler) SetupWithManager(mgr ctrl.Manager, rateOpts controller.
debtDetectionCycleSecond := env.GetInt64EnvWithDefault(DebtDetectionCycleEnv, 60)
r.DebtDetectionCycle = time.Duration(debtDetectionCycleSecond) * time.Second

smsConfig, err := setupSmsConfig()
if err != nil {
r.Logger.Error(err, "Failed to set up SMS configuration")
} else {
r.SmsConfig = smsConfig
}

/*
{"DebtConfig":{
"ApproachingDeletionPeriod":345600,
Expand Down
39 changes: 39 additions & 0 deletions controllers/account/controllers/debt_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright © 2023 sealos.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package controllers

import (
"testing"
)

func Test_splitSmsCodeMap(t *testing.T) {
codeMap, err := splitSmsCodeMap("0:SMS_123456,1:SMS_654321,2:SMS_987654")
if err != nil {
t.Fatal(err)
}
t.Logf("codeMap: %v", codeMap)
if len(codeMap) != 3 {
t.Fatal("invalid codeMap")
}
if codeMap[0] != "SMS_123456" {
t.Fatal("invalid codeMap")
}
if codeMap[1] != "SMS_654321" {
t.Fatal("invalid codeMap")
}
if codeMap[2] != "SMS_987654" {
t.Fatal("invalid codeMap")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"os"
"time"

"github.com/labring/sealos/controllers/pkg/database/mongo"

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

"github.com/go-logr/logr"
Expand Down Expand Up @@ -59,7 +61,7 @@ type NamespaceBillingHistoryReconciler struct {
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *NamespaceBillingHistoryReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
dbCtx := context.Background()
dbClient, err := database.NewMongoDB(dbCtx, r.MongoDBURI)
dbClient, err := mongo.NewMongoInterface(dbCtx, r.MongoDBURI)
if err != nil {
r.Logger.Error(err, "connect mongo client failed")
return ctrl.Result{Requeue: true}, err
Expand Down Expand Up @@ -91,7 +93,7 @@ func (r *NamespaceBillingHistoryReconciler) Reconcile(ctx context.Context, req c
return ctrl.Result{}, client.IgnoreNotFound(err)
}

func (r *NamespaceBillingHistoryReconciler) reconcile(ctx context.Context, req ctrl.Request, dbClient database.Interface, nsHistory *accountv1.NamespaceBillingHistory) error {
func (r *NamespaceBillingHistoryReconciler) reconcile(ctx context.Context, req ctrl.Request, dbClient database.Account, nsHistory *accountv1.NamespaceBillingHistory) error {
user := &userv1.User{}
if err := r.Get(ctx, client.ObjectKey{Namespace: req.Namespace, Name: getUsername(req.Namespace)}, user); err != nil {
return fmt.Errorf("get user failed: %w", err)
Expand Down
Loading
Loading