Prerequisites

Registry Credential

When building a function, you’ll need to push your function container image to a container registry like Docker Hub or Quay.io. To do that you’ll need to generate a secret for your container registry first.

You can create this secret by filling in the REGISTRY_SERVER, REGISTRY_USER and REGISTRY_PASSWORD fields, and then run the following command.

  1. REGISTRY_SERVER=https://index.docker.io/v1/
  2. REGISTRY_USER=<your_registry_user>
  3. REGISTRY_PASSWORD=<your_registry_password>
  4. kubectl create secret docker-registry push-secret \
  5. --docker-server=$REGISTRY_SERVER \
  6. --docker-username=$REGISTRY_USER \
  7. --docker-password=$REGISTRY_PASSWORD

Source repository Credential

If your source code is in a private git repository, you’ll need to create a secret containing the private git repo’s username and password:

  1. USERNAME=<your_git_username>
  2. PASSWORD=<your_git_password>
  3. kubectl create secret generic git-repo-secret \
  4. --username=$USERNAME \
  5. --password=$USERNAME

You can then reference this secret in the Function CR’s spec.build.srcRepo.credentials

  1. apiVersion: core.openfunction.io/v1beta1
  2. kind: Function
  3. metadata:
  4. name: function-sample
  5. spec:
  6. version: "v2.0.0"
  7. image: "openfunctiondev/sample-go-func:v1"
  8. imageCredentials:
  9. name: push-secret
  10. port: 8080 # default to 8080
  11. build:
  12. builder: openfunction/builder-go:latest
  13. env:
  14. FUNC_NAME: "HelloWorld"
  15. FUNC_CLEAR_SOURCE: "true"
  16. srcRepo:
  17. url: "https://github.com/OpenFunction/samples.git"
  18. sourceSubPath: "functions/knative/hello-world-go"
  19. revision: "main"
  20. credentials:
  21. name: git-repo-secret
  22. serving:
  23. template:
  24. containers:
  25. - name: function # DO NOT change this
  26. imagePullPolicy: IfNotPresent
  27. runtime: "knative"

Kafka

Async functions can be triggered by events in message queues like Kafka, here you can find steps to setup a Kafka cluster for demo purpose.

  1. Install strimzi-kafka-operator in the default namespace.

    1. helm repo add strimzi https://strimzi.io/charts/
    2. helm install kafka-operator -n default strimzi/strimzi-kafka-operator
  2. Run the following command to create a Kafka cluster and Kafka Topic in the default namespace. The Kafka and Zookeeper clusters created by this command have a storage type of ephemeral and are demonstrated using emptyDir.

    Here we create a 1-replica Kafka server named <kafka-server> and a 1-replica topic named <kafka-topic> with 10 partitions

    1. cat <<EOF | kubectl apply -f -
    2. apiVersion: kafka.strimzi.io/v1beta2
    3. kind: Kafka
    4. metadata:
    5. name: <kafka-server>
    6. namespace: default
    7. spec:
    8. kafka:
    9. version: 3.1.0
    10. replicas: 1
    11. listeners:
    12. - name: plain
    13. port: 9092
    14. type: internal
    15. tls: false
    16. - name: tls
    17. port: 9093
    18. type: internal
    19. tls: true
    20. config:
    21. offsets.topic.replication.factor: 1
    22. transaction.state.log.replication.factor: 1
    23. transaction.state.log.min.isr: 1
    24. default.replication.factor: 1
    25. min.insync.replicas: 1
    26. inter.broker.protocol.version: "3.1"
    27. storage:
    28. type: ephemeral
    29. zookeeper:
    30. replicas: 1
    31. storage:
    32. type: ephemeral
    33. entityOperator:
    34. topicOperator: {}
    35. userOperator: {}
    36. ---
    37. apiVersion: kafka.strimzi.io/v1beta2
    38. kind: KafkaTopic
    39. metadata:
    40. name: <kafka-topic>
    41. namespace: default
    42. labels:
    43. strimzi.io/cluster: <kafka-server>
    44. spec:
    45. partitions: 10
    46. replicas: 1
    47. config:
    48. retention.ms: 7200000
    49. segment.bytes: 1073741824
    50. EOF
  3. Run the following command to check Pod status and wait for Kafka and Zookeeper to run and start.

    1. $ kubectl get po
    2. NAME READY STATUS RESTARTS AGE
    3. <kafka-server>-entity-operator-568957ff84-nmtlw 3/3 Running 0 8m42s
    4. <kafka-server>-kafka-0 1/1 Running 0 9m13s
    5. <kafka-server>-zookeeper-0 1/1 Running 0 9m46s
    6. strimzi-cluster-operator-687fdd6f77-cwmgm 1/1 Running 0 11m

    Run the following command to view the metadata for the Kafka cluster.

    1. $ kafkacat -L -b <kafka-server>-kafka-brokers:9092