Jinja Functions¶
Jinja Helm
Source code in src/adeploy/common/jinja/globals.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 | |
create_docker_registry_secret
¶
create_docker_registry_secret(
server: str,
username: str,
password: Union[SecretsProvider, str],
email: str = None,
name: str = None,
use_pass: bool = True,
use_gopass_cat: bool = True,
custom_cmd: bool = False,
) -> str
Creates a secret from type "kubernetes.io/dockerconfigjson"
Creates a secret from type "kubernetes.io/dockerconfigjson" that can be used i.e. as an image pull secret.
See Create Image Pull Secret for details and examples.
| PARAMETER | DESCRIPTION |
|---|---|
|
The server name for the Docker registry. Must be specified directly.
TYPE:
|
|
The username for the Docker registry. Must be specified directly.
TYPE:
|
|
A SecretsProvider object which provides the password for the Docker registry. Deprecated! If a string is passed instead of a SecretsProvider object, the password is retrieved using the deprecated options below.
TYPE:
|
|
An optional email address (specified directly) that is added to the secret if specified.
TYPE:
|
|
An optional name for the secret. Auto-generated deterministically if not specified.
TYPE:
|
|
Deprecated! Don't use and replace by kwarg using the Gopass SecretProvider.
TYPE:
|
|
Deprecated! Don't use and replace by kwarg using the Gopass SecretProvider.
TYPE:
|
|
Deprecated! Don't use and replace by kwarg using the Shell SecretProvider.
TYPE:
|
Warning
use_pass: is deprecated and will be removed in a future version. Use from_gopass() instead.
use_gopass_cat: is deprecated and will be removed in a future version. Use from_gopass() instead.
custom_cmd: is deprecated and will be removed in a future version. Use from_shell_command() instead.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The generated or specified secret name, see
TYPE:
|
Example
Source code in src/adeploy/common/jinja/globals.py
create_labels
¶
create_labels(
name: str = None,
instance: str = None,
version: str = None,
component: str = None,
part_of: str = None,
managed_by: str = "adeploy",
labels: Union[dict, list] = None,
**kwargs: dict,
) -> str
Creates a dict of custom and common labels
This can be used to create (and update) label objects ready to use in k8s manifests.
Next to the specified custom labels, a set of standardised labels following best practise can be generated, see Recommended Labels.
| PARAMETER | DESCRIPTION |
|---|---|
|
Creates standard label
TYPE:
|
|
Creates standard label
TYPE:
|
|
Creates standard label
TYPE:
|
|
Creates standard label
TYPE:
|
|
Creates standard label
TYPE:
|
|
Creates standard label
TYPE:
|
|
An optional dict or list with custom labels to use.
TYPE:
|
|
Optionally, more labels can be specified as args.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
A string with a JSON object with the specified labels ready to add to k8s manifests
TYPE:
|
You can create_labels() to create a global label object i.e. in your defaults.yml and use create_labels()
a second time e.g. in your templates/deployment.yml to extend the labels with object related labels. See
Labels and Nested Labels for more info.
Example: Nested Labels
You can use create_labels() to define global labels i.e. in your defaults.yml:
And use create_labels() another time to add object related labels i.e. in your template files:
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ name }}-{{ release }}
namespace: {{ namespace }}
labels: {{ create_labels(name="nginx", labels=labels) }}
spec:
selector:
matchLabels: {{ create_labels(name="nginx", labels=labels) }}
replicas: 1
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
labels: {{ create_labels(name="nginx", labels=labels) }}
This will result in the following labels:
apiVersion: apps/v1
kind: Deployment
metadata:
name: 006-nested-labels-prod
namespace: playground
labels:
app.kubernetes.io/name: "nginx"
app.kubernetes.io/instance: "prod"
app.kubernetes.io/component: "my-deployment"
app.kubernetes.io/part-of: "006-nested-labels"
app.kubernetes.io/managed-by: "adeploy"
Source code in src/adeploy/common/jinja/globals.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
create_secret
¶
create_secret(
name: str = None,
use_pass: bool = True,
use_gopass_cat: bool = True,
custom_cmd: bool = False,
as_ref: bool = False,
data: dict = None,
**kwargs: Dict[str, SecretsProvider],
) -> str
Creates k8s secrets
Creates a k8s secret if it does not exist and returns the secret name. It won't overwrite any existing secret.
If adeploy --recreate-secrets was specified, the secret will be re-created. This can be used to update the
secret or auto-rotate random hashes.
See Secrets for details and examples.
| PARAMETER | DESCRIPTION |
|---|---|
|
An optional secret name. If not specified, a unique secret name will be created that is deterministic to the given secret data.
TYPE:
|
|
Format the return value as a YAML object including
TYPE:
|
|
The secret data to use. The key is the secret key and the value is the secret data. The secret data must be passed as SecretProvider object
TYPE:
|
|
Deprecated! Don't use and replace by kwarg using the Gopass SecretProvider.
TYPE:
|
|
Deprecated! Don't use and replace by kwarg using the Gopass SecretProvider.
TYPE:
|
|
Deprecated! Don't use and replace by kwarg using the Shell SecretProvider.
TYPE:
|
|
Deprecated! Don't use and replace by kwargs using SecretProviders
TYPE:
|
Warning
use_pass: is deprecated and will be removed in a future version. Use from_gopass() instead.
use_gopass_cat: is deprecated and will be removed in a future version. Use from_gopass() instead.
custom_cmd: is deprecated and will be removed in a future version. Use from_shell_command() instead.
data is deprecated and will be removed without replacement in a future version.
Pass all secret data as kwargs instead.
| RETURNS | DESCRIPTION |
|---|---|
str
|
Either a YAML dict including
TYPE:
|
Example: Create a secret for a password stored in Gopass
Use the following in your defaults.yml or in our namespace/release configuration:
{# Debug, print the secret value #}
{{ from_gopass(path="/my/path") }}
{# Create a secret with the secret value and use it in a deployment #}
my_deployment:
config:
secretName: {{ create_secret(my_secret=from_gopass(path="/my/path")) }}
secretKey: my_secret
This will create a secret containing with a key my_secret and value from gopass cat /my/path.
Example: Return a secret reference
You can set use_ref=True in order to return a formatted YAML object containing key and name:
Your can now use the returned YAML object in your templates as follows:
Example: Use a custom command to retrieve a password
Set custom_cmd=True to skip Gopass and specify a custom bash command:
secrets:
my_secret: {{ create_secret(custom_cmd=True, my_secret='/usr/bin/passwd-gen 32 -n "My Random Password"') }}
You can use this secret as usual:
env:
- name: MY_SECRET
valueFrom:
secretKeyRef:
name: {{ secrets.my_secret }}
key: my_secret
Note that the secret name is determined deterministically from the specified secret data i.e. my_secret
and /usr/bin/passwd-gen 32 -n "My Random Password". So if you need multiple secret i.e. from command
passwd-gen, you should specify a custom string i.e. My Random Password to make the name unique.
Note that this will create a secret with a random password. You can rotate this random password using
adeploy --recreate-secrets otherwise the random password will not change since adeploy will not overwrite
existing secrets.
Source code in src/adeploy/common/jinja/globals.py
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | |
create_tls_secret
¶
create_tls_secret(
cert: Union[str, SecretsProvider],
key: Union[str, SecretsProvider],
name: str = None,
use_pass: bool = True,
use_gopass_cat: bool = True,
custom_cmd: bool = False,
) -> str
Creates a secret from type kubernetes.io/tls
Creates a TLS secret from type kubernetes.io/tls by using create_secret().
In doing so, you can specify a Gopass path (default), a custom command (custom_cmd=True) or direct data
(use_pass=False) for the cert and key values.
See Create TLS Secrets for details and examples.
| PARAMETER | DESCRIPTION |
|---|---|
|
A SecretsProvider object which provides the server certificate. Deprecated: If a string is passed instead of a SecretsProvider object, the certificate is retrieved using the deprecated options below.
TYPE:
|
|
A SecretsProvider object which provides the certificate key data. Deprecated! If a string is passed instead of a SecretsProvider object, the certificate key is retrieved using the deprecated options below.
TYPE:
|
|
An optional name for the secret, see
TYPE:
|
|
Deprecated! Don't use and replace by kwarg using the Gopass SecretProvider.
TYPE:
|
|
Deprecated! Don't use and replace by kwarg using the Gopass SecretProvider.
TYPE:
|
|
Deprecated! Don't use and replace by kwarg using the Shell SecretProvider.
TYPE:
|
Warning
cert: After the deprecation period, a passed string will be treated as certificate data.
use_pass: is deprecated and will be removed in a future version. Use from_gopass() instead.
use_gopass_cat: is deprecated and will be removed in a future version. Use from_gopass() instead.
custom_cmd: is deprecated and will be removed in a future version. Use from_shell_command() instead.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The generated or specified secret name, see
TYPE:
|
Example
ingress:
className: external
tls:
- hosts:
- mydomain.com
secretName: {{ create_tls_secret(
custom_cmd=True,
cert=from_shell_command(
'cat namespaces/playground/secrets/domain_prod/mydomain.com.crt'
),
key=from_shell_command(
'cat namespaces/playground/secrets/domain_prod/mydomain.com.key'
)
) }}
Source code in src/adeploy/common/jinja/globals.py
from_gopass
¶
Get a secret from gopass.
| PARAMETER | DESCRIPTION |
|---|---|
|
The path to the secret in gopass. The path is searched in the gopass repositories in the order they are defined
TYPE:
|
|
Use
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
gopass_secret
|
The secret provider object. The object can either be used in the create_secret() function or rendered directly in the Jinja template for debugging purposes. Don't render a secret in the Jinja template in production code - this will likely break CI setups.
TYPE:
|
Example
Source code in src/adeploy/common/jinja/globals.py
from_json_or_yaml
¶
from_json_or_yaml(
path: str,
jq_query: str = None,
force_type: Literal["json", "yaml"] = None,
) -> Union[dict, str, list]
Include data from an external JSON or YAML file in your defaults.yml or namespace / release configuration.
Optionally apply a jq query. Useful if a var is not in the defaults.yml or in the namespace / release
configuration but in an external file - for example an ansible hostvars file.
Using the environment variable ADEPLOY_EXTERNAL_INCLUDE_BASEDIR, you can specify a base directory which
is used as prefix for the path to the file to import.
| PARAMETER | DESCRIPTION |
|---|---|
|
The path to the file to import. Variables are expanded.
TYPE:
|
|
An optional jq query to apply to the imported structured data. See here for details.
TYPE:
|
|
Force the file type to be either
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
data
|
The content of the file or loaded by json.loads() or yaml.load(). If a jq_query is given it is applied.
TYPE:
|
Example
jq / yq queries
The external data is first transformed into python data objects, then the jq query is applied. Therefore, jq is well suited to also filter you yaml data. Have a look at the jq manual for details.
Source code in src/adeploy/common/jinja/globals.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
from_plaintext
¶
Creates a plaintext secret provider for use in the create_secret method.
| PARAMETER | DESCRIPTION |
|---|---|
|
The "secret" in plaintext. Don't use this for production.
|
| RETURNS | DESCRIPTION |
|---|---|
plaintext_secret
|
The secret provider object. The object can either be used in the create_secret() function or rendered directly in the Jinja template for debugging purposes.
TYPE:
|
Source code in src/adeploy/common/jinja/globals.py
from_shell_command
¶
| PARAMETER | DESCRIPTION |
|---|---|
|
The command to execute. It will be passed to `subprocess.run(cmd, shell=True)
|
Returns:
Source code in src/adeploy/common/jinja/globals.py
get_version
¶
include_file
¶
include_file(
path: str,
direct: bool = False,
render: bool = True,
indent: int = 4,
skip: List[str] = None,
escape: List[str] = None,
) -> str
Include and optionally render arbitrary files into your manifest
Reads the content of the specified file and returns the corresponding format to include the read content into your YAML manifest file or your config map.
See Includes for details and examples.
| PARAMETER | DESCRIPTION |
|---|---|
|
The path to the file to include. This path must be relative to the project directory where
TYPE:
|
|
Set
TYPE:
|
|
Set
TYPE:
|
|
Indents the content block for the specified amount.
TYPE:
|
|
A list of characters to remove from the read file content. See Skip & Escape.
TYPE:
|
|
A list of characters to escape from the read file content. See Skip & Escape.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The (rendered) file content in the requested format to include it either into a YAML document or as a block string to use it as value i.e. in config maps.
TYPE:
|
Example: Include config files to config maps:
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ name }}-{{ release }}-config
namespace: {{ namespace }}
labels:
app.kubernetes.io/part-of: {{ name }}
app.kubernetes.io/instance: {{ release }}
app.kubernetes.io/component: config
data:
nginx.conf: {{ include_file('files/nginx.conf', render=False) }}
{% for name, content in list_dir('files/conf.d', render=False).items() %}
{{ name }}: {{ content }}
{% endfor %}
Example: Render and include YAML parts into YAML documents:
template:
metadata:
labels:
app.kubernetes.io/part-of: {{ name }}
app.kubernetes.io/instance: {{ release }}
app.kubernetes.io/component: redis
annotations:
checksum/config: |
{{ include_file("common/configmap.yml") | sha256sum }}
spec:
{{ include_file('common/__affinity.yml', direct=True, indent=6) }}
containers:
- name: main
image: {{ redis.image }}:{{ version('redis') }}
imagePullPolicy: Always
{{ include_file('common/__env.yml', direct=True, indent=10) }}
ports:
- name: redis
containerPort: 6379
livenessProbe:
exec:
command:
- redis-cli
- ping
initialDelaySeconds: 5
timeoutSeconds: 5
readinessProbe:
exec:
command:
- redis-cli
- ping
initialDelaySeconds: 5
timeoutSeconds: 5
Example: Download and include CRDs from GitHub:
Source code in src/adeploy/common/jinja/globals.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | |
list_dir
¶
list_dir(
dir: str,
direct: bool = False,
render: bool = True,
indent: int = 4,
skip: List[str] = None,
escape: List[str] = None,
) -> dict
Include files from a directory
This will include and optionally render all files from the given directory. Expect the dir arg, you can
pass the same args to list_dir() as for include_file()
to control how the files inside dir should be handled.
See (Include Arbitrary Files)[includes.md#include-arbitrary-files] for details and examples.
| PARAMETER | DESCRIPTION |
|---|---|
|
The path to the directory to include. This path must be relative to the project directory where
TYPE:
|
|
See
TYPE:
|
|
See
TYPE:
|
|
See
TYPE:
|
|
See
TYPE:
|
|
See
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
A dict with filename as key and the formatted content as value.
TYPE:
|
Example: Include all files form files/conf.d into a config map
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ name }}-{{ release }}-config
namespace: {{ namespace }}
labels:
app.kubernetes.io/part-of: {{ name }}
app.kubernetes.io/instance: {{ release }}
app.kubernetes.io/component: config
data:
nginx.conf: {{ include_file('files/nginx.conf', render=False) }}
{% for name, content in list_dir('files/conf.d', render=False).items() %}
{{ name }}: {{ content }}
{% endfor %}
files/nginx.conf as well as scan the directory files/conf.d for files, read the
content and include them into the ConfigMap using the filename as key.
Source code in src/adeploy/common/jinja/globals.py
random_string
¶
Creates a secure random string secret provider for use in the create_secret method.
| PARAMETER | DESCRIPTION |
|---|---|
|
An optional length. Default is 32. The length must be at least 16.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
random_secret
|
The secret provider object. The object can either be used in the create_secret() function or rendered directly in the Jinja template for debugging purposes.
TYPE:
|
Source code in src/adeploy/common/jinja/globals.py
uuid
¶
Generates a UUIDv4 or a short UUID
| PARAMETER | DESCRIPTION |
|---|---|
|
Set
TYPE:
|
|
Random string length if using ShortUUID().
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The generated UUID
TYPE:
|
Example
You can use this Jinja function to create unique names i.e. for Jobs:
Source code in src/adeploy/common/jinja/globals.py
version
¶
Returns the version from the given package
This returns the version from the dict in versions that can be specified in defaults.yml and in the
namespace/release configuration.
If versions is specified in both configurations, it will be merged while
the namespace/release configuration takes precedence. See Usage#variables.
| PARAMETER | DESCRIPTION |
|---|---|
|
The package to retrieve the version from.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The version or "latest" if no version was defined.
TYPE:
|
Example