Skip to content

Commit

Permalink
use env when set
Browse files Browse the repository at this point in the history
  • Loading branch information
matmerr committed Aug 26, 2024
1 parent 1e043b5 commit 88e1345
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
37 changes: 14 additions & 23 deletions cmd/legacy/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,18 @@ func (d *Daemon) Start() error {
}

fmt.Println("init client-go")

var cfg *rest.Config
cfg, err = kcfg.GetConfig()
if err != nil {
panic(err)
if kubeconfig := os.Getenv("KUBECONFIG"); kubeconfig != "" {
fmt.Println("KUBECONFIG set, using kubeconfig: ", kubeconfig)
cfg, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return fmt.Errorf("creating controller-runtime manager: %w", err)
}
} else {
cfg, err = kcfg.GetConfig()
if err != nil {
panic(err)
}
}

fmt.Println("api server: ", cfg.Host)
Expand Down Expand Up @@ -192,25 +199,8 @@ func (d *Daemon) Start() error {

mgr, err := crmgr.New(cfg, mgrOption)
if err != nil {
env := "KUBECONFIG"
fmt.Println("failed to load kubeconfig via controller runtime: ", err)
fmt.Println("checking with env: ", env)
kubeconfig := os.Getenv("KUBECONFIG")
if kubeconfig == "" {
hardcode := "./kubeconfig"
fmt.Println("kubeconfig not found in env, using hardcoded path", hardcode)
kubeconfig = hardcode
}
cfg, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
mainLogger.Error("Unable build kubeconfig", zap.Error(err))
return fmt.Errorf("creating controller-runtime manager: %w", err)
}
mgr, err = crmgr.New(cfg, mgrOption)
if err != nil {
mainLogger.Error("Unable to start manager", zap.Error(err))
return fmt.Errorf("creating controller-runtime manager: %w", err)
}
mainLogger.Error("Unable to start manager", zap.Error(err))
return fmt.Errorf("creating controller-runtime manager: %w", err)
}

//+kubebuilder:scaffold:builder
Expand All @@ -222,6 +212,7 @@ func (d *Daemon) Start() error {
mainLogger.Fatal("Unable to set up ready check", zap.Error(addReadyCheckErr))
}

// k8s Client used for informers
cl := kubernetes.NewForConfigOrDie(mgr.GetConfig())

serverVersion, err := cl.Discovery().ServerVersion()
Expand Down
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ func init() {
rootCmd.Flags().StringVar(&probeAddr, "health-probe-bind-address", ":18081", "The address the probe endpoint binds to.")
rootCmd.Flags().BoolVar(&enableLeaderElection, "leader-elect", false, "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
rootCmd.Flags().StringVar(&cfgFile, "config", configFileName, "config file")
rootCmd.Flags().StringVar(&kubeConfigFileName, "kubeconfig", kubeConfigFileName, "noop we just need cobra to not check since controller runtime can use this flag") // this is read during GetConfigOrDie, not explicitly passed to any of our logic

// this is read during GetConfigOrDie, not explicitly passed to any of our logic
rootCmd.Flags().StringVar(&kubeConfigFileName, "kubeconfig", kubeConfigFileName, "noop we just need cobra to not check since controller runtime can use this flag")
}

func Execute() {
Expand Down

0 comments on commit 88e1345

Please sign in to comment.