Skip to content

Commit

Permalink
🌱 Create GitHub client only if it is necessary (#92)
Browse files Browse the repository at this point in the history
* Create GitHub client only if it is necessary

Fixes #80

Signed-off-by: Roman Hros <roman.hros@dnation.cloud>

* Defer unlocking of mutex

Based on SovereignCloudStack/cluster-stack-operator#70

Signed-off-by: Roman Hros <roman.hros@dnation.cloud>

* Create a GitHub client before mutex

Signed-off-by: Roman Hros <roman.hros@dnation.cloud>

---------

Signed-off-by: Roman Hros <roman.hros@dnation.cloud>
  • Loading branch information
chess-knight authored Feb 16, 2024
1 parent 171b131 commit 6aa18f2
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions internal/controller/openstackclusterstackrelease_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,6 @@ func (r *OpenStackClusterStackReleaseReconciler) Reconcile(ctx context.Context,
}
}()

gc, err := r.GitHubClientFactory.NewClient(ctx)
if err != nil {
conditions.MarkFalse(openstackclusterstackrelease,
apiv1alpha1.GitAPIAvailableCondition,
apiv1alpha1.GitTokenOrEnvVariableNotSetReason,
clusterv1beta1.ConditionSeverityError,
err.Error(),
)
record.Warnf(openstackclusterstackrelease, "GitTokenOrEnvVariableNotSet", err.Error())
logger.Error(err, "failed to create Github client")
return ctrl.Result{}, nil
}

conditions.MarkTrue(openstackclusterstackrelease, apiv1alpha1.GitAPIAvailableCondition)

// name of OpenStackClusterStackRelease object is same as the release tag
releaseTag := openstackclusterstackrelease.Name

Expand All @@ -138,17 +123,32 @@ func (r *OpenStackClusterStackReleaseReconciler) Reconcile(ctx context.Context,
if download {
conditions.MarkFalse(openstackclusterstackrelease, apiv1alpha1.ClusterStackReleaseAssetsReadyCondition, apiv1alpha1.ReleaseAssetsNotDownloadedYetReason, clusterv1beta1.ConditionSeverityInfo, "assets not downloaded yet")

gc, err := r.GitHubClientFactory.NewClient(ctx)
if err != nil {
conditions.MarkFalse(openstackclusterstackrelease,
apiv1alpha1.GitAPIAvailableCondition,
apiv1alpha1.GitTokenOrEnvVariableNotSetReason,
clusterv1beta1.ConditionSeverityError,
err.Error(),
)
record.Warnf(openstackclusterstackrelease, "GitTokenOrEnvVariableNotSet", err.Error())
logger.Error(err, "failed to create Github client")
return ctrl.Result{}, nil
}

conditions.MarkTrue(openstackclusterstackrelease, apiv1alpha1.GitAPIAvailableCondition)

// this is the point where we download the release from github
// acquire lock so that only one reconcile loop can download the release
r.openStackClusterStackRelDownloadDirectoryMutex.Lock()

defer r.openStackClusterStackRelDownloadDirectoryMutex.Unlock()

if err := downloadReleaseAssets(ctx, releaseTag, releaseAssets.LocalDownloadPath, gc); err != nil {
logger.Error(err, "failed to download release assets")
return ctrl.Result{RequeueAfter: 1 * time.Minute}, nil
}

r.openStackClusterStackRelDownloadDirectoryMutex.Unlock()

record.Eventf(openstackclusterstackrelease, "ClusterStackReleaseAssetsReady", "successfully downloaded ClusterStackReleaseAssets %q", releaseTag)
// requeue to make sure release assets can be accessed
return ctrl.Result{Requeue: true}, nil
Expand Down

0 comments on commit 6aa18f2

Please sign in to comment.