Skip to content

Commit

Permalink
feat: add debt message code (#3724)
Browse files Browse the repository at this point in the history
  • Loading branch information
bxy4543 committed Aug 22, 2023
1 parent 205a5bb commit cd47600
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
10 changes: 9 additions & 1 deletion controllers/account/api/v1/debt_webhook.go
Expand Up @@ -22,6 +22,10 @@ import (
"os"
"strings"

account2 "github.com/labring/sealos/controllers/common/account"

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

admissionV1 "k8s.io/api/admission/v1"
"k8s.io/apimachinery/pkg/types"

Expand Down Expand Up @@ -164,7 +168,7 @@ func checkOption(ctx context.Context, logger logr.Logger, c client.Client, nsNam

for _, account := range accountList.Items {
if account.Status.Balance < account.Status.DeductionBalance {
return admission.ValidationResponse(false, fmt.Sprintf("account balance less than 0,now account is %.2f¥", float64(account.Status.Balance-account.Status.DeductionBalance)/1000000))
return admission.ValidationResponse(false, fmt.Sprintf(common.MessageFormat, common.CodeInsufficientBalance, fmt.Sprintf("account balance less than 0,now account is %.2f¥", GetAccountDebtBalance(account))))
}
}
return admission.Allowed("pass user " + user)
Expand All @@ -174,4 +178,8 @@ func getDefaultQuotaName(namespace string) string {
return fmt.Sprintf("quota-%s", namespace)
}

func GetAccountDebtBalance(account Account) float64 {
return account2.GetCurrencyBalance(account.Status.Balance - account.Status.DeductionBalance)
}

const debtLimit0QuotaName = "debt-limit0"
6 changes: 6 additions & 0 deletions controllers/common/account/count.go
Expand Up @@ -16,5 +16,11 @@ limitations under the License.

package account

import "math"

// CurrencyUnit is the unit of currency for accounting.
const CurrencyUnit = 1000000

func GetCurrencyBalance(balance int64) float64 {
return math.Ceil(float64(balance) / CurrencyUnit)
}
10 changes: 10 additions & 0 deletions controllers/pkg/common/common.go
@@ -0,0 +1,10 @@
package common

const (
MessageFormat = "%d: %s"
)

// code
const (
CodeInsufficientBalance = 40001
)

0 comments on commit cd47600

Please sign in to comment.