Skip to content

Commit

Permalink
fix: test to focus on checking hpa deletion for paused Scaledbject ke…
Browse files Browse the repository at this point in the history
…dacore#4253

Signed-off-by: Tobo Atchou <tobo.atchou@gmail.com>
  • Loading branch information
tobotg committed Apr 24, 2023
1 parent 4b2bb99 commit 44d4fd6
Showing 1 changed file with 17 additions and 50 deletions.
67 changes: 17 additions & 50 deletions controllers/keda/scaledobject_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
clientcache "k8s.io/client-go/tools/cache"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
Expand Down Expand Up @@ -845,7 +844,7 @@ var _ = Describe("ScaledObjectController", func() {
})

// Fix issue 4253
It("stops scale loop and delete existing hpa when scaledobject is updated to have pause annotation", func() {
It("deletes hpa when scaledobject has pause annotation", func() {
// Create the scaling target.
deploymentName := "to-be-paused-name"
soName := "so-" + deploymentName
Expand All @@ -854,7 +853,13 @@ var _ = Describe("ScaledObjectController", func() {

// Create the ScaledObject without specifying name.
so := &kedav1alpha1.ScaledObject{
ObjectMeta: metav1.ObjectMeta{Name: soName, Namespace: "default"},
ObjectMeta: metav1.ObjectMeta{
Name: soName,
Namespace: "default",
Annotations: map[string]string{
kedacontrollerutil.PausedReplicasAnnotation: "0",
},
},
Spec: kedav1alpha1.ScaledObjectSpec{
ScaleTargetRef: &kedav1alpha1.ScaleTarget{
Name: deploymentName,
Expand All @@ -878,21 +883,6 @@ var _ = Describe("ScaledObjectController", func() {
err = k8sClient.Create(context.Background(), so)
Expect(err).ToNot(HaveOccurred())

// Get and confirm the HPA.
hpa := &autoscalingv2.HorizontalPodAutoscaler{}
Eventually(func() error {
return k8sClient.Get(context.Background(), types.NamespacedName{Name: fmt.Sprintf("keda-hpa-%s", soName), Namespace: "default"}, hpa)
}).ShouldNot(HaveOccurred())
Expect(hpa.Name).To(Equal(fmt.Sprintf("keda-hpa-%s", soName)))

// Pause scaledObject (add annotation)
Eventually(func() error {
err = k8sClient.Get(context.Background(), types.NamespacedName{Name: soName, Namespace: "default"}, so)
Expect(err).ToNot(HaveOccurred())
so.Annotations[kedacontrollerutil.PausedReplicasAnnotation] = "0"
return k8sClient.Update(context.Background(), so)
}).ShouldNot(HaveOccurred())

// wait so's ready condition Ready
Eventually(func() metav1.ConditionStatus {
err := k8sClient.Get(context.Background(), types.NamespacedName{Name: soName, Namespace: "default"}, so)
Expand All @@ -902,40 +892,17 @@ var _ = Describe("ScaledObjectController", func() {
return so.Status.Conditions.GetReadyCondition().Status
}).Should(Equal(metav1.ConditionTrue))

// And validate that old hpa is deleted.
err = k8sClient.Get(context.Background(), types.NamespacedName{Name: fmt.Sprintf("keda-hpa-%s", soName), Namespace: "default"}, hpa)
Expect(err).Should(HaveOccurred())
Expect(errors.IsNotFound(err)).To(Equal(true))

//
key, err := clientcache.MetaNamespaceKeyFunc(so)
Expect(err).Should(HaveOccurred())
Expect(key).To(Equal(nil))
// validate annotation is set correctly
err = k8sClient.Get(context.Background(), types.NamespacedName{Name: soName, Namespace: "default"}, so)
Expect(err).ToNot(HaveOccurred())
_, paused := so.GetAnnotations()[kedacontrollerutil.PausedReplicasAnnotation]
Expect(paused).To(Equal(true))

// Resume scaledObject (remove annotation)
// And validate that hpa is deleted.
hpa := &autoscalingv2.HorizontalPodAutoscaler{}
Eventually(func() error {
err = k8sClient.Get(context.Background(), types.NamespacedName{Name: soName, Namespace: "default"}, so)
Expect(err).ToNot(HaveOccurred())
delete(so.Annotations, kedacontrollerutil.PausedReplicasAnnotation)
return k8sClient.Update(context.Background(), so)
}).ShouldNot(HaveOccurred())

// wait so's ready condition Ready
Eventually(func() metav1.ConditionStatus {
err := k8sClient.Get(context.Background(), types.NamespacedName{Name: soName, Namespace: "default"}, so)
if err != nil {
return metav1.ConditionUnknown
}
return so.Status.Conditions.GetReadyCondition().Status
}).Should(Equal(metav1.ConditionTrue))

// And validate that hpa is created.
err = k8sClient.Get(context.Background(), types.NamespacedName{Name: fmt.Sprintf("keda-hpa-%s", soName), Namespace: "default"}, hpa)
Expect(err).ShouldNot(HaveOccurred())

//
_, err = clientcache.MetaNamespaceKeyFunc(so)
Expect(err).ShouldNot(HaveOccurred())
return k8sClient.Get(context.Background(), types.NamespacedName{Name: fmt.Sprintf("keda-hpa-%s", soName), Namespace: "default"}, hpa)
}).Should(HaveOccurred())
})
})

Expand Down

0 comments on commit 44d4fd6

Please sign in to comment.