Skip to content

Commit

Permalink
Merge pull request kubernetes#5284 from olagacek/master
Browse files Browse the repository at this point in the history
Check owner reference in scale down planner to avoid double-counting
  • Loading branch information
k8s-ci-robot authored Nov 25, 2022
2 parents bc48327 + 6b7c291 commit 41e1ea6
Show file tree
Hide file tree
Showing 4 changed files with 548 additions and 62 deletions.
88 changes: 88 additions & 0 deletions cluster-autoscaler/core/scaledown/planner/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package planner

import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/autoscaler/cluster-autoscaler/utils/kubernetes"
)

type controllerCalculatorImpl struct {
listers kubernetes.ListerRegistry
}

func newControllerReplicasCalculator(listers kubernetes.ListerRegistry) controllerReplicasCalculator {
return &controllerCalculatorImpl{listers: listers}
}

func (c *controllerCalculatorImpl) getReplicas(ownerRef metav1.OwnerReference, namespace string) (*replicasInfo, error) {
result := &replicasInfo{}
switch ownerRef.Kind {
case "StatefulSet":
sSet, err := c.listers.StatefulSetLister().StatefulSets(namespace).Get(ownerRef.Name)
if err != nil {
return nil, err
}
result.currentReplicas = sSet.Status.CurrentReplicas
if sSet.Spec.Replicas != nil {
result.targetReplicas = *sSet.Spec.Replicas
} else {
result.targetReplicas = 1
}
case "ReplicaSet":
rSet, err := c.listers.ReplicaSetLister().ReplicaSets(namespace).Get(ownerRef.Name)
if err != nil {
return nil, err
}
result.currentReplicas = rSet.Status.Replicas
if rSet.Spec.Replicas != nil {
result.targetReplicas = *rSet.Spec.Replicas
} else {
result.targetReplicas = 1
}
case "ReplicationController":
rController, err := c.listers.ReplicationControllerLister().ReplicationControllers(namespace).Get(ownerRef.Name)
if err != nil {
return nil, err
}
result.currentReplicas = rController.Status.Replicas
if rController.Spec.Replicas != nil {
result.targetReplicas = *rController.Spec.Replicas
} else {
result.targetReplicas = 1
}
case "Job":
job, err := c.listers.JobLister().Jobs(namespace).Get(ownerRef.Name)
if err != nil {
return nil, err
}
result.currentReplicas = job.Status.Active
if job.Spec.Parallelism != nil {
result.targetReplicas = *job.Spec.Parallelism
} else {
result.targetReplicas = 1
}
if job.Spec.Completions != nil && *job.Spec.Completions-job.Status.Succeeded < result.targetReplicas {
result.targetReplicas = *job.Spec.Completions - job.Status.Succeeded
}
default:
return nil, fmt.Errorf("unhandled controller type: %s", ownerRef.Kind)
}
return result, nil
}
230 changes: 230 additions & 0 deletions cluster-autoscaler/core/scaledown/planner/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package planner

import (
"fmt"
"testing"

"github.com/gogo/protobuf/proto"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
kube_util "k8s.io/autoscaler/cluster-autoscaler/utils/kubernetes"
"k8s.io/autoscaler/cluster-autoscaler/utils/test"
)

var podLabels = map[string]string{
"app": "test",
}

func TestReplicasCounter(t *testing.T) {
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "job",
Namespace: "default",
UID: types.UID("batch/v1/namespaces/default/jobs/job"),
},
Spec: batchv1.JobSpec{
Parallelism: proto.Int32(3),
Selector: metav1.SetAsLabelSelector(podLabels),
},
Status: batchv1.JobStatus{Active: 1},
}
unsetJob := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "unset_job",
Namespace: "default",
UID: types.UID("batch/v1/namespaces/default/jobs/unset_job"),
},
}
jobWithSucceededReplicas := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "succeeded_job",
Namespace: "default",
UID: types.UID("batch/v1/namespaces/default/jobs/succeeded_job"),
},
Spec: batchv1.JobSpec{
Parallelism: proto.Int32(3),
Completions: proto.Int32(3),
Selector: metav1.SetAsLabelSelector(podLabels),
},
Status: batchv1.JobStatus{
Active: 1,
Succeeded: 2,
},
}
rs := &appsv1.ReplicaSet{
ObjectMeta: metav1.ObjectMeta{
Name: "rs",
Namespace: "default",
UID: types.UID("apps/v1/namespaces/default/replicasets/rs"),
},
Spec: appsv1.ReplicaSetSpec{
Replicas: proto.Int32(1),
Selector: metav1.SetAsLabelSelector(podLabels),
},
Status: appsv1.ReplicaSetStatus{
Replicas: 1,
},
}
unsetRs := &appsv1.ReplicaSet{
ObjectMeta: metav1.ObjectMeta{
Name: "unset_rs",
Namespace: "default",
UID: types.UID("apps/v1/namespaces/default/replicasets/unset_rs"),
},
}
rC := &apiv1.ReplicationController{
ObjectMeta: metav1.ObjectMeta{
Name: "rc",
Namespace: "default",
UID: types.UID("core/v1/namespaces/default/replicationcontrollers/rc"),
},
Spec: apiv1.ReplicationControllerSpec{
Replicas: proto.Int32(1),
Selector: podLabels,
},
Status: apiv1.ReplicationControllerStatus{
Replicas: 0,
},
}
sS := &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: "sset",
Namespace: "default",
UID: types.UID("apps/v1/namespaces/default/statefulsets/sset"),
},
Spec: appsv1.StatefulSetSpec{
Replicas: proto.Int32(3),
Selector: metav1.SetAsLabelSelector(podLabels),
},
Status: appsv1.StatefulSetStatus{
Replicas: 1,
},
}
rcLister, _ := kube_util.NewTestReplicationControllerLister([]*apiv1.ReplicationController{rC})
jobLister, _ := kube_util.NewTestJobLister([]*batchv1.Job{job, unsetJob, jobWithSucceededReplicas})
rsLister, _ := kube_util.NewTestReplicaSetLister([]*appsv1.ReplicaSet{rs, unsetRs})
ssLister, _ := kube_util.NewTestStatefulSetLister([]*appsv1.StatefulSet{sS})
listers := kube_util.NewListerRegistry(nil, nil, nil, nil, nil, nil, rcLister, jobLister, rsLister, ssLister)
testCases := []struct {
name string
ownerRef metav1.OwnerReference
wantReplicas replicasInfo
expectErr bool
}{
{
name: "job owner reference",
ownerRef: ownerRef("Job", job.Name),
wantReplicas: replicasInfo{
currentReplicas: 1,
targetReplicas: 3,
},
},
{
name: "job without parallelism owner reference",
ownerRef: ownerRef("Job", unsetJob.Name),
wantReplicas: replicasInfo{
currentReplicas: 0,
targetReplicas: 1,
},
},
{
name: "job with succeeded replicas owner reference",
ownerRef: ownerRef("Job", jobWithSucceededReplicas.Name),
wantReplicas: replicasInfo{
currentReplicas: 1,
targetReplicas: 1,
},
},
{
name: "replica set owner reference",
ownerRef: ownerRef("ReplicaSet", rs.Name),
wantReplicas: replicasInfo{
currentReplicas: 1,
targetReplicas: 1,
},
},
{
name: "replica set without replicas spec specified owner reference",
ownerRef: ownerRef("ReplicaSet", unsetRs.Name),
wantReplicas: replicasInfo{
currentReplicas: 0,
targetReplicas: 1,
},
},
{
name: "replica controller owner reference",
ownerRef: ownerRef("ReplicationController", rC.Name),
wantReplicas: replicasInfo{
currentReplicas: 0,
targetReplicas: 1,
},
},
{
name: "stateful set owner reference",
ownerRef: ownerRef("StatefulSet", sS.Name),
wantReplicas: replicasInfo{
currentReplicas: 0,
targetReplicas: 3,
},
},
{
name: "not existing job owner ref",
ownerRef: ownerRef("Job", "j"),
expectErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
c := newControllerReplicasCalculator(listers)
res, err := c.getReplicas(tc.ownerRef, "default")
if tc.expectErr {
assert.Error(t, err)
} else {
if diff := cmp.Diff(tc.wantReplicas, *res, cmp.AllowUnexported(replicasInfo{})); diff != "" {
t.Errorf("getReplicas() diff (-want +got):\n%s", diff)
}
}
})
}
}

func ownerRef(ownerType, ownerName string) metav1.OwnerReference {
api := ""
strType := ""
switch ownerType {
case "ReplicaSet":
api = "apps/v1"
strType = "replicasets"
case "StatefulSet":
api = "apps/v1"
strType = "statefulsets"
case "ReplicationController":
api = "core/v1"
strType = "replicationcontrollers"
case "Job":
api = "batch/v1"
strType = "jobs"
}
return test.GenerateOwnerReferences(ownerName, ownerType, api, types.UID(fmt.Sprintf("%s/namespaces/default/%s/%s", api, strType, ownerName)))[0]
}
Loading

0 comments on commit 41e1ea6

Please sign in to comment.