Skip to content

Commit

Permalink
When removing ImageRepository, check if there is another ImageRepository
Browse files Browse the repository at this point in the history
for the same repository, if there is, don't remove repository but only
robot accounts for ImageRepository which is to be removed

STONEBLD-2876

Signed-off-by: Robert Cerven <[email protected]>
  • Loading branch information
rcerven committed Nov 25, 2024
1 parent 28a83b3 commit d4f8369
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions controllers/imagerepository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,13 @@ func (r *ImageRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}

if controllerutil.ContainsFinalizer(imageRepository, ImageRepositoryFinalizer) {
// Check if there isn't other ImageRepository for the same repository from other component
imageRepositoryFound, err := r.ImageRepositoryForSameUrlExists(ctx, imageRepository)
if err != nil{
return ctrl.Result{}, err
}
// Do not block deletion on failures
r.CleanupImageRepository(ctx, imageRepository)
r.CleanupImageRepository(ctx, imageRepository, !imageRepositoryFound)

controllerutil.RemoveFinalizer(imageRepository, ImageRepositoryFinalizer)
if err := r.Client.Update(ctx, imageRepository); err != nil {
Expand Down Expand Up @@ -535,7 +540,7 @@ func (r *ImageRepositoryReconciler) RegenerateImageRepositoryAccessToken(ctx con
}

// CleanupImageRepository deletes image repository and corresponding robot account(s).
func (r *ImageRepositoryReconciler) CleanupImageRepository(ctx context.Context, imageRepository *imagerepositoryv1alpha1.ImageRepository) {
func (r *ImageRepositoryReconciler) CleanupImageRepository(ctx context.Context, imageRepository *imagerepositoryv1alpha1.ImageRepository, removeRepository bool) {
log := ctrllog.FromContext(ctx).WithName("RepositoryCleanup")

robotAccountName := imageRepository.Status.Credentials.PushRobotAccountName
Expand All @@ -558,6 +563,11 @@ func (r *ImageRepositoryReconciler) CleanupImageRepository(ctx context.Context,
}
}

if !removeRepository {
log.Info("Won't remove image repository, because other ImageRepository is using it", "RepoName", imageRepository.Status.Image.URL)
return
}

imageRepositoryName := imageRepository.Spec.Image.Name
isImageRepositoryDeleted, err := r.QuayClient.DeleteRepository(r.QuayOrganization, imageRepositoryName)
if err != nil {
Expand Down Expand Up @@ -918,3 +928,26 @@ func generateDockerconfigSecretData(quayImageURL string, robotAccount *quay.Robo
quayImageURL, base64.StdEncoding.EncodeToString([]byte(authString)))
return secretData
}

func (r *ImageRepositoryReconciler) ImageRepositoryForSameUrlExists(ctx context.Context, imageRepository *imagerepositoryv1alpha1.ImageRepository) (bool, error) {
log := ctrllog.FromContext(ctx)
imageRepositoriesList := &imagerepositoryv1alpha1.ImageRepositoryList{}
if err := r.Client.List(ctx, imageRepositoriesList, &client.ListOptions{Namespace: imageRepository.Namespace}); err != nil {
log.Error(err, "failed to list image repositories")
return false, err
}

imageRepositoryUrl := imageRepository.Status.Image.URL
imageRepositoryName := imageRepository.ObjectMeta.Name
imageRepositoryFound := false
for _, imageRepo := range imageRepositoriesList.Items {
if imageRepositoryUrl == imageRepo.Status.Image.URL {
if imageRepositoryName == imageRepo.ObjectMeta.Name {
continue
}
imageRepositoryFound = true
}
}

return imageRepositoryFound, nil
}

0 comments on commit d4f8369

Please sign in to comment.