Skip to content

Commit

Permalink
refactor(monitoring): remove monitoring resources when disabled (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyy17 authored Oct 16, 2024
1 parent a236c37 commit 4da82a7
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 8 deletions.
31 changes: 31 additions & 0 deletions controllers/greptimedbcluster/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion controllers/greptimedbcluster/deployers/datanode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/greptimedbcluster/deployers/flownode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/greptimedbcluster/deployers/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/greptimedbcluster/deployers/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions controllers/greptimedbcluster/deployers/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
19 changes: 19 additions & 0 deletions tests/e2e/greptimedbcluster/test_cluster_enable_monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 4da82a7

Please sign in to comment.