Skip to content

Commit e337f68

Browse files
committed
fix(sub): Only update subscriptions that have changes in status
Currently, when resolution happens, every subscription will get updated regardless if there are any changes applied or not. This resolves into unnecessary API update calls. This commit will filter out subscriptions that don't get changed and only changed ones get updated. Note: Remove an additional update API call from one of subscription sync methods as well. Signed-off-by: Vu Dinh <[email protected]>
1 parent 5812e0b commit e337f68

File tree

2 files changed

+64
-80
lines changed

2 files changed

+64
-80
lines changed

pkg/controller/operators/catalog/operator.go

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -962,15 +962,15 @@ func (o *Operator) syncResolvingNamespace(obj interface{}) error {
962962
// not-satisfiable error
963963
if _, ok := err.(solver.NotSatisfiable); ok {
964964
logger.WithError(err).Debug("resolution failed")
965-
subs = o.setSubsCond(subs, v1alpha1.SubscriptionResolutionFailed, "ConstraintsNotSatisfiable", err.Error(), true)
965+
subs = o.setSubsCond(subs, v1alpha1.SubscriptionResolutionFailed, "ConstraintsNotSatisfiable", err.Error(), corev1.ConditionTrue)
966966
_, updateErr := o.updateSubscriptionStatuses(subs)
967967
if updateErr != nil {
968968
logger.WithError(updateErr).Debug("failed to update subs conditions")
969969
return updateErr
970970
}
971971
return nil
972972
}
973-
subs = o.setSubsCond(subs, v1alpha1.SubscriptionResolutionFailed, "ErrorPreventedResolution", err.Error(), true)
973+
subs = o.setSubsCond(subs, v1alpha1.SubscriptionResolutionFailed, "ErrorPreventedResolution", err.Error(), corev1.ConditionTrue)
974974
_, updateErr := o.updateSubscriptionStatuses(subs)
975975
if updateErr != nil {
976976
logger.WithError(updateErr).Debug("failed to update subs conditions")
@@ -979,14 +979,6 @@ func (o *Operator) syncResolvingNamespace(obj interface{}) error {
979979
return err
980980
}
981981

982-
defer func() {
983-
subs = o.setSubsCond(subs, v1alpha1.SubscriptionResolutionFailed, "", "", false)
984-
_, updateErr := o.updateSubscriptionStatuses(subs)
985-
if updateErr != nil {
986-
logger.WithError(updateErr).Warn("failed to update subscription conditions")
987-
}
988-
}()
989-
990982
// create installplan if anything updated
991983
if len(updatedSubs) > 0 {
992984
logger.Debug("resolution caused subscription changes, creating installplan")
@@ -1017,17 +1009,35 @@ func (o *Operator) syncResolvingNamespace(obj interface{}) error {
10171009
return err
10181010
}
10191011
updatedSubs = o.setIPReference(updatedSubs, maxGeneration+1, installPlanReference)
1020-
for _, updatedSub := range updatedSubs {
1021-
for i, sub := range subs {
1022-
if sub.Name == updatedSub.Name && sub.Namespace == updatedSub.Namespace {
1023-
subs[i] = updatedSub
1024-
}
1025-
}
1026-
}
10271012
} else {
10281013
logger.Debugf("no subscriptions were updated")
10291014
}
10301015

1016+
// Remove resolutionfailed condition from subscriptions
1017+
subs = o.removeSubsCond(subs, v1alpha1.SubscriptionResolutionFailed)
1018+
newSub := true
1019+
for _, updatedSub := range updatedSubs {
1020+
for i, sub := range subs {
1021+
if sub.Name == updatedSub.Name && sub.Namespace == updatedSub.Namespace {
1022+
subs[i] = updatedSub
1023+
newSub = false
1024+
break
1025+
}
1026+
}
1027+
if newSub {
1028+
subs = append(subs, updatedSub)
1029+
continue
1030+
}
1031+
newSub = true
1032+
}
1033+
1034+
// Update subscriptions with all changes so far
1035+
_, updateErr := o.updateSubscriptionStatuses(subs)
1036+
if updateErr != nil {
1037+
logger.WithError(updateErr).Warn("failed to update subscription conditions")
1038+
return updateErr
1039+
}
1040+
10311041
return nil
10321042
}
10331043

@@ -1084,12 +1094,7 @@ func (o *Operator) ensureSubscriptionInstallPlanState(logger *logrus.Entry, sub
10841094
out.Status.CurrentCSV = out.Spec.StartingCSV
10851095
out.Status.LastUpdated = o.now()
10861096

1087-
updated, err := o.client.OperatorsV1alpha1().Subscriptions(sub.GetNamespace()).UpdateStatus(context.TODO(), out, metav1.UpdateOptions{})
1088-
if err != nil {
1089-
return nil, false, err
1090-
}
1091-
1092-
return updated, true, nil
1097+
return out, true, nil
10931098
}
10941099

10951100
func (o *Operator) ensureSubscriptionCSVState(logger *logrus.Entry, sub *v1alpha1.Subscription, querier SourceQuerier) (*v1alpha1.Subscription, bool, error) {
@@ -1225,23 +1230,50 @@ func (o *Operator) createInstallPlan(namespace string, gen int, subs []*v1alpha1
12251230
return reference.GetReference(res)
12261231
}
12271232

1228-
func (o *Operator) setSubsCond(subs []*v1alpha1.Subscription, condType v1alpha1.SubscriptionConditionType, reason, message string, setTrue bool) []*v1alpha1.Subscription {
1233+
// setSubsCond will set the condition to the subscription if it doesn't already
1234+
// exist or if it is different
1235+
// Only return the list of updated subscriptions
1236+
func (o *Operator) setSubsCond(subs []*v1alpha1.Subscription, condType v1alpha1.SubscriptionConditionType, reason, message string, status corev1.ConditionStatus) []*v1alpha1.Subscription {
12291237
var (
12301238
lastUpdated = o.now()
1239+
subList []*v1alpha1.Subscription
12311240
)
1241+
newCond := v1alpha1.SubscriptionCondition{
1242+
Type: condType,
1243+
Status: status,
1244+
Reason: reason,
1245+
Message: message,
1246+
}
12321247
for _, sub := range subs {
1248+
cond := sub.Status.GetCondition(condType)
1249+
if cond.Equals(newCond) {
1250+
continue
1251+
}
12331252
sub.Status.LastUpdated = lastUpdated
1253+
sub.Status.SetCondition(newCond)
1254+
subList = append(subList, sub)
1255+
}
1256+
return subList
1257+
}
1258+
1259+
// removeSubsCond will remove the condition to the subscription if it exists
1260+
// Only return the list of updated subscriptions
1261+
func (o *Operator) removeSubsCond(subs []*v1alpha1.Subscription, condType v1alpha1.SubscriptionConditionType) []*v1alpha1.Subscription {
1262+
var (
1263+
lastUpdated = o.now()
1264+
)
1265+
var subList []*v1alpha1.Subscription
1266+
for _, sub := range subs {
12341267
cond := sub.Status.GetCondition(condType)
1235-
cond.Reason = reason
1236-
cond.Message = message
1237-
if setTrue {
1238-
cond.Status = corev1.ConditionTrue
1239-
} else {
1240-
cond.Status = corev1.ConditionFalse
1268+
// if status is ConditionUnknown, the condition doesn't exist. Just skip
1269+
if cond.Status == corev1.ConditionUnknown {
1270+
continue
12411271
}
1242-
sub.Status.SetCondition(cond)
1272+
sub.Status.LastUpdated = lastUpdated
1273+
sub.Status.RemoveConditions(condType)
1274+
subList = append(subList, sub)
12431275
}
1244-
return subs
1276+
return subList
12451277
}
12461278

12471279
func (o *Operator) updateSubscriptionStatuses(subs []*v1alpha1.Subscription) ([]*v1alpha1.Subscription, error) {

pkg/controller/operators/catalog/subscriptions_test.go

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,6 @@ func TestSyncSubscriptions(t *testing.T) {
155155
},
156156
LastUpdated: now,
157157
InstallPlanGeneration: 1,
158-
Conditions: []v1alpha1.SubscriptionCondition{
159-
{
160-
Type: "ResolutionFailed",
161-
Status: corev1.ConditionFalse,
162-
Reason: "",
163-
Message: "",
164-
},
165-
},
166158
},
167159
},
168160
},
@@ -306,14 +298,6 @@ func TestSyncSubscriptions(t *testing.T) {
306298
},
307299
LastUpdated: now,
308300
InstallPlanGeneration: 1,
309-
Conditions: []v1alpha1.SubscriptionCondition{
310-
{
311-
Type: "ResolutionFailed",
312-
Status: corev1.ConditionFalse,
313-
Reason: "",
314-
Message: "",
315-
},
316-
},
317301
},
318302
},
319303
},
@@ -462,14 +446,6 @@ func TestSyncSubscriptions(t *testing.T) {
462446
},
463447
InstallPlanGeneration: 1,
464448
LastUpdated: now,
465-
Conditions: []v1alpha1.SubscriptionCondition{
466-
{
467-
Type: "ResolutionFailed",
468-
Status: corev1.ConditionFalse,
469-
Reason: "",
470-
Message: "",
471-
},
472-
},
473449
},
474450
},
475451
},
@@ -623,14 +599,6 @@ func TestSyncSubscriptions(t *testing.T) {
623599
},
624600
LastUpdated: now,
625601
InstallPlanGeneration: 1,
626-
Conditions: []v1alpha1.SubscriptionCondition{
627-
{
628-
Type: "ResolutionFailed",
629-
Status: corev1.ConditionFalse,
630-
Reason: "",
631-
Message: "",
632-
},
633-
},
634602
},
635603
},
636604
},
@@ -807,14 +775,6 @@ func TestSyncSubscriptions(t *testing.T) {
807775
},
808776
LastUpdated: now,
809777
InstallPlanGeneration: 1,
810-
Conditions: []v1alpha1.SubscriptionCondition{
811-
{
812-
Type: "ResolutionFailed",
813-
Status: corev1.ConditionFalse,
814-
Reason: "",
815-
Message: "",
816-
},
817-
},
818778
},
819779
},
820780
},
@@ -998,14 +958,6 @@ func TestSyncSubscriptions(t *testing.T) {
998958
},
999959
LastUpdated: now,
1000960
InstallPlanGeneration: 2,
1001-
Conditions: []v1alpha1.SubscriptionCondition{
1002-
{
1003-
Type: "ResolutionFailed",
1004-
Status: corev1.ConditionFalse,
1005-
Reason: "",
1006-
Message: "",
1007-
},
1008-
},
1009961
},
1010962
},
1011963
},

0 commit comments

Comments
 (0)