Skip to content

Commit a78e1e7

Browse files
author
Francisco Barros
committed
First part....
1 parent 38a4cd7 commit a78e1e7

5 files changed

+56
-24
lines changed

api/v1alpha1/drupalsite_types.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,12 @@ type Configuration struct {
106106
// +optional
107107
WebDAVPassword string `json:"webDAVPassword,omitempty"`
108108

109-
// ScheduledBackups when "true" will enable Scheduled Velero backups for the site and when "false" will disable scheduled backups
110-
// +kubebuilder:validation:Enum:=enabled;disabled
111-
// +kubebuilder:default=enabled
109+
// isPrimary defines if the DrupalSite instance is the "main" one of the project.
110+
// Currently, it defines Scheduled backups
111+
// When "true" will enable Scheduled Velero backups for the site and when "false" will disable scheduled backups
112+
// +kubebuilder:default=true
112113
// +optional
113-
ScheduledBackups string `json:"scheduledBackups,omitempty"`
114+
IsPrimary bool `json:"isPrimary,omitempty"`
114115
}
115116

116117
// QoSClass specifies the website's performance and availability requirements
@@ -153,6 +154,9 @@ type DrupalSiteStatus struct {
153154
// It should be copied to Gitlab.
154155
// +optional
155156
GitlabWebhookURL string `json:"gitlabWebhookURL,omitempty"`
157+
158+
// IsPrimary states if the Drupalsite is the main instance of the project
159+
IsPrimary bool `json:"isPrimary,omitempty"`
156160
}
157161

158162
// ReleaseID reports the actual release of CERN Drupal Distribution that is being used in the deployment.

config/crd/bases/drupal.webservices.cern.ch_drupalsites.yaml

+11-9
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ spec:
7676
through a Git repo, following these docs
7777
pattern: '[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)'
7878
type: string
79+
isPrimary:
80+
default: true
81+
description: isPrimary defines if the DrupalSite instance is the
82+
"main" one of the project. Currently, it defines Scheduled backups
83+
When "true" will enable Scheduled Velero backups for the site
84+
and when "false" will disable scheduled backups
85+
type: boolean
7986
qosClass:
8087
default: standard
8188
description: QoSClass specifies the website's performance and
@@ -85,15 +92,6 @@ spec:
8592
- test
8693
- standard
8794
type: string
88-
scheduledBackups:
89-
default: enabled
90-
description: ScheduledBackups when "true" will enable Scheduled
91-
Velero backups for the site and when "false" will disable scheduled
92-
backups
93-
enum:
94-
- enabled
95-
- disabled
96-
type: string
9795
webDAVPassword:
9896
description: WebDAVPassword sets the HTTP basic auth password
9997
for WebDAV file access. A default is auto-generated if a value
@@ -213,6 +211,10 @@ spec:
213211
of the site's image after changes on its source Gitlab "extraConfigurationRepo".
214212
It should be copied to Gitlab.
215213
type: string
214+
isPrimary:
215+
description: IsPrimary states if the Drupalsite is the main instance
216+
of the project
217+
type: boolean
216218
releaseID:
217219
description: ReleaseID reports the actual release of CERN Drupal Distribution
218220
that is being used in the deployment.

controllers/drupalsite_controller.go

+21-10
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,14 @@ func (r *DrupalSiteReconciler) Reconcile(ctx context.Context, req ctrl.Request)
235235

236236
// 1. Init: Check if finalizer is set. If not, set it, validate and update CR status
237237

238-
if update, err := r.ensureSpecFinalizer(ctx, drupalSite, log); err != nil {
238+
if update, err, site := r.ensureSpecFinalizer(ctx, drupalSite, log); err != nil {
239239
log.Error(err, fmt.Sprintf("%v failed to ensure DrupalSite spec defaults", err.Unwrap()))
240240
setErrorCondition(drupalSite, err)
241-
return r.updateCRStatusOrFailReconcile(ctx, log, drupalSite)
241+
return r.updateCRSpecAndStatusOrFailReconcile(ctx, log, site)
242242
} else if update {
243243
log.V(3).Info("Initializing DrupalSite Spec")
244-
return r.updateCRorFailReconcile(ctx, log, drupalSite)
244+
return r.updateCRSpecAndStatusOrFailReconcile(ctx, log, site)
245+
//return r.updateCRorFailReconcile(ctx, log, drupalSite)
245246
}
246247
if err := validateSpec(drupalSite.Spec); err != nil {
247248
log.Error(err, fmt.Sprintf("%v failed to validate DrupalSite spec", err.Unwrap()))
@@ -512,7 +513,7 @@ func validateSpec(drpSpec webservicesv1a1.DrupalSiteSpec) reconcileError {
512513

513514
// ensureSpecFinalizer ensures that the spec is valid, adding extra info if necessary, and that the finalizer is there,
514515
// then returns if it needs to be updated.
515-
func (r *DrupalSiteReconciler) ensureSpecFinalizer(ctx context.Context, drp *webservicesv1a1.DrupalSite, log logr.Logger) (update bool, err reconcileError) {
516+
func (r *DrupalSiteReconciler) ensureSpecFinalizer(ctx context.Context, drp *webservicesv1a1.DrupalSite, log logr.Logger) (update bool, err reconcileError, site *webservicesv1a1.DrupalSite) {
516517
if !controllerutil.ContainsFinalizer(drp, finalizerStr) {
517518
log.V(3).Info("Adding finalizer")
518519
controllerutil.AddFinalizer(drp, finalizerStr)
@@ -528,9 +529,9 @@ func (r *DrupalSiteReconciler) ensureSpecFinalizer(ctx context.Context, drp *web
528529
err := r.Get(ctx, types.NamespacedName{Name: string(drp.Spec.Configuration.CloneFrom), Namespace: drp.Namespace}, &sourceSite)
529530
switch {
530531
case k8sapierrors.IsNotFound(err):
531-
return false, newApplicationError(fmt.Errorf("CloneFrom DrupalSite doesn't exist"), ErrInvalidSpec)
532+
return false, newApplicationError(fmt.Errorf("CloneFrom DrupalSite doesn't exist"), ErrInvalidSpec), drp
532533
case err != nil:
533-
return false, newApplicationError(err, ErrClientK8s)
534+
return false, newApplicationError(err, ErrClientK8s), drp
534535
}
535536
// The destination disk size must be at least as large as the source
536537
if drp.Spec.Configuration.DiskSize < sourceSite.Spec.Configuration.DiskSize {
@@ -546,13 +547,23 @@ func (r *DrupalSiteReconciler) ensureSpecFinalizer(ctx context.Context, drp *web
546547
}
547548
update = true
548549
}
549-
550550
// Initialize 'Spec.Configuration.ScheduledBackups' if empty
551-
if len(drp.Spec.Configuration.ScheduledBackups) == 0 {
552-
drp.Spec.Configuration.ScheduledBackups = "enabled"
551+
if drp.Spec.Configuration.IsPrimary {
552+
drupalsiteList := &webservicesv1a1.DrupalSiteList{}
553+
r.Client.List(ctx, drupalsiteList, &client.ListOptions{Namespace: drp.Namespace})
554+
drp.Status.IsPrimary = drp.Spec.IsPrimary
555+
for _, site := range drupalsiteList.Items {
556+
//if site.Spec.IsPrimary && site.Name != drp.Name {
557+
if site.Spec.IsPrimary {
558+
update = true
559+
site.Spec.IsPrimary = false
560+
return update, nil, &site
561+
}
562+
}
563+
//drp.Status.IsPrimary = drp.Spec.IsPrimary
553564
update = true
554565
}
555-
return update, nil
566+
return update, nil, drp
556567
}
557568

558569
// getRunningdeployment fetches the running drupal deployment

controllers/drupalsite_resources.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ func (r *DrupalSiteReconciler) ensureResources(drp *webservicesv1a1.DrupalSite,
287287
}
288288

289289
// 5. Cluster-scoped: Backup schedule, Tekton RBAC
290-
if drp.Spec.Configuration.ScheduledBackups == "enabled" {
290+
if drp.Spec.Configuration.IsPrimary {
291291
if transientErr := r.ensureResourceX(ctx, drp, "backup_schedule", log); transientErr != nil {
292292
transientErrs = append(transientErrs, transientErr.Wrap("%v: for Velero Schedule"))
293293
}

controllers/reconciler_utils.go

+15
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,21 @@ func (r *DrupalSiteReconciler) updateCRStatusOrFailReconcile(ctx context.Context
148148
return reconcile.Result{}, nil
149149
}
150150

151+
// updateCRStatusOrFailReconcile tries to update the Custom Resource Status and logs any error
152+
func (r *DrupalSiteReconciler) updateCRSpecAndStatusOrFailReconcile(ctx context.Context, log logr.Logger, drp *webservicesv1a1.DrupalSite) (
153+
reconcile.Result, error) {
154+
if err := r.Client.Patch(ctx, drp, nil); err != nil {
155+
//if err := r.Status().Update(ctx, drp); err != nil {
156+
if k8sapierrors.IsConflict(err) {
157+
log.V(4).Info("Object changed while reconciling. Requeuing.")
158+
return reconcile.Result{Requeue: true}, nil
159+
}
160+
log.Error(err, fmt.Sprintf("%v failed to update the application status", ErrClientK8s))
161+
return reconcile.Result{}, err
162+
}
163+
return reconcile.Result{}, nil
164+
}
165+
151166
// getBuildStatus gets the build status from one of the builds for a given resources
152167
func (r *DrupalSiteReconciler) getBuildStatus(ctx context.Context, resource string, drp *webservicesv1a1.DrupalSite) (buildv1.BuildPhase, error) {
153168
buildList := &buildv1.BuildList{}

0 commit comments

Comments
 (0)