Skip to content

Commit

Permalink
pkg/upgrade: deleteResources: add custom Matcher and signal job perfo…
Browse files Browse the repository at this point in the history
…rmed

Prepare to replace deleteDeploymentsAndCheck/deleteStatefulsetsAndCheck

It will require to filter with labels, so make it possible to
override matching against Path and Values with own function. Use old
method if not set.

Return true if Delete() was called. Since it's needed only for the
special cases, rename and make a wrapper with old interface.

Signed-off-by: Yauheni Kaliuta <[email protected]>
  • Loading branch information
ykaliuta committed Apr 9, 2024
1 parent 419701f commit 0d485d7
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions pkg/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ type JobSpec struct {
Values []string
// hook to call before action (delete)
Fixup func(ctx context.Context, c client.Client, obj *unstructured.Unstructured) error
// if set used to match the object instead of Path/Values
Matcher func(obj *unstructured.Unstructured) (bool, error)
}

// TODO: move to common place.
Expand Down Expand Up @@ -366,27 +368,39 @@ func CleanupExistingResource(ctx context.Context, cli client.Client, platform de
}

func deleteResources(ctx context.Context, c client.Client, jobs *[]JobSpec) error {
_, err := deleteResourcesWithDone(ctx, c, jobs)
return err
}

// deleteResourcesWithDone takes array of Jobs and deletes all matching resources. Returns true if any was deleted or error
func deleteResourcesWithDone(ctx context.Context, c client.Client, jobs *[]JobSpec) (bool, error) {
var errors *multierror.Error
didWork := false

for _, job := range *jobs {
err := deleteOneJob(ctx, c, job)
w, err := deleteOneJob(ctx, c, job)
errors = multierror.Append(errors, err)
if w {
didWork = true
}
}

return errors.ErrorOrNil()
return didWork, errors.ErrorOrNil()
}

func deleteOneJob(ctx context.Context, c client.Client, job JobSpec) error {
// deleteOneJob takes one Job and deletes all matching resources. Returns true if any was deleted or error
func deleteOneJob(ctx context.Context, c client.Client, job JobSpec) (bool, error) {
didWork := false
list := &unstructured.UnstructuredList{}
list.SetGroupVersionKind(job.Gvk)

err := c.List(ctx, list, client.InNamespace(job.Namespace))
if err != nil {
if errors.Is(err, &meta.NoKindMatchError{}) {
fmt.Printf("Could not delete %v: CRD not found\n", job.Gvk)
return nil
return didWork, nil
}
return fmt.Errorf("failed to list %s: %w", job.Gvk.Kind, err)
return didWork, fmt.Errorf("failed to list %s: %w", job.Gvk.Kind, err)
}

for _, item := range list.Items {
Expand Down Expand Up @@ -414,9 +428,13 @@ func deleteOneJob(ctx context.Context, c client.Client, job JobSpec) error {
return false, nil
}

if job.Matcher != nil {
isMatched = job.Matcher
}

matched, err := isMatched(&item)
if err != nil {
return err
return didWork, err
}

if !matched {
Expand All @@ -426,18 +444,19 @@ func deleteOneJob(ctx context.Context, c client.Client, job JobSpec) error {
if job.Fixup != nil {
err = job.Fixup(ctx, c, &item)
if err != nil {
return fmt.Errorf("failed to fix %s %s/%s: %w", job.Gvk.Kind, job.Namespace, item.GetName(), err)
return didWork, fmt.Errorf("failed to fix %s %s/%s: %w", job.Gvk.Kind, job.Namespace, item.GetName(), err)
}
}

err = c.Delete(ctx, &item)
if err != nil {
return fmt.Errorf("failed to delete %s %s/%s: %w", job.Gvk.Kind, job.Namespace, item.GetName(), err)
return didWork, fmt.Errorf("failed to delete %s %s/%s: %w", job.Gvk.Kind, job.Namespace, item.GetName(), err)
}
didWork = true
fmt.Println("Deleted object", item.GetName(), job.Gvk, "in namespace", job.Namespace)
}

return nil
return didWork, nil
}

func RemoveKfDefInstances(ctx context.Context, cli client.Client) error {
Expand Down

0 comments on commit 0d485d7

Please sign in to comment.