From 96b8f73b5a009bae3dd74dd614d84358b050bd11 Mon Sep 17 00:00:00 2001 From: Iramis Valentin <85186645+git-ival@users.noreply.github.com> Date: Wed, 9 Oct 2024 18:37:45 +0000 Subject: [PATCH] enable creating a rancher client by directly passing a *Config --- clients/rancher/client.go | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/clients/rancher/client.go b/clients/rancher/client.go index e2745e77..3d799f4c 100644 --- a/clients/rancher/client.go +++ b/clients/rancher/client.go @@ -68,16 +68,35 @@ func NewClient(bearerToken string, session *session.Session) (*Client, error) { if bearerToken == "" { bearerToken = rancherConfig.AdminToken } + rancherConfig.AdminToken = bearerToken c := &Client{ RancherConfig: rancherConfig, Flags: &environmentFlags, } - session.CleanupEnabled = *rancherConfig.Cleanup + return newClient(c, rancherConfig, session) +} + +// NewClientForConfig is the constructor for initializing a rancher Client for the given config and session. +func NewClientForConfig(config *Config, session *session.Session) (*Client, error) { + environmentFlags := environmentflag.NewEnvironmentFlags() + environmentflag.LoadEnvironmentFlags(environmentflag.ConfigurationFileKey, environmentFlags) + c := &Client{ + RancherConfig: config, + Flags: &environmentFlags, + } + + return newClient(c, config, session) +} + +func newClient(c *Client, config *Config, session *session.Session) (*Client, error) { + if session != nil { + session.CleanupEnabled = *config.Cleanup + } var err error - restConfig := newRestConfig(bearerToken, rancherConfig) + restConfig := newRestConfig(config.AdminToken, config) c.restConfig = restConfig c.Session = session c.Management, err = management.NewClient(clientOpts(restConfig, c.RancherConfig)) @@ -94,8 +113,8 @@ func NewClient(bearerToken string, session *session.Session) (*Client, error) { c.Steve.Ops.Session = session - if rancherConfig.RancherCLI { - c.CLI, err = ranchercli.NewClient(session, bearerToken, rancherConfig.Host, c.Management) + if config.RancherCLI { + c.CLI, err = ranchercli.NewClient(session, config.AdminToken, config.Host, c.Management) if err != nil { return nil, err } @@ -115,7 +134,7 @@ func NewClient(bearerToken string, session *session.Session) (*Client, error) { c.WranglerContext = wranglerContext - splitBearerKey := strings.Split(bearerToken, ":") + splitBearerKey := strings.Split(config.AdminToken, ":") token, err := c.Management.Token.ByID(splitBearerKey[0]) if err != nil { return nil, err