timflannagan / rukpak

Rukpak runs in a Kubernetes cluster and defines an API for installing cloud native bundle content
Apache License 2.0
0 stars 0 forks source link

clean up this condition block #54

Open github-actions[bot] opened 2 years ago

github-actions[bot] commented 2 years ago

Bundle resource that's specified in the BundleInstance parameter's

spec.Template configuration is present on cluster, and if not, creates

a new Bundle resource matching that desired specification.

by metadata.CreationTimestamp in the case there's multiple Bundles

that match the label selector.

specified in the BI resource, and if not, generate a new matches the template.

Bundle resource that exist on cluster that match the label selector specified

in the BI parameter's spec.Selector field.

that no longer match the desired Bundle template.

https://github.com/timflannagan/rukpak/blob/c4bb0db08a01a70f1c25b25f913e7907da67a05a/internal/provisioner/plain/controllers/bundleinstance_controller.go#L264


        Status: metav1.ConditionTrue,
        Reason: rukpakv1alpha1.ReasonInstallationSucceeded,
    })
    bi.Status.InstalledBundleName = bundle.GetName()

    if _, err := r.reconcileOldBundles(ctx, oldBundles, bundle); err != nil {
        // TODO: clean up this condition block
        meta.SetStatusCondition(&bi.Status.Conditions, metav1.Condition{
            Type:    "Pivoted",
            Status:  metav1.ConditionFalse,
            Reason:  "ReplacementFailed",
            Message: err.Error(),
        })
        return ctrl.Result{}, err
    }

    return ctrl.Result{}, nil
}

// reconcileDesiredBundle is responsible for checking whether the desired
// Bundle resource that's specified in the BundleInstance parameter's
// spec.Template configuration is present on cluster, and if not, creates
// a new Bundle resource matching that desired specification.
func (r *BundleInstanceReconciler) reconcileDesiredBundle(ctx context.Context, bi *rukpakv1alpha1.BundleInstance) (*rukpakv1alpha1.Bundle, []*rukpakv1alpha1.Bundle, error) {
    // get the set of Bundle resources that already exist on cluster, and sort
    // by metadata.CreationTimestamp in the case there's multiple Bundles
    // that match the label selector.
    existingBundles, err := r.getBundlesForBundleInstanceSelector(ctx, bi)
    if err != nil {
        return nil, nil, err
    }
    sort.Sort(util.BundlesByCreationTimestamp(existingBundles))

    // check whether there's an existing Bundle that matches the desired Bundle template
    // specified in the BI resource, and if not, generate a new matches the template.
    b := util.CheckExistingBundlesMatchesTemplate(existingBundles, bi.Spec.Template)
    if b == nil {
        controllerRef := metav1.NewControllerRef(bi, bi.GroupVersionKind())
        b = &rukpakv1alpha1.Bundle{
            ObjectMeta: metav1.ObjectMeta{
                Name:            util.GenerateBundleName(bi.GetName()),
                OwnerReferences: []metav1.OwnerReference{*controllerRef},
                Labels:          bi.Spec.Template.Labels,
            },
            Spec: bi.Spec.Template.Spec,
        }
        if err := r.Create(ctx, b); err != nil {
            return nil, nil, err
        }
    }
    return b, existingBundles, err
}

// getBundlesForBundleInstanceSelector is responsible for returning a list of
// Bundle resource that exist on cluster that match the label selector specified
// in the BI parameter's spec.Selector field.
func (r *BundleInstanceReconciler) getBundlesForBundleInstanceSelector(ctx context.Context, bi *rukpakv1alpha1.BundleInstance) ([]*rukpakv1alpha1.Bundle, error) {
    bundleSelector, err := metav1.LabelSelectorAsSelector(bi.Spec.Selector)
    if err != nil {
        return nil, fmt.Errorf("failed to parse the %s label selector: %w", bi.Spec.Selector.String(), err)
    }
    bundleList := &rukpakv1alpha1.BundleList{}
    if err := r.List(ctx, bundleList, &client.ListOptions{
        LabelSelector: bundleSelector,
    }); err != nil {
        return nil, fmt.Errorf("failed to list bundles using the %s selector: %w", bundleSelector.String(), err)
    }
    bundles := []*rukpakv1alpha1.Bundle{}
    for _, b := range bundleList.Items {
        bundles = append(bundles, b.DeepCopy())
    }

    return bundles, nil
}

// reconcileOldBundles is responsible for garbage collecting any Bundles
// that no longer match the desired Bundle template.
func (r *BundleInstanceReconciler) reconcileOldBundles(ctx context.Context, oldBundles []*rukpakv1alpha1.Bundle, currBundle *rukpakv1alpha1.Bundle) (int, error) {
    var (
        errors         []error
        deletedBundles int
    )
    for _, b := range oldBundles {
        if b.GetName() == currBundle.GetName() {
            continue
        }
        if err := r.Delete(ctx, b); err != nil {
            errors = append(errors, err)
            continue
        }
        deletedBundles++
    }
    return deletedBundles, utilerrors.NewAggregate(errors)
}

type releaseState string

const (
github-actions[bot] commented 1 year ago

This issue has become stale because it has been open 60 days with no activity. The maintainers of this repo will remove this label during issue triage or it will be removed automatically after an update. Adding the lifecycle/frozen label will cause this issue to ignore lifecycle events.