This is an automated email from the ASF dual-hosted git repository.

houston pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr-operator.git


The following commit(s) were added to refs/heads/main by this push:
     new 579eec1  Ability to schedule automatic restarts for 
SolrPrometheusExporters (#313)
579eec1 is described below

commit 579eec13c8710bbb866c75f11afd5f9455f1702d
Author: Houston Putman <[email protected]>
AuthorDate: Thu Aug 26 15:36:15 2021 -0400

    Ability to schedule automatic restarts for SolrPrometheusExporters (#313)
---
 api/v1beta1/solrprometheusexporter_types.go        | 13 +++++++
 .../solr.apache.org_solrprometheusexporters.yaml   |  3 ++
 controllers/solrprometheusexporter_controller.go   | 45 +++++++++++++++++-----
 helm/solr-operator/Chart.yaml                      |  9 ++++-
 helm/solr-operator/crds/crds.yaml                  |  3 ++
 5 files changed, 63 insertions(+), 10 deletions(-)

diff --git a/api/v1beta1/solrprometheusexporter_types.go 
b/api/v1beta1/solrprometheusexporter_types.go
index 3339354..e344963 100644
--- a/api/v1beta1/solrprometheusexporter_types.go
+++ b/api/v1beta1/solrprometheusexporter_types.go
@@ -61,6 +61,19 @@ type SolrPrometheusExporterSpec struct {
        // with the `spec.solrReference.solrTLS.mountedServerTLSDir` option
        // +optional
        BusyBoxImage *ContainerImage `json:"busyBoxImage,omitempty"`
+
+       // Perform a scheduled restart on the given schedule, in CRON format.
+       //
+       // Multiple CRON syntaxes are supported
+       //   - Standard CRON (e.g. "CRON_TZ=Asia/Seoul 0 6 * * ?")
+       //   - Predefined Schedules (e.g. "@yearly", "@weekly", etc.)
+       //   - Intervals (e.g. "@every 10h30m")
+       //
+       // For more information please check this reference:
+       // 
https://pkg.go.dev/github.com/robfig/cron/v3?utm_source=godoc#hdr-CRON_Expression_Format
+       //
+       // +optional
+       RestartSchedule string `json:"restartSchedule,omitempty"`
 }
 
 func (ps *SolrPrometheusExporterSpec) withDefaults(namespace string) (changed 
bool) {
diff --git a/config/crd/bases/solr.apache.org_solrprometheusexporters.yaml 
b/config/crd/bases/solr.apache.org_solrprometheusexporters.yaml
index 5682b7f..751b864 100644
--- a/config/crd/bases/solr.apache.org_solrprometheusexporters.yaml
+++ b/config/crd/bases/solr.apache.org_solrprometheusexporters.yaml
@@ -3376,6 +3376,9 @@ spec:
                 description: Number of threads to use for the prometheus 
exporter Defaults to 1
                 format: int32
                 type: integer
+              restartSchedule:
+                description: "Perform a scheduled restart on the given 
schedule, in CRON format. \n Multiple CRON syntaxes are supported   - Standard 
CRON (e.g. \"CRON_TZ=Asia/Seoul 0 6 * * ?\")   - Predefined Schedules (e.g. 
\"@yearly\", \"@weekly\", etc.)   - Intervals (e.g. \"@every 10h30m\") \n For 
more information please check this reference: 
https://pkg.go.dev/github.com/robfig/cron/v3?utm_source=godoc#hdr-CRON_Expression_Format";
+                type: string
               scrapeInterval:
                 description: The interval to scrape Solr at (in seconds) 
Defaults to 60 seconds
                 format: int32
diff --git a/controllers/solrprometheusexporter_controller.go 
b/controllers/solrprometheusexporter_controller.go
index bf9cf07..431e102 100644
--- a/controllers/solrprometheusexporter_controller.go
+++ b/controllers/solrprometheusexporter_controller.go
@@ -85,13 +85,15 @@ func (r *SolrPrometheusExporterReconciler) Reconcile(req 
ctrl.Request) (ctrl.Res
                return ctrl.Result{Requeue: true}, nil
        }
 
+       requeueOrNot := ctrl.Result{}
+
        configMapKey := util.PrometheusExporterConfigMapKey
        configXmlMd5 := ""
        if prometheusExporter.Spec.Config == "" && 
prometheusExporter.Spec.CustomKubeOptions.ConfigMapOptions != nil && 
prometheusExporter.Spec.CustomKubeOptions.ConfigMapOptions.ProvidedConfigMap != 
"" {
                foundConfigMap := &corev1.ConfigMap{}
                err = r.Get(context.TODO(), types.NamespacedName{Name: 
prometheusExporter.Spec.CustomKubeOptions.ConfigMapOptions.ProvidedConfigMap, 
Namespace: prometheusExporter.Namespace}, foundConfigMap)
                if err != nil {
-                       return ctrl.Result{}, err
+                       return requeueOrNot, err
                }
 
                if foundConfigMap.Data != nil {
@@ -99,11 +101,11 @@ func (r *SolrPrometheusExporterReconciler) Reconcile(req 
ctrl.Request) (ctrl.Res
                        if ok {
                                configXmlMd5 = fmt.Sprintf("%x", 
md5.Sum([]byte(configXml)))
                        } else {
-                               return ctrl.Result{}, fmt.Errorf("required '%s' 
key not found in provided ConfigMap %s",
+                               return requeueOrNot, fmt.Errorf("required '%s' 
key not found in provided ConfigMap %s",
                                        configMapKey, 
prometheusExporter.Spec.CustomKubeOptions.ConfigMapOptions.ProvidedConfigMap)
                        }
                } else {
-                       return ctrl.Result{}, fmt.Errorf("provided ConfigMap %s 
has no data",
+                       return requeueOrNot, fmt.Errorf("provided ConfigMap %s 
has no data",
                                
prometheusExporter.Spec.CustomKubeOptions.ConfigMapOptions.ProvidedConfigMap)
                }
        }
@@ -138,7 +140,7 @@ func (r *SolrPrometheusExporterReconciler) Reconcile(req 
ctrl.Request) (ctrl.Res
                        }
                }
                if err != nil {
-                       return ctrl.Result{}, err
+                       return requeueOrNot, err
                }
        }
 
@@ -166,13 +168,13 @@ func (r *SolrPrometheusExporterReconciler) Reconcile(req 
ctrl.Request) (ctrl.Res
                }
        }
        if err != nil {
-               return ctrl.Result{}, err
+               return requeueOrNot, err
        }
 
        // Get the ZkConnectionString to connect to
        solrConnectionInfo := util.SolrConnectionInfo{}
        if solrConnectionInfo, err = getSolrConnectionInfo(r, 
prometheusExporter); err != nil {
-               return ctrl.Result{}, err
+               return requeueOrNot, err
        }
 
        // Make sure the TLS config is in order
@@ -180,7 +182,7 @@ func (r *SolrPrometheusExporterReconciler) Reconcile(req 
ctrl.Request) (ctrl.Res
        if prometheusExporter.Spec.SolrReference.SolrTLS != nil {
                tls, err = r.reconcileTLSConfig(prometheusExporter)
                if err != nil {
-                       return ctrl.Result{}, err
+                       return requeueOrNot, err
                }
        }
 
@@ -207,6 +209,31 @@ func (r *SolrPrometheusExporterReconciler) Reconcile(req 
ctrl.Request) (ctrl.Res
        deploymentLogger := logger.WithValues("deployment", deploy.Name)
        foundDeploy := &appsv1.Deployment{}
        err = r.Get(context.TODO(), types.NamespacedName{Name: deploy.Name, 
Namespace: deploy.Namespace}, foundDeploy)
+
+       // Set the annotation for a scheduled restart, if necessary.
+       if nextRestartAnnotation, reconcileWaitDuration, err := 
util.ScheduleNextRestart(prometheusExporter.Spec.RestartSchedule, 
foundDeploy.Spec.Template.Annotations); err != nil {
+               logger.Error(err, "Cannot parse restartSchedule cron: %s", 
prometheusExporter.Spec.RestartSchedule)
+       } else {
+               if nextRestartAnnotation != "" {
+                       if deploy.Spec.Template.Annotations == nil {
+                               deploy.Spec.Template.Annotations = 
make(map[string]string, 1)
+                       }
+                       // Set the new restart time annotation
+                       
deploy.Spec.Template.Annotations[util.SolrScheduledRestartAnnotation] = 
nextRestartAnnotation
+                       // TODO: Create event for the CRD.
+               } else if existingRestartAnnotation, exists := 
foundDeploy.Spec.Template.Annotations[util.SolrScheduledRestartAnnotation]; 
exists {
+                       if deploy.Spec.Template.Annotations == nil {
+                               deploy.Spec.Template.Annotations = 
make(map[string]string, 1)
+                       }
+                       // Keep the existing nextRestart annotation if it 
exists and we aren't setting a new one.
+                       
deploy.Spec.Template.Annotations[util.SolrScheduledRestartAnnotation] = 
existingRestartAnnotation
+               }
+               if reconcileWaitDuration != nil {
+                       // Set the requeueAfter if it has not been set, or is 
greater than the time we need to wait to restart again
+                       updateRequeueAfter(&requeueOrNot, 
*reconcileWaitDuration)
+               }
+       }
+
        if err != nil && errors.IsNotFound(err) {
                deploymentLogger.Info("Creating Deployment")
                if err = 
controllerutil.SetControllerReference(prometheusExporter, deploy, r.scheme); 
err == nil {
@@ -225,7 +252,7 @@ func (r *SolrPrometheusExporterReconciler) Reconcile(req 
ctrl.Request) (ctrl.Res
                ready = foundDeploy.Status.ReadyReplicas > 0
        }
        if err != nil {
-               return ctrl.Result{}, err
+               return requeueOrNot, err
        }
 
        if ready != prometheusExporter.Status.Ready {
@@ -234,7 +261,7 @@ func (r *SolrPrometheusExporterReconciler) Reconcile(req 
ctrl.Request) (ctrl.Res
                err = r.Status().Update(context.TODO(), prometheusExporter)
        }
 
-       return ctrl.Result{}, err
+       return requeueOrNot, err
 }
 
 func getSolrConnectionInfo(r *SolrPrometheusExporterReconciler, 
prometheusExporter *solrv1beta1.SolrPrometheusExporter) (solrConnectionInfo 
util.SolrConnectionInfo, err error) {
diff --git a/helm/solr-operator/Chart.yaml b/helm/solr-operator/Chart.yaml
index 1080e83..78525ff 100644
--- a/helm/solr-operator/Chart.yaml
+++ b/helm/solr-operator/Chart.yaml
@@ -64,7 +64,7 @@ annotations:
         - name: Zookeeper Operator Release Notes
           url: 
https://github.com/pravega/zookeeper-operator/releases/tag/v0.2.12
     - kind: added
-      description: Ability to schedule automatic restarts
+      description: Ability to schedule automatic restarts for SolrClouds
       links:
         - name: Github Issue
           url: https://github.com/apache/solr-operator/issues/281
@@ -142,6 +142,13 @@ annotations:
           url: https://github.com/apache/solr-operator/issues/268
         - name: Github PR
           url: https://github.com/apache/solr-operator/pull/293
+    - kind: added
+      description: Ability to schedule automatic restarts for 
SolrPrometheusExporters
+      links:
+        - name: Github Issue
+          url: https://github.com/apache/solr-operator/issues/310
+        - name: Github PR
+          url: https://github.com/apache/solr-operator/pull/313
   artifacthub.io/images: |
     - name: solr-operator
       image: apache/solr-operator:v0.4.0-prerelease
diff --git a/helm/solr-operator/crds/crds.yaml 
b/helm/solr-operator/crds/crds.yaml
index b42f33a..ae4cc91 100644
--- a/helm/solr-operator/crds/crds.yaml
+++ b/helm/solr-operator/crds/crds.yaml
@@ -10035,6 +10035,9 @@ spec:
                 description: Number of threads to use for the prometheus 
exporter Defaults to 1
                 format: int32
                 type: integer
+              restartSchedule:
+                description: "Perform a scheduled restart on the given 
schedule, in CRON format. \n Multiple CRON syntaxes are supported   - Standard 
CRON (e.g. \"CRON_TZ=Asia/Seoul 0 6 * * ?\")   - Predefined Schedules (e.g. 
\"@yearly\", \"@weekly\", etc.)   - Intervals (e.g. \"@every 10h30m\") \n For 
more information please check this reference: 
https://pkg.go.dev/github.com/robfig/cron/v3?utm_source=godoc#hdr-CRON_Expression_Format";
+                type: string
               scrapeInterval:
                 description: The interval to scrape Solr at (in seconds) 
Defaults to 60 seconds
                 format: int32

Reply via email to