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

Commit 6bbb7d1

Browse files
andresmgotsebgoa
authored andcommitted
Return proper error (#312)
* Return proper error * Avoid to redefine err. Add integration test for updating * Do not use temporary files * Wait until the updated pod is ready * Fix condition * Increase timeout and debug info * Increase debug * Wait in two steps * Change matching expression * Remove debug code * Increase timeout * Add debug * Redirect debug info * Change approach * Return to initial state * Move update test * Fix RBAC roles permission. Restore order and approach * Remove unnecessary code
1 parent acb60a1 commit 6bbb7d1

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

examples/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ get-python:
55
get-python-verify:
66
kubeless function call get-python |egrep hello.world
77

8+
get-python-update:
9+
printf 'def foo():\n%4sreturn "hello world updated"\n' | kubeless function update get-python --runtime python2.7 --handler helloget.foo --from-file /dev/stdin
10+
echo "curl localhost:8080/api/v1/proxy/namespaces/default/services/get-python/"
11+
12+
get-python-update-verify:
13+
kubeless function call get-python |egrep hello.world.updated
14+
815
get-python-34:
916
kubeless function deploy get-python --trigger-http --runtime python3.4 --handler helloget.foo --from-file python/helloget.py
1017
echo "curl localhost:8080/api/v1/proxy/namespaces/default/services/get-python/"

kubeless-rbac.jsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ local kubeless = import "kubeless.jsonnet";
77
local controller_account = kubeless.controller_account;
88
local controller_roles = [{
99
apiGroups: ["*"],
10-
resources: ["services", "deployments", "functions", "configmaps"],
10+
resources: ["services", "deployments", "functions", "configmaps", "pods", "replicasets"],
1111
verbs: ["*"]
1212
}];
1313

pkg/utils/k8sutil.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,8 @@ func ensureFuncConfigMap(client kubernetes.Interface, funcObj *spec.Function, or
776776

777777
_, err = client.Core().ConfigMaps(funcObj.Metadata.Namespace).Create(configMap)
778778
if err != nil && k8sErrors.IsAlreadyExists(err) {
779-
data, err := json.Marshal(configMap)
779+
var data []byte
780+
data, err = json.Marshal(configMap)
780781
if err != nil {
781782
return err
782783
}
@@ -807,12 +808,12 @@ func ensureFuncService(client kubernetes.Interface, funcObj *spec.Function, or [
807808
}
808809
_, err := client.Core().Services(funcObj.Metadata.Namespace).Create(svc)
809810
if err != nil && k8sErrors.IsAlreadyExists(err) {
810-
data, err := json.Marshal(svc)
811+
var data []byte
812+
data, err = json.Marshal(svc)
811813
if err != nil {
812814
return err
813815
}
814816
_, err = client.Core().Services(funcObj.Metadata.Namespace).Patch(svc.Name, types.StrategicMergePatchType, data)
815-
816817
}
817818
return err
818819
}
@@ -959,7 +960,8 @@ func ensureFuncDeployment(client kubernetes.Interface, funcObj *spec.Function, o
959960

960961
_, err = client.Extensions().Deployments(funcObj.Metadata.Namespace).Create(dpm)
961962
if err != nil && k8sErrors.IsAlreadyExists(err) {
962-
data, err := json.Marshal(dpm)
963+
var data []byte
964+
data, err = json.Marshal(dpm)
963965
if err != nil {
964966
return err
965967
}
@@ -971,7 +973,11 @@ func ensureFuncDeployment(client kubernetes.Interface, funcObj *spec.Function, o
971973
// kick existing function pods then it will be recreated
972974
// with the new data mount from updated configmap.
973975
// TODO: This is a workaround. Do something better.
974-
pods, err := GetPodsByLabel(client, funcObj.Metadata.Namespace, "function", funcObj.Metadata.Name)
976+
var pods *v1.PodList
977+
pods, err = GetPodsByLabel(client, funcObj.Metadata.Namespace, "function", funcObj.Metadata.Name)
978+
if err != nil {
979+
return err
980+
}
975981
for _, pod := range pods.Items {
976982
err = client.Core().Pods(funcObj.Metadata.Namespace).Delete(pod.Name, &metav1.DeleteOptions{})
977983
if err != nil && !k8sErrors.IsNotFound(err) {
@@ -1014,14 +1020,12 @@ func ensureFuncJob(client kubernetes.Interface, funcObj *spec.Function, or []met
10141020

10151021
_, err := client.BatchV2alpha1().CronJobs(funcObj.Metadata.Namespace).Create(job)
10161022
if err != nil && k8sErrors.IsAlreadyExists(err) {
1017-
data, err := json.Marshal(job)
1023+
var data []byte
1024+
data, err = json.Marshal(job)
10181025
if err != nil {
10191026
return err
10201027
}
10211028
_, err = client.BatchV2alpha1().CronJobs(funcObj.Metadata.Namespace).Patch(job.Name, types.StrategicMergePatchType, data)
1022-
if err != nil {
1023-
return err
1024-
}
10251029
}
10261030

10271031
return err

script/libtest.bash

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ k8s_wait_for_pod_ready() {
5050
sleep 1
5151
done
5252
}
53+
k8s_wait_for_uniq_pod() {
54+
echo_info "Waiting for pod '${@}' to be the only one running ... "
55+
local -i cnt=${TEST_MAX_WAIT_SEC:?}
56+
until [[ $(kubectl get pod "${@}" -ogo-template='{{.items|len}}') == 1 ]]; do
57+
((cnt=cnt-1)) || return 1
58+
sleep 1
59+
done
60+
k8s_wait_for_pod_ready "${@}"
61+
echo "Finished waiting"
62+
}
5363
k8s_wait_for_pod_gone() {
5464
echo_info "Waiting for pod '${@}' to be gone ... "
5565
local -i cnt=${TEST_MAX_WAIT_SEC:?}
@@ -243,4 +253,12 @@ test_kubeless_function() {
243253
esac
244254
make -sC examples ${func}-verify
245255
}
256+
257+
test_kubeless_function_update() {
258+
local func=${1:?} func_topic
259+
echo_info "UPDATE: $func"
260+
make -sC examples ${func}-update
261+
k8s_wait_for_uniq_pod -l function=${func}
262+
make -sC examples ${func}-update-verify
263+
}
246264
# vim: sw=4 ts=4 et si

tests/integration-tests.bats

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ load ../script/libtest
3737
# 'bats' lacks loop support, unroll-them-all ->
3838
@test "Test function: get-python" {
3939
test_kubeless_function get-python
40+
test_kubeless_function_update get-python
4041
}
4142
@test "Test function: get-nodejs" {
4243
test_kubeless_function get-nodejs

0 commit comments

Comments
 (0)