Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
If HPA already exists, perform an update. (#1117)
Browse files Browse the repository at this point in the history
  • Loading branch information
d0x2f authored Mar 2, 2020
1 parent 3eff84c commit ec2aac1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
3 changes: 3 additions & 0 deletions pkg/controller/function_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ func (c *FunctionController) ensureK8sResources(funcObj *kubelessApi.Function) e
}
}
err = utils.CreateAutoscale(c.clientset, funcObj.Spec.HorizontalPodAutoscaler)
if err != nil && k8sErrors.IsAlreadyExists(err) {
err = utils.UpdateAutoscale(c.clientset, funcObj.Spec.HorizontalPodAutoscaler)
}
if err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/utils/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,12 @@ func doRESTReq(restIface rest.Interface, groupVersion, verb, resource, elem, nam
// CreateAutoscale creates HPA object for function
func CreateAutoscale(client kubernetes.Interface, hpa v2beta1.HorizontalPodAutoscaler) error {
_, err := client.AutoscalingV2beta1().HorizontalPodAutoscalers(hpa.ObjectMeta.Namespace).Create(&hpa)
if err != nil {
return err
}
return err
}

// UpdateAutoscale updates an existing HPA object for a function
func UpdateAutoscale(client kubernetes.Interface, hpa v2beta1.HorizontalPodAutoscaler) error {
_, err := client.AutoscalingV2beta1().HorizontalPodAutoscalers(hpa.ObjectMeta.Namespace).Update(&hpa)
return err
}

Expand Down
39 changes: 39 additions & 0 deletions pkg/utils/k8sutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,45 @@ func TestCreateAutoscaleResource(t *testing.T) {
}
}

func TestUpdateAutoscaleResource(t *testing.T) {
clientset := fake.NewSimpleClientset()
name := "foo"
ns := "myns"

// Create a pre-existing HPA
hpaDef := v2beta1.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: ns,
},
}
if err := CreateAutoscale(clientset, hpaDef); err != nil {
t.Fatalf("Creating autoscale returned err: %v", err)
}

// Perform an update
hpaDef = v2beta1.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: ns,
Labels: map[string]string{
"baz": "qux",
},
},
}
if err := UpdateAutoscale(clientset, hpaDef); err != nil {
t.Fatalf("Updating autoscale returned err: %v", err)
}

hpa, err := clientset.AutoscalingV2beta1().HorizontalPodAutoscalers(ns).Get(name, metav1.GetOptions{})
if err != nil {
t.Fatalf("Updating autoscale returned err: %v", err)
}
if hpa.ObjectMeta.Name != "foo" {
t.Fatalf("Updating wrong scale target name")
}
}

func TestDeleteAutoscaleResource(t *testing.T) {
myNsFoo := metav1.ObjectMeta{
Namespace: "myns",
Expand Down

0 comments on commit ec2aac1

Please sign in to comment.