Is there a way to define environment variables tha...
# ask-metaflow
i
Is there a way to define environment variables that get injected into every kubernetes job by default? I have users using an internal API that they are typing repeatedly in a
.env
and then redefining in
@environment
, it would be really convenient if I could define this value for them so it gets injected into every step that has the
@kubernetes
decorator
1
i
Possibly, but I think I solved my own issue. I put the environment variable into the k8s secret at
METAFLOW_KUBERNETES_SECRETS
which is working for my use case
a
that works too
v
this is a good excuse to advertise new `Config`s too 🙂 instead of
.env
, you could read the desired env vars from a config which you can inject in every
@kubernetes
step like this:
Copy code
import os
from pprint import pprint
from functools import wraps

from metaflow import FlowSpec, step, Config, kubernetes

def my_k8s(f):
    @wraps(f)
    def wrapper(self):
        os.environ.update(self.config)
        f(self)
    return kubernetes(wrapper)

class CommonEnvFlow(FlowSpec):

    config = Config('config', default='config.json')

    @my_k8s
    @step
    def start(self):
        pprint(dict(os.environ))
        self.next(self.end)

    @my_k8s
    @step
    def end(self):
        pprint(dict(os.environ))

if __name__ == '__main__':
    CommonEnvFlow()
and then your config is something like
Copy code
{
  "FIRST_VAR": "hello",
  "SECOND_VAR": "world"
}