pro-builder

by Helm

Helm chartv0.6.16

Build OpenFaaS functions via a REST API

About this chart

OpenFaaS Pro Function Builder Chart

Build OpenFaaS functions securely via REST API.

The Function Builder aka pro-builder provides a convenient way to build functions for multiple tenants via a REST API. It is designed to be used with OpenFaaS, but can also be purchased and used independently to publish kind container images.

Prerequisites

  • Obtain a license key

    You will need a license for OpenFaaS for Enterprises. Find out more

  • A container image registry that is accessible from your cluster

    You can generate a valid container registry login file by:

    • Running faas-cli registry-login (preferred)
    • Or, disable the keychain in Docker, then run docker login, and supply the $HOME/.docker/config.json file.

    Cloud-hosted registries such as GCR and AWS ECR are supported via bundled Docker credential helper binaries, and may require additional configuration.

  • openfaas namespace must exist

    The chart is designed to be installed into the openfaas namespace, an OpenFaaS installation is not required to use the builder.

Installation

Create a registry push secret for the Pro Builder to use to push images to your registry.

Registry authentication

For testing with ttl.sh, create an empty auths section:

cat << EOF > ttlsh-config.json
{
  "auths": {}
}
EOF

Then create the secret within the cluster:

kubectl create secret generic registry-secret \
    --from-file config.json=./ttlsh-config.json -n openfaas

When you want to authenticate to a private registry, you must either:

  • Use faas-cli registry-login, to create a file in the .credentials folder of the current working directory
  • Or, turn off the credential store for Docker Desktop, delete ~/docker/config.json and then run: docker login and enter your credentials.

And remember to delete any existing secret from the cluster first: kubectl delete secret registry-secret -n openfaas.

For pushing images to ECR see: Push images to Amazon ECR

Plain HTTP registry

The Function Builder uses HTTPS when pushing images to a registry. To push to a trusted local development registry that only supports plain HTTP, enable the insecure registry option in your custom values file:

proBuilder:
  insecureRegistry: true

This option applies to every registry used by the Function Builder. Keep it disabled when publishing to production or other untrusted registries.

Signing secret

Generate an HMAC signing secret and create it in the cluster:

faas-cli secret generate -o payload.txt

kubectl create secret generic payload-secret \
  --from-file payload-secret=payload.txt -n openfaas

Optional: build secrets

Build secrets let you pass private registry tokens, CA certificates, or other sensitive values into RUN --mount=type=secret instructions. Secrets are encrypted client-side before sending to the builder.

Generate a keypair and create the Kubernetes secret:

faas-cli secret keygen

kubectl create secret generic -n openfaas \
  pro-builder-build-secrets-key \
  --from-file key=./key

Enable the feature in your custom values file:

buildSecrets:
  privateKeySecret: pro-builder-build-secrets-key

Distribute key.pub to build clients. They seal secrets with:

faas-cli secret seal key.pub \
  --from-literal pip_token=s3cr3t

Or fetch the public key from a running builder:

curl -s http://127.0.0.1:8081/publickey | jq -r '.public_key' > key.pub

See the Function Builder documentation for full usage examples.

Rotating secrets

Rotating the HMAC payload secret

faas-cli secret generate -o payload.txt

kubectl create secret generic payload-secret \
  --from-file payload-secret=payload.txt -n openfaas \
  --dry-run=client -o yaml | kubectl apply -f -

kubectl rollout restart deployment/pro-builder -n openfaas

Update the payload.txt file on any build clients.

Rotating the build secrets keypair

faas-cli secret keygen

kubectl create secret generic pro-builder-build-secrets-key \
  --from-file key=./key -n openfaas \
  --dry-run=client -o yaml | kubectl apply -f -

kubectl rollout restart deployment/pro-builder -n openfaas

Distribute the new key.pub to build clients. Any pre-sealed com.openfaas.secrets files must be re-sealed with the new key. The key ID changes automatically since it is derived from the key material.

Install the Chart

  • Create the required secret with your OpenFaaS Pro license code:
$ kubectl create secret generic \
    -n openfaas \
    openfaas-license \
    --from-file license=$HOME/.openfaas/LICENSE
  • Add the OpenFaaS chart repo and deploy the pro-builder Pro chart. We recommend installing it in the same namespace as the rest of OpenFaaS
$ helm repo add openfaas https://openfaas.github.io/faas-netes/
$ helm repo update

Next, you need to decide whether you're going to run the builder as root, or in a rootless, restricted mode for production.

Create a custom-values.yaml file accordingly.

Root mode, for development, or where rootless for some reason isn't working:

buildkit:
  rootless: false

Rootless mode (preferred, if possible):

buildkit:
  rootless: true

Then install the chart using its official path and the custom YAML file:

$ helm upgrade pro-builder openfaas/pro-builder \
    --install \
    --namespace openfaas \
    -f custom-values.yaml

When installing from the faas-netes repository on your local computer, whilst making changes or customising the chart, you can run this command instead from the faas-netes folder:

$ helm upgrade pro-builder ./chart/pro-builder \
    --install \
    --namespace openfaas \
    --values ./chart/pro-builder/values.yaml

The Pod for the builder contains two containers:

  • pro-builder - the REST API for building functions
  • buildkit - the daemon for Buildkit

Pass either to the logs command:

# Check the logs of the pro-builder API
kubectl logs -n openfaas \
  deploy/pro-builder -c pro-builder

# Check the logs of the buildkit daemon
kubectl logs -n openfaas \
  deploy/pro-builder -c buildkit

# Check the events for any missing secrets or volumes
kubectl get events -n openfaas

To test the builder head over to the Function Builder API Documentation

Push images to Amazon ECR

Your image name should be of a format similar to: ACCOUNT.dkr.ecr.REGION.amazonaws.com/REPOSITORY/IMAGE_NAME:IMAGE_TAG

Bear in mind that unlike i.e. the Docker Hub or ghcr.io, Amazon ECR requires all repositories to be created via a CreateRepository API call before you can publish images to them from a container builder.

This can be achieved simply using a function or some custom code using one of the various AWS SDKs.

During development and testing, you can create a repository manually using the AWS Console or CLI.

Valid AWS ECR repositories need to be created for each function you want to push.

  • Flat "test-fn" repository: ACCOUNT.dkr.ecr.REGION.amazonaws.com/test-fn
  • Namespaced "test-fn" repository: ACCOUNT.dkr.ecr.REGION.amazonaws.com/test-fn/namespace

Push using a shared credential (recommended for initial testing)

A shared credential file is the easiest way to test everything works before going into more advanced configurations like IAM roles for service accounts (IRSA).

Configure the Pro Builder to use the Amazon ECR credential helper. The required config.json file can be generated by the faas-cli:

faas-cli registry-login --ecr \
  --account-id <aws_account_id> \
  --region <region>
kubectl create secret generic registry-secret \
    --from-file config.json=credentials/config.json -n openfaas

For more details on the configuration see: amazon-ecr-credentials-helper

Create a secret for the AWS credentials file. The credentials must have a policy applied that allows access to Amazon ECR.

Create the credentials file:

export AWS_ACCESS_KEY_ID=""
export AWS_ACCESS_SECRET_KEY=""
export AWS_REGION="us-east-1" # Your AWS region

cat > ecr-credentials.txt <<EOF
[default]
region = $AWS_REGION
aws_access_key_id=$AWS_ACCESS_KEY_ID
aws_secret_access_key=$AWS_ACCESS_SECRET_KEY
EOF

Create a secret in the openfaas namespace with the contents of the credentials file:

kubectl create secret generic -n openfaas \
  aws-ecr-credentials --from-file aws-ecr-credentials=./ecr-credentials.txt

Modify your values.yaml file accordingly to mount the secret to the pro-builder Pod:

awsCredentialsSecret: aws-ecr-credentials

Then perform an installation.

Push using AWS IAM roles and IAM roles for service accounts (IRSA)

If you are running on EKS, you can use IAM roles for service accounts (IRSA) to allow the Pro Builder to push images to ECR without creating a shared credential file.

You must first configure IRSA properly in your EKS cluster, which may involve configuring add-ons and an OIDC provider in your AWS account. For more instructions see the AWS documentation on IAM roles for service accounts.

Create an appropriate policy for the pro-builder Pod to use with the ECR credential helper.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ecr:GetAuthorizationToken",
        "ecr:BatchCheckLayerAvailability",
        "ecr:GetDownloadUrlForLayer",
        "ecr:GetRepositoryPolicy",
        "ecr:DescribeRepositories",
        "ecr:ListImages",
        "ecr:DescribeImages",
        "ecr:BatchGetImage",
        "ecr:InitiateLayerUpload",
        "ecr:UploadLayerPart",
        "ecr:CompleteLayerUpload",
        "ecr:PutImage"
      ],
      "Resource": "*"
    }
  ]
}

Attach it to a service account named pro-builder-ecr in the openfaas namespace.

Next, when you create the registry-secret, configure the credential helper to use the ECR credential helper.

Update the ACCOUNT and REGION appropriately from the following example:

{
  "credHelpers": {
    "ACCOUNT.dkr.ecr.REGION.amazonaws.com": "ecr-login"
  }
}

Edit values.yaml, ensure you have the following in place:

serviceAccount: pro-builder-ecr

securityContext:
  fsGroup: 65534

Then install the chart using Helm.

You can verify that the service account is properly configured through an exec command.

kubectl exec --namespace openfaas -t -i  deploy/pro-builder -c pro-builder -- cat /var/run/secrets/kubernetes.io/serviceaccount/token ; echo

Copy the output to https://jwt.io

  • The sub field should be "sub": "system:serviceaccount:openfaas:pro-builder-ecr"
  • The iss field should be "iss": "https://oidc.eks.REGION.amazonaws.com/id/ACCOUNT_ID"
  • The name field under serviceaccount under kubernetes.io should read: "pro-builder-ecr"

For further testing, run the following:

kubectl exec --namespace openfaas -t -i  deploy/pro-builder -c pro-builder -- docker-credential-ecr-login list; echo

This command parses the registry secret to check the helper is configured properly, you should see a repository such as:

{"https://ACCOUNT.dkr.ecr.REGION.amazonaws.com":"AWS"}

In our testing, whilst the pro-builder does push images properly, the docker-credential-ecr-login get command appears to hang. Images are still pushed successfully, so this is not an issue.

Configuration

Additional pro-builder options in values.yaml.

ParameterDescriptionDefault
replicasHow many replicas of buildkit and the pro-builder API to create1
proBuilder.imageContainer image to use for the pro-builderSee values.yaml
proBuilder.insecureRegistryAllow pushing to plain HTTP registries for trusted local development onlyfalse
proBuilder.maxInflightLimit the total amount of concurrent builds for the pro-builder replicaSee values.yaml
buildkit.imageImage version for the buildkit daemon when buildkit.rootless is falseSee values.yaml
buildkitRootless.imageImage version for the buildkit daemon when buildkit.rootless is trueSee values.yaml
buildkit.rootlessWhen set to true, uses user-namespaces to avoid a privileged containertrue
buildkit.securityContextUsed to set security policy for buildkitSee values.yaml
imagePullPolicyThe policy for pulling either of the containers deployed by this chartIfNotPresent
disableHmacThis setting disable request verification, so should never to set to truefalse
enableLchownToggle whether Lchown is used by buildkit, toggle if you run into errorsfalse
buildSecrets.privateKeySecretName

Deploy pro-builder with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has pro-builder on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartpro-builder is pinned at v0.6.16 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply