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

[scheduled-scaler]support cpu target based upscale #43

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor
bin
.idea
20 changes: 11 additions & 9 deletions pkg/apis/scaling/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ type ScheduledScalerTarget struct {
}

type ScheduledScalerStep struct {
Runat string `json:"runat"`
Mode string `json:"mode"`
MinReplicas *int32 `json:"minReplicas"`
MaxReplicas *int32 `json:"maxReplicas"`
Replicas *int32 `json:"replicas"`
Runat string `json:"runat"`
Mode string `json:"mode"`
MinReplicas *int32 `json:"minReplicas"`
MaxReplicas *int32 `json:"maxReplicas"`
Replicas *int32 `json:"replicas"`
TargetCPUUtilizationPercentage *int32 `json:"targetCPUUtilizationPercentage,omitempty"`
}

type ScheduledScalerSpec struct {
Expand All @@ -49,11 +50,12 @@ type ScheduledScalerSpec struct {
TimeZone string `json:"timeZone"`
}

// FooStatus is the status for a Foo resource
// ScheduledScalerStatus is the status for a ScheduledScaler resource
type ScheduledScalerStatus struct {
Mode string `json:"mode"`
MinReplicas int32 `json:"minReplicas"`
MaxReplicas int32 `json:"maxReplicas"`
Mode string `json:"mode"`
MinReplicas int32 `json:"minReplicas"`
MaxReplicas int32 `json:"maxReplicas"`
TargetCPUUtilizationPercentage *int32 `json:"targetCPUUtilizationPercentage,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
4 changes: 3 additions & 1 deletion pkg/services/scaling/step/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package step

import scalingv1alpha1 "k8s.restdev.com/operators/pkg/apis/scaling/v1alpha1"

func Parse(step scalingv1alpha1.ScheduledScalerStep) (min, max *int32) {
func Parse(step scalingv1alpha1.ScheduledScalerStep) (min, max, cpuTarget *int32) {
if step.Mode == "range" {
min = step.MinReplicas
max = step.MaxReplicas
cpuTarget = step.TargetCPUUtilizationPercentage
}

if step.Mode == "fixed" {
min = step.Replicas
max = step.Replicas
cpuTarget = step.TargetCPUUtilizationPercentage
}

return
Expand Down
16 changes: 14 additions & 2 deletions scaling-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (c *ScheduledScalerController) scheduledScalerHpaCronAdd(scheduledScaler *s
var mutex sync.Mutex
for key := range ssCopy.Spec.Steps {
step := scheduledScaler.Spec.Steps[key]
min, max := scalingstep.Parse(step)
min, max, cpuTarget := scalingstep.Parse(step)
c.cronProxy.Push(stepsCron, step.Runat, func() {
// If this scheduled scaler retries, don't let the "next" one get overwritten by its retry.
mutex.Lock()
Expand All @@ -167,6 +167,9 @@ func (c *ScheduledScalerController) scheduledScalerHpaCronAdd(scheduledScaler *s
}
hpa.Spec.MinReplicas = min
hpa.Spec.MaxReplicas = *max
if cpuTarget != nil {
hpa.Spec.TargetCPUUtilizationPercentage = cpuTarget
}
_, err = hpaClient.Update(hpa)
if apierr.IsConflict(err) {
glog.Infof("FAILED TO UPDATE HPA: %s - %v; retrying", scheduledScaler.Spec.Target.Name, err)
Expand Down Expand Up @@ -194,6 +197,7 @@ func (c *ScheduledScalerController) scheduledScalerHpaCronAdd(scheduledScaler *s
ss.Status.Mode = step.Mode
ss.Status.MinReplicas = *min
ss.Status.MaxReplicas = *max
ss.Status.TargetCPUUtilizationPercentage = cpuTarget
_, err = ssClient.Update(ss)
if apierr.IsConflict(err) {
glog.Infof("FAILED TO UPDATE SCHEDULED SCALER STATUS: %s - %v; retrying", scheduledScaler.Name, err)
Expand Down Expand Up @@ -248,11 +252,19 @@ func (c *ScheduledScalerController) scheduledScalerIgCronAdd(scheduledScaler *sc
}
for key := range scheduledScaler.Spec.Steps {
step := scheduledScaler.Spec.Steps[key]
min, max := scalingstep.Parse(step)
min, max, cpuTarget := scalingstep.Parse(step)
c.cronProxy.Push(stepsCron, step.Runat, func() {
autoscaler, err = computeService.Autoscalers.Get(projectId, zone, scheduledScaler.Spec.Target.Name).Do()
autoscaler.AutoscalingPolicy.MaxNumReplicas = int64(*max)
autoscaler.AutoscalingPolicy.MinNumReplicas = int64(*min)
if cpuTarget != nil {
if autoscaler.AutoscalingPolicy.CpuUtilization == nil {
utilruntime.HandleError(fmt.Errorf(
"%s - IG autoscaler doesn't use CpuUtilization Policys", scheduledScaler.Spec.Target.Name))
return
}
autoscaler.AutoscalingPolicy.CpuUtilization.UtilizationTarget = float64(*cpuTarget / 100.)
}
_, err := computeService.Autoscalers.Update(projectId, zone, autoscaler).Do()
if err != nil {
utilruntime.HandleError(fmt.Errorf(
Expand Down