Skip to content

Commit

Permalink
Fix token registration broken since v0.11.0 (#167)
Browse files Browse the repository at this point in the history
Fixes #166
  • Loading branch information
Yusuke Kuoka authored Nov 11, 2020
1 parent bc35bdf commit e613219
Showing 1 changed file with 50 additions and 30 deletions.
80 changes: 50 additions & 30 deletions controllers/runner_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,13 @@ func (r *RunnerReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
return ctrl.Result{}, err
}

newPod, err := r.newPod(ctx, runner)
if updated, err := r.updateRegistrationToken(ctx, runner); err != nil {
return ctrl.Result{}, err
} else if updated {
return ctrl.Result{Requeue: true}, nil
}

newPod, err := r.newPod(runner)
if err != nil {
log.Error(err, "Could not create pod")
return ctrl.Result{}, err
Expand Down Expand Up @@ -174,7 +180,13 @@ func (r *RunnerReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
}
}

newPod, err := r.newPod(ctx, runner)
if updated, err := r.updateRegistrationToken(ctx, runner); err != nil {
return ctrl.Result{}, err
} else if updated {
return ctrl.Result{Requeue: true}, nil
}

newPod, err := r.newPod(runner)
if err != nil {
log.Error(err, "Could not create pod")
return ctrl.Result{}, err
Expand Down Expand Up @@ -249,21 +261,46 @@ func (r *RunnerReconciler) unregisterRunner(ctx context.Context, org, repo, name
return true, nil
}

func (r *RunnerReconciler) newPod(ctx context.Context, runner v1alpha1.Runner) (corev1.Pod, error) {
func (r *RunnerReconciler) updateRegistrationToken(ctx context.Context, runner v1alpha1.Runner) (bool, error) {
if runner.IsRegisterable() {
return false, nil
}

log := r.Log.WithValues("runner", runner.Name)

rt, err := r.GitHubClient.GetRegistrationToken(ctx, runner.Spec.Organization, runner.Spec.Repository, runner.Name)
if err != nil {
r.Recorder.Event(&runner, corev1.EventTypeWarning, "FailedUpdateRegistrationToken", "Updating registration token failed")
log.Error(err, "Failed to get new registration token")
return false, err
}

updated := runner.DeepCopy()
updated.Status.Registration = v1alpha1.RunnerStatusRegistration{
Organization: runner.Spec.Organization,
Repository: runner.Spec.Repository,
Labels: runner.Spec.Labels,
Token: rt.GetToken(),
ExpiresAt: metav1.NewTime(rt.GetExpiresAt().Time),
}

if err := r.Status().Update(ctx, updated); err != nil {
log.Error(err, "Failed to update runner status")
return false, err
}

r.Recorder.Event(&runner, corev1.EventTypeNormal, "RegistrationTokenUpdated", "Successfully update registration token")
log.Info("Updated registration token", "repository", runner.Spec.Repository)

return true, nil
}

func (r *RunnerReconciler) newPod(runner v1alpha1.Runner) (corev1.Pod, error) {
var (
privileged bool = true
dockerdInRunner bool = runner.Spec.DockerdWithinRunnerContainer != nil && *runner.Spec.DockerdWithinRunnerContainer
err error
)

token := runner.Status.Registration.Token
if !runner.IsRegisterable() {
token, err = r.getRegistrationToken(ctx, runner)
if err != nil {
return corev1.Pod{}, err
}
}

runnerImage := runner.Spec.Image
if runnerImage == "" {
runnerImage = r.RunnerImage
Expand Down Expand Up @@ -297,7 +334,7 @@ func (r *RunnerReconciler) newPod(ctx context.Context, runner v1alpha1.Runner) (
},
{
Name: "RUNNER_TOKEN",
Value: token,
Value: runner.Status.Registration.Token,
},
{
Name: "DOCKERD_IN_RUNNER",
Expand Down Expand Up @@ -484,20 +521,3 @@ func removeFinalizer(finalizers []string) ([]string, bool) {

return result, removed
}

func (r *RunnerReconciler) getRegistrationToken(ctx context.Context, runner v1alpha1.Runner) (string, error) {
log := r.Log.WithValues("runner", runner.Name)
if runner.IsRegisterable() {
return runner.Status.Registration.Token, nil
} else {
rt, err := r.GitHubClient.GetRegistrationToken(ctx, runner.Spec.Organization, runner.Spec.Repository, runner.Name)
if err != nil {
r.Recorder.Event(&runner, corev1.EventTypeWarning, "FailedUpdateRegistrationToken", "Updating registration token failed")
log.Error(err, "Failed to get new registration token")
return "", err
}

log.Info("Updated registration token", "repository", runner.Spec.Repository)
return rt.GetToken(), nil
}
}

0 comments on commit e613219

Please sign in to comment.