Skip to content

Instantly share code, notes, and snippets.

@inspirit941
Created October 25, 2024 03:01
Show Gist options
  • Select an option

  • Save inspirit941/7c9a1186cd15adb9ffe7986a7c64db18 to your computer and use it in GitHub Desktop.

Select an option

Save inspirit941/7c9a1186cd15adb9ffe7986a7c64db18 to your computer and use it in GitHub Desktop.
func (r *Reconciler) FinalizeKind(ctx context.Context, source *sourcesv1alpha1.GitHubSource) pkgreconciler.Event {
// If a webhook was created, try to delete it
if source.Status.WebhookIDKey != "" {
// Get access token
accessToken, err := r.secretFrom(ctx, source.Namespace, source.Spec.AccessToken.SecretKeyRef)
if apierrors.IsNotFound(err) {
source.Status.MarkNoSecrets("AccessTokenNotFound", "%s", err)
controller.GetEventRecorder(ctx).Eventf(source, corev1.EventTypeWarning,
"WebhookDeletionSkipped", "Could not delete webhook %q: %v", source.Status.WebhookIDKey, err)
// return EventTypeNormal to avoid finalize loop
return pkgreconciler.NewEvent(corev1.EventTypeNormal, "WebhookDeletionSkipped", "Could not delete webhook %q: %v", source.Status.WebhookIDKey, err)
} else if err != nil {
controller.GetEventRecorder(ctx).Eventf(source, corev1.EventTypeWarning,
"WebhookDeletionFailed", "Could not delete webhook %q: %v", source.Status.WebhookIDKey, err)
return fmt.Errorf("error getting secret: %v", err)
}
args := &webhookArgs{
source: source,
accessToken: accessToken,
alternateGitHubAPIURL: source.Spec.GitHubAPIURL,
hookID: source.Status.WebhookIDKey,
}
// Delete the webhook using the access token and stored webhook ID
err = r.deleteWebhook(ctx, args)
var gherr *ghclient.ErrorResponse
if errors.As(err, &gherr) {
if gherr.Response.StatusCode == http.StatusNotFound {
controller.GetEventRecorder(ctx).Eventf(source, corev1.EventTypeWarning, "WebhookDeletionSkipped", "Could not delete webhook %q: %v", source.Status.WebhookIDKey, err)
// return EventTypeNormal to avoid finalize loop
return pkgreconciler.NewEvent(corev1.EventTypeNormal, "WebhookDeletionSkipped", "Could not delete webhook %q: %v", source.Status.WebhookIDKey, err)
}
} else {
controller.GetEventRecorder(ctx).Eventf(source, corev1.EventTypeWarning,
"WebhookDeletionFailed", "Could not delete webhook %q: %v", source.Status.WebhookIDKey, err)
return fmt.Errorf("error deleting webhook: %v", err)
}
// Webhook deleted, clear ID
source.Status.WebhookIDKey = ""
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment