Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update several logic
  • Loading branch information
ZhuoxiLi committed Jan 28, 2022
commit 15be1ac5e804c3678fa66019783eec2e1d07b842
3 changes: 3 additions & 0 deletions controllers/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,7 @@ const (

//DefaultSubDeleteTimeout is the default timeout for deleting a subscription
DefaultSubDeleteTimeout = 10 * time.Minute

//DefaultCSVWaitPeriod is the default period for wait CSV ready
DefaultCSVWaitPeriod = 1 * time.Minute
)
87 changes: 46 additions & 41 deletions controllers/operatorchecker/operatorchecker_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@
package operatorchecker

import (
"fmt"
"time"
"errors"
"context"
"strings"
"time"

olmv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -41,62 +37,71 @@ type Reconciler struct {

// Reconcile watchs on the Subscription of the target namespace and apply the recovery to fixing the Subscription failed error
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, reconcileErr error) {
// Fetch the subscription instance
subscriptionInstance := &olmv1alpha1.Subscription{}
if err := r.Client.Get(ctx, req.NamespacedName, subscriptionInstance); err != nil {
subscriptionInstance, err := r.getSubscription(ctx, req)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}

klog.V(2).Info("Operator Checker is monitoring Subscription...")

if _, ok := subscriptionInstance.Labels[constant.OpreqLabel]; !ok {
return
return ctrl.Result{RequeueAfter: constant.DefaultRequeueDuration}, nil
}

if subscriptionInstance.Status.CurrentCSV == "" && subscriptionInstance.Status.State == "" {
// cover fresh install case
csv, err := r.getCSVBySubscription(ctx, subscriptionInstance)
csvList, err := r.getCSVBySubscription(ctx, subscriptionInstance)
if err != nil {
// not found csv
klog.Error(err)
return
return ctrl.Result{RequeueAfter: constant.DefaultRequeueDuration}, nil
}
if len(csvList) != 1 {
return ctrl.Result{RequeueAfter: constant.DefaultRequeueDuration}, nil
}
csv := csvList[0]

time.Sleep(constant.DefaultCSVWaitPeriod)
subscriptionInstance, err := r.getSubscription(ctx, req)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
err = wait.PollImmediate(time.Second*3, time.Minute*1, func() (bool, error) {
if subscriptionInstance.Status.CurrentCSV == "" && subscriptionInstance.Status.State == "" {
return false, nil
}
return true, nil
})
if subscriptionInstance.Status.CurrentCSV == "" && subscriptionInstance.Status.State == "" {
r.deleteCSV(ctx, csv.Name, csv.Namespace)
if err = r.deleteCSV(ctx, csv.Name, csv.Namespace); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
}
}

if subscriptionInstance.Status.CurrentCSV != "" && subscriptionInstance.Status.State == "UpgradePending" {
// cover upgrade case
csv, err := r.getCSVBySubscription(ctx, subscriptionInstance)
csvList, err := r.getCSVBySubscription(ctx, subscriptionInstance)
if err != nil {
// get multi versions of CSV
klog.Error(err)
return
return ctrl.Result{RequeueAfter: constant.DefaultRequeueDuration}, nil
}
if subscriptionInstance.Status.CurrentCSV != fmt.Sprintf("%v", csv.Spec.Version) {
err = wait.PollImmediate(time.Second*3, time.Minute*1, func() (bool, error) {
if subscriptionInstance.Status.CurrentCSV != "" && subscriptionInstance.Status.State == "UpgradePending" {
return false, nil
}
return true, nil
})
if len(csvList) != 1 {
return ctrl.Result{RequeueAfter: constant.DefaultRequeueDuration}, nil
}
csv := csvList[0]

if subscriptionInstance.Status.CurrentCSV != csv.Spec.Version.String() {
time.Sleep(constant.DefaultCSVWaitPeriod)
subscriptionInstance, err := r.getSubscription(ctx, req)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
if subscriptionInstance.Status.CurrentCSV != "" && subscriptionInstance.Status.State == "UpgradePending" {
r.deleteCSV(ctx, csv.Name, csv.Namespace)
if err = r.deleteCSV(ctx, csv.Name, csv.Namespace); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
}
}
}

return ctrl.Result{RequeueAfter: constant.DefaultRequeueDuration}, nil
}

func (r *Reconciler) getCSVBySubscription(ctx context.Context, subscriptionInstance *olmv1alpha1.Subscription) (*olmv1alpha1.ClusterServiceVersion, error) {
func (r *Reconciler) getCSVBySubscription(ctx context.Context, subscriptionInstance *olmv1alpha1.Subscription) ([]olmv1alpha1.ClusterServiceVersion, error) {
csvList := &olmv1alpha1.ClusterServiceVersionList{}
opts := []client.ListOption{
client.InNamespace(subscriptionInstance.Namespace),
Expand All @@ -111,22 +116,22 @@ func (r *Reconciler) getCSVBySubscription(ctx context.Context, subscriptionInsta
matchCSVList = append(matchCSVList, csv)
}
}
return matchCSVList, nil
}

if len(matchCSVList) == 1 {
return &matchCSVList[0], nil
func (r *Reconciler) getSubscription(ctx context.Context, req ctrl.Request) (*olmv1alpha1.Subscription, error) {
// Fetch the subscription instance
subscriptionInstance := &olmv1alpha1.Subscription{}
if err := r.Client.Get(ctx, req.NamespacedName, subscriptionInstance); err != nil {
return nil, err
}
return nil, errors.New("Fail to find matched CSV")
return subscriptionInstance, nil
}

func (r *Reconciler) deleteCSV(ctx context.Context, name, namespace string) error {
csvInstance := &olmv1alpha1.ClusterServiceVersion{}
csvKey := types.NamespacedName{
Name: name,
Namespace: namespace,
}
if err := r.Client.Get(ctx, csvKey, csvInstance); err != nil {
return client.IgnoreNotFound(err)
}
csvInstance.Name = name
csvInstance.Namespace = namespace
if err := r.Client.Delete(ctx, csvInstance); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just use the delete command

return client.IgnoreNotFound(err)
}
Expand Down