Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example Pod to publish some tester messages #4

Closed
alexellis opened this issue Dec 27, 2019 · 26 comments · Fixed by #5
Closed

Add example Pod to publish some tester messages #4

alexellis opened this issue Dec 27, 2019 · 26 comments · Fixed by #5
Assignees
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@alexellis
Copy link
Member

alexellis commented Dec 27, 2019

Description

There is no way included with the repo to test this connector, create a Go application and an example Pod that will publish some test messages. An OpenFaaS function using the classic Go template would also be fine.

Also change faas-request to another topic (nats-test?) so that it doesn't clash with the built-in NATS Streaming topic.

Update the README with how to test / use the connector.

Other connectors with examples:

This should be an absolute minimal example, simply to publish the input on the topic.

@alexellis alexellis added good first issue Good for newcomers help wanted Extra attention is needed labels Dec 27, 2019
@LucasRoesler
Copy link
Member

Derek assign: me

@alexellis
Copy link
Member Author

It would be /assign: me

@LucasRoesler
Copy link
Member

@alexellis i started on this in a separate repo (we can fold it in once it is correct) but I am not sure it is working as it should: https://github.com/LucasRoesler/nats-connector-example

When I set asynchronous_invocation: false the nats-connector logs show

2019/12/28 22:39:20 Received topic: faas-req, message: Hello World
2019/12/28 22:39:20 Invoke function: pycho.openfaas-fn
2019/12/28 22:39:20 connector-sdk got result: [404] faas-req => pycho.openfaas-fn (40) bytes
[404] faas-req => pycho.openfaas-fn
Cannot find service: pycho.openfaas-fn.

But, as you can see, the svc is there

$ openfaas kubectl get svc -n openfaas-fn
NAME    TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
echo    ClusterIP   10.103.93.33    <none>        8080/TCP   3d10h
env     ClusterIP   10.98.222.110   <none>        8080/TCP   3d10h
pycho   ClusterIP   10.103.2.188    <none>        8080/TCP   3d9h

When I set asynchronous_invocation: true I get

2019/12/28 22:45:18 Received topic: faas-req, message: Hello World
2019/12/28 22:45:18 Invoke function: pycho.openfaas-fn
2019/12/28 22:45:18 connector-sdk got result: [202] faas-req => pycho.openfaas-fn (0) bytes
[202] faas-req => pycho.openfaas-fn

Obviouslly in both cases I do not see any invocations of my function, the logs are completely empty

I have already verified that the function is deployed and can be called both directly and async. The function works 100% as expected.

I have built and pushed an image, so the instructions in the attached repo should work without needing to clone it.

@alexellis
Copy link
Member Author

Thank you for writing the producer.

Interesting error. I wonder if it's related to multiple namespaces? Is this the latest connector-sdk? @viveksyngh can you also take a look please?

@LucasRoesler
Copy link
Member

@alexellis and @viveksyngh i have updated the example/test repo to be pure openfaas functions now, see this branch https://github.com/LucasRoesler/nats-connector-example/tree/test-with-listener. I think this is easier to actually use for testing/verifying the connector behavior. Of course it still has the same exact issue as before, i.e. Cannot find service: republish.openfaas-fn

@alexellis
Copy link
Member Author

How are you deploying OpenFaaS @LucasRoesler?

@LucasRoesler
Copy link
Member

This was tested in KinD and in GKE. Both deployed using the latest helm chart with the default values.

@alexellis
Copy link
Member Author

Ok, Kubernetes 👍

@viveksyngh
Copy link
Contributor

I tried running these test function with a fresh OpenFaaS installation on my local desktop.
This is what I get from in the logs from each of the service,

kubectl logs pod/nats-connector-7d449d7696-4v4tr -n openfaas
2019/12/29 17:11:30 Configured topics: [faas-req]
2019/12/29 17:11:30 Binding to topic: faas-req
2019/12/29 17:12:11 Received topic: faas-req, message: test message
2019/12/29 17:12:11 Invoke function: republish.openfaas-fn
[202] faas-req => republish.openfaas-fn
2019/12/29 17:12:11 connector-sdk got result: [202] faas-req => republish.openfaas-fn (0) bytes

➜  nats-connector-example git:(test-with-listener) kubectl logs pod/republish-7d688cf9d-vn7b4 -n openfaas-fn
Forking - ./handler []
2019/12/29 17:10:54 Started logging stderr from function.
2019/12/29 17:10:54 Started logging stdout from function.
2019/12/29 17:10:54 OperationalMode: http
2019/12/29 17:10:54 Timeouts: read: 10s, write: 10s hard: 10s.
2019/12/29 17:10:54 Listening on port: 8080
2019/12/29 17:10:54 Writing lock-file to: /tmp/.lock
2019/12/29 17:10:54 Metrics listening on port: 8081
2019/12/29 17:12:11 stderr: 2019/12/29 17:12:11 Connecting to: nats://nats.openfaas:4222
2019/12/29 17:12:11 stderr: 2019/12/29 17:12:11 Publishing "test message" to: faas-resp
2019/12/29 17:12:11 POST / - 200 OK - ContentLength: 59
➜  nats-connector-example git:(test-with-listener) kubectl logs pod/connector-test-79db659c89-jdkth  -n openfaas-fn
2019/12/29 17:10:42 Started logging stderr from function.
2019/12/29 17:10:42 Started logging stdout from function.
2019/12/29 17:10:42 OperationalMode: http
2019/12/29 17:10:42 Timeouts: read: 10s, write: 10s hard: 10s.
2019/12/29 17:10:42 Listening on port: 8080
2019/12/29 17:10:42 Writing lock-file to: /tmp/.lock
2019/12/29 17:10:42 Metrics listening on port: 8081
Forking - ./handler []
2019/12/29 17:12:11 stderr: 2019/12/29 17:12:11 Sending "test message" to "faas-req"
2019/12/29 17:12:11 stderr: 2019/12/29 17:12:11 Waiting for response
2019/12/29 17:12:11 POST / - 200 OK - ContentLength: 8

Looks like republish function is getting the message from nats queue but not able to republish. Are functions getting timeouts before they can do their work ?

@viveksyngh
Copy link
Contributor

I even tried the function present in the master branch instruction and I can see the function is getting called.

2019/12/29 17:46:11 POST / - 200 OK - ContentLength: 11
2019/12/29 17:46:16 stdout: 2019-12-29 17:46:16,131:DEBUG:host=pycho.openfaas-fn.svc.cluster.local:8080
2019/12/29 17:46:16 stdout: 2019-12-29 17:46:16,132:DEBUG:path=/
2019/12/29 17:46:16 stdout: 2019-12-29 17:46:16,132:DEBUG:headers=Host=pycho.openfaas-fn.svc.cluster.local:8080
2019/12/29 17:46:16 stdout: 2019-12-29 17:46:16,132:DEBUG:headers=User-Agent=Go-http-client/1.1
2019/12/29 17:46:16 stdout: 2019-12-29 17:46:16,132:DEBUG:headers=Transfer-Encoding=chunked
2019/12/29 17:46:16 stdout: 2019-12-29 17:46:16,132:DEBUG:headers=Accept-Encoding=gzip
2019/12/29 17:46:16 stdout: 2019-12-29 17:46:16,132:DEBUG:headers=X-Call-Id=263c495b-618f-4cbb-b8bb-a4ea2770e16f
2019/12/29 17:46:16 stdout: 2019-12-29 17:46:16,134:DEBUG:headers=X-Forwarded-For=10.1.1.36:52950
2019/12/29 17:46:16 stdout: 2019-12-29 17:46:16,135:DEBUG:headers=X-Forwarded-Host=gateway.openfaas.svc.cluster.local:8080
2019/12/29 17:46:16 stdout: 2019-12-29 17:46:16,135:DEBUG:headers=X-Start-Time=1577641339194154108,1577641576130160374
2019/12/29 17:46:16 stdout: 2019-12-29 17:46:16,136:DEBUG:body=Hello World
2019/12/29 17:46:16 stdout: 2019-12-29 17:46:16,139:INFO:127.0.0.1 - - [29/Dec/2019 17:46:16] "POST / HTTP/1.1" 200 -
2019/12/29 17:46:16 POST / - 200 OK - ContentLength: 11

@viveksyngh
Copy link
Contributor

There seems to issue with invocation count not getting updated but I confirm from the logs that functions are getting called.

@alexellis
Copy link
Member Author

That seems to be working as expected. What about without the async invoke?

How did you do your install of OpenFaaS Vivek?

@viveksyngh
Copy link
Contributor

viveksyngh commented Dec 29, 2019

I tried without async invoke as well. Seems to be working for me.

Here is nats connector log.

2019/12/29 17:53:11 Received topic: faas-req, message: Hello World
[200] faas-req => republish.openfaas-fn
The msg: "Hello World" has been republished to "faas-resp"
2019/12/29 17:53:11 Invoke function: connector-test.openfaas-fn
2019/12/29 17:53:11 connector-sdk got result: [200] faas-req => republish.openfaas-fn (58) bytes
2019/12/29 17:53:16 Invoke function: pycho.openfaas-fn
2019/12/29 17:53:16 connector-sdk got result: [500] faas-req => connector-test.openfaas-fn (42) bytes
[500] faas-req => connector-test.openfaas-fn
failed waiting for response: nats: timeout
2019/12/29 17:53:16 Invoke function: republish.openfaas-fn
2019/12/29 17:53:16 connector-sdk got result: [200] faas-req => pycho.openfaas-fn (11) bytes
[200] faas-req => pycho.openfaas-fn
Hello World

@viveksyngh
Copy link
Contributor

I installed using helm chart. This is what I used for the installation

helm upgrade openfaas --install openfaas/openfaas \
    --namespace openfaas  \
    --set basic_auth=true \
    --set functionNamespace=openfaas-fn \
    --set clusterRole=true 

@alexellis
Copy link
Member Author

alexellis commented Dec 29, 2019

I deployed with k3sup and k3d (k3s with k8s 1.16 I got this error when running the invoke sample after a successful faas-cli deploy:

kosmos:nats-connector-example alex$ faas-cli invoke connector-test <<< "test message"
Handling connection for 8080
Server returned unexpected status code: 500 - failed waiting for response: nats: timeout
kosmos:nats-connector-example alex$ faas-cli invoke connector-test <<< "test message"
Handling connection for 8080
Server returned unexpected status code: 500 - failed waiting for response: nats: timeout
kosmos:nats-connector-example alex$ 

For the functions @LucasRoesler it might be more intuitive to have something like this:

functions:
  publish-message:
  # Simply publishes a message

  receive-message:
  # Prints a message to its logs

I was a bit confused by the names connector-test and republish

The example for Kafka is reasonably straight-forward: https://github.com/openfaas-incubator/kafka-connector#try-it-out - fortunately they already provided a tester that we could run.

@viveksyngh
Copy link
Contributor

connector-test is publishing a message on faas-req and waiting for some message on faas-resp.

republish is getting invoked on faas-req message and publishing the same to faas-resp.

In the end connector-test is matching the published message to faas-req to the message received from faas-resp.

The error message is while connector-test is waiting for message on faas-resp

@alexellis
Copy link
Member Author

Strange.. the YAML file says faas-req,, but the container outputs as following, as if it ignored the env-var.

kubectl logs -n openfaas deploy/nats-connector
2019/12/29 18:07:08 Configured topics: [faas-request]
2019/12/29 18:07:08 Binding to topic: faas-request

alexellis added a commit that referenced this issue Dec 29, 2019
* Changes the example to use a completely different topic to
prevent confusion with the NATS Streaming topic that OpenFaaS
uses for async invocations.

Ref:

#4

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
@alexellis
Copy link
Member Author

Basically, what I was looking for was the equivalent of the following from @LucasRoesler' connector-test:

	nc, err := nats.Connect(natsURL)
	if err != nil {
		r := handler.Response{
			Body:       []byte(fmt.Sprintf("can not connect to nats: %s", err)),
			StatusCode: http.StatusInternalServerError,
		}
		return r, err
	}
	defer nc.Close()

	log.Printf("Sending \"%s\" to \"%s\"\n", msg, subject)
	err = nc.Publish(subject, []byte(msg))
	if err != nil {
		log.Println(err)
		r := handler.Response{
			Body:       []byte(fmt.Sprintf("can not publish to nats: %s", err)),
			StatusCode: http.StatusInternalServerError,
		}
		return r, err
	}

Also, if you're not aware, \"%s\" can be written as %q, and you'll get the same effect.

Feel free to keep your repo, but would you be up for contributing the equivalent of above as a function into the repo? Might be best to use the legacy go template, or if an external template adds a lot of value, then the stack.yml file can now accept a list of templates to pull. When deploying I got an error and either of those options would make the experience a bit better. The CLI feature is relatively new, added by @martindekov.

@alexellis
Copy link
Member Author

Please also see this change: dd4fdb0

@LucasRoesler
Copy link
Member

Not sure what happened, but it is working now. In the test-with-listener branch of my example repo

$ kind create cluster
$ kubectl apply -f https://raw.githubusercontent.com/openfaas/faas-netes/master/namespaces.yml
$ helm upgrade openfaas --install openfaas/openfaas \
    --namespace openfaas  \
    --set functionNamespace=openfaas-fn \
    --set clusterRole=true \
    --set generateBasicAuth=true

$ kubectl apply -f https://raw.githubusercontent.com/LucasRoesler/nats-connector-example/master/yaml/connector-dep.yaml
$ kubectl port-forward -n openfaas svc/gateway 8080:8080 &
$ faas-cli login --password $(kubectl -n openfaas get secret basic-auth -o jsonpath="{.data.basic-auth-password}" | base64 --decode)
$ faas-cli deploy 

$ faas-cli invoke connector-test <<< "test message"
Success!

$ faas-cli logs connector-test
2019-12-29T19:06:50Z 2019/12/29 19:06:50 stderr: 2019/12/29 19:06:50 Sending "test message" to "faas-req"
2019-12-29T19:06:50Z 2019/12/29 19:06:50 stderr: 2019/12/29 19:06:50 Waiting for response
2019-12-29T19:06:50Z 2019/12/29 19:06:50 POST / - 200 OK - ContentLength: 8

$ faas-cli logs republish 
2019-12-29T19:05:46Z Forking - ./handler []
2019-12-29T19:05:46Z 2019/12/29 19:05:46 Started logging stderr from function.
2019-12-29T19:05:46Z 2019/12/29 19:05:46 Started logging stdout from function.
2019-12-29T19:05:46Z 2019/12/29 19:05:46 OperationalMode: http
2019-12-29T19:05:46Z 2019/12/29 19:05:46 Timeouts: read: 10s, write: 10s hard: 10s.
2019-12-29T19:05:46Z 2019/12/29 19:05:46 Listening on port: 8080
2019-12-29T19:05:46Z 2019/12/29 19:05:46 Writing lock-file to: /tmp/.lock
2019-12-29T19:05:46Z 2019/12/29 19:05:46 Metrics listening on port: 8081
2019-12-29T19:06:50Z 2019/12/29 19:06:50 stderr: 2019/12/29 19:06:50 Connecting to: nats://nats.openfaas:4222
2019-12-29T19:06:50Z 2019/12/29 19:06:50 POST / - 200 OK - ContentLength: 59
2019-12-29T19:06:50Z 2019/12/29 19:06:50 stderr: 2019/12/29 19:06:50 Publishing "test message" to: faas-resp

@LucasRoesler
Copy link
Member

@alexellis i can rename the functions but calling one publisher and one receiver isn't really accurate, unless you want me to remove the check that the correct message was received. If we do that, then we need to give instructions on how to do that actual verification via the faas-cli logs. My original intent was to reduce the number of steps required to test the connector. With the current design, it is only two commands faas-cli deploy and faas-cli invoke. But I do agree that the functions should be renamed to something else.

To your comment about an external template, I don't understand what you mean by external templates, i am just using the golang-http template from the store. I found it easier to write, especially for logging output. Also, I would prefer to use templates that use of-watchdog, in general. Especially since people may base their own code on this example, we should use a template that we want to encourage use of.

To your comment about dd4fdb0 , I am not sure why your test attempt used the faas-request but that name does not exist in the example code, it should be using faas-req. But changing to nats-test is easy enough

@alexellis
Copy link
Member Author

Just looking for a piece of code to publish a message and a function that receives it. No need for the full-duplex or send/receive etc for this repo, but feel free to keep the separate repo.

I was just looking for 5-10 lines of code that could run in cluster and trigger the connector, sorry if I didn't explain that well enough in the issue.

@LucasRoesler
Copy link
Member

I will rewrite it

@LucasRoesler
Copy link
Member

@alexellis i rewrote that branch again, it is just a publish-message function and a receive-message function that use the nats-test topic. These instructions should work now

  1. Install OpenFaaS in k8s with basic auth enabled

    helm upgrade openfaas --install openfaas/openfaas \
        --namespace openfaas  \
        --set functionNamespace=openfaas-fn \
        --set clusterRole=true \
        --set generateBasicAuth=true 
  2. Deploy the nats-connector with it configured to listen to the nats-test topic

    kubectl apply -f https://raw.githubusercontent.com/LucasRoesler/nats-connector-example/test-with-listener/yaml/connector-dep.yaml
  3. Deploy the pub/sub functions

    faas-cli deploy -f stack.yml

    Alternatively, this should also work

    faas-cli deploy --name publish-message --image theaxer/publish-message:latest --fprocess='./handler' --env nats_url=nats://nats.openfaas:4222
    faas-cli deploy --name receive-message --image theaxer/receive-message:latest --fprocess='./handler' --annotation topic="nats-test"
  4. Trigger a test message

    faas-cli invoke publish-message <<< "test message"
  5. Verify that the receiver was invoked

    faas-cli logs receive-message

@burtonr
Copy link

burtonr commented Dec 31, 2019

I was able to confirm with the instructions @LucasRoesler posted, that the publish and receive work as expected.

  • Logs from the receive-message function
2019/12/31 04:26:47 Listening on port: 8080
2019/12/31 04:26:47 Writing lock-file to: /tmp/.lock
2019/12/31 04:26:47 Metrics listening on port: 8081
2019/12/31 04:26:58 POST / - 200 OK - ContentLength: 28
2019/12/31 04:26:58 stderr: 2019/12/31 04:26:58 received "test message"

For what it's worth, this was tested with:

  • Azure Kubernetes Service
  • K8s v1.12.8
  • OpenFaaS Operator 0.9.9
  • OpenFaaS Gateway 0.17.0
    a bit out-of-date I know, but it's my stable setup that "just works"

@LucasRoesler
Copy link
Member

Awesome, I will create a PR

LucasRoesler added a commit to LucasRoesler/nats-connector that referenced this issue Dec 31, 2019
**What**
- Add test publisher and receiver functions and instructions for how to
  use them to test the installations of the `nats-connector`. This
  should make the installation easier for users.

Resolves openfaas#4

Signed-off-by: Lucas Roesler <[email protected]>
alexellis pushed a commit that referenced this issue Jan 2, 2020
**What**
- Add test publisher and receiver functions and instructions for how to
  use them to test the installations of the `nats-connector`. This
  should make the installation easier for users.

Resolves #4

Signed-off-by: Lucas Roesler <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants