Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alekc committed Jun 30, 2024
1 parent 858dd58 commit ceaf02e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 34 deletions.
56 changes: 31 additions & 25 deletions controllers/runner_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controllers

import (
"context"
"gitlab.k8s.alekc.dev/internal/types"
"reflect"
"time"

Expand Down Expand Up @@ -74,31 +75,7 @@ func (r *RunnerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr

logger.Info("reconciling")
if runnerObj.IsBeingDeleted() {
logger.Info("runner is being deleted")

if !runnerObj.IsAuthenticated() {
logger.Info("removing runner/s from gitlab")
for _, reg := range runnerObj.RegistrationConfig() {
cl := r.GitlabApiClient
if cl == nil {
if cl, err = api.NewGitlabClient(*reg.Token, reg.GitlabUrl); err != nil {
logger.Error(err, "cannot get gitlab api client for deletion of the runner")
continue
}
}
if _, err = cl.DeleteByToken(reg.AuthToken); err != nil {
// do not interrupt execution flow, just report it
logger.Error(err, "warning: cannot delete token from gitlab")
}
}
}

runnerObj.RemoveFinalizer()
if err = runnerObj.Update(ctx, r); err != nil {
logger.Error(err, "cannot remove finalizer")
return resultRequeueAfterDefaultTimeout, err
}
return resultRequeueNow, nil
return r.manageDeletion(ctx, runnerObj, logger)
}

// update the status when done processing in case there is anything pending
Expand Down Expand Up @@ -258,3 +235,32 @@ func (r *RunnerReconciler) SetupWithManager(mgr ctrl.Manager) error {
Owns(&appsv1.Deployment{}).
Complete(r)
}

func (r *RunnerReconciler) manageDeletion(ctx context.Context, runnerObj types.RunnerInfo, logger logr.Logger) (ctrl.Result, error) {
logger.Info("runner is being deleted")
var err error

if !runnerObj.IsAuthenticated() {
logger.Info("removing runner/s from gitlab")
for _, reg := range runnerObj.RegistrationConfig() {
cl := r.GitlabApiClient
if cl == nil {
if cl, err = api.NewGitlabClient(*reg.Token, reg.GitlabUrl); err != nil {
logger.Error(err, "cannot get gitlab api client for deletion of the runner")
continue
}
}
if _, err = cl.DeleteByToken(reg.AuthToken); err != nil {
// do not interrupt execution flow, just report it
logger.Error(err, "warning: cannot delete token from gitlab")
}
}
}

runnerObj.RemoveFinalizer()
if err = runnerObj.Update(ctx, r); err != nil {
logger.Error(err, "cannot remove finalizer")
return resultRequeueAfterDefaultTimeout, err
}
return resultRequeueNow, nil
}
2 changes: 1 addition & 1 deletion internal/api/gitlab.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package api

import (
"gitlab.k8s.alekc.dev/internal/data/pointer"
"io"

"github.com/xanzy/go-gitlab"
"gitlab.k8s.alekc.dev/api/v1beta1"
"gitlab.k8s.alekc.dev/internal/pointer"
)

type GitlabClient interface {
Expand Down
13 changes: 5 additions & 8 deletions internal/crud/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package crud

import (
"context"
"gitlab.k8s.alekc.dev/internal/data/maps"

"github.com/go-logr/logr"
gitlabv1beta1 "gitlab.k8s.alekc.dev/api/v1beta1"
Expand All @@ -14,7 +15,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

// SingleRunner fetches single runner from k8s
// SingleRunner init single runner from k8s
func SingleRunner(ctx context.Context, client client.Client, nsName types.NamespacedName) (internalTypes.RunnerInfo, error) {
runnerObj := &gitlabv1beta1.Runner{}
err := client.Get(ctx, nsName, runnerObj)
Expand All @@ -27,13 +28,9 @@ func MultiRunner(ctx context.Context, client client.Client, nsName types.Namespa
runnerObj := &gitlabv1beta1.MultiRunner{}
err := client.Get(ctx, nsName, runnerObj)

// explicit init of the status maps
if runnerObj.Status.AuthTokens == nil {
runnerObj.Status.AuthTokens = make(map[string]string, 0)
}
if runnerObj.Status.LastRegistrationTags == nil {
runnerObj.Status.LastRegistrationTags = make(map[string][]string, 0)
}
maps.InitIfNil(&runnerObj.Status.AuthTokens)
maps.InitSliceIfNil(&runnerObj.Status.LastRegistrationTags)

return runnerObj, err
}

Expand Down
13 changes: 13 additions & 0 deletions internal/data/maps/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package maps

func InitIfNil(mapVar *map[string]string) {
if *mapVar == nil {
*mapVar = make(map[string]string)
}
}

func InitSliceIfNil(mapVar *map[string][]string) {
if *mapVar == nil {
*mapVar = make(map[string][]string)
}
}
File renamed without changes.

0 comments on commit ceaf02e

Please sign in to comment.