From 4da82a73a72da73c8a103318307c4e0caf233c1f Mon Sep 17 00:00:00 2001 From: zyy17 Date: Wed, 16 Oct 2024 13:19:21 +0800 Subject: [PATCH] refactor(monitoring): remove monitoring resources when disabled (#193) --- controllers/greptimedbcluster/controller.go | 31 +++++++++++++++++++ .../greptimedbcluster/deployers/datanode.go | 2 +- .../greptimedbcluster/deployers/flownode.go | 2 +- .../greptimedbcluster/deployers/frontend.go | 2 +- .../greptimedbcluster/deployers/meta.go | 2 +- .../greptimedbcluster/deployers/monitoring.go | 8 ++--- .../test_cluster_enable_monitoring.go | 19 ++++++++++++ 7 files changed, 58 insertions(+), 8 deletions(-) diff --git a/controllers/greptimedbcluster/controller.go b/controllers/greptimedbcluster/controller.go index 36c0be31..9a3ddd62 100644 --- a/controllers/greptimedbcluster/controller.go +++ b/controllers/greptimedbcluster/controller.go @@ -33,6 +33,8 @@ import ( "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1" "github.com/GreptimeTeam/greptimedb-operator/cmd/operator/app/options" + "github.com/GreptimeTeam/greptimedb-operator/controllers/common" + "github.com/GreptimeTeam/greptimedb-operator/controllers/constant" "github.com/GreptimeTeam/greptimedb-operator/controllers/greptimedbcluster/deployers" "github.com/GreptimeTeam/greptimedb-operator/pkg/deployer" ) @@ -171,6 +173,13 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu return ctrl.Result{}, err } + // FIXME(zyy17): The following code should be elegant to move to the deployers. + if !cluster.Spec.Monitoring.IsEnabled() { + if err := r.removeMonitoringDB(ctx, cluster); err != nil { + return ctrl.Result{}, err + } + } + return r.sync(ctx, cluster) } @@ -298,3 +307,25 @@ func (r *Reconciler) setObservedGeneration(cluster *v1alpha1.GreptimeDBCluster) cluster.Status.ObservedGeneration = &generation } } + +func (r *Reconciler) removeMonitoringDB(ctx context.Context, cluster *v1alpha1.GreptimeDBCluster) error { + objects := map[string]client.Object{ + common.MonitoringServiceName(cluster.Name): new(v1alpha1.GreptimeDBStandalone), + constant.DefaultVectorConfigName: new(corev1.ConfigMap), + } + + for name, obj := range objects { + if err := r.Get(ctx, client.ObjectKey{Namespace: cluster.Namespace, Name: name}, obj); err != nil { + if k8serrors.IsNotFound(err) { + continue + } + return err + } + + if err := r.Delete(ctx, obj); err != nil { + return err + } + } + + return nil +} diff --git a/controllers/greptimedbcluster/deployers/datanode.go b/controllers/greptimedbcluster/deployers/datanode.go index 62f71b93..9684ad0f 100644 --- a/controllers/greptimedbcluster/deployers/datanode.go +++ b/controllers/greptimedbcluster/deployers/datanode.go @@ -459,7 +459,7 @@ func (b *datanodeBuilder) generatePodTemplateSpec() corev1.PodTemplateSpec { b.AddLogsVolume(podTemplateSpec, logging.GetLogsDir()) } - if b.Cluster.GetMonitoring() != nil && b.Cluster.GetMonitoring().GetVector() != nil { + if b.Cluster.GetMonitoring().IsEnabled() && b.Cluster.GetMonitoring().GetVector() != nil { b.AddVectorConfigVolume(podTemplateSpec) b.AddVectorSidecar(podTemplateSpec, v1alpha1.DatanodeComponentKind) } diff --git a/controllers/greptimedbcluster/deployers/flownode.go b/controllers/greptimedbcluster/deployers/flownode.go index e718097b..b192998b 100644 --- a/controllers/greptimedbcluster/deployers/flownode.go +++ b/controllers/greptimedbcluster/deployers/flownode.go @@ -260,7 +260,7 @@ func (b *flownodeBuilder) generatePodTemplateSpec() corev1.PodTemplateSpec { b.AddLogsVolume(podTemplateSpec, logging.GetLogsDir()) } - if b.Cluster.GetMonitoring() != nil && b.Cluster.GetMonitoring().GetVector() != nil { + if b.Cluster.GetMonitoring().IsEnabled() && b.Cluster.GetMonitoring().GetVector() != nil { b.AddVectorConfigVolume(podTemplateSpec) b.AddVectorSidecar(podTemplateSpec, v1alpha1.FlownodeComponentKind) } diff --git a/controllers/greptimedbcluster/deployers/frontend.go b/controllers/greptimedbcluster/deployers/frontend.go index 80451e44..f3a8d219 100644 --- a/controllers/greptimedbcluster/deployers/frontend.go +++ b/controllers/greptimedbcluster/deployers/frontend.go @@ -279,7 +279,7 @@ func (b *frontendBuilder) generatePodTemplateSpec() *corev1.PodTemplateSpec { b.AddLogsVolume(podTemplateSpec, logging.GetLogsDir()) } - if b.Cluster.GetMonitoring() != nil && b.Cluster.GetMonitoring().GetVector() != nil { + if b.Cluster.GetMonitoring().IsEnabled() && b.Cluster.GetMonitoring().GetVector() != nil { b.AddVectorConfigVolume(podTemplateSpec) b.AddVectorSidecar(podTemplateSpec, v1alpha1.FrontendComponentKind) } diff --git a/controllers/greptimedbcluster/deployers/meta.go b/controllers/greptimedbcluster/deployers/meta.go index 09f5f14c..c8f1eadc 100644 --- a/controllers/greptimedbcluster/deployers/meta.go +++ b/controllers/greptimedbcluster/deployers/meta.go @@ -329,7 +329,7 @@ func (b *metaBuilder) generatePodTemplateSpec() *corev1.PodTemplateSpec { b.AddLogsVolume(podTemplateSpec, logging.GetLogsDir()) } - if b.Cluster.GetMonitoring() != nil && b.Cluster.GetMonitoring().GetVector() != nil { + if b.Cluster.GetMonitoring().IsEnabled() && b.Cluster.GetMonitoring().GetVector() != nil { b.AddVectorConfigVolume(podTemplateSpec) b.AddVectorSidecar(podTemplateSpec, v1alpha1.MetaComponentKind) } diff --git a/controllers/greptimedbcluster/deployers/monitoring.go b/controllers/greptimedbcluster/deployers/monitoring.go index acb42059..11fa624f 100644 --- a/controllers/greptimedbcluster/deployers/monitoring.go +++ b/controllers/greptimedbcluster/deployers/monitoring.go @@ -74,7 +74,7 @@ func (d *MonitoringDeployer) CheckAndUpdateStatus(ctx context.Context, crdObject return false, err } - if cluster.GetMonitoring() == nil || cluster.GetMonitoring().GetStandalone() == nil { + if !cluster.GetMonitoring().IsEnabled() || cluster.GetMonitoring().GetStandalone() == nil { return true, nil } @@ -92,7 +92,7 @@ func (d *MonitoringDeployer) CheckAndUpdateStatus(ctx context.Context, crdObject return false, nil } - if standalone.Status.StandalonePhase == v1alpha1.PhaseRunning { + if cluster.GetMonitoring().IsEnabled() && standalone.Status.StandalonePhase == v1alpha1.PhaseRunning { if err := d.createPipeline(cluster); err != nil { klog.Errorf("failed to create pipeline for standalone, err: '%v'", err) return false, err @@ -196,7 +196,7 @@ type monitoringBuilder struct { } func (b *monitoringBuilder) BuildGreptimeDBStandalone() deployer.Builder { - if b.Cluster.GetMonitoring() == nil || b.Cluster.GetMonitoring().GetStandalone() == nil { + if !b.Cluster.GetMonitoring().IsEnabled() || b.Cluster.GetMonitoring().GetStandalone() == nil { return b } @@ -222,7 +222,7 @@ func (b *monitoringBuilder) BuildGreptimeDBStandalone() deployer.Builder { } func (b *monitoringBuilder) BuildConfigMap() deployer.Builder { - if b.Cluster.GetMonitoring() == nil || b.Cluster.GetMonitoring().GetVector() == nil { + if !b.Cluster.GetMonitoring().IsEnabled() || b.Cluster.GetMonitoring().GetVector() == nil { return b } diff --git a/tests/e2e/greptimedbcluster/test_cluster_enable_monitoring.go b/tests/e2e/greptimedbcluster/test_cluster_enable_monitoring.go index 9874cd08..9b60b930 100644 --- a/tests/e2e/greptimedbcluster/test_cluster_enable_monitoring.go +++ b/tests/e2e/greptimedbcluster/test_cluster_enable_monitoring.go @@ -94,6 +94,25 @@ func TestClusterEnableMonitoring(ctx context.Context, h *helper.Helper) { err = testMonitoringStandalone(ctx, monitoringAddr) Expect(err).NotTo(HaveOccurred(), "failed to test monitoring") + // Disable monitoring. + By("Disable monitoring") + testCluster.Spec.Monitoring.Enabled = false + err = h.Update(ctx, testCluster) + Expect(err).NotTo(HaveOccurred(), "failed to update cluster") + By("Check the status of testCluster") + Eventually(func() error { + clusterPhase, err := h.GetPhase(ctx, testCluster.Namespace, testCluster.Name, new(greptimev1alpha1.GreptimeDBCluster)) + if err != nil { + return err + } + + if clusterPhase != greptimev1alpha1.PhaseRunning { + return fmt.Errorf("cluster is not running") + } + + return nil + }, helper.DefaultTimeout, time.Second).ShouldNot(HaveOccurred()) + By("Kill the port forwarding process") h.KillPortForwardProcess()