func run ignores -e and --env options
We implemented a knative function using a go-lang handler. This handler reads its configuration from environment variables retrieved via the envconfig library.
The environment variables are correctly parsed and read if the function is deployed on a cluster with the CLI via func deploy, so technically it works in our production environment.
However when the function is run locally with func run there is no way to pass the environment variables to the function.
func run -e 'URL=http://example.com' or func run --env 'URL=http://example.com' does not seem to work properly and passes nothing into our function. Setting the parameters in the func.yaml also does not pass them into the function when func run is used, but works perfectly fine with func deploy.
The Start function looks like the following:
func (h *CloserHandler) Start(ctx context.Context, args map[string]string) error {
h.logger = GetLogger(levelFromString(h.config.LogLevel), "function", "closer", "version", "1.0.0")
h.logger.Info(fmt.Sprintf("%#v", h.config))
// Print all environment variables
for _, env := range os.Environ() {
h.logger.Info(env)
}
// Parse the environment variables
err := envconfig.Process("", &h.config)
if err != nil {
return errors.Join(ErrUnparseableEnvironment, fmt.Errorf("failed to parse environment variables: %w", err))
}
}
The config.go looks like this:
package function
import "net/url"
type darktraceConfig struct {
PublicToken string `envconfig:"DARKTRACE_PUBLIC_TOKEN" required:"true"`
PrivateToken string `envconfig:"DARKTRACE_PRIVATE_TOKEN" required:"true"`
URL *url.URL `envconfig:"DARKTRACE_URL" required:"true"`
SkipVerify bool `envconfig:"DARKTRACE_SKIP_VERIFY" default:"false"`
}
type cfg struct {
// LogLevel is the log level for the structured logger.
LogLevel string `envconfig:"LOG_LEVEL" default:"debug"`
ValueExtractor valueExtractor `envconfig:"EXTRACTOR"`
Darktrace darktraceConfig `envconfig:"DARKTRACE" required:"true"`
}
Given func run -e DARKTRACE_PUBLIC_TOKEN=test only the enviromenment variables PORT and PWD are printed out by the Start function.