From d547790c9b437f1b20e29e77827ee44d8cc68531 Mon Sep 17 00:00:00 2001 From: Kensei Nakada Date: Tue, 5 Mar 2024 16:46:52 +0900 Subject: [PATCH] use .spec.replicas instead to take a desired replica number --- .../deletion-no-delete/before/deployment.yaml | 1 - .../deletion-policy-all/before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - .../before/deployment.yaml | 1 - controllers/tortoise_controller.go | 16 +++++++++++----- 24 files changed, 11 insertions(+), 28 deletions(-) diff --git a/controllers/testdata/deletion-no-delete/before/deployment.yaml b/controllers/testdata/deletion-no-delete/before/deployment.yaml index 04ac49e3..169b372b 100644 --- a/controllers/testdata/deletion-no-delete/before/deployment.yaml +++ b/controllers/testdata/deletion-no-delete/before/deployment.yaml @@ -25,5 +25,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/deletion-policy-all/before/deployment.yaml b/controllers/testdata/deletion-policy-all/before/deployment.yaml index 04ac49e3..169b372b 100644 --- a/controllers/testdata/deletion-policy-all/before/deployment.yaml +++ b/controllers/testdata/deletion-policy-all/before/deployment.yaml @@ -25,5 +25,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/mutable-autoscalingpolicy-add-another-horizontal/before/deployment.yaml b/controllers/testdata/mutable-autoscalingpolicy-add-another-horizontal/before/deployment.yaml index 04ac49e3..169b372b 100644 --- a/controllers/testdata/mutable-autoscalingpolicy-add-another-horizontal/before/deployment.yaml +++ b/controllers/testdata/mutable-autoscalingpolicy-add-another-horizontal/before/deployment.yaml @@ -25,5 +25,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/mutable-autoscalingpolicy-no-hpa-and-add-horizontal/before/deployment.yaml b/controllers/testdata/mutable-autoscalingpolicy-no-hpa-and-add-horizontal/before/deployment.yaml index 04ac49e3..169b372b 100644 --- a/controllers/testdata/mutable-autoscalingpolicy-no-hpa-and-add-horizontal/before/deployment.yaml +++ b/controllers/testdata/mutable-autoscalingpolicy-no-hpa-and-add-horizontal/before/deployment.yaml @@ -25,5 +25,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/mutable-autoscalingpolicy-remove-horizontal-2/before/deployment.yaml b/controllers/testdata/mutable-autoscalingpolicy-remove-horizontal-2/before/deployment.yaml index 04ac49e3..169b372b 100644 --- a/controllers/testdata/mutable-autoscalingpolicy-remove-horizontal-2/before/deployment.yaml +++ b/controllers/testdata/mutable-autoscalingpolicy-remove-horizontal-2/before/deployment.yaml @@ -25,5 +25,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/mutable-autoscalingpolicy-remove-horizontal/before/deployment.yaml b/controllers/testdata/mutable-autoscalingpolicy-remove-horizontal/before/deployment.yaml index 04ac49e3..169b372b 100644 --- a/controllers/testdata/mutable-autoscalingpolicy-remove-horizontal/before/deployment.yaml +++ b/controllers/testdata/mutable-autoscalingpolicy-remove-horizontal/before/deployment.yaml @@ -25,5 +25,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/reconcile-for-the-istio-enabled-pod-working/before/deployment.yaml b/controllers/testdata/reconcile-for-the-istio-enabled-pod-working/before/deployment.yaml index 2e8dbfaf..693bb43d 100644 --- a/controllers/testdata/reconcile-for-the-istio-enabled-pod-working/before/deployment.yaml +++ b/controllers/testdata/reconcile-for-the-istio-enabled-pod-working/before/deployment.yaml @@ -24,5 +24,4 @@ spec: requests: cpu: "10" memory: 10Gi -status: replicas: 10 diff --git a/controllers/testdata/reconcile-for-the-multiple-containers-pod-all-off/before/deployment.yaml b/controllers/testdata/reconcile-for-the-multiple-containers-pod-all-off/before/deployment.yaml index 04ac49e3..169b372b 100644 --- a/controllers/testdata/reconcile-for-the-multiple-containers-pod-all-off/before/deployment.yaml +++ b/controllers/testdata/reconcile-for-the-multiple-containers-pod-all-off/before/deployment.yaml @@ -25,5 +25,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/reconcile-for-the-multiple-containers-pod-all-vertical/before/deployment.yaml b/controllers/testdata/reconcile-for-the-multiple-containers-pod-all-vertical/before/deployment.yaml index 04ac49e3..169b372b 100644 --- a/controllers/testdata/reconcile-for-the-multiple-containers-pod-all-vertical/before/deployment.yaml +++ b/controllers/testdata/reconcile-for-the-multiple-containers-pod-all-vertical/before/deployment.yaml @@ -25,5 +25,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/reconcile-for-the-multiple-containers-pod-during-emergency/before/deployment.yaml b/controllers/testdata/reconcile-for-the-multiple-containers-pod-during-emergency/before/deployment.yaml index 04ac49e3..169b372b 100644 --- a/controllers/testdata/reconcile-for-the-multiple-containers-pod-during-emergency/before/deployment.yaml +++ b/controllers/testdata/reconcile-for-the-multiple-containers-pod-during-emergency/before/deployment.yaml @@ -25,5 +25,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/reconcile-for-the-multiple-containers-pod-emergency-started/before/deployment.yaml b/controllers/testdata/reconcile-for-the-multiple-containers-pod-emergency-started/before/deployment.yaml index 04ac49e3..169b372b 100644 --- a/controllers/testdata/reconcile-for-the-multiple-containers-pod-emergency-started/before/deployment.yaml +++ b/controllers/testdata/reconcile-for-the-multiple-containers-pod-emergency-started/before/deployment.yaml @@ -25,5 +25,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/reconcile-for-the-multiple-containers-pod-one-off/before/deployment.yaml b/controllers/testdata/reconcile-for-the-multiple-containers-pod-one-off/before/deployment.yaml index 04ac49e3..169b372b 100644 --- a/controllers/testdata/reconcile-for-the-multiple-containers-pod-one-off/before/deployment.yaml +++ b/controllers/testdata/reconcile-for-the-multiple-containers-pod-one-off/before/deployment.yaml @@ -25,5 +25,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/reconcile-for-the-multiple-containers-pod-suggested-too-small/before/deployment.yaml b/controllers/testdata/reconcile-for-the-multiple-containers-pod-suggested-too-small/before/deployment.yaml index 2ed73339..d037c045 100644 --- a/controllers/testdata/reconcile-for-the-multiple-containers-pod-suggested-too-small/before/deployment.yaml +++ b/controllers/testdata/reconcile-for-the-multiple-containers-pod-suggested-too-small/before/deployment.yaml @@ -21,5 +21,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/reconcile-for-the-multiple-containers-pod-working/before/deployment.yaml b/controllers/testdata/reconcile-for-the-multiple-containers-pod-working/before/deployment.yaml index 04ac49e3..169b372b 100644 --- a/controllers/testdata/reconcile-for-the-multiple-containers-pod-working/before/deployment.yaml +++ b/controllers/testdata/reconcile-for-the-multiple-containers-pod-working/before/deployment.yaml @@ -25,5 +25,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/reconcile-for-the-single-container-pod-backtonormal/before/deployment.yaml b/controllers/testdata/reconcile-for-the-single-container-pod-backtonormal/before/deployment.yaml index 34693daa..22809372 100644 --- a/controllers/testdata/reconcile-for-the-single-container-pod-backtonormal/before/deployment.yaml +++ b/controllers/testdata/reconcile-for-the-single-container-pod-backtonormal/before/deployment.yaml @@ -19,5 +19,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/reconcile-for-the-single-container-pod-dryrun/before/deployment.yaml b/controllers/testdata/reconcile-for-the-single-container-pod-dryrun/before/deployment.yaml index 34693daa..22809372 100644 --- a/controllers/testdata/reconcile-for-the-single-container-pod-dryrun/before/deployment.yaml +++ b/controllers/testdata/reconcile-for-the-single-container-pod-dryrun/before/deployment.yaml @@ -19,5 +19,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/reconcile-for-the-single-container-pod-emergency-started/before/deployment.yaml b/controllers/testdata/reconcile-for-the-single-container-pod-emergency-started/before/deployment.yaml index 34693daa..22809372 100644 --- a/controllers/testdata/reconcile-for-the-single-container-pod-emergency-started/before/deployment.yaml +++ b/controllers/testdata/reconcile-for-the-single-container-pod-emergency-started/before/deployment.yaml @@ -19,5 +19,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/reconcile-for-the-single-container-pod-gathering-data-finished/before/deployment.yaml b/controllers/testdata/reconcile-for-the-single-container-pod-gathering-data-finished/before/deployment.yaml index 34693daa..22809372 100644 --- a/controllers/testdata/reconcile-for-the-single-container-pod-gathering-data-finished/before/deployment.yaml +++ b/controllers/testdata/reconcile-for-the-single-container-pod-gathering-data-finished/before/deployment.yaml @@ -19,5 +19,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/reconcile-for-the-single-container-pod-gathering-data/before/deployment.yaml b/controllers/testdata/reconcile-for-the-single-container-pod-gathering-data/before/deployment.yaml index 34693daa..22809372 100644 --- a/controllers/testdata/reconcile-for-the-single-container-pod-gathering-data/before/deployment.yaml +++ b/controllers/testdata/reconcile-for-the-single-container-pod-gathering-data/before/deployment.yaml @@ -19,5 +19,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/reconcile-for-the-single-container-pod-hpa-changed/before/deployment.yaml b/controllers/testdata/reconcile-for-the-single-container-pod-hpa-changed/before/deployment.yaml index 34693daa..22809372 100644 --- a/controllers/testdata/reconcile-for-the-single-container-pod-hpa-changed/before/deployment.yaml +++ b/controllers/testdata/reconcile-for-the-single-container-pod-hpa-changed/before/deployment.yaml @@ -19,5 +19,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/reconcile-for-the-single-container-pod-initializing/before/deployment.yaml b/controllers/testdata/reconcile-for-the-single-container-pod-initializing/before/deployment.yaml index 34693daa..22809372 100644 --- a/controllers/testdata/reconcile-for-the-single-container-pod-initializing/before/deployment.yaml +++ b/controllers/testdata/reconcile-for-the-single-container-pod-initializing/before/deployment.yaml @@ -19,5 +19,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/reconcile-for-the-single-container-pod-partly-working/before/deployment.yaml b/controllers/testdata/reconcile-for-the-single-container-pod-partly-working/before/deployment.yaml index 34693daa..22809372 100644 --- a/controllers/testdata/reconcile-for-the-single-container-pod-partly-working/before/deployment.yaml +++ b/controllers/testdata/reconcile-for-the-single-container-pod-partly-working/before/deployment.yaml @@ -19,5 +19,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/testdata/reconcile-for-the-single-container-pod-working/before/deployment.yaml b/controllers/testdata/reconcile-for-the-single-container-pod-working/before/deployment.yaml index 34693daa..22809372 100644 --- a/controllers/testdata/reconcile-for-the-single-container-pod-working/before/deployment.yaml +++ b/controllers/testdata/reconcile-for-the-single-container-pod-working/before/deployment.yaml @@ -19,5 +19,4 @@ spec: requests: cpu: "4" memory: 4Gi -status: replicas: 10 diff --git a/controllers/tortoise_controller.go b/controllers/tortoise_controller.go index 665edeaf..b95e3acf 100644 --- a/controllers/tortoise_controller.go +++ b/controllers/tortoise_controller.go @@ -166,7 +166,13 @@ func (r *TortoiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ logger.Error(err, "failed to get deployment", "tortoise", req.NamespacedName) return ctrl.Result{}, err } - currentReplicaNum := dm.Status.Replicas + if dm.Spec.Replicas == nil { + logger.Error(nil, "the deployment doesn't have the number of replicas and tortoise cannot calculate the recommendation", "tortoise", req.NamespacedName, "deployment", klog.KObj(dm)) + return ctrl.Result{}, nil + + } + + currentDesiredReplicaNum := *dm.Spec.Replicas // Use the desired replica number. if tortoise.Spec.UpdateMode == autoscalingv1beta3.UpdateModeOff /* When Off, ContainerResourceRequests should be reset */ || tortoise.Status.Conditions.ContainerResourceRequests == nil /* The first reconciliation */ { @@ -191,7 +197,7 @@ func (r *TortoiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ if tortoise.Status.TortoisePhase == autoscalingv1beta3.TortoisePhaseInitializing { logger.Info("initializing tortoise", "tortoise", req.NamespacedName) // need to initialize HPA and VPA. - if err := r.initializeVPAAndHPA(ctx, tortoise, currentReplicaNum, now); err != nil { + if err := r.initializeVPAAndHPA(ctx, tortoise, currentDesiredReplicaNum, now); err != nil { return ctrl.Result{}, fmt.Errorf("initialize VPA and HPA: %w", err) } @@ -204,7 +210,7 @@ func (r *TortoiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ return ctrl.Result{}, fmt.Errorf("add finalizer: %w", err) } - tortoise, err = r.HpaService.UpdateHPASpecFromTortoiseAutoscalingPolicy(ctx, tortoise, hpa, currentReplicaNum, now) + tortoise, err = r.HpaService.UpdateHPASpecFromTortoiseAutoscalingPolicy(ctx, tortoise, hpa, currentDesiredReplicaNum, now) if err != nil { logger.Error(err, "update HPA spec from Tortoise autoscaling policy", "tortoise", req.NamespacedName) return ctrl.Result{}, err @@ -243,7 +249,7 @@ func (r *TortoiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ tortoise = r.TortoiseService.UpdateContainerRecommendationFromVPA(tortoise, monitorvpa, now) - tortoise, err = r.RecommenderService.UpdateRecommendations(ctx, tortoise, hpa, currentReplicaNum, now) + tortoise, err = r.RecommenderService.UpdateRecommendations(ctx, tortoise, hpa, currentDesiredReplicaNum, now) if err != nil { logger.Error(err, "update recommendation in tortoise", "tortoise", req.NamespacedName) return ctrl.Result{}, err @@ -266,7 +272,7 @@ func (r *TortoiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ return ctrl.Result{}, err } - tortoise, err = r.TortoiseService.UpdateResourceRequest(ctx, tortoise, currentReplicaNum, now) + tortoise, err = r.TortoiseService.UpdateResourceRequest(ctx, tortoise, currentDesiredReplicaNum, now) if err != nil { logger.Error(err, "update VPA based on the recommendation in tortoise", "tortoise", req.NamespacedName) return ctrl.Result{}, err