From 60431839e65683199795a55201c163ef9614d1af Mon Sep 17 00:00:00 2001 From: jiangpengcheng Date: Mon, 4 Mar 2024 10:06:51 +0800 Subject: [PATCH] Delete HPA when it's disabled --- controllers/common.go | 32 ++++++++++++++++++++++++++++++++ controllers/function.go | 16 ++++++++++++++-- controllers/sink.go | 16 ++++++++++++++-- controllers/source.go | 16 ++++++++++++++-- 4 files changed, 74 insertions(+), 6 deletions(-) diff --git a/controllers/common.go b/controllers/common.go index 33ab9f11..eaa41ab5 100644 --- a/controllers/common.go +++ b/controllers/common.go @@ -43,6 +43,38 @@ const ( CleanUpFinalizerName = "cleanup.subscription.finalizer" ) +func deleteHPAV2Beta2(ctx context.Context, r client.Client, name types.NamespacedName) error { + hpa := &autoscalingv2beta2.HorizontalPodAutoscaler{} + err := r.Get(ctx, name, hpa) + if err != nil { + if errors.IsNotFound(err) { + return nil + } + return err + } + err = r.Delete(ctx, hpa) + if err != nil { + return err + } + return nil +} + +func deleteHPA(ctx context.Context, r client.Client, name types.NamespacedName) error { + hpa := &autov2.HorizontalPodAutoscaler{} + err := r.Get(ctx, name, hpa) + if err != nil { + if errors.IsNotFound(err) { + return nil + } + return err + } + err = r.Delete(ctx, hpa) + if err != nil { + return err + } + return nil +} + func observeVPA(ctx context.Context, r client.Reader, name types.NamespacedName, vpaSpec *v1alpha1.VPASpec, conditions map[v1alpha1.Component]v1alpha1.ResourceCondition) error { _, ok := conditions[v1alpha1.VPA] diff --git a/controllers/function.go b/controllers/function.go index 0baeb716..67d5ad4b 100644 --- a/controllers/function.go +++ b/controllers/function.go @@ -167,6 +167,7 @@ func (r *FunctionReconciler) ApplyFunctionService(ctx context.Context, function func (r *FunctionReconciler) ObserveFunctionHPA(ctx context.Context, function *v1alpha1.Function) error { if function.Spec.MaxReplicas == nil { // HPA not enabled, skip further action + delete(function.Status.Conditions, v1alpha1.HPA) return nil } @@ -212,7 +213,12 @@ func (r *FunctionReconciler) ObserveFunctionHPA(ctx context.Context, function *v func (r *FunctionReconciler) ApplyFunctionHPA(ctx context.Context, function *v1alpha1.Function, newGeneration bool) error { if function.Spec.MaxReplicas == nil { - // HPA not enabled, skip further action + // HPA not enabled, delete HPA if it exists + err := deleteHPA(ctx, r.Client, types.NamespacedName{Namespace: function.Namespace, Name: function.Name}) + if err != nil { + r.Log.Error(err, "failed to delete HPA for function", "namespace", function.Namespace, "name", function.Name) + return err + } return nil } condition := function.Status.Conditions[v1alpha1.HPA] @@ -237,6 +243,7 @@ func (r *FunctionReconciler) ApplyFunctionHPA(ctx context.Context, function *v1a func (r *FunctionReconciler) ObserveFunctionHPAV2Beta2(ctx context.Context, function *v1alpha1.Function) error { if function.Spec.MaxReplicas == nil { // HPA not enabled, skip further action + delete(function.Status.Conditions, v1alpha1.HPA) return nil } @@ -282,7 +289,12 @@ func (r *FunctionReconciler) ObserveFunctionHPAV2Beta2(ctx context.Context, func func (r *FunctionReconciler) ApplyFunctionHPAV2Beta2(ctx context.Context, function *v1alpha1.Function, newGeneration bool) error { if function.Spec.MaxReplicas == nil { - // HPA not enabled, skip further action + // HPA not enabled, delete HPA if it exists + err := deleteHPAV2Beta2(ctx, r.Client, types.NamespacedName{Namespace: function.Namespace, Name: function.Name}) + if err != nil { + r.Log.Error(err, "failed to delete HPA for function", "namespace", function.Namespace, "name", function.Name) + return err + } return nil } condition := function.Status.Conditions[v1alpha1.HPA] diff --git a/controllers/sink.go b/controllers/sink.go index 874e9abd..a7ad1ab0 100644 --- a/controllers/sink.go +++ b/controllers/sink.go @@ -165,6 +165,7 @@ func (r *SinkReconciler) ApplySinkService(ctx context.Context, sink *v1alpha1.Si func (r *SinkReconciler) ObserveSinkHPA(ctx context.Context, sink *v1alpha1.Sink) error { if sink.Spec.MaxReplicas == nil { // HPA not enabled, skip further action + delete(sink.Status.Conditions, v1alpha1.HPA) return nil } @@ -209,7 +210,12 @@ func (r *SinkReconciler) ObserveSinkHPA(ctx context.Context, sink *v1alpha1.Sink func (r *SinkReconciler) ApplySinkHPA(ctx context.Context, sink *v1alpha1.Sink, newGeneration bool) error { if sink.Spec.MaxReplicas == nil { - // HPA not enabled, skip further action + // HPA not enabled, delete HPA if it exists + err := deleteHPA(ctx, r.Client, types.NamespacedName{Namespace: sink.Namespace, Name: sink.Name}) + if err != nil { + r.Log.Error(err, "failed to delete HPA for sink", "namespace", sink.Namespace, "name", sink.Name) + return err + } return nil } condition := sink.Status.Conditions[v1alpha1.HPA] @@ -234,6 +240,7 @@ func (r *SinkReconciler) ApplySinkHPA(ctx context.Context, sink *v1alpha1.Sink, func (r *SinkReconciler) ObserveSinkHPAV2Beta2(ctx context.Context, sink *v1alpha1.Sink) error { if sink.Spec.MaxReplicas == nil { // HPA not enabled, skip further action + delete(sink.Status.Conditions, v1alpha1.HPA) return nil } @@ -278,7 +285,12 @@ func (r *SinkReconciler) ObserveSinkHPAV2Beta2(ctx context.Context, sink *v1alph func (r *SinkReconciler) ApplySinkHPAV2Beta2(ctx context.Context, sink *v1alpha1.Sink, newGeneration bool) error { if sink.Spec.MaxReplicas == nil { - // HPA not enabled, skip further action + // HPA not enabled, delete HPA if it exists + err := deleteHPAV2Beta2(ctx, r.Client, types.NamespacedName{Namespace: sink.Namespace, Name: sink.Name}) + if err != nil { + r.Log.Error(err, "failed to delete HPA for sink", "namespace", sink.Namespace, "name", sink.Name) + return err + } return nil } condition := sink.Status.Conditions[v1alpha1.HPA] diff --git a/controllers/source.go b/controllers/source.go index eddcba3d..bbd14558 100644 --- a/controllers/source.go +++ b/controllers/source.go @@ -166,6 +166,7 @@ func (r *SourceReconciler) ApplySourceService(ctx context.Context, source *v1alp func (r *SourceReconciler) ObserveSourceHPA(ctx context.Context, source *v1alpha1.Source) error { if source.Spec.MaxReplicas == nil { // HPA not enabled, skip further action + delete(source.Status.Conditions, v1alpha1.HPA) return nil } @@ -210,7 +211,12 @@ func (r *SourceReconciler) ObserveSourceHPA(ctx context.Context, source *v1alpha func (r *SourceReconciler) ApplySourceHPA(ctx context.Context, source *v1alpha1.Source, newGeneration bool) error { if source.Spec.MaxReplicas == nil { - // HPA not enabled, skip further action + // HPA not enabled, delete HPA if it exists + err := deleteHPA(ctx, r.Client, types.NamespacedName{Namespace: source.Namespace, Name: source.Name}) + if err != nil { + r.Log.Error(err, "failed to delete HPA for source", "namespace", source.Namespace, "name", source.Name) + return err + } return nil } condition := source.Status.Conditions[v1alpha1.HPA] @@ -235,6 +241,7 @@ func (r *SourceReconciler) ApplySourceHPA(ctx context.Context, source *v1alpha1. func (r *SourceReconciler) ObserveSourceHPAV2Beta2(ctx context.Context, source *v1alpha1.Source) error { if source.Spec.MaxReplicas == nil { // HPA not enabled, skip further action + delete(source.Status.Conditions, v1alpha1.HPA) return nil } @@ -280,7 +287,12 @@ func (r *SourceReconciler) ObserveSourceHPAV2Beta2(ctx context.Context, source * func (r *SourceReconciler) ApplySourceHPAV2Beta2(ctx context.Context, source *v1alpha1.Source, newGeneration bool) error { if source.Spec.MaxReplicas == nil { - // HPA not enabled, skip further action + // HPA not enabled, delete HPA if it exists + err := deleteHPAV2Beta2(ctx, r.Client, types.NamespacedName{Namespace: source.Namespace, Name: source.Name}) + if err != nil { + r.Log.Error(err, "failed to delete HPA for source", "namespace", source.Namespace, "name", source.Name) + return err + } return nil } condition := source.Status.Conditions[v1alpha1.HPA]