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

check whether the metadata differs (e.g. labels, annotations, etc.) #50

Open github-actions[bot] opened 2 years ago

github-actions[bot] commented 2 years ago

https://github.com/timflannagan/rukpak/blob/7bbdbf12679aff744ff6501e2ce7fcbf829b737c/internal/provisioner/plain/controllers/bundleinstance_controller.go#L319


        Status: metav1.ConditionTrue,
        Reason: rukpakv1alpha1.ReasonInstallationSucceeded,
    })
    bi.Status.InstalledBundleRef = util.BundleToObjectReference(bundle)

    deletedBundles, err := r.reconcileOldBundles(ctx, oldBundles, bundle)
    if err != nil {
        meta.SetStatusCondition(&bi.Status.Conditions, metav1.Condition{
            Type:    "Pivoted",
            Status:  metav1.ConditionFalse,
            Reason:  "ReplacementFailed",
            Message: err.Error(),
        })
        return ctrl.Result{}, err
    }
    l.Info("cleaned up old bundles selecting current bundleinstance", "deleted bundles", deletedBundles)

    return ctrl.Result{}, nil
}

// 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: %v", 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: %v", bundleSelector.String(), err)
    }
    bundles := []*rukpakv1alpha1.Bundle{}
    for _, bundle := range bundleList.Items {
        bundles = append(bundles, &bundle)
    }
    return bundles, 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, fmt.Errorf("failed to get bundles from label selector: %v", err)
    }
    sort.Sort(util.BundlesByCreationTimestamp(existingBundles))

    // TODO: do need to put a limit on how many bundles are generated to avoid
    // hotlooping scenarios?
    // TODO: check whether the metadata differs (e.g. labels, annotations, etc.)
    // TODO: move this to it's own dedicated function
    var b *rukpakv1alpha1.Bundle
    for _, bundle := range existingBundles {
        if !reflect.DeepEqual(bundle.Spec, bi.Spec.Template.Spec) {
            continue
        }
        b = bundle
        break
    }
    if len(existingBundles) == 0 || b == nil {
        controllerRef := metav1.NewControllerRef(bi, bi.GroupVersionKind())
        b = &rukpakv1alpha1.Bundle{
            ObjectMeta: metav1.ObjectMeta{
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.