Skip to content

Commit

Permalink
add license with account v2
Browse files Browse the repository at this point in the history
  • Loading branch information
bxy4543 committed Mar 15, 2024
1 parent 055fe0b commit b52d0cc
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 236 deletions.
2 changes: 1 addition & 1 deletion controllers/account/deploy/Kubefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ 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 create -f manifests/account-manager-config.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/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
Expand Up @@ -10,6 +10,7 @@ data:
OSAdminSecret: '{{ .OSAdminSecret }}'
OSInternalEndpoint: '{{ .OSInternalEndpoint }}'
OSNamespace: '{{ .OSNamespace }}'
MONGO_URI: '{{ .MONGO_URI }}'
LOCAL_COCKROACH_URI: '{{ .LOCAL_COCKROACH_URI }}'
GLOBAL_COCKROACH_URI: '{{ .GLOBAL_COCKROACH_URI }}'
LOCAL_REGION: '{{ .LOCAL_REGION }}'
Expand Down
5 changes: 0 additions & 5 deletions controllers/account/deploy/manifests/deploy.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -1294,11 +1294,6 @@ spec:
fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
- name: MONGO_URI
valueFrom:
secretKeyRef:
key: MONGO_URI
name: mongo-secret
envFrom:
- secretRef:
name: payment-secret
Expand Down
9 changes: 0 additions & 9 deletions controllers/account/deploy/manifests/mongo-secret.yaml.tmpl

This file was deleted.

17 changes: 16 additions & 1 deletion controllers/license/cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
"flag"
"os"

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

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"
Expand Down Expand Up @@ -109,7 +112,19 @@ func main() {
}
setupLog.Info("cluster id", "id", clusterID)

if err = (&controller.LicenseReconciler{ClusterID: clusterID}).SetupWithManager(mgr, db); err != nil {
accountDB, err := cockroach.NewCockRoach(os.Getenv(database2.GlobalCockroachURI), os.Getenv(database2.LocalCockroachURI))
if err != nil {
setupLog.Error(err, "unable to connect to cockroach")
os.Exit(1)
}
defer func() {
err := accountDB.Close()
if err != nil {
setupLog.Error(err, "unable to disconnect from cockroach")
}
}()

if err = (&controller.LicenseReconciler{ClusterID: clusterID}).SetupWithManager(mgr, db, accountDB); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "License")
os.Exit(1)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# 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.

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
Expand Down
2 changes: 1 addition & 1 deletion controllers/license/config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

# Adds namespace to all resources.
namespace: license-system
namespace: account-system

# Value of this field is prepended to the
# names of all resources, e.g. a deployment named
Expand Down
20 changes: 0 additions & 20 deletions controllers/license/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,6 @@ metadata:
creationTimestamp: null
name: manager-role
rules:
- apiGroups:
- account.sealos.io
resources:
- accounts
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- account.sealos.io
resources:
- accounts/status
verbs:
- get
- patch
- update
- apiGroups:
- license.sealos.io
resources:
Expand Down
61 changes: 12 additions & 49 deletions controllers/license/deploy/manifests/deploy.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# 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.

apiVersion: v1
kind: Namespace
metadata:
Expand All @@ -23,7 +9,7 @@ metadata:
app.kubernetes.io/name: namespace
app.kubernetes.io/part-of: license
control-plane: controller-manager
name: license-system
name: account-system
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
Expand Down Expand Up @@ -106,7 +92,7 @@ metadata:
app.kubernetes.io/name: serviceaccount
app.kubernetes.io/part-of: license
name: license-controller-manager
namespace: license-system
namespace: account-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
Expand All @@ -119,7 +105,7 @@ metadata:
app.kubernetes.io/name: role
app.kubernetes.io/part-of: license
name: license-leader-election-role
namespace: license-system
namespace: account-system
rules:
- apiGroups:
- ""
Expand Down Expand Up @@ -159,26 +145,6 @@ metadata:
creationTimestamp: null
name: license-manager-role
rules:
- apiGroups:
- account.sealos.io
resources:
- accounts
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- account.sealos.io
resources:
- accounts/status
verbs:
- get
- patch
- update
- apiGroups:
- license.sealos.io
resources:
Expand Down Expand Up @@ -259,15 +225,15 @@ metadata:
app.kubernetes.io/name: rolebinding
app.kubernetes.io/part-of: license
name: license-leader-election-rolebinding
namespace: license-system
namespace: account-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: license-leader-election-role
subjects:
- kind: ServiceAccount
name: license-controller-manager
namespace: license-system
namespace: account-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
Expand All @@ -287,7 +253,7 @@ roleRef:
subjects:
- kind: ServiceAccount
name: license-controller-manager
namespace: license-system
namespace: account-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
Expand All @@ -307,7 +273,7 @@ roleRef:
subjects:
- kind: ServiceAccount
name: license-controller-manager
namespace: license-system
namespace: account-system
---
apiVersion: v1
kind: Service
Expand All @@ -321,7 +287,7 @@ metadata:
app.kubernetes.io/part-of: license
control-plane: controller-manager
name: license-controller-manager-metrics-service
namespace: license-system
namespace: account-system
spec:
ports:
- name: https
Expand All @@ -343,7 +309,7 @@ metadata:
app.kubernetes.io/part-of: license
control-plane: controller-manager
name: license-controller-manager
namespace: license-system
namespace: account-system
spec:
replicas: 1
selector:
Expand Down Expand Up @@ -386,12 +352,9 @@ spec:
- --leader-elect
command:
- /manager
env:
- name: MONGO_URI
valueFrom:
secretKeyRef:
key: MONGO_URI
name: license-secret
envFrom:
- configMapRef:
name: account-manager-env
image: ghcr.io/labring/sealos-license-controller:latest
livenessProbe:
httpGet:
Expand Down
7 changes: 0 additions & 7 deletions controllers/license/deploy/manifests/secret.yaml.tmpl

This file was deleted.

42 changes: 34 additions & 8 deletions controllers/license/internal/controller/license_activator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,53 @@
package controller

import (
"context"
"fmt"
"strings"

licensev1 "github.com/labring/sealos/controllers/license/api/v1"
accountutil "github.com/labring/sealos/controllers/license/internal/util/account"
claimsutil "github.com/labring/sealos/controllers/license/internal/util/claims"
licenseutil "github.com/labring/sealos/controllers/license/internal/util/license"
count "github.com/labring/sealos/controllers/pkg/account"
database2 "github.com/labring/sealos/controllers/pkg/database"
types2 "github.com/labring/sealos/controllers/pkg/types"
"github.com/labring/sealos/controllers/pkg/utils/logger"

"sigs.k8s.io/controller-runtime/pkg/client"
licensev1 "github.com/labring/sealos/controllers/license/api/v1"
)

type LicenseActivator struct {
client.Client
accountDB database2.AccountV2
}

func (a *LicenseActivator) Active(ctx context.Context, license *licensev1.License) error {
func (l *LicenseActivator) Active(license *licensev1.License) error {
// TODO mv to active function
switch license.Spec.Type {
case licensev1.AccountLicenseType:
if err := accountutil.Recharge(ctx, a.Client, license); err != nil {
return err
if err := l.Recharge(license); err != nil {
return fmt.Errorf("recharge account failed: %w", err)
}
case licensev1.ClusterLicenseType:
// TODO implement cluster license
}
return nil
}

func (l *LicenseActivator) Recharge(license *licensev1.License) error {
claims, err := licenseutil.GetClaims(license)
if err != nil {
return err
}

var data = &claimsutil.AccountClaimData{}
if err := claims.Data.SwitchToAccountData(data); err != nil {
return err
}
owner := GetNameByNameSpace(license.Namespace)

logger.Info("recharge account", "crName", owner, "amount", data.Amount)

return l.accountDB.AddBalance(&types2.UserQueryOpts{Owner: owner}, data.Amount*count.CurrencyUnit)
}

func GetNameByNameSpace(ns string) string {
return strings.TrimPrefix(ns, "ns-")
}
10 changes: 5 additions & 5 deletions controllers/license/internal/controller/license_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"errors"
"time"

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

"github.com/go-logr/logr"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -52,8 +54,6 @@ type LicenseReconciler struct {
// +kubebuilder:rbac:groups=license.sealos.io,resources=licenses,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=license.sealos.io,resources=licenses/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=license.sealos.io,resources=licenses/finalizers,verbs=update
// +kubebuilder:rbac:groups=account.sealos.io,resources=accounts,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=account.sealos.io,resources=accounts/status,verbs=get;update;patch

func (r *LicenseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
r.Logger.V(1).Info("start reconcile for license")
Expand Down Expand Up @@ -110,7 +110,7 @@ func (r *LicenseReconciler) reconcile(ctx context.Context, license *licensev1.Li
return ctrl.Result{}, nil
}

if err := r.activator.Active(ctx, license); err != nil {
if err := r.activator.Active(license); err != nil {
r.Logger.V(1).Error(err, "failed to active license")
return ctrl.Result{}, err
}
Expand All @@ -128,7 +128,7 @@ func (r *LicenseReconciler) reconcile(ctx context.Context, license *licensev1.Li
}

// SetupWithManager sets up the controller with the Manager.
func (r *LicenseReconciler) SetupWithManager(mgr ctrl.Manager, db *database.DataBase) error {
func (r *LicenseReconciler) SetupWithManager(mgr ctrl.Manager, db *database.DataBase, accountDB database2.AccountV2) error {
r.Logger = mgr.GetLogger().WithName("controller").WithName("License")
r.Client = mgr.GetClient()

Expand All @@ -143,7 +143,7 @@ func (r *LicenseReconciler) SetupWithManager(mgr ctrl.Manager, db *database.Data
}

r.activator = &LicenseActivator{
Client: r.Client,
accountDB: accountDB,
}

// reconcile on generation change
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
v1 "github.com/labring/sealos/controllers/license/api/v1"
"github.com/labring/sealos/controllers/license/internal/util/database"
"github.com/labring/sealos/controllers/license/internal/util/meta"

"go.mongodb.org/mongo-driver/mongo"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down
Loading

0 comments on commit b52d0cc

Please sign in to comment.