Skip to content

Commit

Permalink
Include override configuration for Prefect Agent (#813)
Browse files Browse the repository at this point in the history
* prefect override config

* prefect override config

* format files

* update overrides option for Prefect helm chart

* Add agent auth credentials override option

* Add agent auth credentials override option

* Add prefect overrides specification doc

* allow arbitrary environment variables to be set on the prefect agent

* Add prefect overrides extraVar option

* Remove duplicate loop

* Update extraEnvVars and update docs

* don't require base64encoding fix

* Fix missing envVars and add secretEnvVars field

* Add overrides ability to create secrets

* rm commented code

* Update docs

* Update docs

* Update docs

Co-authored-by: Adam-D-Lewis <>
Co-authored-by: leej3 <johnleenimh@gmail.com>
  • Loading branch information
viniciusdc and leej3 authored Sep 29, 2021
1 parent bc80e7c commit c10358a
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 19 deletions.
52 changes: 51 additions & 1 deletion docs/source/admin_guide/prefect.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ The `TF_VAR_prefect_token` API key is set as `PREFECT__CLOUD__AGENT__AUTH_TOKEN`
environment variable in the agent. It is used while deploying Prefect Agent so that
it can connect to Prefect Cloud and query flows.


## Prefect Cloud

Prefect Cloud is a fully hosted, production-ready backend for Prefect Core.
Expand All @@ -53,6 +52,57 @@ runs, and allocates resources for them on their respective deployment platforms.
When you enable prefect via `qhub-config.yml` prefect agent is deployed on the
QHub's kubernetes cluster, which querys the Prefect Cloud for flow runs.

## Agent configuration overrides
You can override your agent configuration without having to modify the helm files directly. The extra variable `overrides` makes this
possible by changing the default values for the Agent chart according to the settings presented on your qhub-config.yaml file.

The current variables, originaly available in the [Agent helm chart](https://github.com/PrefectHQ/server/blob/master/helm/prefect-server/templates/agent/deployment.yaml) that can be overridden include:

```
- IMAGE_PULL_SECRETS
- PREFECT__CLOUD__AGENT__LABELS
- JOB_MEM_REQUEST
- JOB_MEM_LIMIT
- JOB_CPU_REQUEST
- JOB_CPU_LIMIT
- IMAGE_PULL_POLICY
```

For example, if you just want to override the amount of CPU limits for each job, you would need to craft a declarative configuration,
in you qhub-config.yaml file, as follows:

```yaml
prefect:
enabled: true
overrides:
prefect_agent:
job:
resources:
limit:
cpu: 4
```
Also, if you would like to include an extra variable to the agent environment configuration, that was not previcosly in the helm chart, you can do it by including it under
the `envVars` field in the overrides block. For example, if you would like to add `MY_VAR: "<value>"` to you agent environment, you can do so by adding the following to your qhub-config
```yaml
prefect:
enabled: true
overrides:
envVars:
MY_VAR: "<value>"
```
### Adding secrets to you Agent configuration
Overrides also allow you to define extra secrets to pass through your agent configuration, for example, when using [default secrets](https://docs.prefect.io/core/concepts/secrets.html#default-secrets) to automatically authenticate your flow with the listed service. In the Google cloud case, for `GCP_CREDENTIALS` context secret, you can do it by adding that specific key value pair into your configuration:

```yalm
prefect:
enabled: true
overrides:
secretEnvVars:
PREFECT__CONTEXT__SECRETS__GCP_CREDENTIALS: '<Your value>'
```
This secret will then be stored as a [kubernetes secret](https://kubernetes.io/docs/concepts/configuration/secret/) variable into you QHub secrets volume.

## Flows

Prefect agent can only orchestrate your flows, you need an actual flow to run via
Expand Down
2 changes: 1 addition & 1 deletion qhub/render/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(self, environment):
super().__init__(environment)

def yamlify(obj):
return yaml.dump(obj)
return yaml.dump(obj, default_flow_style=False)

environment.filters["yamlify"] = yamlify

Expand Down
1 change: 1 addition & 0 deletions qhub/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class ClearML(Base):
class Prefect(Base):
enabled: bool
image: typing.Optional[str]
overrides: typing.Optional[typing.Dict]


# ============= Terraform ===============
Expand Down
3 changes: 2 additions & 1 deletion qhub/template/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
},
"prefect": {
"enabled": null,
"image": null
"image": null,
"overrides": null
},
"security": null,
"default_images": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ module "prefect" {
{% if cookiecutter.prefect.image is defined -%}
image = "{{ cookiecutter.prefect.image }}"
{% endif -%}
{% if cookiecutter.prefect.overrides is defined %}
overrides = [<<EOT
{{ cookiecutter.prefect.overrides|yamlify -}}
EOT
]
{% endif %}
}
{% endif -%}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,22 @@ spec:
serviceAccountName: {{ .Values.serviceAccount }}
containers:
- args:
- prefect agent start kubernetes -e JUPYTERHUB_API_TOKEN=$JUPYTERHUB_API_TOKEN
- prefect agent start kubernetes -e JUPYTERHUB_API_TOKEN=$JUPYTERHUB_API_TOKEN {{- range $k, $v := .Values.secretEnvVars }} {{ printf " -e %s=$%s" $k $k -}} {{ end }}
command:
- /bin/bash
- -c
env:
{{- range $k, $v := .Values.envVars }}
- name: {{ $k }}
value: {{ squote $v -}}
{{ end }}
{{- range $i, $k := keys .Values.secretEnvVars }}
- name: {{ $k }}
valueFrom:
secretKeyRef:
name: prefect-envsecret-{{ $i }}
key: prefectEnvSecret{{ $i -}}
{{ end }}
- name: PREFECT__CLOUD__AGENT__AUTH_TOKEN
valueFrom:
secretKeyRef:
Expand All @@ -37,20 +48,6 @@ spec:
value: {{ .Values.cloudApi }}
- name: NAMESPACE
value: {{ .Values.namespace }}
- name: IMAGE_PULL_SECRETS
value: ''
- name: PREFECT__CLOUD__AGENT__LABELS
value: '[]'
- name: JOB_MEM_REQUEST
value: ''
- name: JOB_MEM_LIMIT
value: ''
- name: JOB_CPU_REQUEST
value: ''
- name: JOB_CPU_LIMIT
value: ''
- name: IMAGE_PULL_POLICY
value: ''
- name: SERVICE_ACCOUNT_NAME
value: {{ .Values.serviceAccount }}
- name: PREFECT__BACKEND
Expand Down Expand Up @@ -111,4 +108,4 @@ roleRef:
name: prefect-agent-rbac
subjects:
- kind: ServiceAccount
name: {{ .Values.serviceAccount }}
name: {{ .Values.serviceAccount }}
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,18 @@ metadata:
name: jupyterhub-token
data:
jupyterHubToken: "{{ .Values.jupyterHubToken | b64enc }}"

---
{{ $index := dict "index" "0" }}
{{- range $k, $v := .Values.secretEnvVars }}
{{ $i := get $index "index" }}
{{ $ni := add1 $i }}
apiVersion: v1
kind: Secret
metadata:
name: prefect-envsecret-{{ $i }}
data:
prefectEnvSecret{{ $i }}: "{{ $v | b64enc }}"
{{ $_ := set $index "index" $ni }}
---
{{ end }}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ resource "helm_release" "prefect" {
namespace = var.namespace
chart = "${path.module}/chart"

values = concat([
file("${path.module}/values.yaml")], var.overrides)

set_sensitive {
name = "prefectToken"
value = var.prefect_token
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

envVars: {}

secretEnvVars: {}

prefect_agent:
job:
imagePullSecrets: ''
imagePullPolicy: ''
resources:
requests:
memory: ''
cpu: ''
limits:
memory: ''
cpu: ''
prefectLabels: '[]'
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ variable "cloud_api" {
type = string
default = "https://api.prefect.io"
}

variable "overrides" {
description = "Prefect helm chart list of overrides"
type = list(string)
default = []
}

0 comments on commit c10358a

Please sign in to comment.