Skip to content

Commit

Permalink
Merge pull request #211 from stefanprodan/bundle-delete-revers
Browse files Browse the repository at this point in the history
Uninstall bundle instances in reverse order
  • Loading branch information
stefanprodan authored Oct 7, 2023
2 parents f68b7c3 + bf0ea52 commit 36290ec
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/timoni/bundle_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func applyBundleInstance(ctx context.Context, cuectx *cue.Context, instance engi
}

if !nsExists {
log.Info(colorizeJoin(colorizeNamespaceFromArgs(), ssa.CreatedAction))
log.Info(colorizeJoin(colorizeSubject("Namespace/"+instance.Namespace), ssa.CreatedAction))
}
} else {
log.Info(fmt.Sprintf("upgrading %s in namespace %s", instance.Name, instance.Namespace))
Expand Down
11 changes: 9 additions & 2 deletions cmd/timoni/bundle_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@ func deleteBundleByName(ctx context.Context, bundle string) error {
return err
}

for _, instance := range instances {
if len(instances) == 0 {
return fmt.Errorf("no instances found in bundle")
}

// delete in revers order (last installed, first to uninstall)
for index := len(instances) - 1; index >= 0; index-- {
instance := instances[index]
log.Info(fmt.Sprintf("deleting instance %s from bundle %s", instance.Name, bundleDelArgs.name))
if err := deleteBundleInstance(ctx, engine.BundleInstance{
Bundle: bundle,
Expand Down Expand Up @@ -159,7 +165,8 @@ func deleteBundleFromFile(ctx context.Context, cmd *cobra.Command) error {
return fmt.Errorf("no instances found in bundle")
}

for _, instance := range bundle.Instances {
for index := len(bundle.Instances) - 1; index >= 0; index-- {
instance := bundle.Instances[index]
log.Info(fmt.Sprintf("deleting instance %s", instance.Name))
if err := deleteBundleInstance(ctx, instance, bundleDelArgs.wait, bundleDelArgs.dryrun); err != nil {
return err
Expand Down
6 changes: 6 additions & 0 deletions cmd/timoni/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"context"
"io"
"sort"

apiv1 "github.com/stefanprodan/timoni/api/v1alpha1"

Expand Down Expand Up @@ -66,6 +67,11 @@ func runListCmd(cmd *cobra.Command, args []string) error {
return err
}

// alphabetical sort by instance name
sort.Slice(instances, func(i, j int) bool {
return instances[i].Name < instances[j].Name
})

var rows [][]string
for _, inv := range instances {
row := []string{}
Expand Down
28 changes: 20 additions & 8 deletions internal/runtime/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package runtime
import (
"context"
"fmt"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -110,29 +111,40 @@ func (s *StorageManager) Get(ctx context.Context, name, namespace string) (*apiv
// List returns the instances found in the given namespace.
func (s *StorageManager) List(ctx context.Context, namespace, bundle string) ([]*apiv1.Instance, error) {
var res []*apiv1.Instance
cmList := &corev1.SecretList{}
instanceList := &corev1.SecretList{}
labels := s.getOwnerLabels()
if bundle != "" {
labels[apiv1.BundleNameLabelKey] = bundle
}
err := s.resManager.Client().List(ctx, cmList, client.InNamespace(namespace), labels)
err := s.resManager.Client().List(ctx, instanceList, client.InNamespace(namespace), labels)
if err != nil {
return res, err
}

for _, cm := range cmList.Items {
if _, ok := cm.Data[storageDataKey]; !ok {
if len(instanceList.Items) == 0 {
return res, nil
}

var instances = instanceList.Items

// order list by installed date
sort.Slice(instances, func(i, j int) bool {
return instances[i].CreationTimestamp.Before(&instances[j].CreationTimestamp)
})

for _, instance := range instances {
if _, ok := instance.Data[storageDataKey]; !ok {
return res, fmt.Errorf("instance data not found in Secret/%s/%s",
cm.GetNamespace(), cm.GetName())
instance.GetNamespace(), instance.GetName())
}

var inst apiv1.Instance
err = json.Unmarshal(cm.Data[storageDataKey], &inst)
err = json.Unmarshal(instance.Data[storageDataKey], &inst)
if err != nil {
return res, fmt.Errorf("invalid instance found in Secret/%s/%s: %w",
cm.GetNamespace(), cm.GetName(), err)
instance.GetNamespace(), instance.GetName(), err)
}
inst.Labels = cm.Labels
inst.Labels = instance.Labels
res = append(res, &inst)
}

Expand Down

0 comments on commit 36290ec

Please sign in to comment.