Skip to content

Commit

Permalink
enable creating a rancher client by directly passing a *Config
Browse files Browse the repository at this point in the history
  • Loading branch information
git-ival committed Oct 9, 2024
1 parent ebfd445 commit 96b8f73
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions clients/rancher/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit 96b8f73

Please sign in to comment.