Skip to content

Commit dcfe198

Browse files
committed
Prioritize the Catalog which ODLM is deployed from
Signed-off-by: Daniel Fan <[email protected]>
1 parent 0e8efe4 commit dcfe198

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

controllers/operator/manager.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ func (m *ODLMOperator) GetOperandRegistry(ctx context.Context, key types.Namespa
7676
if reg.Annotations != nil && reg.Annotations["excluded-catalogsource"] != "" {
7777
excludedCatalogSources = strings.Split(reg.Annotations["excluded-catalogsource"], ",")
7878
}
79+
// Get catalog used by ODLM itself by check its own subscription
80+
opts := []client.ListOption{
81+
client.MatchingLabels{fmt.Sprintf("operators.coreos.com/ibm-odlm.%s", util.GetOperatorNamespace()): ""},
82+
client.InNamespace(util.GetOperatorNamespace()),
83+
}
84+
odlmSubList := &olmv1alpha1.SubscriptionList{}
85+
if err := m.Reader.List(ctx, odlmSubList, opts...); err != nil || len(odlmSubList.Items) == 0 {
86+
klog.Errorf("No Subscription found for ibm-odlm in the namespace %s", util.GetOperatorNamespace())
87+
return nil, err
88+
}
89+
odlmCatalog := odlmSubList.Items[0].Spec.CatalogSource
90+
odlmCatalogNs := odlmSubList.Items[0].Spec.CatalogSourceNamespace
7991

8092
for i, o := range reg.Spec.Operators {
8193
if o.Scope == "" {
@@ -91,7 +103,7 @@ func (m *ODLMOperator) GetOperandRegistry(ctx context.Context, key types.Namespa
91103
reg.Spec.Operators[i].Namespace = key.Namespace
92104
}
93105
if o.SourceName == "" || o.SourceNamespace == "" {
94-
catalogSourceName, catalogSourceNs, err := m.GetCatalogSourceFromPackage(ctx, o.PackageName, reg.Spec.Operators[i].Namespace, o.Channel, key.Namespace, excludedCatalogSources)
106+
catalogSourceName, catalogSourceNs, err := m.GetCatalogSourceFromPackage(ctx, o.PackageName, reg.Spec.Operators[i].Namespace, o.Channel, key.Namespace, odlmCatalog, odlmCatalogNs, excludedCatalogSources)
95107
if err != nil {
96108
return nil, err
97109
}
@@ -107,11 +119,13 @@ func (m *ODLMOperator) GetOperandRegistry(ctx context.Context, key types.Namespa
107119
}
108120

109121
type CatalogSource struct {
110-
Name string
111-
Namespace string
112-
OpNamespace string
113-
RegistryNamespace string
114-
Priority int
122+
Name string
123+
Namespace string
124+
OpNamespace string
125+
RegistryNamespace string
126+
Priority int
127+
ODLMCatalog string
128+
ODLMCatalogNamespace string
115129
}
116130

117131
type sortableCatalogSource []CatalogSource
@@ -120,27 +134,40 @@ func (s sortableCatalogSource) Len() int { return len(s) }
120134
func (s sortableCatalogSource) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
121135
func (s sortableCatalogSource) Less(i, j int) bool {
122136

123-
// Check if the catalogsource is in the same namespace as operator
137+
// Check if the catalogsource is in the same namespace as operator.
138+
// CatalogSources in operator namespace (private CatalogSource) have a higher priority than those in other namespaces (global CatalogSource).
124139
inOpNsI, inOpNsJ := s[i].Namespace == s[i].OpNamespace, s[j].Namespace == s[j].OpNamespace
125140
if inOpNsI && !inOpNsJ {
126141
return true
127142
}
128143
if !inOpNsI && inOpNsJ {
129144
return false
130145
}
146+
131147
// Compare catalogsource priorities first, higher priority comes first
132148
iPriority, jPriority := s[i].Priority, s[j].Priority
133149
if iPriority != jPriority {
134150
return iPriority > jPriority
135151
}
152+
153+
// Check if the catalogsource is in the same catalog as ODLM itself.
154+
// CatalogSources in the same catalog as ODLM have a higher priority than those in other catalogs.
155+
inODLMNsI, inODLMNsJ := s[i].Name == s[i].ODLMCatalog && s[i].Namespace == s[i].ODLMCatalogNamespace, s[j].Name == s[j].ODLMCatalog && s[j].Namespace == s[j].ODLMCatalogNamespace
156+
if inODLMNsI && !inODLMNsJ {
157+
return true
158+
}
159+
if !inODLMNsI && inODLMNsJ {
160+
return false
161+
}
162+
136163
// If their namespaces are the same, then compare the name of the catalogsource
137164
if s[i].Namespace == s[j].Namespace {
138165
return s[i].Name < s[j].Name
139166
}
140167
return s[i].Namespace < s[j].Namespace
141168
}
142169

143-
func (m *ODLMOperator) GetCatalogSourceFromPackage(ctx context.Context, packageName, namespace, channel, registryNs string, excludedCatalogSources []string) (catalogSourceName string, catalogSourceNs string, err error) {
170+
func (m *ODLMOperator) GetCatalogSourceFromPackage(ctx context.Context, packageName, namespace, channel, registryNs, odlmCatalog, odlmCatalogNs string, excludedCatalogSources []string) (catalogSourceName string, catalogSourceNs string, err error) {
144171
packageManifestList := &operatorsv1.PackageManifestList{}
145172
opts := []client.ListOption{
146173
client.MatchingFields{"metadata.name": packageName},
@@ -172,7 +199,7 @@ func (m *ODLMOperator) GetCatalogSourceFromPackage(ctx context.Context, packageN
172199
klog.Warning(err)
173200
continue
174201
}
175-
catalogSourceCandidate = append(catalogSourceCandidate, CatalogSource{Name: pm.Status.CatalogSource, Namespace: pm.Status.CatalogSourceNamespace, OpNamespace: namespace, RegistryNamespace: registryNs, Priority: catalogsource.Spec.Priority})
202+
catalogSourceCandidate = append(catalogSourceCandidate, CatalogSource{Name: pm.Status.CatalogSource, Namespace: pm.Status.CatalogSourceNamespace, OpNamespace: namespace, RegistryNamespace: registryNs, Priority: catalogsource.Spec.Priority, ODLMCatalog: odlmCatalog, ODLMCatalogNamespace: odlmCatalogNs})
176203
}
177204
if len(catalogSourceCandidate) == 0 {
178205
klog.Errorf("Not found PackageManifest %s in the namespace %s has channel %s", packageName, namespace, channel)

0 commit comments

Comments
 (0)