Skip to content

Commit

Permalink
Delete HPA when it's disabled (#726)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangpengcheng authored Mar 12, 2024
1 parent d1dd41c commit 70b9691
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 6 deletions.
32 changes: 32 additions & 0 deletions controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
16 changes: 14 additions & 2 deletions controllers/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

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

Expand Down Expand Up @@ -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]
Expand Down
16 changes: 14 additions & 2 deletions controllers/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

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

Expand Down Expand Up @@ -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]
Expand Down
16 changes: 14 additions & 2 deletions controllers/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

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

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

0 comments on commit 70b9691

Please sign in to comment.