https://prefect.io logo
#prefect-community
Title
# prefect-community
w

Will Milner

05/26/2020, 1:03 PM
for prefect core, is it possible to set context variables for flow runs in kubernetes agent? I tried using the --env flag but whenever I try and run my flow I get this error
Copy code
May 26th 2020 at 9:00:18am | agent
ERROR 
(400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Audit-Id': '9f273015-9326-4806-8497-6847f42b700b', 'Content-Type': 'application/json', 'Date': 'Tue, 26 May 2020 13:00:18 GMT', 'Content-Length': '549'})
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Job in version \"v1\" cannot be handled as a Job: v1.Job.Spec: v1.JobSpec.Template: v1.PodTemplateSpec.Spec: v1.PodSpec.Containers: []v1.Container: v1.Container.Env: []v1.EnvVar: v1.EnvVar.Value: ReadString: expects \" or n, but found {, error found in #10 byte of ...|\"value\": {\"context\":|..., bigger context ...|.CloudTaskRunner\"}, {\"name\": \"prefect\", \"value\": {\"context\": {\"aws_secret\": |...","reason":"BadRequest","code":400}
I don't have this problem when running on docker agent
z

Zachary Hughes

05/26/2020, 1:20 PM
Hi @Will Milner, this should definitely be possible. Let me open an issue for you and we'll take a look!
j

josh

05/26/2020, 1:23 PM
Hey @Will Milner could I see an example of what you’re passing in for the
--env
flag?
w

Will Milner

05/26/2020, 1:25 PM
this is the full command i'm running
Copy code
prefect agent install --env PREFECT__CONTEXT__SECRETS__AWS_CREDENTIALS='"{\"aws_access_key\": \"ACCESS_KEY\",\"aws_secret\": \"SECRET_KEYi\"}" kubernetes --rbac --backend server --api <http://10.0.0.61:4200> --image-pull-secrets my-secret | kubectl apply -f -
j

josh

05/26/2020, 1:28 PM
Ah I don’t thing that would work (due to yaml parsing / k8s / etc…). You can pass many
env
options to that command and each should be a string. So it would look more like this:
Copy code
prefect agent install --env PREFECT__CONTEXT__SECRETS__AWS_CREDENTIALS__AWS_ACCESS_KEY=YOUR_KEY --env PREFECT__CONTEXT__SECRETS__AWS_CREDENTIALS__AWS_SECRET=YOURSECRET
Slack formats this kind of weird but it’s pretty much
Copy code
prefect agent install -e KEY=VAL -e KEY2=VAL
w

Will Milner

05/26/2020, 1:33 PM
i also tried that and still got the same error
Copy code
prefect agent install --env PREFECT__CONTEXT__AWS_CREDENTIALS__AWS_ACCESS_KEY="ACCESS_KEY" --env PREFECT__CONTEXT__AWS_CREDENTIALS__AWS_SECRET="SECRET_KEY" kubernetes --rbac --backend server --api <http://10.0.0.61:4200> --image-pull-secrets my-secret | kubectl apply -f -
j

josh

05/26/2020, 1:41 PM
🤔 Interesting, some stuff recently changed with our env parsing so it could be that. Could you open an issue with some more info?
w

Will Milner

05/26/2020, 1:42 PM
yup
j

josh

05/26/2020, 1:44 PM
Thanks! It looks like the agent env vars set under a heading of
Copy code
PREFECT__CLOUD__AGENT__ENV_VARS__
So I’m now thinking that there’s just some weird stuff happening when also trying to set context that deep.
w

Will Milner

05/26/2020, 1:51 PM
posted an issue, linking here in case anyone else runs into this issue and wants to track - https://github.com/PrefectHQ/prefect/issues/2652
this also happens when trying to set ANY environment variable, not just the context
thanks for the reply josh, your suggestion worked
j

josh

05/26/2020, 2:24 PM
Awesome! Will look into the behavior, thanks for finding the bug haha
w

Will Milner

05/26/2020, 2:51 PM
quick follow up, when I pass in an environment variable to kubernetes in this way, how do I go about accessing it in my flows since it won't be in the context
j

josh

05/26/2020, 2:52 PM
You can grab it using something like
os.getenv("YOUR_KEY")
w

Will Milner

05/26/2020, 3:04 PM
ah turns out the keys get set to lower case inside of the kubernetes job
j

josh

05/26/2020, 3:08 PM
Oh interesting 🤔 I’ll make note of that