Links
Comment on page

Substitution (dynamic templates)

Substitution provides a powerful mechanism to dynamically transform application configuration that is tailored for each device.

Substitution

Synpse provides the ability to expand, or substitute, application and device metadata to facilitate dynamic application configurations.

Available Environment Variables

  • Device environment variables (defined by user)
  • Synpse environment variables (automatically set, reference here)

Dynamic Application Spec

Most of the values in the application spec can be changed, for example:
  • Container name
  • Image name (you can adapt image name based on architecture)
  • Environment variables
  • Secrets
  • Volumes
Application name, scheduling configuration or the yaml structure itself cannot be changed as it will prevent yaml from being correctly marshalled.

Example Image Based On CPU Arch

If you have an application that is deployed on multiple devices of various CPU architectures (amd64, arm, arm64), you can specify image substitution (see available environment variables):
name: example-app
scheduling:
type: AllDevices
spec:
containers:
- name: my-app
image: docker.io/my-org/my-app-${SYNPSE_DEVICE_ARCH}:latest
On a regular amd64 (x86) machine it will be:
name: example-app
scheduling:
type: AllDevices
spec:
containers:
- name: my-app
image: docker.io/my-org/my-app-amd64:latest
While on a 32-bit arm machine the spec will become:
name: example-app
scheduling:
type: AllDevices
spec:
containers:
- name: my-app
image: docker.io/my-org/my-app-arm:latest

Example Device Env Substitution

Let's say you have set an environment variable on the device called:
REGION=sillicon-valley
Now, you can use that environment variable to create something else, for example a URL on which the device will be accessible by users (assuming you have configured domain, etc.):
name: example-app
scheduling:
type: AllDevices
spec:
containers:
- name: my-app
image: docker.io/my-org/my-app:latest
env:
- name: PUBLIC_ADDRESS
value: https://${REGION}.example.com
And your application will then see an environment variable which equals
PUBLIC_ADDRESS=https://sillicon-valley.example.com

String Operations

Synpse provides partial emulation for bash string operations. This can be used to manipulate string values prior to substitution.
  • Example variable substitution with substring:
name: example-app
scheduling:
type: AllDevices
spec:
containers:
- name: my-app
image: docker.io/my-org/my-app:latest
env:
- name: CUSTOMER
value: ${CUSTOMER_ID:0:4} # Getting first 4 symbols
Synpse emulates the below string operations:
${parameter^}
${parameter^^}
${parameter,}
${parameter,,}
${parameter:position}
${parameter:position:length}
${parameter#substring}
${parameter##substring}
${parameter%substring}
${parameter%%substring}
${parameter/substring/replacement}
${parameter//substring/replacement}
${parameter/#substring/replacement}
${parameter/%substring/replacement}
${#parameter}
${parameter=default}
${parameter:=default}
${parameter:-default}
Synpse makes a best-effort to emulate these operations however we do not promise perfect emulation.

Escaping

If you do not want the system to evaluate an expression it must be escaped:
name: example-app
scheduling:
type: AllDevices
spec:
containers:
- name: my-app
image: docker.io/my-org/my-app:latest
env:
- name: DEVICE_NAME
value: $${MY_DEVICE_NAME}

Caveats

If your environment variable contains *, it will be automatically quoted to avoid parsing issues. For example:
env:
- name: MY_DOMAIN
value: ${MY_DOMAIN}
Where MY_DOMAIN=*.example.com will be rendered as:
env:
- name: MY_DOMAIN
value: *.example.com
However, the following example
env:
- name: MY_DOMAIN
value: https://${MY_DOMAIN}
Will be rendered as:
env:
- name: MY_DOMAIN
value: https://"*.example.com"
Which can lead to issues in your applications. In these scenarios, try to avoid using environment variable substitution.