Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

func run ignores -e and --env options #2460

Open
andy-d2019 opened this issue Aug 19, 2024 · 0 comments
Open

func run ignores -e and --env options #2460

andy-d2019 opened this issue Aug 19, 2024 · 0 comments

Comments

@andy-d2019
Copy link

andy-d2019 commented Aug 19, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

1 participant