Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD report data for failed InstallPlans #125

Open
acmenezes opened this issue Jul 28, 2022 · 2 comments
Open

ADD report data for failed InstallPlans #125

acmenezes opened this issue Jul 28, 2022 · 2 comments
Labels
future-work kind/feature Categorizes issue or PR as related to a new feature.

Comments

@acmenezes
Copy link
Contributor

InstallPlan may contain information for tracing and debugging that can later be used for improving reporting on the tool.

@acmenezes acmenezes added the kind/feature Categorizes issue or PR as related to a new feature. label Jul 28, 2022
@acmenezes
Copy link
Contributor Author

acmenezes commented Jul 28, 2022

Here is the old code that can be repurposed to get data from InstallPlan:

package operator

import (
	"context"
	"fmt"
	"time"

	operatorv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/apimachinery/pkg/util/wait"
	runtimeClient "sigs.k8s.io/controller-runtime/pkg/client"
)

// ApproveInstallPlan waits and approves installPlans
func (c operatorClient) ApproveInstallPlan(ctx context.Context, sub *operatorv1alpha1.Subscription) error {
	subKey := types.NamespacedName{
		Namespace: sub.GetNamespace(),
		Name:      sub.GetName(),
	}

	ipCheck := wait.ConditionFunc(func() (done bool, err error) {
		if err := c.Client.Get(ctx, subKey, sub); err != nil {
			return false, err
		}
		if sub.Status.InstallPlanRef != nil {
			return true, nil
		}
		return false, nil
	})

	if err := wait.PollImmediateUntil(200*time.Millisecond, ipCheck, ctx.Done()); err != nil {
		logger.Errorf("install plan is not available for the subscription %s: %w", sub.Name, err)
		return fmt.Errorf("install plan is not available for the subscription %s: %v", sub.Name, err)
	}

	err := c.installPlanApprove(sub.ObjectMeta.Namespace)
	if err != nil {
		logger.Debugf("Error creating subscriptions: %w", err)
		return err
	}

	return nil
}

// InstallPlanApprove logic
// TODO: consolidate ApproveInstallPlan and InstallPlanApprove in a single and cleaner function
func (c operatorClient) installPlanApprove(namespace string) error {
	installPlanList := operatorv1alpha1.InstallPlanList{}

	listOpts := runtimeClient.ListOptions{
		Namespace: namespace,
	}

	err := c.Client.List(context.Background(), &installPlanList, &listOpts)
	if err != nil {
		logger.Errorf("Unable to list InstallPlans in Namespace %s: %w", namespace, err)
		return err
	}

	if len(installPlanList.Items) == 0 {
		logger.Errorf("no installPlan found in namespace %s: %w", namespace, err)
		return fmt.Errorf("no installPlan found in namespace %s", fmt.Sprint(len(installPlanList.Items)))
	}

	installPlan := operatorv1alpha1.InstallPlan{}

	err = c.Client.Get(context.Background(), types.NamespacedName{Name: installPlanList.Items[0].ObjectMeta.Name, Namespace: namespace}, &installPlan)

	if err != nil {
		logger.Errorf("no installPlan found in namespace %s: %w", namespace, err)
		return err
	}

	if installPlan.Spec.Approval == operatorv1alpha1.ApprovalManual {

		installPlan.Spec.Approved = true
		logger.Debugf("%s installPlan approved in Namespace %s", installPlan.ObjectMeta.Name, namespace)
		err := c.Client.Update(context.Background(), &installPlan)
		if err != nil {
			logger.Errorf("Error: %w", err)
			return err
		}
	}
	return nil
}

@mgoerens
Copy link
Contributor

Also check the refactored version of the code above. Available here: https://github.com/opdev/opcap/blob/645e401e8ab7428895cfdc1f0b4efc9e8ace5f23/internal/operator/install_plan.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
future-work kind/feature Categorizes issue or PR as related to a new feature.
Projects
None yet
Development

No branches or pull requests

3 participants