Skip to content

Commit 70b9691

Browse files
Delete HPA when it's disabled (#726)
1 parent d1dd41c commit 70b9691

File tree

4 files changed

+74
-6
lines changed

4 files changed

+74
-6
lines changed

controllers/common.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,38 @@ const (
4343
CleanUpFinalizerName = "cleanup.subscription.finalizer"
4444
)
4545

46+
func deleteHPAV2Beta2(ctx context.Context, r client.Client, name types.NamespacedName) error {
47+
hpa := &autoscalingv2beta2.HorizontalPodAutoscaler{}
48+
err := r.Get(ctx, name, hpa)
49+
if err != nil {
50+
if errors.IsNotFound(err) {
51+
return nil
52+
}
53+
return err
54+
}
55+
err = r.Delete(ctx, hpa)
56+
if err != nil {
57+
return err
58+
}
59+
return nil
60+
}
61+
62+
func deleteHPA(ctx context.Context, r client.Client, name types.NamespacedName) error {
63+
hpa := &autov2.HorizontalPodAutoscaler{}
64+
err := r.Get(ctx, name, hpa)
65+
if err != nil {
66+
if errors.IsNotFound(err) {
67+
return nil
68+
}
69+
return err
70+
}
71+
err = r.Delete(ctx, hpa)
72+
if err != nil {
73+
return err
74+
}
75+
return nil
76+
}
77+
4678
func observeVPA(ctx context.Context, r client.Reader, name types.NamespacedName, vpaSpec *v1alpha1.VPASpec,
4779
conditions map[v1alpha1.Component]v1alpha1.ResourceCondition) error {
4880
_, ok := conditions[v1alpha1.VPA]

controllers/function.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ func (r *FunctionReconciler) ApplyFunctionService(ctx context.Context, function
167167
func (r *FunctionReconciler) ObserveFunctionHPA(ctx context.Context, function *v1alpha1.Function) error {
168168
if function.Spec.MaxReplicas == nil {
169169
// HPA not enabled, skip further action
170+
delete(function.Status.Conditions, v1alpha1.HPA)
170171
return nil
171172
}
172173

@@ -212,7 +213,12 @@ func (r *FunctionReconciler) ObserveFunctionHPA(ctx context.Context, function *v
212213
func (r *FunctionReconciler) ApplyFunctionHPA(ctx context.Context, function *v1alpha1.Function,
213214
newGeneration bool) error {
214215
if function.Spec.MaxReplicas == nil {
215-
// HPA not enabled, skip further action
216+
// HPA not enabled, delete HPA if it exists
217+
err := deleteHPA(ctx, r.Client, types.NamespacedName{Namespace: function.Namespace, Name: function.Name})
218+
if err != nil {
219+
r.Log.Error(err, "failed to delete HPA for function", "namespace", function.Namespace, "name", function.Name)
220+
return err
221+
}
216222
return nil
217223
}
218224
condition := function.Status.Conditions[v1alpha1.HPA]
@@ -237,6 +243,7 @@ func (r *FunctionReconciler) ApplyFunctionHPA(ctx context.Context, function *v1a
237243
func (r *FunctionReconciler) ObserveFunctionHPAV2Beta2(ctx context.Context, function *v1alpha1.Function) error {
238244
if function.Spec.MaxReplicas == nil {
239245
// HPA not enabled, skip further action
246+
delete(function.Status.Conditions, v1alpha1.HPA)
240247
return nil
241248
}
242249

@@ -282,7 +289,12 @@ func (r *FunctionReconciler) ObserveFunctionHPAV2Beta2(ctx context.Context, func
282289
func (r *FunctionReconciler) ApplyFunctionHPAV2Beta2(ctx context.Context, function *v1alpha1.Function,
283290
newGeneration bool) error {
284291
if function.Spec.MaxReplicas == nil {
285-
// HPA not enabled, skip further action
292+
// HPA not enabled, delete HPA if it exists
293+
err := deleteHPAV2Beta2(ctx, r.Client, types.NamespacedName{Namespace: function.Namespace, Name: function.Name})
294+
if err != nil {
295+
r.Log.Error(err, "failed to delete HPA for function", "namespace", function.Namespace, "name", function.Name)
296+
return err
297+
}
286298
return nil
287299
}
288300
condition := function.Status.Conditions[v1alpha1.HPA]

controllers/sink.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ func (r *SinkReconciler) ApplySinkService(ctx context.Context, sink *v1alpha1.Si
165165
func (r *SinkReconciler) ObserveSinkHPA(ctx context.Context, sink *v1alpha1.Sink) error {
166166
if sink.Spec.MaxReplicas == nil {
167167
// HPA not enabled, skip further action
168+
delete(sink.Status.Conditions, v1alpha1.HPA)
168169
return nil
169170
}
170171

@@ -209,7 +210,12 @@ func (r *SinkReconciler) ObserveSinkHPA(ctx context.Context, sink *v1alpha1.Sink
209210

210211
func (r *SinkReconciler) ApplySinkHPA(ctx context.Context, sink *v1alpha1.Sink, newGeneration bool) error {
211212
if sink.Spec.MaxReplicas == nil {
212-
// HPA not enabled, skip further action
213+
// HPA not enabled, delete HPA if it exists
214+
err := deleteHPA(ctx, r.Client, types.NamespacedName{Namespace: sink.Namespace, Name: sink.Name})
215+
if err != nil {
216+
r.Log.Error(err, "failed to delete HPA for sink", "namespace", sink.Namespace, "name", sink.Name)
217+
return err
218+
}
213219
return nil
214220
}
215221
condition := sink.Status.Conditions[v1alpha1.HPA]
@@ -234,6 +240,7 @@ func (r *SinkReconciler) ApplySinkHPA(ctx context.Context, sink *v1alpha1.Sink,
234240
func (r *SinkReconciler) ObserveSinkHPAV2Beta2(ctx context.Context, sink *v1alpha1.Sink) error {
235241
if sink.Spec.MaxReplicas == nil {
236242
// HPA not enabled, skip further action
243+
delete(sink.Status.Conditions, v1alpha1.HPA)
237244
return nil
238245
}
239246

@@ -278,7 +285,12 @@ func (r *SinkReconciler) ObserveSinkHPAV2Beta2(ctx context.Context, sink *v1alph
278285

279286
func (r *SinkReconciler) ApplySinkHPAV2Beta2(ctx context.Context, sink *v1alpha1.Sink, newGeneration bool) error {
280287
if sink.Spec.MaxReplicas == nil {
281-
// HPA not enabled, skip further action
288+
// HPA not enabled, delete HPA if it exists
289+
err := deleteHPAV2Beta2(ctx, r.Client, types.NamespacedName{Namespace: sink.Namespace, Name: sink.Name})
290+
if err != nil {
291+
r.Log.Error(err, "failed to delete HPA for sink", "namespace", sink.Namespace, "name", sink.Name)
292+
return err
293+
}
282294
return nil
283295
}
284296
condition := sink.Status.Conditions[v1alpha1.HPA]

controllers/source.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ func (r *SourceReconciler) ApplySourceService(ctx context.Context, source *v1alp
166166
func (r *SourceReconciler) ObserveSourceHPA(ctx context.Context, source *v1alpha1.Source) error {
167167
if source.Spec.MaxReplicas == nil {
168168
// HPA not enabled, skip further action
169+
delete(source.Status.Conditions, v1alpha1.HPA)
169170
return nil
170171
}
171172

@@ -210,7 +211,12 @@ func (r *SourceReconciler) ObserveSourceHPA(ctx context.Context, source *v1alpha
210211

211212
func (r *SourceReconciler) ApplySourceHPA(ctx context.Context, source *v1alpha1.Source, newGeneration bool) error {
212213
if source.Spec.MaxReplicas == nil {
213-
// HPA not enabled, skip further action
214+
// HPA not enabled, delete HPA if it exists
215+
err := deleteHPA(ctx, r.Client, types.NamespacedName{Namespace: source.Namespace, Name: source.Name})
216+
if err != nil {
217+
r.Log.Error(err, "failed to delete HPA for source", "namespace", source.Namespace, "name", source.Name)
218+
return err
219+
}
214220
return nil
215221
}
216222
condition := source.Status.Conditions[v1alpha1.HPA]
@@ -235,6 +241,7 @@ func (r *SourceReconciler) ApplySourceHPA(ctx context.Context, source *v1alpha1.
235241
func (r *SourceReconciler) ObserveSourceHPAV2Beta2(ctx context.Context, source *v1alpha1.Source) error {
236242
if source.Spec.MaxReplicas == nil {
237243
// HPA not enabled, skip further action
244+
delete(source.Status.Conditions, v1alpha1.HPA)
238245
return nil
239246
}
240247

@@ -280,7 +287,12 @@ func (r *SourceReconciler) ObserveSourceHPAV2Beta2(ctx context.Context, source *
280287
func (r *SourceReconciler) ApplySourceHPAV2Beta2(ctx context.Context, source *v1alpha1.Source,
281288
newGeneration bool) error {
282289
if source.Spec.MaxReplicas == nil {
283-
// HPA not enabled, skip further action
290+
// HPA not enabled, delete HPA if it exists
291+
err := deleteHPAV2Beta2(ctx, r.Client, types.NamespacedName{Namespace: source.Namespace, Name: source.Name})
292+
if err != nil {
293+
r.Log.Error(err, "failed to delete HPA for source", "namespace", source.Namespace, "name", source.Name)
294+
return err
295+
}
284296
return nil
285297
}
286298
condition := source.Status.Conditions[v1alpha1.HPA]

0 commit comments

Comments
 (0)