Skip to content

Instantly share code, notes, and snippets.

@banks
Created January 10, 2020 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banks/35e4af7feb4c8f0db7a8a645de29bde9 to your computer and use it in GitHub Desktop.
Save banks/35e4af7feb4c8f0db7a8a645de29bde9 to your computer and use it in GitHub Desktop.

Adding a Simple Config Field for Client Agents

  • Add the field to the Config struct (or an appropriate sub-struct) in agent/config/config.go.
  • Add the field to the actual RuntimeConfig struct in agent/config/runtime.go.
  • Add an appropriate parser/setter in agent/config/builder.go to translate.
  • Add the new field with a random value to both the JSON and HCL blobs in TestFullConfig in agent/config/runtime_test.go, it should fail now, then add the same random value to the expected struct in that test so it passes again.
  • Add the new field and it's default value to TestSanitize in the same file. (Running the test first gives you a nice diff which can save working out where etc.)
  • If your new config field needed some validation as it's only valid in some cases or with some values (often true).
    • Add validation to Validate in builder.go.
    • Add a test case to the table test TestConfigFlagsAndEdgeCases in runtime_test.go.
  • If your new config field needs a non-zero-value default.
    • Add that to DefaultSource in defaults.go.
    • Add a test case to the table test TestConfigFlagsAndEdgeCases in runtime_test.go.
  • If your config should take effect on a reload/HUP.
    • Add necessary code to to trigger a safe (locked or atomic) update to any state the feature needs changing. This needs to be added to one or more of the following places:
      • ReloadConfig in agent/agent.go if it needs to affect the local client state or another client agent component.
      • ReloadConfig in agent/consul/client.go if it needs to affect state for client agent's RPC client.
    • Add a test to agent/agent_test.go similar to others with prefix TestAgent_reloadConfig*.
  • If the new config field(s) include an array of structs or maps.
    • Add the path to the call to lib.PatchSliceOfMaps in Parse in config.go.
    • If none of the tests in runtime_test.go failed before you did that, then you didn't actually test the slice part yet, go back and add tests that populate that slice.
  • Add documentation to website/source/docs/agent/options.html.md.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment