Skip to content

Commit

Permalink
add recorder interface
Browse files Browse the repository at this point in the history
  • Loading branch information
lingdie committed Oct 12, 2023
1 parent cb84e27 commit 66783aa
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
4 changes: 3 additions & 1 deletion controllers/license/api/v1/license_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ type LicenseStatusPhase string

const (
LicenseStatusPhasePending LicenseStatusPhase = "Pending"
LicenseStatusPhaseSuccess LicenseStatusPhase = "Active"
LicenseStatusPhaseFailed LicenseStatusPhase = "Failed"
LicenseStatusPhaseActive LicenseStatusPhase = "Active"
)

// LicenseStatus defines the observed state of License
type LicenseStatus struct {
// +kubebuilder:validation:Enum=Pending;Failed;Active:default=Pending
Phase LicenseStatusPhase `json:"phase,omitempty"`
}

Expand Down
42 changes: 42 additions & 0 deletions controllers/license/internal/controller/license_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type LicenseReconciler struct {
finalizer *ctrlsdk.Finalizer

validator *LicenseValidator
recorder *LicenseRecorder
}

// +kubebuilder:rbac:groups=license.sealos.io,resources=licenses,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -74,6 +75,42 @@ func (r *LicenseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
func (r *LicenseReconciler) reconcile(ctx context.Context, license *licensev1.License) (ctrl.Result, error) {
r.Logger.V(1).Info("reconcile for license", "license", license.Namespace+"/"+license.Name)

// TODO fix logic and make it more readable

validate, err := r.validator.Validate(*license)
if err != nil {
return ctrl.Result{}, err
}

_, found, err := r.recorder.Get(*license)
if err != nil {
return ctrl.Result{}, err
}

if !validate && !found {
r.Logger.V(1).Info("license is invalid", "license", license.Namespace+"/"+license.Name)

// Update license status to failed
license.Status.Phase = licensev1.LicenseStatusPhaseFailed
_ = r.Status().Update(ctx, license)
} else {
r.Logger.V(1).Info("license is valid", "license", license.Namespace+"/"+license.Name)
// TODO do something after license validated and license is active

// charge account or update cluster license based on license type
switch license.Spec.Type {
case licensev1.AccountLicenseType:
// TODO charge account
case licensev1.ClusterLicenseType:
// TODO update cluster license
}

// record license token and key to database to prevent reuse

// update license status to active
license.Status.Phase = licensev1.LicenseStatusPhaseActive
_ = r.Status().Update(ctx, license)
}
return ctrl.Result{}, nil
}

Expand All @@ -88,6 +125,11 @@ func (r *LicenseReconciler) SetupWithManager(mgr ctrl.Manager) error {
Client: r.Client,
}

// TODO fix this
r.recorder = &LicenseRecorder{
Client: r.Client,
}

// reconcile on generation change
return ctrl.NewControllerManagedBy(mgr).
For(&licensev1.License{}, builder.WithPredicates(predicate.And(predicate.GenerationChangedPredicate{}))).
Expand Down
23 changes: 23 additions & 0 deletions controllers/license/internal/controller/license_recorder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package controller

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

type LicenseRecorder struct {
// maybe you need more or less information to record license, add/delete if you need
client.Client
db *util.DataBase
}

// TODO fix this

func (r *LicenseRecorder) Store(license licensev1.License) error {
return nil
}

func (r *LicenseRecorder) Get(license licensev1.License) (licensev1.License, bool, error) {
return licensev1.License{}, false, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ func (v *LicenseValidator) Validate(license licensev1.License) (bool, error) {
// TODO validate license
// step1: check if license type matches license mode (P2)
// step2: check if license token and key is valid (P1)
// step3: check if license token and key already been used (P1)
return true, nil
}

0 comments on commit 66783aa

Please sign in to comment.