Skip to content

Commit

Permalink
optimize account service deploy step
Browse files Browse the repository at this point in the history
  • Loading branch information
bxy4543 committed Feb 23, 2024
1 parent 75979b4 commit d4529e0
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 50 deletions.
12 changes: 10 additions & 2 deletions controllers/account/api/v1/debt_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,16 @@ type DebtSpec struct {

// DebtStatus defines the observed state of Debt
type DebtStatus struct {
LastUpdateTimestamp int64 `json:"lastUpdateTimestamp,omitempty"`
AccountDebtStatus DebtStatusType `json:"status,omitempty"`
LastUpdateTimestamp int64 `json:"lastUpdateTimestamp,omitempty"`
DebtStatusRecords []DebtStatusRecord `json:"debtStatusRecords,omitempty"`
AccountDebtStatus DebtStatusType `json:"status,omitempty"`
}

// DebtStatusRecord defines the observed state of Debt
type DebtStatusRecord struct {
LastStatus DebtStatusType `json:"lastDebtStatus,omitempty"`
CurrentStatus DebtStatusType `json:"currentStatus,omitempty"`
UpdateTime int64 `json:"updateTime,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
1 change: 1 addition & 0 deletions controllers/account/controllers/accountv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func TestAccount_V1ToV2(t *testing.T) {
}
}()
wg, ctx := errgroup.WithContext(context.Background())
wg.SetLimit(20)
for _, a := range accounts.Items {
account := a
wg.Go(func() error {
Expand Down
5 changes: 3 additions & 2 deletions controllers/account/deploy/Kubefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ ENV DEFAULT_NAMESPACE account-system
ENV cloudDomain="cloud.sealos.io"
ENV cloudPort=""
ENV MONGO_URI "mongodb://mongo:27017/resources"
ENV COCKROACH_URI ""
ENV GLOBAL_COCKROACH_URI ""
ENV LOCAL_COCKROACH_URI ""
ENV LOCAL_REGION ""
ENV OSNamespace="objectstorage-system"
ENV OSAdminSecret=""
ENV OSInternalEndpoint=""

CMD ["( kubectl create ns $DEFAULT_NAMESPACE || true ) && ( kubectl create -f manifests/mongo-secret.yaml -n $DEFAULT_NAMESPACE || true ) && kubectl apply -f manifests/deploy.yaml -n $DEFAULT_NAMESPACE"]
CMD ["( kubectl create ns $DEFAULT_NAMESPACE || true ) && ( kubectl create -f manifests/mongo-secret.yaml -n $DEFAULT_NAMESPACE || true ) && ( kubectl create -f manifests/account-manager-config.yaml -n $DEFAULT_NAMESPACE || true ) && kubectl apply -f manifests/deploy.yaml -n $DEFAULT_NAMESPACE"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: account-manager-config
data:
ApproachingDeletionPeriod: '{{ .ApproachingDeletionPeriod | default "345600" }}'
ImminentDeletionPeriod: '{{ .ImminentDeletionPeriod | default "259200" }}'
FinalDeletionPeriod: '{{ .FinalDeletionPeriod | default "604800" }}'
DebtDetectionCycleSeconds: '{{ .DebtDetectionCycleSeconds | default "30" }}'
OSAdminSecret: '{{ .OSAdminSecret }}'
OSInternalEndpoint: '{{ .OSInternalEndpoint }}'
OSNamespace: '{{ .OSNamespace }}'
LOCAL_COCKROACH_URI: '{{ .LOCAL_COCKROACH_URI }}'
GLOBAL_COCKROACH_URI: '{{ .GLOBAL_COCKROACH_URI }}'
LOCAL_REGION: '{{ .LOCAL_REGION }}'
DOMAIN: '{{ .cloudDomain }}'
PORT: '{{ .cloudPort }}'
BASE_BALANCE: '{{ .BASE_BALANCE | default "ri79LzQiQrs6CVa1ctE308+AseBXbOua0RIMCXAH5hc3irs=" }}'


22 changes: 2 additions & 20 deletions controllers/account/deploy/manifests/deploy.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -1283,16 +1283,10 @@ spec:
command:
- /manager
env:
- name: DOMAIN
value: '{{ .cloudDomain }}'
- name: PORT
value: '{{ .cloudPort }}'
- name: ACCOUNT_NAMESPACE
value: sealos-system
- name: NAMESPACE_NAME
value: user-system
- name: NEW_ACCOUNT_AMOUNT
value: ri79LzQiQrs6CVa1ctE308+AseBXbOua0RIMCXAH5hc3irs=
- name: WHITELIST
value: licenses.License.license.sealos.io/v1,notifications.Notification.notification.sealos.io/v1,payments.Payment.account.sealos.io/v1,billingrecordqueries.BillingRecordQuery.account.sealos.io/v1,billinginfoqueries.BillingInfoQuery.account.sealos.io/v1,pricequeries.PriceQuery.account.sealos.io/v1
- name: ACCOUNT_SYSTEM_NAMESPACE
Expand All @@ -1305,24 +1299,12 @@ spec:
secretKeyRef:
key: MONGO_URI
name: mongo-secret
- name: ApproachingDeletionPeriod
value: "345600"
- name: ImminentDeletionPeriod
value: "259200"
- name: FinalDeletionPeriod
value: "604800"
- name: DebtDetectionCycleSeconds
value: "30"
- name: OSAdminSecret
value: '{{ .OSAdminSecret }}'
- name: OSInternalEndpoint
value: '{{ .OSInternalEndpoint }}'
- name: OSNamespace
value: '{{ .OSNamespace }}'
envFrom:
- secretRef:
name: payment-secret
optional: true
- configMapRef:
name: account-manager-config
image: ghcr.io/labring/sealos-account-controller:latest
imagePullPolicy: Always
livenessProbe:
Expand Down
3 changes: 2 additions & 1 deletion controllers/pkg/database/cockroach/accountv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ func (g *Cockroach) CreateAccount(ops *types.UserQueryOpts, account *types.Accou

func (g *Cockroach) CreateErrorAccountCreate(account *types.Account, owner, errorMsg string) error {
if err := g.DB.Create(&types.ErrorAccountCreate{
Account: *account,
Account: *account,
//ErrorTime: time.Now().UTC(),
Message: errorMsg,
RegionUserOwner: owner,
RegionUID: g.LocalRegion.UID,
Expand Down
55 changes: 31 additions & 24 deletions controllers/pkg/database/mongo/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -848,36 +848,43 @@ func (m *mongoDB) QueryBillingRecords(billingRecordQuery *accountv1.BillingRecor
//}

func (m *mongoDB) GetBillingCount(accountType common.Type, startTime, endTime time.Time) (count, amount int64, err error) {
filter := bson.M{
"type": accountType,
"time": bson.M{
"$gte": startTime,
"$lte": endTime,
pipeline := bson.A{
bson.M{
"$match": bson.M{
"type": accountType,
"time": bson.M{
"$gte": startTime,
"$lte": endTime,
},
},
},
bson.M{
"$group": bson.M{
"_id": nil,
"count": bson.M{"$sum": 1},
"amount": bson.M{"$sum": "$amount"},
},
},
}
cursor, err := m.getBillingCollection().Find(context.Background(), filter)

cursor, err := m.getBillingCollection().Aggregate(context.Background(), pipeline)
if err != nil {
return 0, 0, err
}
defer cursor.Close(context.Background())
var accountBalanceList []AccountBalanceSpecBSON
err = cursor.All(context.Background(), &accountBalanceList)
if err != nil {
return 0, 0, fmt.Errorf("failed to decode all billing record: %w", err)
}
for i := range accountBalanceList {
count++
amount += accountBalanceList[i].Amount
}
//for cursor.Next(context.Background()) {
// var accountBalance AccountBalanceSpecBSON
// if err := cursor.Decode(&accountBalance); err != nil {
// return 0, 0, err
// }
// count++
// amount += accountBalance.Amount
//}
return

var result struct {
Count int64 `bson:"count"`
Amount int64 `bson:"amount"`
}

if cursor.Next(context.Background()) {
if err := cursor.Decode(&result); err != nil {
return 0, 0, fmt.Errorf("failed to decode aggregation result: %w", err)
}
}

return result.Count, result.Amount, nil
}

func (m *mongoDB) getMeteringCollection() *mongo.Collection {
Expand Down
1 change: 1 addition & 0 deletions controllers/pkg/types/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (TransferAccountV1) TableName() string {

type ErrorAccountCreate struct {
Account
ErrorTime time.Time `gorm:"type:timestamp(3) with time zone;default:current_timestamp();not null"`
RegionUID uuid.UUID `gorm:"column:regionUid;type:uuid;not null"`
RegionUserOwner string `gorm:"column:regionUserOwner;type:text;not null"`
Message string `gorm:"type:text;not null"`
Expand Down
11 changes: 10 additions & 1 deletion service/account/deploy/manifests/deploy.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apiVersion: v1
kind: Service
metadata:
name: account-service
namespace: account-system
labels:
cloud.sealos.io/app-deploy-manager: account-service
spec:
Expand All @@ -15,6 +16,7 @@ apiVersion: apps/v1
kind: Deployment
metadata:
name: account-service
namespace: account-system
annotations:
originImageName: ghcr.io/labring/sealos-account-service:latest
deploy.cloud.sealos.io/minReplicas: '1'
Expand Down Expand Up @@ -43,7 +45,13 @@ spec:
image: ghcr.io/labring/sealos-account-service:latest
env:
- name: MONGO_URI
value: {{ .MONGO_URI }}
valueFrom:
secretKeyRef:
key: MONGO_URI
name: mongo-secret
envFrom:
- configMapRef:
name: account-manager-config
resources:
requests:
cpu: 50m
Expand All @@ -55,4 +63,5 @@ spec:
- containerPort: 2333
imagePullPolicy: Always
volumeMounts: []
serviceAccountName: account-controller-manager
volumes: []

0 comments on commit d4529e0

Please sign in to comment.