Skip to content

Commit

Permalink
chore: Add a license manager stub
Browse files Browse the repository at this point in the history
  • Loading branch information
rg0now committed Dec 14, 2024
1 parent c8384c6 commit b80d42e
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 17 deletions.
36 changes: 36 additions & 0 deletions internal/licensemanager/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Implementation of the license manager for unlocking enterprise features
package licensemanager

import (
"context"

"github.com/go-logr/logr"
"github.com/l7mp/stunner-gateway-operator/internal/event"
licensecfg "github.com/l7mp/stunner/pkg/config/license"
)

var licenseManagerConstructor = NewStubManager

// Manager is a global license manager that encapsulates the license management logics
type Manager interface {
// Start runs the license manager.
Start(context.Context) error
// Validate checks whether a client is entitled to use a feature.
Validate(feature licensecfg.Feature) bool
// SubscriptionType returns the current subscription type (e.g., free, member, enterprise).
SubscriptionType() string
// SetOperatorChannel sets up the operator channel where the manager can send rendering
SetOperatorChannel(c chan event.Event)
// LastError returns the last license manager error.
LastError() error
}

type Config struct {
CustomerKey string
LicenseManagerClient any
Logger logr.Logger
}

func NewManager(config Config) Manager {
return licenseManagerConstructor(config)
}
19 changes: 19 additions & 0 deletions internal/licensemanager/stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package licensemanager

import (
"context"

"github.com/l7mp/stunner-gateway-operator/internal/event"
licensecfg "github.com/l7mp/stunner/pkg/config/license"
)

// license manager stub
type stubMgr struct{}

func NewStubManager(_ Config) Manager { return &stubMgr{} }

func (_ *stubMgr) Start(_ context.Context) error { return nil }
func (_ *stubMgr) Validate(_ licensecfg.Feature) bool { return true }
func (_ *stubMgr) SubscriptionType() string { return "free" }
func (_ *stubMgr) LastError() error { return nil }
func (_ *stubMgr) SetOperatorChannel(_ chan event.Event) {}
8 changes: 6 additions & 2 deletions internal/renderer/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ import (

"github.com/l7mp/stunner-gateway-operator/internal/config"
"github.com/l7mp/stunner-gateway-operator/internal/event"
licensemgr "github.com/l7mp/stunner-gateway-operator/internal/licensemanager"
)

type RendererConfig struct {
Scheme *runtime.Scheme
Logger logr.Logger
Scheme *runtime.Scheme
LicenseManager licensemgr.Manager
Logger logr.Logger
}

type Renderer struct {
ctx context.Context
scheme *runtime.Scheme
licmgr licensemgr.Manager
gen int
renderCh, operatorCh chan event.Event
*config.ProgressTracker
Expand All @@ -28,6 +31,7 @@ type Renderer struct {
func NewRenderer(cfg RendererConfig) *Renderer {
return &Renderer{
scheme: cfg.Scheme,
licmgr: cfg.LicenseManager,
renderCh: make(chan event.Event, 10),
gen: 0,
ProgressTracker: config.NewProgressTracker(),
Expand Down
51 changes: 40 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/l7mp/stunner/pkg/buildinfo"

"github.com/l7mp/stunner-gateway-operator/internal/config"
licensemgr "github.com/l7mp/stunner-gateway-operator/internal/licensemanager"
"github.com/l7mp/stunner-gateway-operator/internal/operator"
"github.com/l7mp/stunner-gateway-operator/internal/renderer"
"github.com/l7mp/stunner-gateway-operator/internal/updater"
Expand All @@ -54,8 +55,9 @@ import (
)

const (
envVarMode = "STUNNER_GATEWAY_OPERATOR_DATAPLANE_MODE"
envVarAddress = "STUNNER_GATEWAY_OPERATOR_ADDRESS"
envVarMode = "STUNNER_GATEWAY_OPERATOR_DATAPLANE_MODE"
envVarAddress = "STUNNER_GATEWAY_OPERATOR_ADDRESS"
envVarCustomerKey = "CUSTOMER_KEY"
)

var (
Expand Down Expand Up @@ -131,6 +133,13 @@ func main() {
config.DataplaneMode = config.NewDataplaneMode(dataplaneMode)
setupLog.Info("dataplane mode", "mode", config.DataplaneMode.String())

customerKey, keyStatus := "", "MISSING"
if key, ok := os.LookupEnv(envVarCustomerKey); ok {
customerKey = key
keyStatus = "AVAILABLE"
}
setupLog.Info("subscriber key", "status", keyStatus)

// CDS address not overrridden on the command line: use env var
config.ConfigDiscoveryAddress = cdsAddr
if podAddr, ok := os.LookupEnv(envVarAddress); ok {
Expand Down Expand Up @@ -176,10 +185,17 @@ func main() {
os.Exit(1)
}

setupLog.Info("setting up STUNner config renderer")
setupLog.Info("setting up license manager")
m := licensemgr.NewManager(licensemgr.Config{
CustomerKey: customerKey,
Logger: logger,
})

setupLog.Info("setting up config renderer")
r := renderer.NewRenderer(renderer.RendererConfig{
Scheme: scheme,
Logger: logger,
Scheme: scheme,
LicenseManager: m,
Logger: logger,
})

setupLog.Info("setting up updater client")
Expand All @@ -201,6 +217,7 @@ func main() {
Logger: logger,
})

m.SetOperatorChannel(op.GetOperatorChannel())
r.SetOperatorChannel(op.GetOperatorChannel())
u.SetAckChannel(op.GetOperatorChannel())
op.SetProgressReporters(r, u, c)
Expand All @@ -209,15 +226,27 @@ func main() {
mgrCtx, mgrCancel := context.WithCancel(context.Background())
defer mgrCancel()

setupLog.Info("starting renderer thread")
setupLog.Info("starting the license manager")
if err := m.Start(mgrCtx); err != nil {
setupLog.Error(err, "error running the license manager")
os.Exit(1)
}

setupLog.Info("starting the license manager")
if err := m.Start(mgrCtx); err != nil {
setupLog.Error(err, "error running the license manager")
os.Exit(1)
}

setupLog.Info("starting the renderer")
if err := r.Start(mgrCtx); err != nil {
setupLog.Error(err, "problem running renderer")
setupLog.Error(err, "error running the renderer")
os.Exit(1)
}

setupLog.Info("starting updater thread")
setupLog.Info("starting the updater")
if err := u.Start(mgrCtx); err != nil {
setupLog.Error(err, "could not run updater")
setupLog.Error(err, "error running the updater")
os.Exit(1)
}

Expand All @@ -228,13 +257,13 @@ func main() {
}

opCtx := ctrl.SetupSignalHandler()
setupLog.Info("starting operator thread")
setupLog.Info("starting the operator")
if err := op.Start(opCtx, mgrCancel); err != nil {
setupLog.Error(err, "problem running operator")
os.Exit(1)
}

setupLog.Info("starting Kubernetes controller manager")
setupLog.Info("starting the Kubernetes controller manager")
if err := mgr.Start(mgrCtx); err != nil {
setupLog.Error(err, "problem running manager")
// no way to gracefully terminate: give up and exit with an error
Expand Down
18 changes: 14 additions & 4 deletions test/integration_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
gwapiv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"

"github.com/l7mp/stunner-gateway-operator/internal/config"
licensemgr "github.com/l7mp/stunner-gateway-operator/internal/licensemanager"
"github.com/l7mp/stunner-gateway-operator/internal/operator"
"github.com/l7mp/stunner-gateway-operator/internal/renderer"
"github.com/l7mp/stunner-gateway-operator/internal/testutils"
Expand All @@ -54,7 +55,8 @@ import (
stnrgwv1 "github.com/l7mp/stunner-gateway-operator/api/v1"
)

var _ = fmt.Sprintf("%d", 1)
// Empty subscriber key for testing
var customerTestKey string

// Define utility constants for object names and testing timeouts/durations and intervals.
const (
Expand Down Expand Up @@ -173,10 +175,17 @@ func initOperator(mgrCtx, opCtx context.Context) {
})
Expect(err).NotTo(HaveOccurred())

setupLog.Info("setting up STUNner config renderer")
setupLog.Info("setting up license manager")
m := licensemgr.NewManager(licensemgr.Config{
CustomerKey: customerTestKey,
Logger: ctrl.Log,
})

setupLog.Info("setting up config renderer")
r := renderer.NewRenderer(renderer.RendererConfig{
Scheme: scheme,
Logger: ctrl.Log,
Scheme: scheme,
LicenseManager: m,
Logger: ctrl.Log,
})
Expect(r).NotTo(BeNil())

Expand All @@ -202,6 +211,7 @@ func initOperator(mgrCtx, opCtx context.Context) {
Logger: ctrl.Log,
})

m.SetOperatorChannel(op.GetOperatorChannel())
r.SetOperatorChannel(op.GetOperatorChannel())
u.SetAckChannel(op.GetOperatorChannel())
op.SetProgressReporters(r, u, c)
Expand Down

0 comments on commit b80d42e

Please sign in to comment.