Skip to content

Commit

Permalink
Add flags for sync interval and cache directory
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelattwood committed Jan 15, 2025
1 parent 8e896c5 commit 9993084
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
25 changes: 14 additions & 11 deletions cmd/jetstream-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ func run() error {
crdConnect := flag.Bool("crd-connect", false, "If true, then NATS connections will be made from CRD config, not global config. Ignored if running with control loop, CRD options will always override global config")
cleanupPeriod := flag.Duration("cleanup-period", 30*time.Second, "Period to run object cleanup")
readOnly := flag.Bool("read-only", false, "Starts the controller without causing changes to the NATS resources")
cacheDir := flag.String("cache-dir", "", "Directory to store cached credential and TLS files")
controlLoop := flag.Bool("control-loop", false, "Experimental: Run controller with a full reconciliation control loop.")
controlLoopSyncInterval := flag.Duration("sync-interval", 5*time.Minute, "Interval to perform scheduled reconcile")

flag.Parse()

Expand Down Expand Up @@ -109,8 +111,10 @@ func run() error {
}

controllerCfg := &controller.Config{
ReadOnly: *readOnly,
Namespace: *namespace,
ReadOnly: *readOnly,
Namespace: *namespace,
CacheDir: *cacheDir,
SyncInterval: *controlLoopSyncInterval,
}

return runControlLoop(config, natsCfg, controllerCfg)
Expand Down Expand Up @@ -164,26 +168,25 @@ func runControlLoop(config *rest.Config, natsCfg *controller.NatsConfig, control
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(v1beta2.AddToScheme(scheme))

// Reconcile periodically to detect configuration drift on the NATS Server
syncPeriod := time.Minute * 5

mgr, err := ctrl.NewManager(config, ctrl.Options{
Scheme: scheme,
Logger: klog.NewKlogr().WithName("controller-runtime"),
Cache: cache.Options{
SyncPeriod: &syncPeriod,
SyncPeriod: &controllerCfg.SyncInterval,
},
})
if err != nil {
return fmt.Errorf("unable to start manager: %w", err)
}

cacheDir, err := os.MkdirTemp(".", "nack")
if err != nil {
return fmt.Errorf("create cache dir: %w", err)
if controllerCfg.CacheDir == "" {
cacheDir, err := os.MkdirTemp(".", "nack")
if err != nil {
return fmt.Errorf("create cache dir: %w", err)
}
defer os.RemoveAll(cacheDir)
controllerCfg.CacheDir = cacheDir
}
defer os.RemoveAll(cacheDir)
controllerCfg.CacheDir = cacheDir

err = controller.RegisterAll(mgr, natsCfg, controllerCfg)
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions internal/controller/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"fmt"
"time"

ctrl "sigs.k8s.io/controller-runtime"
)
Expand All @@ -12,9 +13,10 @@ import (
//
// Namespace restricts the controller to resources of the given namespace.
type Config struct {
ReadOnly bool
Namespace string
CacheDir string
ReadOnly bool
Namespace string
SyncInterval time.Duration
CacheDir string
}

// RegisterAll registers all available jetStream controllers to the manager.
Expand Down

0 comments on commit 9993084

Please sign in to comment.