Skip to content

Commit

Permalink
add restore autoscaling button
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Paskal <paskal.maksim@gmail.com>
  • Loading branch information
maksim-paskal committed Jul 8, 2024
1 parent 8471a68 commit defe549
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
2 changes: 1 addition & 1 deletion e2e/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func TestPods(t *testing.T) {
t.Fatal(err)
}

err = environment.DisableHPA(ctx)
err = environment.DisableHPA(ctx, true)
if err != nil {
t.Fatal(err)
}
Expand Down
1 change: 1 addition & 0 deletions front/pages/_environmentID/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<br />
<b-card bg-variant="light" header="Actions" class="text-center">
<b-button @click="call('make-disable-hpa')">Disable autoscaling</b-button>
<b-button @click="call('make-restore-hpa')">Restore autoscaling</b-button>
<b-button @click="call('make-disable-mtls')">Disable mTLS verification</b-button>
<b-button @click="call('make-snapshot')">Make environment snapshot</b-button>
</b-card>
Expand Down
40 changes: 30 additions & 10 deletions pkg/api/disableHPA.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,60 @@ package api

import (
"context"
"fmt"
"strings"

"github.com/maksim-paskal/kubernetes-manager/pkg/telemetry"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

func (e *Environment) DisableHPA(ctx context.Context) error {
func (e *Environment) DisableHPA(ctx context.Context, disable bool) error {
ctx, span := telemetry.Start(ctx, "api.DisableHPA")
defer span.End()

if e.IsSystemNamespace() {
return errors.Wrap(errIsSystemNamespace, e.Namespace)
}

hpa := e.clientset.AutoscalingV1().HorizontalPodAutoscalers(e.Namespace)

hpas, err := hpa.List(ctx, metav1.ListOptions{})
hpas, err := e.clientset.AutoscalingV1().HorizontalPodAutoscalers(e.Namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return errors.Wrap(err, "error listing hpa")
}

GracePeriodSeconds := int64(0)

opt := &metav1.DeleteOptions{
GracePeriodSeconds: &GracePeriodSeconds,
}
const disabledPrefix = "disabled-"

for _, hpa := range hpas.Items {
err := e.clientset.AutoscalingV1().HorizontalPodAutoscalers(e.Namespace).Delete(ctx, hpa.Name, *opt)
scaleTargetRefKind := strings.TrimPrefix(hpa.Spec.ScaleTargetRef.Kind, disabledPrefix)

if disable {
// if already disabled - skip
if strings.HasPrefix(hpa.Spec.ScaleTargetRef.Kind, disabledPrefix) {
continue
}

scaleTargetRefKind = disabledPrefix + hpa.Spec.ScaleTargetRef.Kind
}

payload := fmt.Sprintf(`{"spec":{"scaleTargetRef":{"kind": "%s" }}}`, scaleTargetRefKind)

_, err := e.clientset.AutoscalingV1().HorizontalPodAutoscalers(e.Namespace).Patch(ctx,
hpa.Name,
types.StrategicMergePatchType,
[]byte(payload),
metav1.PatchOptions{},
)
if err != nil {
return errors.Wrap(err, "error deleting hpa")
}
}

// if we restoring original values - do nothing
if !disable {
return nil
}

err = e.ScaleNamespace(ctx, 1)
if err != nil {
return errors.Wrap(err, "error scaling namespace")
Expand Down
11 changes: 9 additions & 2 deletions pkg/web/handlerEnvironment.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,19 @@ func environmentOperation(ctx context.Context, r *http.Request, environmentID st

result.Result = "Delayed scaleDown on next " + scaledownDelay.Delay
case "make-disable-hpa":
err := environment.DisableHPA(ctx)
err := environment.DisableHPA(ctx, true)
if err != nil {
return result, err
}

result.Result = "Disabled HPA in namespace %s" + environment.Namespace
result.Result = "Disabled autoscaling in namespace " + environment.Namespace
case "make-restore-hpa":
err := environment.DisableHPA(ctx, false)
if err != nil {
return result, err
}

result.Result = "Restored autoscaling in namespace " + environment.Namespace
case "make-disable-mtls":
err := environment.DisableMTLS(ctx)
if err != nil {
Expand Down

0 comments on commit defe549

Please sign in to comment.