Skip to content

Commit

Permalink
Collect resource errors when applying stacks
Browse files Browse the repository at this point in the history
This allows k0s to apply stacks that are not well ordered, i.e. that
don't declare dependencies before their dependents. By attempting
to apply each resource, the dependencies will eventually be applied,
allowing the dependencies to be applied on the next retry.

Signed-off-by: Tom Wieczorek <[email protected]>
  • Loading branch information
twz123 committed May 29, 2024
1 parent 4523f68 commit ee7d523
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions pkg/applier/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ func (s *Stack) Apply(ctx context.Context, prune bool) error {
}
}

var errs []error
for _, resource := range sortedResources {
s.prepareResource(resource)
mapping, err := getRESTMapping(mapper, ptr.To(resource.GroupVersionKind()))
if err != nil {
return err
errs = append(errs, err)
continue
}
var drClient dynamic.ResourceInterface
if mapping.Scope.Name() == meta.RESTScopeNameNamespace {
Expand All @@ -100,14 +102,18 @@ func (s *Stack) Apply(ctx context.Context, prune bool) error {
if apiErrors.IsNotFound(err) {
created, err := drClient.Create(ctx, resource, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("cannot create resource %s: %w", resource.GetName(), err)
err = fmt.Errorf("cannot create resource %s: %w", resource.GetName(), err)
errs = append(errs, err)
continue
}
if isCRD(created) {
s.waitForCRD(ctx, created.GetName())
mapper.Reset() // so that the created CRD gets rediscovered
}
} else if err != nil {
return fmt.Errorf("unknown api error: %w", err)
err = fmt.Errorf("unknown api error: %w", err)
errs = append(errs, err)
continue
} else { // The resource already exists, we need to update/patch it
localChecksum := resource.GetAnnotations()[ChecksumAnnotation]
if serverResource.GetAnnotations()[ChecksumAnnotation] == localChecksum {
Expand All @@ -124,7 +130,9 @@ func (s *Stack) Apply(ctx context.Context, prune bool) error {
resource, err = s.patchResource(ctx, drClient, serverResource, resource)
}
if err != nil {
return fmt.Errorf("can't update resource: %w", err)
err = fmt.Errorf("can't update resource: %w", err)
errs = append(errs, err)
continue
}
if isCRD(resource) {
s.waitForCRD(ctx, resource.GetName())
Expand All @@ -134,11 +142,15 @@ func (s *Stack) Apply(ctx context.Context, prune bool) error {
s.keepResource(resource)
}

if prune {
err = s.prune(ctx, mapper)
if len(errs) > 0 {
return errors.Join(errs...)
}

if !prune {
return nil
}

return err
return s.prune(ctx, mapper)
}

// waitForCRD waits 5 seconds for a CRD to become established on a best-effort basis.
Expand Down

0 comments on commit ee7d523

Please sign in to comment.