From 6a8c9ce88d0fea0624d15d944af0c9efcad5ccbb Mon Sep 17 00:00:00 2001 From: ravineet <rajula.vineet.reddy@cern.ch> Date: Tue, 25 Oct 2022 12:06:36 +0200 Subject: [PATCH] Reset cloneFrom field post cloning & add labels for reference --- controllers/drupalsite_controller_test.go | 14 +++++ controllers/drupalsite_controller_utils.go | 61 ++++++++++++++-------- 2 files changed, 52 insertions(+), 23 deletions(-) diff --git a/controllers/drupalsite_controller_test.go b/controllers/drupalsite_controller_test.go index 21f1dc78..1844dee2 100644 --- a/controllers/drupalsite_controller_test.go +++ b/controllers/drupalsite_controller_test.go @@ -453,6 +453,20 @@ var _ = Describe("DrupalSite controller", func() { }, timeout, interval).Should(ContainElement(expectedOwnerReference)) } + // Check if the clone labels are set on the drupalSite + By("Expecting the clone labels to be set on the cloned site") + Eventually(func() bool { + k8sClient.Get(ctx, types.NamespacedName{Name: keyClone.Name, Namespace: keyClone.Namespace}, &cr) + return (cr.Labels["clonedFrom"] == key.Name && cr.Labels["clonedOn"] != "") + }, timeout, interval).Should(BeTrue()) + + // Check if the cloneFrom field is reset on the drupalSite + By("Expecting the cloneFrom field is reset on the cloned site") + Eventually(func() bool { + k8sClient.Get(ctx, types.NamespacedName{Name: keyClone.Name, Namespace: keyClone.Namespace}, &cr) + return (cr.Spec.Configuration.CloneFrom == "") + }, timeout, interval).Should(BeTrue()) + // Check if the diskSize is set correctly By("Expecting the clone site diskSize is set to the same as source site") Eventually(func() bool { diff --git a/controllers/drupalsite_controller_utils.go b/controllers/drupalsite_controller_utils.go index aae804da..4ad09501 100644 --- a/controllers/drupalsite_controller_utils.go +++ b/controllers/drupalsite_controller_utils.go @@ -20,6 +20,7 @@ import ( "crypto/md5" "encoding/hex" "fmt" + "time" "github.com/go-logr/logr" buildv1 "github.com/openshift/api/build/v1" @@ -170,30 +171,44 @@ func (r *DrupalSiteReconciler) ensureSpecFinalizer(ctx context.Context, drp *web // Validate that CloneFrom is an existing DrupalSite if drp.Spec.Configuration.CloneFrom != "" { - sourceSite := webservicesv1a1.DrupalSite{} - err := r.Get(ctx, types.NamespacedName{Name: string(drp.Spec.Configuration.CloneFrom), Namespace: drp.Namespace}, &sourceSite) - switch { - case k8sapierrors.IsNotFound(err): - return false, newApplicationError(fmt.Errorf("CloneFrom DrupalSite doesn't exist"), ErrInvalidSpec) - case err != nil: - return false, newApplicationError(err, ErrClientK8s) - } + if !drp.ConditionTrue("Initialized") { + sourceSite := webservicesv1a1.DrupalSite{} + err := r.Get(ctx, types.NamespacedName{Name: string(drp.Spec.Configuration.CloneFrom), Namespace: drp.Namespace}, &sourceSite) + switch { + case k8sapierrors.IsNotFound(err): + return false, newApplicationError(fmt.Errorf("CloneFrom DrupalSite doesn't exist"), ErrInvalidSpec) + case err != nil: + return false, newApplicationError(err, ErrClientK8s) + } - // The destination disk size must be at least as large as the source - if drp.Spec.Configuration.DiskSize != sourceSite.Spec.Configuration.DiskSize { - drp.Spec.Configuration.DiskSize = sourceSite.Spec.Configuration.DiskSize - update = true - } - // The extraConfigurationRepo should be set in the clone site if defined in the source - // TODO: Remove logic for ExtraConfigurationRepo once we deprecate the field - if sourceSite.Spec.Configuration.ExtraConfigurationRepo != "" && drp.Spec.Configuration.ExtraConfigurationRepo == "" { - drp.Spec.Configuration.ExtraConfigurationRepo = sourceSite.Spec.Configuration.ExtraConfigurationRepo - update = true - } - // The extraConfigurationRepository should be set in the clone site if defined in the source - if sourceSite.Spec.Configuration.ExtraConfigurationRepository.Branch != "" && sourceSite.Spec.Configuration.ExtraConfigurationRepository.RepositoryUrl != "" && drp.Spec.Configuration.ExtraConfigurationRepository.Branch == "" && drp.Spec.Configuration.ExtraConfigurationRepository.RepositoryUrl != "" { - drp.Spec.Configuration.ExtraConfigurationRepository.Branch = sourceSite.Spec.Configuration.ExtraConfigurationRepository.Branch - drp.Spec.Configuration.ExtraConfigurationRepository.RepositoryUrl = sourceSite.Spec.Configuration.ExtraConfigurationRepository.RepositoryUrl + // The destination disk size must be at least as large as the source + if drp.Spec.Configuration.DiskSize != sourceSite.Spec.Configuration.DiskSize { + drp.Spec.Configuration.DiskSize = sourceSite.Spec.Configuration.DiskSize + update = true + } + // The extraConfigurationRepo should be set in the clone site if defined in the source + // TODO: Remove logic for ExtraConfigurationRepo once we deprecate the field + if sourceSite.Spec.Configuration.ExtraConfigurationRepo != "" && drp.Spec.Configuration.ExtraConfigurationRepo == "" { + drp.Spec.Configuration.ExtraConfigurationRepo = sourceSite.Spec.Configuration.ExtraConfigurationRepo + update = true + } + // The extraConfigurationRepository should be set in the clone site if defined in the source + if sourceSite.Spec.Configuration.ExtraConfigurationRepository.Branch != "" && sourceSite.Spec.Configuration.ExtraConfigurationRepository.RepositoryUrl != "" && drp.Spec.Configuration.ExtraConfigurationRepository.Branch == "" && drp.Spec.Configuration.ExtraConfigurationRepository.RepositoryUrl != "" { + drp.Spec.Configuration.ExtraConfigurationRepository.Branch = sourceSite.Spec.Configuration.ExtraConfigurationRepository.Branch + drp.Spec.Configuration.ExtraConfigurationRepository.RepositoryUrl = sourceSite.Spec.Configuration.ExtraConfigurationRepository.RepositoryUrl + update = true + } + } else { + if drp.Labels == nil { + drp.Labels = map[string]string{} + } + // Source site name from which the clone was requested + drp.Labels["clonedFrom"] = string(drp.Spec.Configuration.CloneFrom) + // Set the time when the clone was requested + loc, _ := time.LoadLocation("") + drp.Labels["clonedOn"] = time.Now().In(loc).Format("Jan_2_2006_3-04pm_UTC") + // Reset the `cloneFrom` field + drp.Spec.Configuration.CloneFrom = "" update = true } } -- GitLab