Skip to content

Commit

Permalink
Enable instance shuting down using Shutdown flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilherme Vicentin committed Mar 25, 2024
1 parent 420caaa commit b0cd2bc
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 7 deletions.
6 changes: 6 additions & 0 deletions api/v1alpha1/rpaasinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ type RpaasInstanceSpec struct {
// +optional
// +kubebuilder:default:=false
Suspend *bool `json:"suspend,omitempty"`

// Shutdown flag tells whether controller should scale down Nginx instances.
// And any assosciated HorizontalPodAutoscaler is remove/created when this flag is toggled.
// +optional
// +kubebuilder:default:=false
Shutdown *bool `json:"shutdown,omitempty"`
}

type DynamicCertificates struct {
Expand Down
45 changes: 44 additions & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions config/crd/bases/extensions.tsuru.io_rpaasflavors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6633,6 +6633,12 @@ spec:
Defaults to true.
type: boolean
type: object
shutdown:
default: false
description: Shutdown flag tells whether controller should scale
down Nginx instances. And any assosciated HorizontalPodAutoscaler
is remove/created when this flag is toggled.
type: boolean
suspend:
default: false
description: Suspend flag tells whether controller should suspend
Expand Down
6 changes: 6 additions & 0 deletions config/crd/bases/extensions.tsuru.io_rpaasinstances.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6381,6 +6381,12 @@ spec:
true.
type: boolean
type: object
shutdown:
default: false
description: Shutdown flag tells whether controller should scale down
Nginx instances. And any assosciated HorizontalPodAutoscaler is
remove/created when this flag is toggled.
type: boolean
suspend:
default: false
description: Suspend flag tells whether controller should suspend
Expand Down
25 changes: 19 additions & 6 deletions controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ func (r *RpaasInstanceReconciler) reconcileHPA(ctx context.Context, instance *v1
var observed autoscalingv2.HorizontalPodAutoscaler
err = r.Client.Get(ctx, types.NamespacedName{Name: desired.Name, Namespace: desired.Namespace}, &observed)
if k8sErrors.IsNotFound(err) {
if !isAutoscaleEnabled(instance.Spec.Autoscale) {
if !isAutoscaleEnabled(instance) {
logger.V(4).Info("Skipping HorizontalPodAutoscaler reconciliation: both HPA resource and desired RpaasAutoscaleSpec not found")
return cleanedKeda, nil
}
Expand All @@ -605,7 +605,7 @@ func (r *RpaasInstanceReconciler) reconcileHPA(ctx context.Context, instance *v1

logger = logger.WithValues("HorizontalPodAutoscaler", types.NamespacedName{Name: observed.Name, Namespace: observed.Namespace})

if !isAutoscaleEnabled(instance.Spec.Autoscale) {
if !isAutoscaleEnabled(instance) {
logger.V(4).Info("Deleting HorizontalPodAutoscaler resource")
if err = r.Client.Delete(ctx, &observed); err != nil {
logger.Error(err, "Unable to delete the HorizontalPodAutoscaler resource")
Expand Down Expand Up @@ -664,7 +664,7 @@ func (r *RpaasInstanceReconciler) reconcileKEDA(ctx context.Context, instance *v
var observed kedav1alpha1.ScaledObject
err = r.Client.Get(ctx, types.NamespacedName{Name: desired.Name, Namespace: desired.Namespace}, &observed)
if k8sErrors.IsNotFound(err) {
if !isAutoscaleEnabled(instance.Spec.Autoscale) {
if !isAutoscaleEnabled(instance) {
return false, nil // nothing to do
}

Expand All @@ -679,7 +679,7 @@ func (r *RpaasInstanceReconciler) reconcileKEDA(ctx context.Context, instance *v
return false, err
}

if !isAutoscaleEnabled(instance.Spec.Autoscale) {
if !isAutoscaleEnabled(instance) {
err = r.Client.Delete(ctx, &observed)
if err != nil {
return false, err
Expand All @@ -699,12 +699,20 @@ func (r *RpaasInstanceReconciler) reconcileKEDA(ctx context.Context, instance *v
return true, nil
}

func isAutoscaleEnabled(a *v1alpha1.RpaasInstanceAutoscaleSpec) bool {
func shouldShutdownInstance(instance *v1alpha1.RpaasInstance) bool {
return instance.Spec.Shutdown != nil && *instance.Spec.Shutdown
}

func isAutoscaleValid(a *v1alpha1.RpaasInstanceAutoscaleSpec) bool {
return a != nil &&
(a.MinReplicas != nil && a.MaxReplicas > 0) &&
(a.TargetCPUUtilizationPercentage != nil || a.TargetMemoryUtilizationPercentage != nil || a.TargetRequestsPerSecond != nil || len(a.Schedules) > 0)
}

func isAutoscaleEnabled(instance *v1alpha1.RpaasInstance) bool {
return !shouldShutdownInstance(instance) && isAutoscaleValid(instance.Spec.Autoscale)
}

func newKEDAScaledObject(instance *v1alpha1.RpaasInstance, nginx *nginxv1alpha1.Nginx) (*kedav1alpha1.ScaledObject, error) {
var triggers []kedav1alpha1.ScaleTriggers
if instance.Spec.Autoscale != nil && instance.Spec.Autoscale.TargetCPUUtilizationPercentage != nil {
Expand Down Expand Up @@ -1186,7 +1194,12 @@ func newNginx(instanceMergedWithFlavors *v1alpha1.RpaasInstance, plan *v1alpha1.
}

replicas := instanceMergedWithFlavors.Spec.Replicas
if isAutoscaleEnabled(instanceMergedWithFlavors.Spec.Autoscale) {
shouldShutdown := shouldShutdownInstance(instanceMergedWithFlavors)
if shouldShutdown {
*replicas = 0
}

if isAutoscaleEnabled(instanceMergedWithFlavors) {
// NOTE: we should avoid changing the number of replicas as it's managed by HPA.
replicas = nil
}
Expand Down

0 comments on commit b0cd2bc

Please sign in to comment.