Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable RpaasInstance shutting down using Shutdown flag #149

Merged
merged 7 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
// Any assosciated HorizontalPodAutoscaler is remove/created when this flag is toggled.
// +optional
// +kubebuilder:default:=false
Shutdown *bool `json:"shutdown,omitempty"`
gvicentin marked this conversation as resolved.
Show resolved Hide resolved
}

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
gvicentin marked this conversation as resolved.
Show resolved Hide resolved
}

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