Skip to content

Commit

Permalink
fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
yxxchange committed Aug 10, 2023
1 parent 4e57e6d commit f59fa0c
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 65 deletions.
4 changes: 3 additions & 1 deletion controllers/licenseissuer/.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# set the environment variables when local testing
MONITOR= "true"
CAN_CONNECT_TO_EXTERNAL_NETWORK= "true"
MONGO_URI= "mongodb://root:7zfqwxsm@127.0.0.1:27017/sealos-resources?authSource=admin&directConnection=true"
MONGO_URI= "mongodb://root:fd4m9rg6@localhost:27017"
PASSWORD_SALT= "Ym1uazlranZscmIybmRxZmNreXQ4MzU1dWVtN3F3OWtpZDFhNGRvbmprbnNzMnFmNThuendrNDJtdzAyaG1wZw=="
52 changes: 0 additions & 52 deletions controllers/licenseissuer/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,58 +49,6 @@ rules:
- patch
- update
- watch
- apiGroups:
- infostream.sealos.io
resources:
- cloudsyncs
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- infostream.sealos.io
resources:
- cloudsyncs/finalizers
verbs:
- update
- apiGroups:
- infostream.sealos.io
resources:
- cloudsyncs/status
verbs:
- get
- patch
- update
- apiGroups:
- infostream.sealos.io
resources:
- collectors
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- infostream.sealos.io
resources:
- collectors/finalizers
verbs:
- update
- apiGroups:
- infostream.sealos.io
resources:
- collectors/status
verbs:
- get
- patch
- update
- apiGroups:
- infostream.sealos.io
resources:
Expand Down
2 changes: 1 addition & 1 deletion controllers/licenseissuer/deploy/manifests/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ spec:
secretKeyRef:
key: PasswordSalt
name: licenseissuer-env
image: ghcr.io/labring/sealos-licenseissuer-controller:latest
image: registry.cn-hangzhou.aliyuncs.com/fckc/cloud-controller:latest
livenessProbe:
httpGet:
path: /healthz
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ import (
// LicenseReconciler reconciles a License object
type LicenseReconciler struct {
client.Client
Scheme *runtime.Scheme
needRecharge bool
logger logr.Logger
Retries int
Scheme *runtime.Scheme
logger logr.Logger

account accountv1.Account
license issuerv1.License
Expand Down Expand Up @@ -75,11 +73,11 @@ func (r *LicenseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

// for notification
nq := &ntf.NoticeEventQueue{}
nm := &ntf.NotificationManager{}
nm := ntf.NewNotificationManager(ctx, r.Client)
nb := (&ntf.Builder{}).WithLevel(notificationv1.High).
WithTitle(util.LicenseNoticeTitle).WithFrom(util.Sealos).
WithType(ntf.General)
receiver := (&ntf.Receiver{}).SetReceiver(req.Namespace)
receiver := ntf.NewReceiver(ctx, r.Client).SetReceiver(req.Namespace)

reader := &util.Reader{}
// get license
Expand Down Expand Up @@ -108,8 +106,6 @@ func (r *LicenseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
// SetupWithManager sets up the controller with the Manager.
func (r *LicenseReconciler) SetupWithManager(mgr ctrl.Manager) error {
r.logger = ctrl.Log.WithName("LicenseReconcile")
r.Retries = 0
r.needRecharge = true
Predicate := predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return e.Object.GetName() == string(util.LicenseName)
Expand Down Expand Up @@ -158,6 +154,13 @@ func (r *LicenseReconciler) Authorize(ctx context.Context) (string, error) {
if !ok {
return message, errors.New("invalid license")
}
// get account
id := types.NamespacedName{Namespace: util.SealosNamespace, Name: r.license.Spec.UID}
err := r.Client.Get(ctx, id, &r.account)
if err != nil {
r.logger.Error(err, "failed to get account")
return util.RechargeFailedMessage, err
}
// recharge
if util.ContainsFields(payload, util.AmountField) {
err := util.RechargeByLicense(ctx, r.Client, r.account, payload)
Expand Down
14 changes: 12 additions & 2 deletions controllers/licenseissuer/internal/controller/util/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,21 @@ func (t initTask) initWork(instance *TaskInstance) error {
// 2. check if the cluster has been registered
// 3. register to cloud (if the monitor is enabled)
// 4. store cluster-info to k8s
const maxRetry = 5

func (t initTask) register(instance *TaskInstance) error {
var err error
// step 1
err := t.presetRootUser(instance)
for cnt := 0; cnt < maxRetry; cnt++ {
err = t.presetRootUser(instance)
if err != nil {
instance.logger.Error(err, "failed to preset root user")
time.Sleep(time.Minute)
continue
}
break
}
if err != nil {
instance.logger.Error(err, "failed to preset root user")
return err
}
ClusterInfo := createClusterInfo()
Expand Down
3 changes: 3 additions & 0 deletions controllers/licenseissuer/internal/controller/util/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func init() {
func LicenseCheckOnExternalNetwork(ctx context.Context, client client.Client, license issuerv1.License) (map[string]interface{}, bool) {
license.Spec.Key = Key
payload, ok := crypto.IsLicenseValid(license)
if ok {
return payload, ok
}
uid, urlMap, err := GetUIDURL(ctx, client)
res := LicenseMonitorRequest{
UID: uid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func Once(_ context.Context, _ time.Duration, t Task) error {

// Periodic func is used to run a task periodically.
func Periodic(ctx context.Context, period time.Duration, t Task) error {
waitForInit()
for {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -151,3 +152,7 @@ func (ti *TaskInstance) Run() error {
func (ti *TaskInstance) Log() *logr.Logger {
return &ti.logger
}

func waitForInit() {
time.Sleep(time.Minute)
}
11 changes: 10 additions & 1 deletion controllers/pkg/notification/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,25 @@ type Receiver struct {
AdminNamespaces []string
}

func NewReceiver(ctx context.Context, client client.Client) *Receiver {
return &Receiver{
Context: ctx,
Client: client,
}
}

func (rv *Receiver) SetReceiver(receiver string, kind ...Kind) *Receiver {
if len(kind) == 0 {
rv.UserNamespaces = append(rv.UserNamespaces, receiver)
return rv
}
switch kind[0] {
case General:
rv.UserNamespaces = append(rv.UserNamespaces, receiver)
case Admin:
rv.AdminNamespaces = append(rv.AdminNamespaces, receiver)
}
return nil
return rv
}

// Cache of the NamespaceCache caches the namespaces in the cluster
Expand Down Expand Up @@ -134,6 +142,7 @@ func (nb *Builder) AddToEventQueue(neq *NoticeEventQueue) {
From: nb.From,
Message: nb.Message,
Level: nb.Level,
Kind: nb.Kind,
})
}

Expand Down
8 changes: 8 additions & 0 deletions controllers/pkg/notification/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ type NotificationManager struct {
NotificationQueue []v1.Notification
}

func NewNotificationManager(ctx context.Context, client client.Client) *NotificationManager {
return &NotificationManager{
Ctx: ctx,
Client: client,
NotificationQueue: []v1.Notification{},
}
}

// Run of the NotificationManager runs the notification manager.
// It writes the notifications in batches
func (nm *NotificationManager) Run() {
Expand Down

0 comments on commit f59fa0c

Please sign in to comment.