|
| 1 | +# Path Parameters Function Go |
| 2 | + |
| 3 | +## Prerequisites |
| 4 | + |
| 5 | +### OpenFunction |
| 6 | + |
| 7 | +You can refer to the [Installation Guide](https://github.com/OpenFunction/OpenFunction#install-openfunction) to setup OpenFunction. |
| 8 | + |
| 9 | +## Run it locally |
| 10 | + |
| 11 | +Build the function locally |
| 12 | + |
| 13 | +```sh |
| 14 | +pack build sample-go-path-params-func --builder openfunction/builder-go:v2.4.0-1.17 --env FUNC_NAME="pathParametersFunction" --env FUNC_CLEAR_SOURCE=true |
| 15 | +``` |
| 16 | + |
| 17 | +Run the function |
| 18 | + |
| 19 | +```sh |
| 20 | +docker compose up |
| 21 | +``` |
| 22 | + |
| 23 | +Send a request |
| 24 | + |
| 25 | +```sh |
| 26 | + |
| 27 | +# http |
| 28 | +curl -X POST "http://localhost:8080/hello/openfunction" |
| 29 | +# {"hello":"openfunction"}% |
| 30 | + |
| 31 | +# cloudevent |
| 32 | +curl -X POST "http://localhost:8080/foo/openfunction" \ |
| 33 | + -H "Content-Type: application/cloudevents+json" \ |
| 34 | + -d '{"specversion":"1.0","type":"dev.knative.samples.helloworld","source":"dev.knative.samples/helloworldsource","id":"536808d3-88be-4077-9d7a-a3f162705f79","data":{"data":"hello"}}' |
| 35 | +# in docker compose terminal: |
| 36 | +# cloudevent - Data: {"{\"data\":\"hello\"}":"openfunction"} |
| 37 | + |
| 38 | + |
| 39 | +# http |
| 40 | +curl -X POST "http://localhost:8080/bar/openfunction" \ |
| 41 | + -d 'hello' |
| 42 | +# {"hello":"openfunction"}% |
| 43 | + |
| 44 | +# Structured CloudEvent |
| 45 | +curl -X POST "http://localhost:8080/bar/openfunction" \ |
| 46 | + -H "Content-Type: application/cloudevents+json" \ |
| 47 | + -d '{"specversion":"1.0","type":"dev.knative.samples.helloworld","source":"dev.knative.samples/helloworldsource","id":"536808d3-88be-4077-9d7a-a3f162705f79","data":{"data":"hello"}}' |
| 48 | +# {"{\"data\":\"hello\"}":"openfunction"}% |
| 49 | + |
| 50 | +# Binary CloudEvent |
| 51 | +curl "http://localhost:8080/bar/openfunction" \ |
| 52 | + -X POST \ |
| 53 | + -H "Ce-Specversion: 1.0" \ |
| 54 | + -H "Ce-Type: dev.knative.samples.helloworld" \ |
| 55 | + -H "Ce-Source: dev.knative.samples/helloworldsource" \ |
| 56 | + -H "Ce-Subject: 123" \ |
| 57 | + -H "Ce-Id: 536808d3-88be-4077-9d7a-a3f162705f79" \ |
| 58 | + -H "Content-Type: application/json" \ |
| 59 | + -d '{"data":"hello"}' |
| 60 | +# {"{\"data\":\"hello\"}":"openfunction"}% |
| 61 | +``` |
| 62 | + |
| 63 | + |
| 64 | +## Deployment |
| 65 | + |
| 66 | +1. Create secret |
| 67 | + |
| 68 | +Generate a secret to access your container registry, such as one on [Docker Hub](https://hub.docker.com/) or [Quay.io](https://quay.io/). |
| 69 | +You can create this secret by editing the ``REGISTRY_SERVER``, ``REGISTRY_USER`` and ``REGISTRY_PASSWORD`` fields in following command, and then run it. |
| 70 | + |
| 71 | + ```bash |
| 72 | + REGISTRY_SERVER=https://index.docker.io/v1/ REGISTRY_USER=<your_registry_user> REGISTRY_PASSWORD=<your_registry_password> |
| 73 | + kubectl create secret docker-registry push-secret \ |
| 74 | + --docker-server=$REGISTRY_SERVER \ |
| 75 | + --docker-username=$REGISTRY_USER \ |
| 76 | + --docker-password=$REGISTRY_PASSWORD |
| 77 | + ``` |
| 78 | + |
| 79 | +2. Create function |
| 80 | + |
| 81 | + For sample function below, modify the ``spec.image`` field in ``function-sample.yaml`` to your own container registry address: |
| 82 | + |
| 83 | + ```yaml |
| 84 | + apiVersion: core.openfunction.io/v1beta1 |
| 85 | + kind: Function |
| 86 | + metadata: |
| 87 | + name: function-sample |
| 88 | + spec: |
| 89 | + image: "<your registry name>/sample-go-path-params-func:latest" |
| 90 | + ``` |
| 91 | +
|
| 92 | + Use the following command to create this Function: |
| 93 | +
|
| 94 | + ```shell |
| 95 | + kubectl apply -f function-sample.yaml |
| 96 | + ``` |
| 97 | + |
| 98 | +3. Access function |
| 99 | + |
| 100 | + You can observe the process of a function with the following command: |
| 101 | + |
| 102 | + ```shell |
| 103 | + kubectl get functions.core.openfunction.io |
| 104 | + |
| 105 | + NAME BUILDSTATE SERVINGSTATE BUILDER SERVING URL AGE |
| 106 | + function-sample Succeeded Running builder-jgnzp serving-gsx8g http://function-sample.default.svc.cluster.local/ 56s |
| 107 | + ``` |
| 108 | + |
| 109 | + The `Function.status.addresses` field provides various methods for accessing functions. |
| 110 | + Get `Function` addresses by running following command: |
| 111 | + ```shell |
| 112 | + kubectl get function function-sample -o=jsonpath='{.status.addresses}' |
| 113 | + ``` |
| 114 | + You will get the following address: |
| 115 | + ```json |
| 116 | + [{"type":"External","value":"http://function-sample.default.ofn.io/"}, |
| 117 | + {"type":"Internal","value":"http://function-sample.default.svc.cluster.local/"}] |
| 118 | + ``` |
| 119 | + |
| 120 | + > You can use the following command to create a pod in the cluster and access the function from the pod: |
| 121 | + > |
| 122 | + > ```shell |
| 123 | + > kubectl run curl --image=radial/busyboxplus:curl -i --tty |
| 124 | + > ``` |
| 125 | + Access functions by the internal address: |
| 126 | + ```shell |
| 127 | + [ root@curl:/ ]$ curl http://function-sample.default.svc.cluster.local/hello/openfunction |
| 128 | + {"hello":"openfunction"}% |
| 129 | + ``` |
| 130 | + |
| 131 | + Access functions by the external address: |
| 132 | + > To access the function via the Address of type `External` in `Funtion.status`, you should configure local domain first, see [Configure Local Domain](https://openfunction.dev/docs/concepts/networking/local-domain). |
| 133 | + ```shell |
| 134 | + [ root@curl:/ ]$ curl http://function-sample.default.ofn.io/hello/openfunction |
| 135 | + {"hello":"openfunction"}% |
| 136 | + ``` |
| 137 | + |
| 138 | + There is also an alternative way to trigger the function via the access address provided by the Knative Services: |
| 139 | + |
| 140 | + ```shell |
| 141 | + kubectl get ksvc |
| 142 | + |
| 143 | + NAME URL LATESTCREATED LATESTREADY READY REASON |
| 144 | + serving-gsx8g-ksvc-6fv9l http://serving-gsx8g-ksvc-6fv9l.default.<external-ip>.sslip.io serving-gsx8g-ksvc-6fv9l-v100 serving-gsx8g-ksvc-6fv9l-v100 True |
| 145 | + ``` |
| 146 | + |
| 147 | + Or get the service address directly with the following command: |
| 148 | + |
| 149 | + > where` <external-ip> `indicates the external address of your gateway service. |
| 150 | + > |
| 151 | + > You can do a simple configuration to use the node ip as the `<external-ip>` as follows (Assuming you are using Kourier as network layer of Knative). Where `1.2.3.4` can be replaced by your node ip. |
| 152 | + > |
| 153 | + > ```shell |
| 154 | + > kubectl patch svc -n kourier-system kourier \ |
| 155 | + > -p '{"spec": {"type": "LoadBalancer", "externalIPs": ["1.2.3.4"]}}' |
| 156 | + > |
| 157 | + > kubectl patch configmap/config-domain -n knative-serving \ |
| 158 | + > --type merge --patch '{"data":{"1.2.3.4.sslip.io":""}}' |
| 159 | + > ``` |
| 160 | + |
| 161 | + ```shell |
| 162 | + kubectl get ksvc serving-gsx8g-ksvc-6fv9l -o jsonpath={.status.url} |
| 163 | + |
| 164 | + http://serving-gsx8g-ksvc-6fv9l.default.<external-ip>.sslip.io |
| 165 | + ``` |
| 166 | + |
| 167 | + Access the above service address via commands such as ``curl``: |
| 168 | + |
| 169 | + ```shell |
| 170 | + curl http://serving-gsx8g-ksvc-6fv9l.default.<external-ip>.sslip.io/hello/openfunction |
| 171 | + |
| 172 | + {"hello":"openfunction"}% |
| 173 | + ``` |
0 commit comments