vmware-archive / runtimes

Kubeless function runtimes: https://kubeless.io/docs/runtimes/
Apache License 2.0
81 stars 89 forks source link

Payload from Trigger CronJob is not send to event.Data #102

Open richie-tt opened 4 years ago

richie-tt commented 4 years ago

Hi,

I noticed that event.Data is empty when it is triggered by payload of CronJob.

I create my test function:

kubeless function deploy test -n kbox --runtime go1.14 --from-file test.go --handler test.Handler --dependencies go.mod

source of file test.go

package kubeless

import (
    "fmt"
    "github.com/kubeless/kubeless/pkg/functions"
)

func Handler(event functions.Event, context functions.Context) (string, error) {
    fmt.Println("datais", event.Data)
    return event.Data, nil
}

then I created the Trigger cronjob: kubeless trigger cronjob create -n kbox test --function test -f test.json --schedule "*/1 * * * *" source of test.json

{"action":"createpod","data":{"name":"mypodname"}}

the logs from pod test-6747bdf7b6-mz96f show that event.Data is empty

2020/10/20 07:55:16 10.104.70.145:39188 "GET /healthz HTTP/1.1" 200 kube-probe/1.17+
2020/10/20 07:55:46 10.104.70.145:39306 "GET /healthz HTTP/1.1" 200 kube-probe/1.17+
datais 
2020/10/20 07:56:03 10.104.81.106:55296 "GET / HTTP/1.1" 200 curl/7.52.1
2020/10/20 07:56:16 10.104.70.145:39452 "GET /healthz HTTP/1.1" 200 kube-probe/1.17+
2020/10/20 07:56:46 10.104.70.145:39570 "GET /healthz HTTP/1.1" 200 kube-probe/1.17+
datais 
2020/10/20 07:57:03 10.104.81.106:55768 "GET / HTTP/1.1" 200 curl/7.52.1
2020/10/20 07:57:16 10.104.70.145:39716 "GET /healthz HTTP/1.1" 200 kube-probe/1.17+
2020/10/20 07:57:46 10.104.70.145:39834 "GET /healthz HTTP/1.1" 200 kube-probe/1.17+

Can You please give me tip what I am doing wrong here.

Thank you in advance.

andresmgot commented 4 years ago

Yes, nothing is wrong there. The cronjob trigger, by design, doesn't allow to modify the data to be sent, it's just a GET request sent in the given schedule. Note that since there is no interaction with the cronjob, any logic to obtain some data can be encoded in the function itself.

Let us know if you have some specific use case in mind for having this data.

richie-tt commented 4 years ago

Hi @andresmgot

Thank you for your answer.

I will put more description of my use case because the purpose of the test example wast only to show missing transfer data from Trigger Cron to Function.

In Kubeless I build a function that communicates with Kubernetes API and on-base what event.Data contain, it will create two different Pods.

I thought that Trigger Cronjob has implemented such functionality, according to the documentions or I miss something.

Thank you in advance for your reply

andresmgot commented 4 years ago

ah, sorry @1ricardo, you are correct, it's possible to set some data in the latest version of Kubeless. Are you running Kubeless v1.0.7 in your cluster? Also, is your trigger properly created? You can check it if you execute kubectl get cronjobtrigger -n kbox test -o yaml.

richie-tt commented 4 years ago

Hi @andresmgot

I checked what you about asked:

so to be sure that I use the latest version I check configmaps of kubeless

❯ kubectl get configmaps -n kubeless -o yaml
apiVersion: v1
items:
- apiVersion: v1
  data:
    builder-image: kubeless/function-image-builder:v1.0.7
   ...

and here is result of kubectl get cronjobtrigger -n kbox test -o yaml

❯ kubectl get cronjobtrigger -n kbox test -o yaml
apiVersion: kubeless.io/v1beta1
kind: CronJobTrigger
metadata:
  creationTimestamp: "2020-10-20T07:52:46Z"
  finalizers:
  - kubeless.io/cronjobtrigger
  generation: 2
  labels:
    created-by: kubeless
  name: test
  namespace: kbox
  resourceVersion: "23919434"
  selfLink: /apis/kubeless.io/v1beta1/namespaces/kbox/cronjobtriggers/test
  uid: b0ee8a64-8b36-47f0-ae92-b21eec5de46d
spec:
  function-name: test
  schedule: '*/1 * * * *'

br

andresmgot commented 4 years ago

Got it, I am able to reproduce it. It seems that the problem is that even if the CLI is able to parse the payload you are giving:

▶ kubeless trigger cronjob create foo --function get-python-37 -f f.json --dryrun --schedule '* * * * *' -o yaml            
apiVersion: kubeless.io/v1beta1
kind: CronJobTrigger
metadata:
  creationTimestamp: null
  labels:
    created-by: kubeless
  name: foo
  namespace: default
spec:
  function-name: get-python-37
  payload:
    f: b
  schedule: '* * * * *'

When creating it, in the cluster, it loses the payload property:

▶ kubeless trigger cronjob create foo --function get-python-37 -f f.json --schedule '* * * * *'                 
INFO[0000] Cronjob trigger foo created in namespace default successfully! 

▶ kubectl get cronjobtrigger foo -o yaml
apiVersion: kubeless.io/v1beta1
kind: CronJobTrigger
metadata:
  creationTimestamp: "2020-10-22T09:02:57Z"
  finalizers:
  - kubeless.io/cronjobtrigger
  generation: 2
  labels:
    created-by: kubeless
  name: foo
  namespace: default
  resourceVersion: "2643761"
  selfLink: /apis/kubeless.io/v1beta1/namespaces/default/cronjobtriggers/foo
  uid: fbd94c72-73df-4ea5-9b60-7c6a683c0ad9
spec:
  function-name: get-python-37
  schedule: '* * * * *'

Adding @delucca to the thread. You contributed this feature. Are you able to reproduce the issue? Would you mind taking a look?

delucca commented 4 years ago

@andresmgot here is the commit where I've added the payload to the kubeless trigger cronjob create command: kubeless#78d3ce

Since our workflow uses Gitops and we don't create resources directly from the CLI in our cluster, chances are that I forgot to test the deployment from the CLI directly to the cluster, and tested it only with the --dry-run flag. I think that must be an easy fix.

This week I'm deploying a new service, so I'll only be able to take a look at it in the weekend. If this issue is not time-sensitve I'll gladly fix it ;)

richie-tt commented 4 years ago

Thanks a lot, @delucca @andresm for taking the time and looking at this matter

richie-tt commented 4 years ago

Hi @delucca @andresmgot

I would like to ask you if there is any progress on that. I will really appreciate any information or you can give me a tip on what field I should add to cron job trigger in Kubernetes so I can move forward with my project.

Thank you in advance

BR

andresmgot commented 4 years ago

@1ricardo as a workaround, you can edit your trigger and add the payload field manually:

kubectl edit cronjobtrigger <name>
...
spec:
  function-name: test
  schedule: '*/1 * * * *'
  payload:
    foo: bar