Skip to content

Commit 99f8d41

Browse files
authored
feat: add clusterId helm value to the agent installation (#671)
* add clusterId helm value to the agent installation * make cluster-id optional
1 parent 5357268 commit 99f8d41

File tree

6 files changed

+25
-14
lines changed

6 files changed

+25
-14
lines changed

cmd/command/cd/cd.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func Commands(clients client.Plural, helmConfiguration *action.Configuration) []
7878
Flags: []cli.Flag{
7979
cli.StringFlag{Name: "url", Usage: "console url", Required: true},
8080
cli.StringFlag{Name: "token", Usage: "deployment token", Required: true},
81+
cli.StringFlag{Name: "cluster-id", Usage: "cluster id to install the operator for", Required: false},
8182
cli.StringFlag{Name: "values", Usage: "values file to use for the deployment agent helm chart", Required: false},
8283
cli.StringFlag{Name: "chart-loc", Usage: "URL or filepath of helm chart tar file. Use if not wanting to install helm chart from default plural repository.", Required: false},
8384
cli.BoolFlag{Name: "force", Usage: "ignore checking if the current cluster is correct"},
@@ -125,22 +126,28 @@ func Commands(clients client.Plural, helmConfiguration *action.Configuration) []
125126
}
126127

127128
func (p *Plural) handleInstallDeploymentsOperator(c *cli.Context) error {
129+
cliClusterId := c.String("cluster-id")
128130
if !c.Bool("force") {
129-
confirm, err := confirmCluster(c.String("url"), c.String("token"))
131+
confirm, clusterId, err := confirmCluster(c.String("url"), c.String("token"))
130132
if err != nil {
131133
return err
132134
}
133135
if !confirm {
134136
return nil
135137
}
138+
cliClusterId = clusterId
139+
}
140+
141+
if cliClusterId == "" {
142+
return fmt.Errorf("cluster id must be provided")
136143
}
137144

138145
// we don't care if this fails to init as this command can be auth-less
139146
if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil {
140147
utils.Warn("Console client was not initialized, reason: %s", err.Error())
141148
}
142149

143-
return p.DoInstallOperator(c.String("url"), c.String("token"), c.String("values"), c.String("chart-loc"))
150+
return p.DoInstallOperator(c.String("url"), c.String("token"), c.String("values"), c.String("chart-loc"), cliClusterId)
144151
}
145152

146153
func (p *Plural) handleUninstallOperator(_ *cli.Context) error {
@@ -151,20 +158,20 @@ func (p *Plural) handleUninstallOperator(_ *cli.Context) error {
151158
return console.UninstallAgent(console.OperatorNamespace)
152159
}
153160

154-
func confirmCluster(url, token string) (bool, error) {
161+
func confirmCluster(url, token string) (bool, string, error) {
155162
consoleClient, err := console.NewConsoleClient(token, url)
156163
if err != nil {
157-
return false, err
164+
return false, "", err
158165
}
159166

160167
myCluster, err := consoleClient.MyCluster()
161168
if err != nil {
162-
return false, err
169+
return false, "", err
163170
}
164171

165172
clusterFragment, err := consoleClient.GetCluster(&myCluster.MyCluster.ID, nil)
166173
if err != nil {
167-
return false, err
174+
return false, "", err
168175
}
169176

170177
handle := "-"
@@ -175,7 +182,7 @@ func confirmCluster(url, token string) (bool, error) {
175182
if clusterFragment.Distro != nil {
176183
provider = string(*clusterFragment.Distro)
177184
}
178-
return common.Confirm(fmt.Sprintf("Are you sure you want to install deploy operator for the cluster:\nName: %s\nHandle: %s\nProvider: %s\n", myCluster.MyCluster.Name, handle, provider), "PLURAL_INSTALL_AGENT_CONFIRM"), nil
185+
return common.Confirm(fmt.Sprintf("Are you sure you want to install deploy operator for the cluster:\nName: %s\nHandle: %s\nProvider: %s\n", myCluster.MyCluster.Name, handle, provider), "PLURAL_INSTALL_AGENT_CONFIRM"), myCluster.MyCluster.ID, nil
179186
}
180187

181188
func (p *Plural) HandleCdLogin(c *cli.Context) (err error) {

cmd/command/cd/cd_clusters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ func (p *Plural) handleClusterBootstrap(c *cli.Context) error {
536536

537537
deployToken := *existing.CreateCluster.DeployToken
538538
utils.Highlight("installing agent on %s with url %s\n", c.String("name"), p.ConsoleClient.Url())
539-
return p.DoInstallOperator(url, deployToken, c.String("values"), c.String("chart-loc"))
539+
return p.DoInstallOperator(url, deployToken, c.String("values"), c.String("chart-loc"), existing.CreateCluster.ID)
540540
}
541541

542542
func getMetadataJson(val string) (*string, error) {

cmd/command/edge/bootstrap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (p *Plural) handleEdgeBootstrap(c *cli.Context) error {
5656
if agentUrl, err := p.ConsoleClient.AgentUrl(cluster.CreateCluster.ID); err == nil {
5757
url = agentUrl
5858
}
59-
return p.DoInstallOperator(url, *cluster.CreateCluster.DeployToken, "", c.String("chart-loc"))
59+
return p.DoInstallOperator(url, *cluster.CreateCluster.DeployToken, "", c.String("chart-loc"), cluster.CreateCluster.ID)
6060
}
6161

6262
func (p *Plural) getClusterAttributes(registration *gqlclient.ClusterRegistrationFragment) (*gqlclient.ClusterAttributes, error) {

pkg/client/plural.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/pluralsh/plural-cli/pkg/utils/git"
99
"github.com/pluralsh/polly/algorithms"
10+
"github.com/samber/lo"
1011
apierrors "k8s.io/apimachinery/pkg/api/errors"
1112

1213
"github.com/urfave/cli"
@@ -196,7 +197,7 @@ func (p *Plural) HandleInit(c *cli.Context) error {
196197
return nil
197198
}
198199

199-
func (p *Plural) DoInstallOperator(url, token, values, chart_loc string) error {
200+
func (p *Plural) DoInstallOperator(url, token, values, chart_loc, clusterId string) error {
200201
err := p.InitKube()
201202
if err != nil {
202203
return err
@@ -249,7 +250,7 @@ func (p *Plural) DoInstallOperator(url, token, values, chart_loc string) error {
249250
}
250251
}
251252
vals = algorithms.Merge(vals, globalVals)
252-
err = console.InstallAgent(url, token, console.OperatorNamespace, version, chart_loc, vals)
253+
err = console.InstallAgent(url, token, console.OperatorNamespace, version, chart_loc, clusterId, vals)
253254
if err == nil {
254255
utils.Success("Deployment operator installed successfully\n")
255256
}
@@ -263,11 +264,13 @@ func (p *Plural) ReinstallOperator(c *cli.Context, id, handle *string, chart_loc
263264
}
264265

265266
url := p.ConsoleClient.ExtUrl()
267+
clusterId := lo.FromPtr(id)
266268
if cluster, err := p.ConsoleClient.GetCluster(id, handle); err == nil {
269+
clusterId = cluster.ID
267270
if agentUrl, err := p.ConsoleClient.AgentUrl(cluster.ID); err == nil {
268271
url = agentUrl
269272
}
270273
}
271274

272-
return p.DoInstallOperator(url, deployToken, c.String("values"), chart_loc)
275+
return p.DoInstallOperator(url, deployToken, c.String("values"), chart_loc, clusterId)
273276
}

pkg/console/agent.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,11 @@ func IsAlreadyAgentInstalled(k8sClient *kubernetes.Clientset) (bool, error) {
9090
return false, nil
9191
}
9292

93-
func InstallAgent(consoleURL, token, namespace, version, helmChartLoc string, values map[string]interface{}) error {
93+
func InstallAgent(consoleURL, token, namespace, version, helmChartLoc, clusterID string, values map[string]interface{}) error {
9494
vals := algorithms.Merge(map[string]interface{}{
9595
"secrets": map[string]string{"deployToken": token},
9696
"consoleUrl": consoleURL,
97+
"clusterId": clusterID,
9798
}, values)
9899

99100
config, err := helm.GetActionConfig(namespace)

pkg/console/clusters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (c *consoleClient) DetachCluster(id string) error {
9393
func (c *consoleClient) CreateCluster(attributes consoleclient.ClusterAttributes) (*consoleclient.CreateCluster, error) {
9494
newCluster, err := c.client.CreateCluster(c.ctx, attributes)
9595
if err != nil {
96-
return nil, api.GetErrorResponse(err, "CreateCluster")
96+
return nil, err
9797
}
9898
return newCluster, nil
9999
}

0 commit comments

Comments
 (0)