Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.
Merged
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
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