Skip to content

Commit

Permalink
fix: disable runner (#64)
Browse files Browse the repository at this point in the history
Co-authored-by: rthalho <rafael.thalhofer@mercedes-benz.com>
Co-authored-by: rafalgalaw <48678421+rafalgalaw@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 22, 2024
1 parent 085265a commit b36d49b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 24 deletions.
36 changes: 19 additions & 17 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,25 +187,27 @@ func run() error {
return fmt.Errorf("unable to create controller Repository: %w", err)
}

eventChan := make(chan event.GenericEvent)
runnerReconciler := &garmcontroller.RunnerReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}
if config.Config.Operator.RunnerReconciliation {
eventChan := make(chan event.GenericEvent)
runnerReconciler := &garmcontroller.RunnerReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}

// setup controller so it can reconcile if events from eventChan are queued
if err = runnerReconciler.SetupWithManager(mgr, eventChan,
controller.Options{
MaxConcurrentReconciles: config.Config.Operator.RunnerConcurrency,
},
); err != nil {
return fmt.Errorf("unable to create controller Runner: %w", err)
}
// setup controller so it can reconcile if events from eventChan are queued
if err = runnerReconciler.SetupWithManager(mgr, eventChan,
controller.Options{
MaxConcurrentReconciles: config.Config.Operator.RunnerConcurrency,
},
); err != nil {
return fmt.Errorf("unable to create controller Runner: %w", err)
}

// fetch runner instances periodically and enqueue reconcile events for runner ctrl if external system has changed
ctx, cancel := context.WithCancel(ctx)
go runnerReconciler.PollRunnerInstances(ctx, eventChan)
defer cancel()
// fetch runner instances periodically and enqueue reconcile events for runner ctrl if external system has changed
ctx, cancel := context.WithCancel(ctx)
go runnerReconciler.PollRunnerInstances(ctx, eventChan)
defer cancel()
}

//+kubebuilder:scaffold:builder

Expand Down
20 changes: 13 additions & 7 deletions internal/controller/runner_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ type RunnerReconciler struct {
//+kubebuilder:rbac:groups=garm-operator.mercedes-benz.com,namespace=xxxxx,resources=runners/finalizers,verbs=update

func (r *RunnerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := log.FromContext(ctx)
log.Info("Reconciling runners...", "Request", req)

instanceClient := garmClient.NewInstanceClient()

return r.reconcile(ctx, req, instanceClient)
}

Expand All @@ -76,9 +72,16 @@ func (r *RunnerReconciler) reconcile(ctx context.Context, req ctrl.Request, inst
}

func (r *RunnerReconciler) handleCreateRunnerCR(ctx context.Context, req ctrl.Request, fetchErr error, garmRunner *params.Instance) (ctrl.Result, error) {
log := log.FromContext(ctx)
if apierrors.IsNotFound(fetchErr) && garmRunner != nil {
return r.createRunnerCR(ctx, garmRunner, req.Namespace)
}

if apierrors.IsNotFound(fetchErr) {
log.Info("object was not found")
return ctrl.Result{}, nil
}

return ctrl.Result{}, fetchErr
}

Expand Down Expand Up @@ -213,9 +216,7 @@ func (r *RunnerReconciler) PollRunnerInstances(ctx context.Context, eventChan ch
close(eventChan)
return
case <-ticker.C:
log.Info("Polling Runners...")
instanceClient := garmClient.NewInstanceClient()

err := r.EnqueueRunnerInstances(ctx, instanceClient, eventChan)
if err != nil {
log.Error(err, "Failed polling runner instances")
Expand Down Expand Up @@ -341,10 +342,15 @@ func (r *RunnerReconciler) fetchRunnerInstancesByNamespacedPools(instanceClient
}

func getRunnerDiff(runnerCRs, garmRunners []string) []string {
cache := make(map[string]struct{})
var diff []string

for _, runner := range garmRunners {
cache[runner] = struct{}{}
}

for _, runnerCR := range runnerCRs {
if !slices.Contains(garmRunners, runnerCR) {
if _, found := cache[runnerCR]; !found {
diff = append(diff, runnerCR)
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type OperatorConfig struct {
EnterpriseConcurrency int `koanf:"enterpriseConcurrency" validate:"gte=1" yaml:"enterpriseConcurrency"`
OrganizationConcurrency int `koanf:"organizationConcurrency" validate:"gte=1" yaml:"organizationConcurrency"`
PoolConcurrency int `koanf:"poolConcurrency" validate:"gte=1" yaml:"poolConcurrency"`
RunnerReconciliation bool `koanf:"runnerReconciliation" yaml:"runnerReconciliation"`
}

type AppConfig struct {
Expand Down
3 changes: 3 additions & 0 deletions pkg/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ const (
DefaultEnterpriseConcurrency = 1
DefaultOrganizationConcurrency = 5
DefaultPoolConcurrency = 10

// default values for controller reconciliation configuration
DefaultRunnerReconciliation = false
)
2 changes: 2 additions & 0 deletions pkg/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func InitiateFlags() *pflag.FlagSet {
f.Int("operator-organization-concurrency", defaults.DefaultOrganizationConcurrency, "Specifies the maximum number of concurrent organizations that can be reconciled simultaneously")
f.Int("operator-pool-concurrency", defaults.DefaultPoolConcurrency, "Specifies the maximum number of concurrent pools that can be reconciled simultaneously")

f.Bool("operator-runner-reconciliation", defaults.DefaultRunnerReconciliation, "Specifies if runner reconciliation should be enabled")

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

0 comments on commit b36d49b

Please sign in to comment.