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

Bug 2255315:[release-4.14] Surface dependent Operator upgradeable conditions #359

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 9 additions & 1 deletion bundle/manifests/odf-operator.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ metadata:
categories: Storage
console.openshift.io/plugins: '["odf-console"]'
containerImage: quay.io/ocs-dev/odf-operator:latest
createdAt: "2023-08-08T09:50:10Z"
createdAt: "2023-12-20T04:32:33Z"
description: OpenShift Data Foundation provides a common control plane for storage
solutions on OpenShift Container Platform.
olm.properties: '[{"type": "olm.maxOpenShiftVersion", "value": "4.15"}]'
Expand Down Expand Up @@ -335,6 +335,14 @@ spec:
- patch
- update
- watch
- apiGroups:
- operators.coreos.com
resources:
- operatorconditions
verbs:
- get
- list
- watch
- apiGroups:
- operators.coreos.com
resources:
Expand Down
8 changes: 8 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ rules:
- patch
- update
- watch
- apiGroups:
- operators.coreos.com
resources:
- operatorconditions
verbs:
- get
- list
- watch
- apiGroups:
- operators.coreos.com
resources:
Expand Down
146 changes: 144 additions & 2 deletions controllers/subscription_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,40 @@ import (
"github.com/go-logr/logr"
"go.uber.org/multierr"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

operatorv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
operatorv2 "github.com/operator-framework/api/pkg/operators/v2"
"github.com/operator-framework/operator-lib/conditions"
odfv1alpha1 "github.com/red-hat-storage/odf-operator/api/v1alpha1"
"github.com/red-hat-storage/odf-operator/pkg/util"
)

// SubscriptionReconciler reconciles a Subscription object
type SubscriptionReconciler struct {
client.Client
Scheme *runtime.Scheme
Recorder *EventReporter
Scheme *runtime.Scheme
Recorder *EventReporter
ConditionName string
OperatorCondition conditions.Condition
}

//+kubebuilder:rbac:groups=operators.coreos.com,resources=subscriptions,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=operators.coreos.com,resources=subscriptions/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=operators.coreos.com,resources=subscriptions/finalizers,verbs=update
//+kubebuilder:rbac:groups=operators.coreos.com,resources=installplans,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=operators.coreos.com,resources=clusterserviceversions/finalizers,verbs=update
//+kubebuilder:rbac:groups=operators.coreos.com,resources=operatorconditions,verbs=get;list;watch

// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.8.3/pkg/reconcile
Expand All @@ -65,6 +75,11 @@ func (r *SubscriptionReconciler) Reconcile(ctx context.Context, req ctrl.Request
return ctrl.Result{}, err
}

err = r.setOperatorCondition(logger, req.NamespacedName.Namespace)
if err != nil {
return ctrl.Result{}, err
}

err = r.ensureSubscriptions(logger, req.NamespacedName.Namespace)
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -117,6 +132,45 @@ func (r *SubscriptionReconciler) ensureSubscriptions(logger logr.Logger, namespa
return err
}

func (r *SubscriptionReconciler) setOperatorCondition(logger logr.Logger, namespace string) error {
ocdList := &operatorv2.OperatorConditionList{}
err := r.Client.List(context.TODO(), ocdList, client.InNamespace(namespace))
if err != nil {
logger.Error(err, "failed to list OperatorConditions")
return err
}

condNames := append(GetVendorCsvNames(StorageClusterKind), GetVendorCsvNames(FlashSystemKind)...)

condMap := make(map[string]struct{}, len(condNames))
for i := range condNames {
condMap[condNames[i]] = struct{}{}
}

for ocdIdx := range ocdList.Items {
// skip operatorconditions of not dependent operators
if _, exist := condMap[ocdList.Items[ocdIdx].GetName()]; !exist {
continue
}

ocd := &ocdList.Items[ocdIdx]
cond := getNotUpgradeableCond(ocd)
if cond != nil {
// operator is not upgradeable
msg := fmt.Sprintf("%s:%s", ocd.GetName(), cond.Message)
logger.Info("setting operator upgradeable status", "status", cond.Status)
return r.OperatorCondition.Set(context.TODO(), cond.Status,
conditions.WithReason(cond.Reason), conditions.WithMessage(msg))
}
}

// all operators are upgradeable
status := metav1.ConditionTrue
logger.Info("setting operator upgradeable status", "status", status)
return r.OperatorCondition.Set(context.TODO(), status,
conditions.WithReason("Dependents"), conditions.WithMessage("No dependent reports not upgradeable status"))
}

// SetupWithManager sets up the controller with the Manager.
func (r *SubscriptionReconciler) SetupWithManager(mgr ctrl.Manager) error {

Expand Down Expand Up @@ -151,10 +205,98 @@ func (r *SubscriptionReconciler) SetupWithManager(mgr ctrl.Manager) error {
},
}

conditionPredicate := predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return false
},
UpdateFunc: func(e event.UpdateEvent) bool {
// not mandatory but these checks wouldn't harm
if e.ObjectOld == nil || e.ObjectNew == nil {
return false
}
oldObj, _ := e.ObjectOld.(*operatorv2.OperatorCondition)
newObj, _ := e.ObjectNew.(*operatorv2.OperatorCondition)
if oldObj == nil || newObj == nil {
return false
}

// skip sending a reconcile event if our own condition is updated
if newObj.GetName() == r.ConditionName {
return false
}

// change in admin set conditions for upgradeability
oldOverride := util.Find(oldObj.Spec.Overrides, func(cond *metav1.Condition) bool {
return cond.Type == operatorv2.Upgradeable
})
newOverride := util.Find(newObj.Spec.Overrides, func(cond *metav1.Condition) bool {
return cond.Type == operatorv2.Upgradeable
})
if oldOverride != nil && newOverride == nil {
// override is removed
return true
}
if newOverride != nil {
if oldOverride == nil {
return true
}
return oldOverride.Status != newOverride.Status
}

// change in operator set conditions for upgradeability
oldCond := util.Find(oldObj.Status.Conditions, func(cond *metav1.Condition) bool {
return cond.Type == operatorv2.Upgradeable
})
newCond := util.Find(newObj.Status.Conditions, func(cond *metav1.Condition) bool {
return cond.Type == operatorv2.Upgradeable
})
if newCond != nil {
if oldCond == nil {
return true
}
return oldCond.Status != newCond.Status
}

return false
},
}
enqueueFromCondition := handler.EnqueueRequestsFromMapFunc(
func(ctx context.Context, obj client.Object) []reconcile.Request {
if _, ok := obj.(*operatorv2.OperatorCondition); !ok {
return []reconcile.Request{}
}
logger := log.FromContext(ctx)
sub, err := GetOdfSubscription(r.Client)
if err != nil {
logger.Error(err, "failed to get ODF Subscription")
return []reconcile.Request{}
}
return []reconcile.Request{{NamespacedName: types.NamespacedName{Name: sub.Name, Namespace: sub.Namespace}}}
},
)

return ctrl.NewControllerManagedBy(mgr).
For(&operatorv1alpha1.Subscription{},
builder.WithPredicates(generationChangedPredicate, subscriptionPredicate)).
Owns(&operatorv1alpha1.Subscription{},
builder.WithPredicates(generationChangedPredicate)).
Watches(&operatorv2.OperatorCondition{}, enqueueFromCondition, builder.WithPredicates(conditionPredicate)).
Complete(r)
}

func getNotUpgradeableCond(ocd *operatorv2.OperatorCondition) *metav1.Condition {
cond := util.Find(ocd.Spec.Overrides, func(cd *metav1.Condition) bool {
return cd.Type == operatorv2.Upgradeable
})
if cond != nil {
if cond.Status != "True" {
return cond
}
// if upgradeable is overridden we should skip checking operator set conditions
return nil
}

return util.Find(ocd.Status.Conditions, func(cd *metav1.Condition) bool {
return cd.Type == operatorv2.Upgradeable && cd.Status != "True"
})
}
23 changes: 12 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ require (
github.com/onsi/gomega v1.27.9
github.com/openshift/api v0.0.0-20230720094506-afcbe27aec7c
github.com/openshift/custom-resource-status v1.1.2
github.com/operator-framework/api v0.17.7-0.20230626210316-aa3e49803e7b
github.com/operator-framework/api v0.20.0
github.com/prometheus/client_golang v1.16.0
github.com/red-hat-storage/ocs-operator/v4 v4.0.0-20230816173704-d1c38f726b18
github.com/stretchr/testify v1.8.4
go.uber.org/multierr v1.11.0
k8s.io/api v0.27.3
k8s.io/apiextensions-apiserver v0.27.3
k8s.io/apimachinery v0.27.3
k8s.io/client-go v0.27.3
k8s.io/api v0.27.7
k8s.io/apiextensions-apiserver v0.27.7
k8s.io/apimachinery v0.27.7
k8s.io/client-go v0.27.7
sigs.k8s.io/controller-runtime v0.15.0
)

Expand Down Expand Up @@ -86,6 +86,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/noobaa/noobaa-operator/v5 v5.0.0-20230712090817-288892c02711 // indirect
github.com/operator-framework/operator-lib v0.11.1-0.20230717184314-6efbe3a22f6f
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
Expand All @@ -98,12 +99,12 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.9.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/term v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.9.3 // indirect
gomodules.xyz/jsonpatch/v2 v2.3.0 // indirect
Expand All @@ -112,7 +113,7 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/component-base v0.27.3 // indirect
k8s.io/component-base v0.27.7 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
k8s.io/utils v0.0.0-20230711102312-30195339c3c7 // indirect
Expand Down
Loading
Loading