From 7b8e051877c71550347b66ee4f156b2aba5688a0 Mon Sep 17 00:00:00 2001 From: Francesco Romani Date: Wed, 31 Jul 2024 12:06:32 +0200 Subject: [PATCH] cmd: lazily create client to avoid to break the render flow, create the client lazily, not eagerly. Signed-off-by: Francesco Romani --- cmd/deployer/main.go | 8 -------- pkg/commands/root.go | 5 +++++ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/cmd/deployer/main.go b/cmd/deployer/main.go index 23c76aec..556af745 100644 --- a/cmd/deployer/main.go +++ b/cmd/deployer/main.go @@ -28,7 +28,6 @@ import ( "k8s.io/klog/v2/klogr" ctrllog "sigs.k8s.io/controller-runtime/pkg/log" - "github.com/k8stopologyawareschedwg/deployer/pkg/clientutil" "github.com/k8stopologyawareschedwg/deployer/pkg/commands" "github.com/k8stopologyawareschedwg/deployer/pkg/deployer" "github.com/k8stopologyawareschedwg/deployer/pkg/options" @@ -65,14 +64,7 @@ func NewVersionCommand(env *deployer.Environment, commonOpts *options.Options) * func main() { ctrllog.SetLogger(klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog))) - cli, err := clientutil.New() - if err != nil { - fmt.Fprintf(os.Stderr, "%v\n", err) - os.Exit(1) - } - env := deployer.Environment{ - Cli: cli, Ctx: context.Background(), Log: stdr.New(log.New(os.Stderr, "", log.LstdFlags)), } diff --git a/pkg/commands/root.go b/pkg/commands/root.go index be0cff03..de57e3a8 100644 --- a/pkg/commands/root.go +++ b/pkg/commands/root.go @@ -120,6 +120,11 @@ func PostSetupOptions(env *deployer.Environment, commonOpts *options.Options, in wait.SetBaseValues(commonOpts.WaitInterval, commonOpts.WaitTimeout) if internalOpts.replicas < 0 { + err := env.EnsureClient() + if err != nil { + return err + } + env.Log.V(4).Info("autodetecting replicas from control plane") info, err := detect.ControlPlaneFromLister(env.Ctx, env.Cli) if err != nil {