@@ -21,7 +21,6 @@ import (
21
21
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
22
22
"k8s.io/apiextensions-apiserver/pkg/apiserver/validation"
23
23
extinf "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions"
24
- k8serrors "k8s.io/apimachinery/pkg/api/errors"
25
24
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
25
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
27
26
"k8s.io/apimachinery/pkg/labels"
@@ -832,6 +831,12 @@ func (o *Operator) syncResolvingNamespace(obj interface{}) error {
832
831
return err
833
832
}
834
833
834
+ ips , err := o .listInstallPlansMap (namespace )
835
+ if err != nil {
836
+ logger .WithError (err ).Debug ("couldn't list installplan" )
837
+ return err
838
+ }
839
+
835
840
// TODO: parallel
836
841
maxGeneration := 0
837
842
subscriptionUpdated := false
@@ -848,7 +853,7 @@ func (o *Operator) syncResolvingNamespace(obj interface{}) error {
848
853
}
849
854
850
855
// ensure the installplan reference is correct
851
- sub , changedIP , err := o .ensureSubscriptionInstallPlanState (logger , sub )
856
+ sub , changedIP , err := o .ensureSubscriptionInstallPlanState (logger , sub , ips )
852
857
if err != nil {
853
858
logger .Debugf ("error ensuring installplan state: %v" , err )
854
859
return err
@@ -941,30 +946,41 @@ func (o *Operator) syncSubscriptions(obj interface{}) error {
941
946
}
942
947
943
948
func (o * Operator ) nothingToUpdate (logger * logrus.Entry , sub * v1alpha1.Subscription ) bool {
944
- if sub .Status .InstallPlanRef != nil && sub .Status .State == v1alpha1 .SubscriptionStateUpgradePending {
945
- if o .isInstallPlanMissing (sub ) {
946
- return false
947
- }
948
- logger .Debugf ("skipping update: installplan already created" )
949
- return true
950
- }
951
949
// Only sync if catalog has been updated since last sync time
952
950
if o .sourcesLastUpdate .Before (sub .Status .LastUpdated .Time ) && sub .Status .State != v1alpha1 .SubscriptionStateNone && sub .Status .State != v1alpha1 .SubscriptionStateUpgradeAvailable {
953
951
logger .Debugf ("skipping update: no new updates to catalog since last sync at %s" , sub .Status .LastUpdated .String ())
954
952
return true
955
953
}
954
+ if sub .Status .InstallPlanRef != nil && sub .Status .State == v1alpha1 .SubscriptionStateUpgradePending {
955
+ logger .Debugf ("skipping update: installplan already created" )
956
+ return true
957
+ }
956
958
return false
957
959
}
958
960
959
- // isInstallPlanMissing checks if the installplan is missing or not
960
- func (o * Operator ) isInstallPlanMissing (sub * v1alpha1.Subscription ) bool {
961
- _ , err := o .client .OperatorsV1alpha1 ().InstallPlans (sub .GetNamespace ()).Get (context .TODO (), sub .Status .Install .Name , metav1.GetOptions {})
962
- return k8serrors .IsNotFound (err )
961
+ // checkMissingInstallPlan checks if the installplan is missing or not when
962
+ // the subscription is in pending upgrade state
963
+ func (o * Operator ) checkMissingInstallPlan (sub * v1alpha1.Subscription , ips map [string ]struct {}) (* v1alpha1.Subscription , bool , error ) {
964
+ _ , ok := ips [sub .Status .InstallPlanRef .Name ]
965
+ if ! ok && sub .Status .State == v1alpha1 .SubscriptionStateUpgradePending {
966
+ out := sub .DeepCopy ()
967
+ out .Status .InstallPlanRef = nil
968
+ out .Status .Install = nil
969
+ out .Status .CurrentCSV = ""
970
+ out .Status .State = v1alpha1 .SubscriptionStateNone
971
+ out .Status .LastUpdated = o .now ()
972
+ updated , err := o .client .OperatorsV1alpha1 ().Subscriptions (sub .GetNamespace ()).UpdateStatus (context .TODO (), out , metav1.UpdateOptions {})
973
+ if err != nil {
974
+ return out , false , nil
975
+ }
976
+ return updated , true , nil
977
+ }
978
+ return sub , false , nil
963
979
}
964
980
965
- func (o * Operator ) ensureSubscriptionInstallPlanState (logger * logrus.Entry , sub * v1alpha1.Subscription ) (* v1alpha1.Subscription , bool , error ) {
966
- if sub .Status .InstallPlanRef != nil {
967
- return sub , false , nil
981
+ func (o * Operator ) ensureSubscriptionInstallPlanState (logger * logrus.Entry , sub * v1alpha1.Subscription , ips map [ string ] struct {} ) (* v1alpha1.Subscription , bool , error ) {
982
+ if sub .Status .InstallPlanRef != nil || sub . Status . Install != nil {
983
+ return o . checkMissingInstallPlan ( sub , ips )
968
984
}
969
985
970
986
logger .Debug ("checking for existing installplan" )
@@ -2030,6 +2046,20 @@ func (o *Operator) listInstallPlans(namespace string) (ips []*v1alpha1.InstallPl
2030
2046
return
2031
2047
}
2032
2048
2049
+ func (o * Operator ) listInstallPlansMap (namespace string ) (ips map [string ]struct {}, err error ) {
2050
+ list , err := o .client .OperatorsV1alpha1 ().InstallPlans (namespace ).List (context .TODO (), metav1.ListOptions {})
2051
+ if err != nil {
2052
+ return
2053
+ }
2054
+
2055
+ ips = make (map [string ]struct {})
2056
+ for i := range list .Items {
2057
+ ips [list .Items [i ].GetName ()] = struct {}{}
2058
+ }
2059
+
2060
+ return
2061
+ }
2062
+
2033
2063
// competingCRDOwnersExist returns true if there exists a CSV that owns at least one of the given CSVs owned CRDs (that's not the given CSV)
2034
2064
func competingCRDOwnersExist (namespace string , csv * v1alpha1.ClusterServiceVersion , existingOwners map [string ][]string ) (bool , error ) {
2035
2065
// Attempt to find a pre-existing owner in the namespace for any owned crd
0 commit comments