Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 create github client only if required #67

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 27 additions & 29 deletions internal/controller/clusterstack_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,33 +109,36 @@ func (r *ClusterStackReconciler) Reconcile(ctx context.Context, req reconcile.Re
return reconcile.Result{}, fmt.Errorf("failed to get latest ready ClusterStackRelease: %w", err)
}

gc, err := r.GitHubClientFactory.NewClient(ctx)
if err != nil {
conditions.MarkFalse(clusterStack,
csov1alpha1.GitAPIAvailableCondition,
csov1alpha1.GitTokenOrEnvVariableNotSetReason,
clusterv1.ConditionSeverityError,
err.Error(),
)
record.Warnf(clusterStack, "GitTokenOrEnvVariableNotSet", err.Error())
return reconcile.Result{}, fmt.Errorf("failed to create Github client: %w", err)
}
var latest *string

conditions.MarkTrue(clusterStack, csov1alpha1.GitAPIAvailableCondition)
if clusterStack.Spec.AutoSubscribe {
gc, err := r.GitHubClientFactory.NewClient(ctx)
if err != nil {
conditions.MarkFalse(clusterStack,
csov1alpha1.GitAPIAvailableCondition,
csov1alpha1.GitTokenOrEnvVariableNotSetReason,
clusterv1.ConditionSeverityError,
err.Error(),
)
record.Warnf(clusterStack, "GitTokenOrEnvVariableNotSet", err.Error())
return reconcile.Result{}, fmt.Errorf("failed to create Github client: %w", err)
}

latest, err := getLatestReleaseFromRemoteRepository(ctx, clusterStack, gc)
if err != nil {
// only log error and mark condition as false, but continue
conditions.MarkFalse(clusterStack,
csov1alpha1.GitReleasesSyncedCondition,
csov1alpha1.FailedToSyncReason,
clusterv1.ConditionSeverityWarning,
err.Error(),
)
logger.Error(err, "failed to get latest release from remote repository")
}
conditions.MarkTrue(clusterStack, csov1alpha1.GitAPIAvailableCondition)
latest, err = getLatestReleaseFromRemoteRepository(ctx, clusterStack, gc)
if err != nil {
// only log error and mark condition as false, but continue
conditions.MarkFalse(clusterStack,
csov1alpha1.GitReleasesSyncedCondition,
csov1alpha1.FailedToSyncReason,
clusterv1.ConditionSeverityWarning,
err.Error(),
)
logger.Error(err, "failed to get latest release from remote repository")
}

conditions.MarkTrue(clusterStack, csov1alpha1.GitReleasesSyncedCondition)
conditions.MarkTrue(clusterStack, csov1alpha1.GitReleasesSyncedCondition)
}

inUse, err := r.getClusterStackReleasesInUse(ctx, req.Namespace)
if err != nil {
Expand Down Expand Up @@ -534,11 +537,6 @@ func getLatestReadyClusterStackRelease(clusterStackReleases []*csov1alpha1.Clust
}

func getLatestReleaseFromRemoteRepository(ctx context.Context, clusterStack *csov1alpha1.ClusterStack, gc githubclient.Client) (*string, error) {
// nothing to do if autoSubscribe is not activated
if !clusterStack.Spec.AutoSubscribe {
return nil, nil
}

ghReleases, resp, err := gc.ListRelease(ctx)
if err != nil {
return nil, fmt.Errorf("failed to list releases on remote Git repository: %w", err)
Expand Down
28 changes: 14 additions & 14 deletions internal/controller/clusterstackrelease_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,6 @@ func (r *ClusterStackReleaseReconciler) Reconcile(ctx context.Context, req recon

controllerutil.AddFinalizer(clusterStackRelease, csov1alpha1.ClusterStackReleaseFinalizer)

gc, err := r.GitHubClientFactory.NewClient(ctx)
if err != nil {
conditions.MarkFalse(clusterStackRelease,
csov1alpha1.GitAPIAvailableCondition,
csov1alpha1.GitTokenOrEnvVariableNotSetReason,
clusterv1.ConditionSeverityError,
err.Error(),
)
record.Warnf(clusterStackRelease, "GitTokenOrEnvVariableNotSet", err.Error())
return reconcile.Result{}, fmt.Errorf("failed to create Github client: %w", err)
}

conditions.MarkTrue(clusterStackRelease, csov1alpha1.GitAPIAvailableCondition)

// name of ClusterStackRelease object is same as the release tag
releaseTag := clusterStackRelease.Name

Expand All @@ -127,6 +113,20 @@ func (r *ClusterStackReleaseReconciler) Reconcile(ctx context.Context, req recon
// acquire lock so that only one reconcile loop can download the release
r.clusterStackRelDownloadDirectoryMutex.Lock()

gc, err := r.GitHubClientFactory.NewClient(ctx)
if err != nil {
conditions.MarkFalse(clusterStackRelease,
csov1alpha1.GitAPIAvailableCondition,
csov1alpha1.GitTokenOrEnvVariableNotSetReason,
clusterv1.ConditionSeverityError,
err.Error(),
)
record.Warnf(clusterStackRelease, "GitTokenOrEnvVariableNotSet", err.Error())
return reconcile.Result{}, fmt.Errorf("failed to create Github client: %w", err)
}

conditions.MarkTrue(clusterStackRelease, csov1alpha1.GitAPIAvailableCondition)

if err := downloadReleaseAssets(ctx, releaseTag, releaseAssets.LocalDownloadPath, gc); err != nil {
return reconcile.Result{}, fmt.Errorf("failed to download release assets: %w", err)
}
Expand Down
Loading