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 test functions and instructions to verify installation #5

Merged
merged 1 commit into from
Jan 2, 2020
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
50 changes: 38 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,50 @@ make build push

## Try it out

Test functions have been created to help you verify the installation of `nats-connector`. See [`contrib/test-functions`](./contrib/test-functions).

### Deploy on Kubernetes

The following instructions show how to run `kafka-connector` on Kubernetes.
The following instructions show how to run and test `nats-connector` on Kubernetes.

Deploy a function with a `topic` annotation:
1. Deploy the receiver functions, the receiver function must have the `topic` annotation:

```bash
export OPENFAAS_URL="http://localhost:8080" # Set your gateway
```bash
export OPENFAAS_URL="http://localhost:8080" # Set your gateway via env variable or the -g flag
faas-cli deploy --name receive-message --image openfaas/receive-message:latest --fprocess='./handler' --annotation topic="nats-test"
```

faas store deploy figlet --annotation topic="nats-test"
```
Alternatively, you can deploy with the `stack.yml` provided in this repo
```
cd contrib/test-functions
faas-cli deploy -f stack.yml
```

Deploy the connector with:
2. Deploy the connector with:

```bash
kubectl apply -f ./yaml/kubernetes/connector-dep.yml
```
```bash
kubectl apply -f ./yaml/kubernetes/connector-dep.yml
```

3. Now publish a message on the `nats-test` topic. We have provided a simple function to help do this

```bash
faas-cli deploy --name publish-message --image openfaas/publish-message:latest --fprocess='./handler' --env nats_url=nats://nats.openfaas:4222
```
If you used the `stack.yml` in step 1, then this function is already deployed.

Invoke the publisher
```bash
faas-cli invoke publish-message <<< "test message"
```

4. Verify that the receiver was invoked by checking the logs

```bash
faas-cli logs receive-message

Now publish a message on the `nats-test` topic.
2019-12-29T19:06:50Z 2019/12/29 19:06:50 received "test message"
```

If you want to configure the topic, then edit ./yaml/kubernetes/connector-dep.yaml
### Configuring the NATS topics
If you want to configure the topics that the connector listens to, then edit the `topics` env variable in [yaml/kubernetes/connector-dep.yaml](./yaml/kubernetes/connector-dep.yaml)
9 changes: 9 additions & 0 deletions contrib/test-functions/publish-message/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module publish-message

go 1.13

require (
github.com/nats-io/nats-server/v2 v2.1.2 // indirect
github.com/nats-io/nats.go v1.9.1
github.com/openfaas-incubator/go-function-sdk v0.0.0-20191017092257-70701da50a91
)
27 changes: 27 additions & 0 deletions contrib/test-functions/publish-message/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/nats-io/jwt v0.3.0 h1:xdnzwFETV++jNc4W1mw//qFyJGb2ABOombmZJQS4+Qo=
github.com/nats-io/jwt v0.3.0/go.mod h1:fRYCDE99xlTsqUzISS1Bi75UBJ6ljOJQOAAu5VglpSg=
github.com/nats-io/jwt v0.3.2 h1:+RB5hMpXUUA2dfxuhBTEkMOrYmM+gKIZYS1KjSostMI=
github.com/nats-io/jwt v0.3.2/go.mod h1:/euKqTS1ZD+zzjYrY7pseZrTtWQSjujC7xjPc8wL6eU=
github.com/nats-io/nats-server/v2 v2.1.2 h1:i2Ly0B+1+rzNZHHWtD4ZwKi+OU5l+uQo1iDHZ2PmiIc=
github.com/nats-io/nats-server/v2 v2.1.2/go.mod h1:Afk+wRZqkMQs/p45uXdrVLuab3gwv3Z8C4HTBu8GD/k=
github.com/nats-io/nats.go v1.9.1 h1:ik3HbLhZ0YABLto7iX80pZLPw/6dx3T+++MZJwLnMrQ=
github.com/nats-io/nats.go v1.9.1/go.mod h1:ZjDU1L/7fJ09jvUSRVBR2e7+RnLiiIQyqyzEE/Zbp4w=
github.com/nats-io/nkeys v0.1.0 h1:qMd4+pRHgdr1nAClu+2h/2a5F2TmKcCzjCDazVgRoX4=
github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
github.com/nats-io/nkeys v0.1.3 h1:6JrEfig+HzTH85yxzhSVbjHRJv9cn0p6n3IngIcM5/k=
github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/openfaas-incubator/go-function-sdk v0.0.0-20191017092257-70701da50a91 h1:18SEXx3EzxO9wdrcO+EKePNM0JCquzyLjiPYbgIfX7w=
github.com/openfaas-incubator/go-function-sdk v0.0.0-20191017092257-70701da50a91/go.mod h1:F37Kp+hwdHP+o3UKjkGzikQg4weKiMvcegT9vCQjvjE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e h1:D5TXcfTk7xF7hvieo4QErS3qqCB4teTffacDWr7CI+0=
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
57 changes: 57 additions & 0 deletions contrib/test-functions/publish-message/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package function

import (
"bytes"
"fmt"
"log"
"net/http"
"os"

nats "github.com/nats-io/nats.go"
handler "github.com/openfaas-incubator/go-function-sdk"
)

var (
subject = "nats-test"
defaultMessage = "Hello World"
)

// Handle a serverless request
func Handle(req handler.Request) (handler.Response, error) {
msg := defaultMessage
if len(req.Body) > 0 {
msg = string(bytes.TrimSpace(req.Body))
}

natsURL := nats.DefaultURL
val, ok := os.LookupEnv("nats_url")
if ok {
natsURL = val
}

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 %q to %q\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
}

return handler.Response{
Body: []byte(fmt.Sprintf("Sent %q", msg)),
StatusCode: http.StatusOK,
}, nil
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading