Skip to content

Conversation

sinadarbouy
Copy link
Collaborator

Ticket(s)

#583

Description

This PR addresses a bug in the loadEnvVars function that prevented correct loading of environment variables when their names are in camelCase. The issue was caused by the function’s inability to transform camelCase environment variable names into the expected format for configuration.

Changes Made:

Updated loadEnvVars Function:

  • Replaced the previous transformation logic with a new function, transformEnvVariable, which converts environment variable names based on JSON tags from relevant structs.

Introduced transformEnvVariable Function:

  • This function transforms environment variable names to a format suitable for configuration by using a mapping derived from JSON tags of various structs.

Added generateTagMapping Function:

  • A helper function to generate a mapping of JSON tags to their lower-case versions. This is used by transformEnvVariable to correctly interpret environment variable names.

Expanded Test Coverage:

  • Added tests to verify the correct loading of environment variables, including camelCase names. This ensures that changes in environment variable names are accurately reflected in the configuration.

Development Checklist

  • I have added a descriptive title to this PR.
  • I have squashed related commits together.
  • I have rebased my branch on top of the latest main branch.
  • I have performed a self-review of my own code.
  • I have commented on my code, particularly in hard-to-understand areas.
  • I have added docstring(s) to my code.
  • I have made corresponding changes to the documentation (docs).
  • I have updated docs using make gen-docs command.
  • I have added tests for my changes.
  • I have signed all the commits.

Legal Checklist

…names

- Introduced transformEnvVariable function to format env var names based on JSON tags
- Modified loadEnvVars to use the new transformEnvVariable function
- Added generateTagMapping to create mappings from struct fields' JSON tags
    - Implemented `ServerLoadBalancerStrategyOverwrite` test to verify
      that server load balancer strategy is correctly set and loaded from
      environment variables.
    - Implemented `pluginDefaultPolicyOverwrite` test to verify
      that plugin default policy is correctly set and loaded from
      environment variables.
    - Added `initializeConfig` helper function to facilitate configuration
      setup for tests.
    - Updated `TestLoadEnvVariables` to include the new scenario for
      server load balancer strategy overwrite.
PluginConfigFile: parentDir + PluginsConfigFilename,
})
err := config.InitConfig(ctx)
require.Nil(t, err)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
require.Nil(t, err)
require.NoError(t, err)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config.InitConfig returns a errors.GatewayDError, not a standard error. so, it’s an object, and we can assert it using require.NoError.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So strange, I would have added the Error() string interface to this struct to solve this.

If you don't/cannot want to do this, you can simply rename the variable err, err is type connoted.

Useres or whatever

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GatewayDError is actually an implementation of an error, and we have Error() string. The issue is that our functions return *gerr.GatewayDError instead of the error type. When we return nil as *gerr.GatewayDError, it’s basically like returning (gerr.GatewayDError, nil), and require.NoError can’t handle that. This link explains it better: Go FAQ - nil error.

The solution is to change all return types to the error type. I assume this is outside the scope of this PR.

Renaming it to something else isn’t a great idea because it’s really just an error. Later on, if we switch to using error, it’ll be easier to handle.

Copy link
Member

@mostafa mostafa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

ctx := context.Background()

// Set the environment variable
t.Setenv("GATEWAYD_DEFAULTPOLICY", "test")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have expected

Suggested change
t.Setenv("GATEWAYD_DEFAULTPOLICY", "test")
t.Setenv("GATEWAYD_DEFAULT_POLICY", "test")

Is there a problem or something to code differently?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a test case for the plugin. In the plugin configuration, there isn't a configuration group, so DefaultPolicy is a parameter directly within gatewayd_plugins.yaml.

CompatibilityPolicy: "strict"
DefaultPolicy: "passthrough"

@mostafa mostafa merged commit 0127264 into gatewayd-io:main Aug 5, 2024
@mostafa mostafa mentioned this pull request Oct 6, 2024
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants