Skip to content

feat: use Viper for controller configuration loading#292

Open
rdyjun wants to merge 1 commit into
STRRL:masterfrom
rdyjun:feat/viper-config-loading
Open

feat: use Viper for controller configuration loading#292
rdyjun wants to merge 1 commit into
STRRL:masterfrom
rdyjun:feat/viper-config-loading

Conversation

@rdyjun

@rdyjun rdyjun commented Mar 11, 2026

Copy link
Copy Markdown

Summary

Integrates github.com/spf13/viper to support environment variable-based configuration alongside existing Cobra CLI flags.

Changes

  • Add viper dependency to go.mod / go.sum
  • Bind all Cobra PersistentFlags to viper with BindPFlags
  • Enable AutomaticEnv with hyphen-to-underscore key replacement
  • Read config values from viper inside RunE for consistent precedence
  • Replace os.Getenv() calls in controlled-cloudflared-connector.go with viper (2b5a592)

Config precedence

CLI flags > environment variables

Examples

# These are now equivalent:
--cloudflare-api-token=xxx
CLOUDFLARE_API_TOKEN=xxx

--cloudflare-account-id=xxx
CLOUDFLARE_ACCOUNT_ID=xxx

Closes #291

@rdyjun rdyjun force-pushed the feat/viper-config-loading branch from 49ac78c to 4d75327 Compare March 11, 2026 08:21
@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the controller's configuration flexibility by integrating the Viper library. This allows the application to load configuration parameters not only from command-line flags but also from environment variables, providing a more robust and adaptable setup for deployment in various environments. The change ensures that CLI flags maintain precedence over environment variables, offering predictable behavior.

Highlights

  • Configuration Management: Integrated github.com/spf13/viper to allow environment variables to be used for configuration, complementing existing Cobra CLI flags.
  • Dependency Management: Added viper to the project's go.mod file.
  • CLI Flag Binding: Bound all Cobra PersistentFlags to Viper, enabling automatic loading of configuration from various sources.
  • Environment Variable Support: Configured Viper to automatically read environment variables, with hyphen-to-underscore key replacement for compatibility with flag names.
  • Configuration Precedence: Ensured that configuration values are read from Viper within the RunE function, establishing a clear precedence where CLI flags override environment variables.
Changelog
  • cmd/cloudflare-tunnel-ingress-controller/main.go
    • Imported strings and github.com/spf13/viper.
    • Modified the RunE function to retrieve configuration values from Viper.
    • Configured Viper for automatic environment variable loading and bound Cobra persistent flags to Viper.
  • go.mod
    • Added github.com/spf13/viper as a new dependency.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

gemini-code-assist[bot]

This comment was marked as outdated.

@codecov

codecov Bot commented Mar 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 25.15%. Comparing base (ba10a2a) to head (2b5a592).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
pkg/controller/controlled-cloudflared-connector.go 0.00% 5 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (ba10a2a) and HEAD (2b5a592). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (ba10a2a) HEAD (2b5a592)
integration 1 0
Additional details and impacted files
@@             Coverage Diff             @@
##           master     #292       +/-   ##
===========================================
- Coverage   46.85%   25.15%   -21.70%     
===========================================
  Files          11       10        -1     
  Lines         683      652       -31     
===========================================
- Hits          320      164      -156     
- Misses        340      487      +147     
+ Partials       23        1       -22     
Flag Coverage Δ
integration ?
unit 25.15% <0.00%> (+0.15%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@STRRL

STRRL commented Mar 11, 2026

Copy link
Copy Markdown
Owner

@codex PTAL

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4d7532713b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread go.mod
github.com/onsi/gomega v1.39.1
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.10.2
github.com/spf13/viper v1.20.1

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Add go.sum checksums for the new Viper module

This adds github.com/spf13/viper to go.mod but does not include matching go.sum entries, which causes reproducible/read-only module builds to fail with missing go.sum entry for module providing package github.com/spf13/viper (for example when running with -mod=readonly). Please commit the generated go.sum updates alongside this dependency change.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the catch!
The go.sum updates have been included in 2b5a592.

Integrates github.com/spf13/viper to support environment variable-based
configuration alongside existing Cobra CLI flags.

- Add viper dependency to go.mod
- Bind all Cobra PersistentFlags to viper with BindPFlags
- Enable AutomaticEnv with hyphen-to-underscore key replacement
- Read config values from viper inside RunE for consistent precedence:
  CLI flags > environment variables

This allows local debugging with .env files without manually wiring
os.Getenv() for each flag.

Examples:
  --cloudflare-api-token=xxx  or  CLOUDFLARE_API_TOKEN=xxx
  --cloudflare-account-id=xxx  or  CLOUDFLARE_ACCOUNT_ID=xxx

Fixes STRRL#291
@rdyjun rdyjun force-pushed the feat/viper-config-loading branch from 4d75327 to 2b5a592 Compare March 12, 2026 00:46
@rdyjun

rdyjun commented Mar 21, 2026

Copy link
Copy Markdown
Author

Hi @STRRL, Please let me know if there is anything further I should take care of.

@STRRL

STRRL commented Mar 21, 2026

Copy link
Copy Markdown
Owner

Hi @STRRL, Please let me know if there is anything further I should take care of.

NP! I will take a look later today

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.

Use Viper for controller configuration loading

2 participants