Skip to content

Commit 0419fa5

Browse files
authored
fix CLI crash (#693)
1 parent 53a885f commit 0419fa5

File tree

4 files changed

+52
-35
lines changed

4 files changed

+52
-35
lines changed

pkg/client/plural.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package client
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
7+
"os/exec"
68
"strings"
79

810
gitutils "github.com/pluralsh/plural-cli/pkg/utils/git"
@@ -128,6 +130,8 @@ func (p *Plural) HandleInit(c *cli.Context) error {
128130
return fmt.Errorf("preflight checks failed: %w", err)
129131
}
130132
fmt.Println("Preflight checks failed, but continuing because --ignore-preflights was specified.")
133+
fmt.Println("Please note that you may encounter issues later on during provisioning.")
134+
displayWarning(err)
131135
}
132136

133137
if !git && common.Affirm("You're attempting to setup plural outside a git repository. Would you like us to set one up for you here?", "PLURAL_INIT_AFFIRM_SETUP_REPO") {
@@ -275,3 +279,21 @@ func (p *Plural) ReinstallOperator(c *cli.Context, id, handle *string, chart_loc
275279

276280
return p.DoInstallOperator(url, deployToken, c.String("values"), chart_loc, clusterId)
277281
}
282+
283+
func displayWarning(err error) {
284+
var outMsg string
285+
var ee *exec.ExitError
286+
// check if the error is of type *exec.ExitError to extract stderr
287+
// it happens for Azure CLI errors for example
288+
if errors.As(err, &ee) {
289+
stderr := strings.TrimSpace(string(ee.Stderr))
290+
if stderr != "" {
291+
outMsg = fmt.Sprintf("%s: %s", err.Error(), stderr)
292+
} else {
293+
outMsg = err.Error()
294+
}
295+
} else {
296+
outMsg = err.Error()
297+
}
298+
fmt.Printf("\033[33mWarning:\033[0m %s\n\n", outMsg)
299+
}

pkg/provider/aws.go

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,20 @@ var (
7272

7373
func mkAWS(conf config.Config) (provider *AWSProvider, err error) {
7474
ctx := context.Background()
75-
75+
provider = &AWSProvider{}
7676
iamSession, callerIdentity, err := GetAWSCallerIdentity(ctx)
7777
if err != nil {
78-
return nil, plrlErrors.ErrorWrap(err, "Failed to get AWS caller identity")
78+
return provider, plrlErrors.ErrorWrap(err, "Failed to get AWS caller identity")
79+
}
80+
provider.goContext = &ctx
81+
provider.ctx = map[string]any{
82+
"IAMSession": iamSession,
7983
}
80-
8184
fmt.Printf("\nUsing %s AWS profile\n", getAWSProfileName())
8285
fmt.Printf("Caller identity ARN: %s\n", lo.FromPtr(callerIdentity.Arn))
8386
fmt.Printf("Caller identity account: %s\n", lo.FromPtr(callerIdentity.Account))
8487
fmt.Printf("Caller identity user ID: %s\n\n", lo.FromPtr(callerIdentity.UserId))
8588

86-
provider = &AWSProvider{
87-
goContext: &ctx,
88-
ctx: map[string]any{
89-
"IAMSession": iamSession,
90-
},
91-
}
92-
9389
var awsSurvey = []*survey.Question{
9490
{
9591
Name: "cluster",
@@ -178,22 +174,22 @@ func (aws *AWSProvider) KubeContext() string {
178174
return fmt.Sprintf("arn:aws:eks:%s:%s:cluster/%s", aws.Region(), aws.project, aws.Cluster())
179175
}
180176

181-
func (p *AWSProvider) mkBucket(name string) error {
182-
client := p.storageClient
183-
_, err := client.HeadBucket(*p.goContext, &s3.HeadBucketInput{Bucket: &name})
177+
func (aws *AWSProvider) mkBucket(name string) error {
178+
client := aws.storageClient
179+
_, err := client.HeadBucket(*aws.goContext, &s3.HeadBucketInput{Bucket: &name})
184180

185181
if err != nil {
186182
bucket := &s3.CreateBucketInput{
187183
Bucket: &name,
188184
}
189185

190-
if p.Region() != "us-east-1" {
186+
if aws.Region() != "us-east-1" {
191187
bucket.CreateBucketConfiguration = &s3Types.CreateBucketConfiguration{
192-
LocationConstraint: s3Types.BucketLocationConstraint(p.Region()),
188+
LocationConstraint: s3Types.BucketLocationConstraint(aws.Region()),
193189
}
194190
}
195191

196-
_, err = client.CreateBucket(*p.goContext, bucket)
192+
_, err = client.CreateBucket(*aws.goContext, bucket)
197193
return err
198194
}
199195

@@ -231,29 +227,29 @@ func (aws *AWSProvider) Preflights() []*preflights.Preflight {
231227
}
232228

233229
func (aws *AWSProvider) Flush() error {
234-
if aws.writer == nil {
230+
if aws == nil || aws.writer == nil {
235231
return nil
236232
}
237233
return aws.writer()
238234
}
239235

240-
func (prov *AWSProvider) Permissions() (permissions.Checker, error) {
241-
return permissions.NewAwsChecker(*prov.goContext)
236+
func (aws *AWSProvider) Permissions() (permissions.Checker, error) {
237+
return permissions.NewAwsChecker(*aws.goContext)
242238
}
243239

244-
func (prov *AWSProvider) Decommision(node *v1.Node) error {
245-
cfg, err := awsConfig.LoadDefaultConfig(*prov.goContext)
240+
func (aws *AWSProvider) Decommision(node *v1.Node) error {
241+
cfg, err := awsConfig.LoadDefaultConfig(*aws.goContext)
246242

247243
if err != nil {
248244
return plrlErrors.ErrorWrap(err, "Failed to establish aws session")
249245
}
250246

251-
cfg.Region = prov.Region()
247+
cfg.Region = aws.Region()
252248

253249
name := "private-dns-name"
254250

255251
svc := ec2.NewFromConfig(cfg)
256-
instances, err := svc.DescribeInstances(*prov.goContext, &ec2.DescribeInstancesInput{
252+
instances, err := svc.DescribeInstances(*aws.goContext, &ec2.DescribeInstancesInput{
257253
Filters: []ec2Types.Filter{
258254
{Name: &name, Values: []string{node.Name}},
259255
},
@@ -265,7 +261,7 @@ func (prov *AWSProvider) Decommision(node *v1.Node) error {
265261

266262
instance := instances.Reservations[0].Instances[0]
267263

268-
_, err = svc.TerminateInstances(*prov.goContext, &ec2.TerminateInstancesInput{
264+
_, err = svc.TerminateInstances(*aws.goContext, &ec2.TerminateInstancesInput{
269265
InstanceIds: []string{*instance.InstanceId},
270266
})
271267

pkg/provider/azure.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type AzureProvider struct {
8888
}
8989

9090
func mkAzure(conf config.Config) (prov *AzureProvider, err error) {
91+
prov = &AzureProvider{}
9192
subId, tenID, subName, err := GetAzureAccount()
9293
if err != nil {
9394
return
@@ -129,14 +130,12 @@ func mkAzure(conf config.Config) (prov *AzureProvider, err error) {
129130
return
130131
}
131132

132-
prov = &AzureProvider{cluster,
133-
resourceGroup,
134-
"",
135-
location,
136-
map[string]any{"SubscriptionId": subId, "TenantId": tenID, "StorageAccount": storageAccount},
137-
nil,
138-
clients,
139-
}
133+
prov.cluster = cluster
134+
prov.resourceGroup = resourceGroup
135+
prov.bucket = ""
136+
prov.region = location
137+
prov.ctx = map[string]any{"SubscriptionId": subId, "TenantId": tenID, "StorageAccount": storageAccount}
138+
prov.clients = clients
140139

141140
projectManifest := manifest.ProjectManifest{
142141
Cluster: prov.Cluster(),
@@ -256,7 +255,7 @@ func (*AzureProvider) Permissions() (permissions.Checker, error) {
256255
}
257256

258257
func (az *AzureProvider) Flush() error {
259-
if az.writer == nil {
258+
if az == nil || az.writer == nil {
260259
return nil
261260
}
262261
return az.writer()

pkg/provider/gcp/provider.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func (in *Provider) Permissions() (permissions.Checker, error) {
185185
}
186186

187187
func (in *Provider) Flush() error {
188-
if in.writer == nil {
188+
if in == nil || in.writer == nil {
189189
return nil
190190
}
191191
return in.writer()
@@ -220,7 +220,7 @@ func NewProvider(options ...Option) (*Provider, error) {
220220

221221
for _, opt := range options {
222222
if err := opt(result); err != nil {
223-
return nil, err
223+
return result, err
224224
}
225225
}
226226

0 commit comments

Comments
 (0)