Skip to content

Latest commit

 

History

History
148 lines (107 loc) · 5.05 KB

File metadata and controls

148 lines (107 loc) · 5.05 KB

Sample Function Go

Prerequisites

OpenFunction

You can refer to the Installation Guide to setup OpenFunction.

Run it locally

Build the function locally

pack build multiple-functions-go --builder openfunction/builder-go:v2.4.0-1.17 --env FUNC_NAME="MultipleFunctions"  --env FUNC_CLEAR_SOURCE=true

Run the function

docker run --rm --env="FUNC_CONTEXT={\"name\":\"MultipleFunctions\",\"version\":\"v1.0.0\",\"port\":\"8080\",\"runtime\":\"Knative\"}" --env="CONTEXT_MODE=self-host" --name multiple-functions-go -p 8080:8080 multiple-functions-go

Send a request

curl http://localhost:8080/bar
# hello, bar!

curl http://localhost:8080/foo
# {"hello":"foo!"}%  

Deployment

  1. Create secret

Generate a secret to access your container registry, such as one on Docker Hub or Quay.io. You can create this secret by editing the REGISTRY_SERVER, REGISTRY_USER and REGISTRY_PASSWORD fields in following command, and then run it.

REGISTRY_SERVER=https://index.docker.io/v1/ REGISTRY_USER=<your_registry_user> REGISTRY_PASSWORD=<your_registry_password>
kubectl create secret docker-registry push-secret \
    --docker-server=$REGISTRY_SERVER \
    --docker-username=$REGISTRY_USER \
    --docker-password=$REGISTRY_PASSWORD
  1. Create function

    For sample function below, modify the spec.image field in function-sample.yaml to your own container registry address:

    apiVersion: core.openfunction.io/v1beta2
    kind: Function
    metadata:
      name: function-sample
    spec:
      image: "<your registry name>/sample-go-multi-func:latest"

    Use the following command to create this Function:

    kubectl apply -f function-sample.yaml
  2. Access function

    You can observe the process of a function with the following command:

    kubectl get functions.core.openfunction.io
    
    NAME              BUILDSTATE   SERVINGSTATE   BUILDER         SERVING         ADDRESS                                              AGE
    function-sample   Succeeded    Running        builder-jgnzp   serving-q6wdp   http://function-sample.default.svc.cluster.local/    22m

    The Function.status.addresses field provides various methods for accessing functions. Get Function addresses by running following command:

    kubectl get function function-sample -o=jsonpath='{.status.addresses}'

    You will get the following address:

    [{"type":"External","value":"http://function-sample.default.ofn.io/"},
    {"type":"Internal","value":"http://function-sample.default.svc.cluster.local/"}]

    You can use the following command to create a pod in the cluster and access the function from the pod:

    kubectl run curl --image=radial/busyboxplus:curl -i --tty

    Access functions by the internal address:

    [ root@curl:/ ]$ curl http://function-sample.default.svc.cluster.local/foo
    {"hello":"foo!"}%

    Access functions by the external address:

    To access the function via the Address of type External in Funtion.status, you should configure local domain first, see Configure Local Domain.

    [ root@curl:/ ]$ curl http://function-sample.default.ofn.io/bar
    hello, bar!

    There is also an alternative way to trigger the function via the access address provided by the Knative Services:

    kubectl get ksvc
     
    NAME                       URL                                                            LATESTCREATED                   LATESTREADY                     READY   REASON
    serving-q6wdp-ksvc-wk6mv   http://serving-q6wdp-ksvc-wk6mv.default.<external-ip>.sslip.io   serving-q6wdp-ksvc-wk6mv-v100   serving-q6wdp-ksvc-wk6mv-v100   True

    Or get the service address directly with the following command:

    where<external-ip>indicates the external address of your gateway service.

    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.

    kubectl patch svc -n kourier-system kourier \
      -p '{"spec": {"type": "LoadBalancer", "externalIPs": ["1.2.3.4"]}}'
    
    kubectl patch configmap/config-domain -n knative-serving \
      --type merge --patch '{"data":{"1.2.3.4.sslip.io":""}}'
    kubectl get ksvc serving-q6wdp-ksvc-wk6mv -o jsonpath={.status.url}
     
    http://serving-q6wdp-ksvc-wk6mv.default.<external-ip>.sslip.io

    Access the above service address via commands such as curl:

    curl http://serving-q6wdp-ksvc-wk6mv.default.<external-ip>.sslip.io/foo
     
    {"hello":"foo!"}%
    
    curl http://serving-q6wdp-ksvc-wk6mv.default.<external-ip>.sslip.io/bar
     
    hello, bar!