Skip to content

Commit

Permalink
fix(cloud): Correctly access data to catch errors
Browse files Browse the repository at this point in the history
Signed-off-by: Cezar Craciunoiu <cezar.craciunoiu@unikraft.io>
  • Loading branch information
craciunoiuc committed Aug 14, 2024
1 parent c5affd2 commit f7b613c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
7 changes: 6 additions & 1 deletion internal/cli/kraft/cloud/compose/down/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,14 @@ func (opts *DownOptions) Run(ctx context.Context, args []string) error {
return fmt.Errorf("getting instances: %w", err)
}

insts, err := instResp.AllOrErr()
if err != nil {
return fmt.Errorf("getting instances: %w", err)
}

instances = []string{}

for _, instance := range instResp.Data.Entries {
for _, instance := range insts {
if instance.Message != "" {
log.G(ctx).Error(instance.Message)
continue
Expand Down
35 changes: 25 additions & 10 deletions internal/cli/kraft/cloud/compose/up/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func Up(ctx context.Context, opts *UpOptions, args ...string) error {
return err
}

instResps := kcclient.ServiceResponse[kcinstances.GetResponseItem]{}
insts := []kcinstances.GetResponseItem{}

for _, serviceName := range args {
service, ok := opts.Project.Services[serviceName]
Expand Down Expand Up @@ -223,10 +223,13 @@ func Up(ctx context.Context, opts *UpOptions, args ...string) error {
}

instResp, err := opts.Client.Instances().WithMetro(opts.Metro).Get(ctx, service.Name)
if err == nil && len(instResp.Data.Entries) == 1 && instResp.Data.Entries[0].Error == nil {
instResps.Data.Entries = append(instResps.Data.Entries, instResp.Data.Entries...)
log.G(ctx).WithField("name", service.Name).Info("service already exists")
continue
if err == nil {
inst, err := instResp.FirstOrErr()
if err == nil && inst != nil {
insts = append(insts, *inst)
log.G(ctx).WithField("name", service.Name).Info("service already exists")
continue
}
}

// Handle environmental variables.
Expand Down Expand Up @@ -256,12 +259,17 @@ func Up(ctx context.Context, opts *UpOptions, args ...string) error {

var volumes []string
for _, volume := range service.Volumes {
vol, ok := volResps[volume.Source]
volResp, ok := volResps[volume.Source]
if !ok {
continue
}

volumes = append(volumes, fmt.Sprintf("%s:%s", vol.Data.Entries[0].UUID, volume.Target))
vol, err := volResp.FirstOrErr()
if err != nil {
continue
}

volumes = append(volumes, fmt.Sprintf("%s:%s", vol.UUID, volume.Target))
}

name := strings.ReplaceAll(fmt.Sprintf("%s-%s", opts.Project.Name, service.Name), "_", "-")
Expand Down Expand Up @@ -335,14 +343,19 @@ func Up(ctx context.Context, opts *UpOptions, args ...string) error {
Volumes: volumes,
}, service.Command...)
if err != nil {
return err
return fmt.Errorf("creating instance: %w", err)
}

inst, err := instResp.FirstOrErr()
if err != nil || inst == nil {
return fmt.Errorf("creating instance: %w", err)
}

instResps.Data.Entries = append(instResps.Data.Entries, instResp.Data.Entries...)
insts = append(insts, *inst)
}

var instances []string
for _, inst := range instResps.Data.Entries {
for _, inst := range insts {
instances = append(instances, inst.Name)
}

Expand Down Expand Up @@ -370,6 +383,8 @@ func Up(ctx context.Context, opts *UpOptions, args ...string) error {
Output: "table",
}, instances...)
} else {
instResps := kcclient.ServiceResponse[kcinstances.GetResponseItem]{}
instResps.Data.Entries = insts
return utils.PrintInstances(ctx, "table", instResps)
}
}
Expand Down
18 changes: 14 additions & 4 deletions internal/cli/kraft/cloud/instance/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,18 @@ func Remove(ctx context.Context, opts *RemoveOptions, args ...string) error {
return fmt.Errorf("could not list instances: %w", err)
}

if len(instListResp.Data.Entries) == 0 {
instList, err := instListResp.AllOrErr()
if err != nil {
return fmt.Errorf("could not list instances: %w", err)
}

if len(instList) == 0 {
log.G(ctx).Info("no instances found")
return nil
}

uuids := make([]string, 0, len(instListResp.Data.Entries))
for _, instItem := range instListResp.Data.Entries {
uuids := make([]string, 0, len(instList))
for _, instItem := range instList {
uuids = append(uuids, instItem.UUID)
}

Expand All @@ -131,8 +136,13 @@ func Remove(ctx context.Context, opts *RemoveOptions, args ...string) error {
return fmt.Errorf("could not get instances: %w", err)
}

instInfos, err := instInfosResp.AllOrErr()
if err != nil {
return fmt.Errorf("could not get instances: %w", err)
}

var stoppedUuids []string
for _, instInfo := range instInfosResp.Data.Entries {
for _, instInfo := range instInfos {
if kcinstances.State(instInfo.State) == kcinstances.StateStopped {
stoppedUuids = append(stoppedUuids, instInfo.UUID)
}
Expand Down
9 changes: 7 additions & 2 deletions internal/cli/kraft/cloud/service/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,18 @@ func Remove(ctx context.Context, opts *RemoveOptions, args ...string) error {
return fmt.Errorf("listing service: %w", err)
}

if len(sgListResp.Data.Entries) == 0 {
sgList, err := sgListResp.AllOrErr()
if err != nil {
return fmt.Errorf("listing service: %w", err)
}

if len(sgList) == 0 {
log.G(ctx).Info("no service found")
return nil
}

args = []string{}
for _, sgItem := range sgListResp.Data.Entries {
for _, sgItem := range sgList {
args = append(args, sgItem.Name)
}
}
Expand Down

0 comments on commit f7b613c

Please sign in to comment.