Skip to content

Commit

Permalink
add function to load vars
Browse files Browse the repository at this point in the history
Signed-off-by: Leandro Mendes <[email protected]>
  • Loading branch information
theflockers committed Jan 27, 2025
1 parent 28b4de7 commit a862e47
Showing 1 changed file with 16 additions and 30 deletions.
46 changes: 16 additions & 30 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func init() {
//+kubebuilder:scaffold:scheme
}

// setupManager creates and returns a new ctrl.Manager instance
func setupManager() ctrl.Manager {

cf := ControllerFlags{}
readControllerFlags(&cf)

Check warning on line 88 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L86-L88

Added lines #L86 - L88 were not covered by tests

Expand Down Expand Up @@ -116,6 +116,7 @@ func setupManager() ctrl.Manager {
}),
Scheme: scheme,
})

if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
Expand All @@ -124,8 +125,9 @@ func setupManager() ctrl.Manager {
return mgr

Check warning on line 125 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L125

Added line #L125 was not covered by tests
}

// readControllerFlags reads the command line arguments, binds them to the controller, parses and sets up the controller
// logger
func readControllerFlags(c *ControllerFlags) {

flag.StringVar(&c.metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&c.probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&c.enableHttp2, "enable-http2", false, "Enable HTTP/2 for the metrics and webhook servers.")
Expand All @@ -149,38 +151,23 @@ func readControllerFlags(c *ControllerFlags) {
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

Check warning on line 151 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L151

Added line #L151 was not covered by tests
}

func main() {
// Set a default value for the DEFAULT_RELEASE_PVC environment variable
if os.Getenv("DEFAULT_RELEASE_PVC") == "" {
err := os.Setenv("DEFAULT_RELEASE_PVC", "release-pvc")
if err != nil {
setupLog.Error(err, "unable to setup DEFAULT_RELEASE_PVC environment variable")
os.Exit(1)
}
}

// Set a default value for the DEFAULT_RELEASE_WORKSPACE_NAME environment variable
if os.Getenv("DEFAULT_RELEASE_WORKSPACE_NAME") == "" {
err := os.Setenv("DEFAULT_RELEASE_WORKSPACE_NAME", "release-workspace")
// loadVariableFromEnv loads an environment variable setting a default value in case of empty or unset
func loadVariableFromEnv(name, defaultValue string) {
if os.Getenv(name) == "" {
err := os.Setenv(name, defaultValue)

Check warning on line 157 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L155-L157

Added lines #L155 - L157 were not covered by tests
if err != nil {
setupLog.Error(err, "unable to setup DEFAULT_RELEASE_WORKSPACE_NAME environment variable")
setupLog.Error(err, fmt.Sprintf("unable to setup %s environment variable", name))

Check warning on line 159 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L159

Added line #L159 was not covered by tests
os.Exit(1)
}
}
setupLog.Info(fmt.Sprintf("loaded env var %s with value %s", name, os.Getenv(name)))

Check warning on line 163 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L163

Added line #L163 was not covered by tests
}

// Set a default value for the DEFAULT_RELEASE_WORKSPACE_SIZE environment variable
if os.Getenv("DEFAULT_RELEASE_WORKSPACE_SIZE") == "" {
err := os.Setenv("DEFAULT_RELEASE_WORKSPACE_SIZE", "1Gi")
if err != nil {
setupLog.Error(err, "unable to setup DEFAULT_RELEASE_WORKSPACE_SIZE environment variable")
os.Exit(1)
}
}
err := os.Setenv("ENTERPRISE_CONTRACT_CONFIG_MAP", "enterprise-contract-service/ec-defaults")
if err != nil {
setupLog.Error(err, "unable to setup ENTERPRISE_CONTRACT_CONFIG_MAP environment variable")
os.Exit(1)
}
func main() {
loadVariableFromEnv("DEFAULT_RELEASE_PVC", "release-pvc")
loadVariableFromEnv("DEFAULT_RELEASE_WORKSPACE_NAME", "release-workspace")
loadVariableFromEnv("DEFAULT_RELEASE_WORKSPACE_SIZE", "1Gi")
loadVariableFromEnv("ENTERPRISE_CONTRACT_CONFIG_MAP", "enterprise-contract-service/ec-defaults")

Check warning on line 170 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L166-L170

Added lines #L166 - L170 were not covered by tests

mgr := setupManager()

Check warning on line 172 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L172

Added line #L172 was not covered by tests
setUpControllers(mgr)
Expand All @@ -206,7 +193,6 @@ func main() {

// setUpControllers sets up controllers.
func setUpControllers(mgr ctrl.Manager) {
fmt.Printf("%+v", mgr)
err := controller.SetupControllers(mgr, nil, controllers.EnabledControllers...)
if err != nil {
setupLog.Error(err, "unable to setup controllers")
Expand Down

0 comments on commit a862e47

Please sign in to comment.