Skip to content

Commit

Permalink
feat: toggle runner sync feature and configurable polling intervall (#39
Browse files Browse the repository at this point in the history
)

fixes #38
  • Loading branch information
rafalgalaw committed Dec 18, 2023
1 parent 837e271 commit 3bb715e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 10 deletions.
2 changes: 1 addition & 1 deletion internal/controller/runner_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (r *RunnerReconciler) SetupWithManager(mgr ctrl.Manager, eventChan chan eve

func (r *RunnerReconciler) PollRunnerInstances(ctx context.Context, eventChan chan event.GenericEvent) {
log := log.FromContext(ctx)
ticker := time.NewTicker(5 * time.Second)
ticker := time.NewTicker(config.Config.Operator.SyncRunnersInterval)
for {
select {
case <-ctx.Done():
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type OperatorConfig struct {
LeaderElection bool `koanf:"leader_election"`
SyncPeriod time.Duration `koanf:"sync_period" validate:"required"`
WatchNamespace string `koanf:"watch_namespace"`
SyncRunnersInterval time.Duration `koanf:"sync_runners_interval" validate:"gte=5s,lte=5m"`
}

type AppConfig struct {
Expand Down
86 changes: 77 additions & 9 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ func TestGenerateConfig(t *testing.T) {
{
name: "ConfigFromDefaultAndEnvs",
envvars: map[string]string{
"GARM_SERVER": "http://localhost:9997",
"GARM_USERNAME": "admin",
"GARM_PASSWORD": "password",
"GARM_SERVER": "http://localhost:9997",
"GARM_USERNAME": "admin",
"GARM_PASSWORD": "password",
"OPERATOR_SYNC_RUNNERS_INTERVAL": "20s",
},
wantCfg: AppConfig{
Operator: OperatorConfig{
Expand All @@ -38,6 +39,7 @@ func TestGenerateConfig(t *testing.T) {
LeaderElection: false,
SyncPeriod: 5 * time.Minute,
WatchNamespace: "",
SyncRunnersInterval: 20 * time.Second,
},
Garm: GarmConfig{
Server: "http://localhost:9997",
Expand All @@ -62,6 +64,7 @@ func TestGenerateConfig(t *testing.T) {
LeaderElection: false,
SyncPeriod: 5 * time.Minute,
WatchNamespace: "",
SyncRunnersInterval: 5 * time.Second,
},
Garm: GarmConfig{
Server: "http://localhost:9997",
Expand All @@ -75,14 +78,16 @@ func TestGenerateConfig(t *testing.T) {
{
name: "ConfigFromDefaultEnvsAndFlags",
envvars: map[string]string{
"GARM_SERVER": "http://localhost:1234",
"GARM_USERNAME": "admin1234",
"GARM_PASSWORD": "password1234",
"GARM_SERVER": "http://localhost:1234",
"GARM_USERNAME": "admin1234",
"GARM_PASSWORD": "password1234",
"OPERATOR_SYNC_RUNNERS_INTERVAL": "20s",
},
flags: map[string]string{
"garm-server": "http://localhost:9997",
"garm-username": "admin",
"garm-password": "password",
"garm-server": "http://localhost:9997",
"garm-username": "admin",
"garm-password": "password",
"operator-sync-runners-interval": "10s",
},
wantCfg: AppConfig{
Operator: OperatorConfig{
Expand All @@ -91,6 +96,7 @@ func TestGenerateConfig(t *testing.T) {
LeaderElection: false,
SyncPeriod: 5 * time.Minute,
WatchNamespace: "",
SyncRunnersInterval: 10 * time.Second,
},
Garm: GarmConfig{
Server: "http://localhost:9997",
Expand Down Expand Up @@ -119,6 +125,67 @@ func TestGenerateConfig(t *testing.T) {
LeaderElection: true,
SyncPeriod: 10 * time.Minute,
WatchNamespace: "garm-operator-namespace",
SyncRunnersInterval: 15 * time.Second,
},
Garm: GarmConfig{
Server: "http://garm-server:9997",
Username: "garm-username",
Password: "garm-password",
Init: true,
Email: "garm-operator@localhost",
},
},
},
{
name: "Invalid Polling Interval Config, less than or equal to 5min",
wantErr: true,
envvars: map[string]string{
"GARM_SERVER": "http://localhost:9997",
"GARM_USERNAME": "admin",
"GARM_PASSWORD": "password",
},
flags: map[string]string{
"operator-metrics-bind-address": ":1234",
"operator-sync-runners-interval": "10m",
},
wantCfg: AppConfig{
Operator: OperatorConfig{
MetricsBindAddress: ":7000",
HealthProbeBindAddress: ":7001",
LeaderElection: true,
SyncPeriod: 10 * time.Minute,
WatchNamespace: "garm-operator-namespace",
SyncRunnersInterval: 5 * time.Second,
},
Garm: GarmConfig{
Server: "http://garm-server:9997",
Username: "garm-username",
Password: "garm-password",
Init: true,
Email: "garm-operator@localhost",
},
},
},
{
name: "Invalid Polling Interval Config, greater than or equal to 5s",
wantErr: true,
envvars: map[string]string{
"GARM_SERVER": "http://localhost:9997",
"GARM_USERNAME": "admin",
"GARM_PASSWORD": "password",
},
flags: map[string]string{
"operator-metrics-bind-address": ":1234",
"operator-sync-runners-interval": "1s",
},
wantCfg: AppConfig{
Operator: OperatorConfig{
MetricsBindAddress: ":7000",
HealthProbeBindAddress: ":7001",
LeaderElection: true,
SyncPeriod: 10 * time.Minute,
WatchNamespace: "garm-operator-namespace",
SyncRunnersInterval: 5 * time.Second,
},
Garm: GarmConfig{
Server: "http://garm-server:9997",
Expand Down Expand Up @@ -148,6 +215,7 @@ func TestGenerateConfig(t *testing.T) {

if err := GenerateConfig(f, configFile); err != nil {
if tt.wantErr {
t.Logf("want error: %s", err.Error())
return
}
t.Errorf("GenerateConfig() error = %v, wantErr = %v", err, tt.wantErr)
Expand Down
1 change: 1 addition & 0 deletions pkg/config/test_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ operator:
leader_election: true
sync_period: "10m"
watch_namespace: "garm-operator-namespace"
sync_runners_interval: 15s
1 change: 1 addition & 0 deletions pkg/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
DefaultLeaderElection = false
DefaultSyncPeriod = 5 * time.Minute
DefaultWatchNamespace = ""
DefaultSyncRunnersInterval = 5 * time.Second

// default values for garm configuration
DefaultGarmInit = true
Expand Down
1 change: 1 addition & 0 deletions pkg/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func InitiateFlags() *pflag.FlagSet {
f.Bool("operator-leader-election", defaults.DefaultLeaderElection, "Enable leader election for controller manager. "+"Enabling this will ensure there is only one active controller manager.")
f.Duration("operator-sync-period", defaults.DefaultSyncPeriod, "The minimum interval at which watched resources are reconciled (e.g. 5m)")
f.String("operator-watch-namespace", defaults.DefaultWatchNamespace, "Namespace that the controller watches to reconcile garm objects. "+"If unspecified, the controller watches for garm objects across all namespaces.")
f.Duration("operator-sync-runners-interval", defaults.DefaultSyncRunnersInterval, "Specifies interval in which runners from garm-api are polled and synced to Runner CustomResource")

f.String("garm-server", "", "The address of the GARM server")
f.String("garm-username", "", "The username for the GARM server")
Expand Down

0 comments on commit 3bb715e

Please sign in to comment.