Skip to content

Commit

Permalink
Merge branch 'labring:main' into aci
Browse files Browse the repository at this point in the history
  • Loading branch information
lingdie committed Jun 30, 2023
2 parents 6f9a253 + b78d365 commit e299bff
Show file tree
Hide file tree
Showing 87 changed files with 2,758 additions and 256 deletions.
9 changes: 9 additions & 0 deletions controllers/account/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,13 @@ resources:
kind: Namespace
path: k8s.io/api/core/v1
version: v1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: sealos.io
group: account
kind: Transfer
path: github.com/labring/sealos/controllers/account/api/v1
version: v1
version: "3"
2 changes: 2 additions & 0 deletions controllers/account/api/v1/accountbalance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const (
Consumption Type = iota
// Recharge 充值
Recharge
TransferIn
TransferOut
)

const (
Expand Down
71 changes: 71 additions & 0 deletions controllers/account/api/v1/transfer_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright 2023.
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 v1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type TransferState int

const (
TransferStatePending TransferState = iota
TransferStateInProgress
TransferStateCompleted
TransferStateFailed
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// TransferSpec defines the desired state of Transfer
type TransferSpec struct {
To string `json:"to"`
// +kubebuilder:validation:Minimum=1000000
Amount int64 `json:"amount"`
}

// TransferStatus defines the observed state of Transfer
type TransferStatus struct {
Reason string `json:"reason,omitempty"`
Progress TransferState `json:"progress,omitempty"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

// Transfer is the Schema for the transfers API
type Transfer struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec TransferSpec `json:"spec,omitempty"`
Status TransferStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// TransferList contains a list of Transfer
type TransferList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Transfer `json:"items"`
}

func init() {
SchemeBuilder.Register(&Transfer{}, &TransferList{})
}
89 changes: 89 additions & 0 deletions controllers/account/api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions controllers/account/config/crd/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ resources:
- bases/account.sealos.io_debts.yaml
- bases/account.sealos.io_billingrecordqueries.yaml
- bases/account.sealos.io_pricequeries.yaml
- bases/account.sealos.io_transfers.yaml
#+kubebuilder:scaffold:crdkustomizeresource

patchesStrategicMerge:
Expand All @@ -19,6 +20,7 @@ patchesStrategicMerge:
- patches/webhook_in_debts.yaml
#- patches/webhook_in_billingrecordqueries.yaml
#- patches/webhook_in_pricequeries.yaml
#- patches/webhook_in_transfers.yaml
#+kubebuilder:scaffold:crdkustomizewebhookpatch

# [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix.
Expand All @@ -29,6 +31,7 @@ patchesStrategicMerge:
- patches/cainjection_in_debts.yaml
#- patches/cainjection_in_billingrecordqueries.yaml
#- patches/cainjection_in_pricequeries.yaml
#- patches/cainjection_in_transfers.yaml
#+kubebuilder:scaffold:crdkustomizecainjectionpatch

# the following config is for teaching kustomize how to do kustomization for CRDs.
Expand Down
26 changes: 26 additions & 0 deletions controllers/account/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,32 @@ rules:
- get
- patch
- update
- apiGroups:
- account.sealos.io
resources:
- transfers
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- account.sealos.io
resources:
- transfers/finalizers
verbs:
- update
- apiGroups:
- account.sealos.io
resources:
- transfers/status
verbs:
- get
- patch
- update
- apiGroups:
- apps
resources:
Expand Down
6 changes: 5 additions & 1 deletion controllers/account/controllers/account_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,11 @@ func (r *AccountReconciler) updateDeductionBalance(ctx context.Context, accountB
return err
}

account.Status.DeductionBalance += accountBalance.Spec.Amount
if accountBalance.Spec.Type == accountv1.TransferIn {
account.Status.Balance += accountBalance.Spec.Amount
} else {
account.Status.DeductionBalance += accountBalance.Spec.Amount
}

if err := r.Status().Update(ctx, account); err != nil {
r.Logger.Error(err, err.Error())
Expand Down

0 comments on commit e299bff

Please sign in to comment.