Skip to content

Commit e6cc305

Browse files
authored
fix(sub): Only update subscriptions that have changes in status (#2399)
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 eabd986 commit e6cc305

File tree

3 files changed

+76
-85
lines changed

3 files changed

+76
-85
lines changed

pkg/controller/operators/catalog/operator.go

+73-34
Original file line numberDiff line numberDiff line change
@@ -962,31 +962,34 @@ 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)
966-
_, updateErr := o.updateSubscriptionStatuses(subs)
965+
_, updateErr := o.updateSubscriptionStatuses(
966+
o.setSubsCond(subs, v1alpha1.SubscriptionCondition{
967+
Type: v1alpha1.SubscriptionResolutionFailed,
968+
Reason: "ConstraintsNotSatisfiable",
969+
Message: err.Error(),
970+
Status: corev1.ConditionTrue,
971+
}))
967972
if updateErr != nil {
968973
logger.WithError(updateErr).Debug("failed to update subs conditions")
969974
return updateErr
970975
}
971976
return nil
972977
}
973-
subs = o.setSubsCond(subs, v1alpha1.SubscriptionResolutionFailed, "ErrorPreventedResolution", err.Error(), true)
974-
_, updateErr := o.updateSubscriptionStatuses(subs)
978+
979+
_, updateErr := o.updateSubscriptionStatuses(
980+
o.setSubsCond(subs, v1alpha1.SubscriptionCondition{
981+
Type: v1alpha1.SubscriptionResolutionFailed,
982+
Reason: "ErrorPreventedResolution",
983+
Message: err.Error(),
984+
Status: corev1.ConditionTrue,
985+
}))
975986
if updateErr != nil {
976987
logger.WithError(updateErr).Debug("failed to update subs conditions")
977988
return updateErr
978989
}
979990
return err
980991
}
981992

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-
990993
// create installplan if anything updated
991994
if len(updatedSubs) > 0 {
992995
logger.Debug("resolution caused subscription changes, creating installplan")
@@ -1017,17 +1020,36 @@ func (o *Operator) syncResolvingNamespace(obj interface{}) error {
10171020
return err
10181021
}
10191022
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-
}
10271023
} else {
10281024
logger.Debugf("no subscriptions were updated")
10291025
}
10301026

1027+
// Remove resolutionfailed condition from subscriptions
1028+
subs = o.removeSubsCond(subs, v1alpha1.SubscriptionResolutionFailed)
1029+
newSub := true
1030+
for _, updatedSub := range updatedSubs {
1031+
updatedSub.Status.RemoveConditions(v1alpha1.SubscriptionResolutionFailed)
1032+
for i, sub := range subs {
1033+
if sub.Name == updatedSub.Name && sub.Namespace == updatedSub.Namespace {
1034+
subs[i] = updatedSub
1035+
newSub = false
1036+
break
1037+
}
1038+
}
1039+
if newSub {
1040+
subs = append(subs, updatedSub)
1041+
continue
1042+
}
1043+
newSub = true
1044+
}
1045+
1046+
// Update subscriptions with all changes so far
1047+
_, updateErr := o.updateSubscriptionStatuses(subs)
1048+
if updateErr != nil {
1049+
logger.WithError(updateErr).Warn("failed to update subscription conditions")
1050+
return updateErr
1051+
}
1052+
10311053
return nil
10321054
}
10331055

@@ -1084,12 +1106,7 @@ func (o *Operator) ensureSubscriptionInstallPlanState(logger *logrus.Entry, sub
10841106
out.Status.CurrentCSV = out.Spec.StartingCSV
10851107
out.Status.LastUpdated = o.now()
10861108

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
1109+
return out, true, nil
10931110
}
10941111

10951112
func (o *Operator) ensureSubscriptionCSVState(logger *logrus.Entry, sub *v1alpha1.Subscription, querier SourceQuerier) (*v1alpha1.Subscription, bool, error) {
@@ -1225,23 +1242,45 @@ func (o *Operator) createInstallPlan(namespace string, gen int, subs []*v1alpha1
12251242
return reference.GetReference(res)
12261243
}
12271244

1228-
func (o *Operator) setSubsCond(subs []*v1alpha1.Subscription, condType v1alpha1.SubscriptionConditionType, reason, message string, setTrue bool) []*v1alpha1.Subscription {
1245+
// setSubsCond will set the condition to the subscription if it doesn't already
1246+
// exist or if it is different
1247+
// Only return the list of updated subscriptions
1248+
func (o *Operator) setSubsCond(subs []*v1alpha1.Subscription, cond v1alpha1.SubscriptionCondition) []*v1alpha1.Subscription {
12291249
var (
12301250
lastUpdated = o.now()
1251+
subList []*v1alpha1.Subscription
12311252
)
1253+
12321254
for _, sub := range subs {
1255+
subCond := sub.Status.GetCondition(cond.Type)
1256+
if subCond.Equals(cond) {
1257+
continue
1258+
}
12331259
sub.Status.LastUpdated = lastUpdated
1260+
sub.Status.SetCondition(cond)
1261+
subList = append(subList, sub)
1262+
}
1263+
return subList
1264+
}
1265+
1266+
// removeSubsCond will remove the condition to the subscription if it exists
1267+
// Only return the list of updated subscriptions
1268+
func (o *Operator) removeSubsCond(subs []*v1alpha1.Subscription, condType v1alpha1.SubscriptionConditionType) []*v1alpha1.Subscription {
1269+
var (
1270+
lastUpdated = o.now()
1271+
)
1272+
var subList []*v1alpha1.Subscription
1273+
for _, sub := range subs {
12341274
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
1275+
// if status is ConditionUnknown, the condition doesn't exist. Just skip
1276+
if cond.Status == corev1.ConditionUnknown {
1277+
continue
12411278
}
1242-
sub.Status.SetCondition(cond)
1279+
sub.Status.LastUpdated = lastUpdated
1280+
sub.Status.RemoveConditions(condType)
1281+
subList = append(subList, sub)
12431282
}
1244-
return subs
1283+
return subList
12451284
}
12461285

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

pkg/controller/operators/catalog/subscriptions_test.go

-48
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
},

test/e2e/subscription_e2e_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2169,14 +2169,14 @@ var _ = Describe("Subscription", func() {
21692169

21702170
updateInternalCatalog(GinkgoT(), c, crc, catSrcName, generatedNamespace.GetName(), []apiextensions.CustomResourceDefinition{crd}, []operatorsv1alpha1.ClusterServiceVersion{csvA, csvB}, packages)
21712171
})
2172-
It("the ResolutionFailed condition previously set in it's status that indicated the resolution error is cleared off", func() {
2172+
It("the ResolutionFailed condition previously set in its status that indicated the resolution error is cleared off", func() {
21732173
Eventually(func() (corev1.ConditionStatus, error) {
21742174
sub, err := crc.OperatorsV1alpha1().Subscriptions(generatedNamespace.GetName()).Get(context.Background(), subName, metav1.GetOptions{})
21752175
if err != nil {
2176-
return corev1.ConditionUnknown, err
2176+
return corev1.ConditionFalse, err
21772177
}
21782178
return sub.Status.GetCondition(operatorsv1alpha1.SubscriptionResolutionFailed).Status, nil
2179-
}).Should(Equal(corev1.ConditionFalse))
2179+
}).Should(Equal(corev1.ConditionUnknown))
21802180
})
21812181
})
21822182
})

0 commit comments

Comments
 (0)