Skip to content

Commit 0ac0bd8

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

File tree

1 file changed

+46
-9
lines changed

1 file changed

+46
-9
lines changed

controllers/operator/manager.go

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ 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+
odlmCatalog := ""
85+
odlmCatalogNs := ""
86+
odlmSubList := &olmv1alpha1.SubscriptionList{}
87+
if err := m.Reader.List(ctx, odlmSubList, opts...); err != nil || len(odlmSubList.Items) == 0 {
88+
klog.Warningf("No Subscription found for ibm-odlm in the namespace %s", util.GetOperatorNamespace())
89+
} else {
90+
odlmCatalog = odlmSubList.Items[0].Spec.CatalogSource
91+
odlmCatalogNs = odlmSubList.Items[0].Spec.CatalogSourceNamespace
92+
}
7993

8094
for i, o := range reg.Spec.Operators {
8195
if o.Scope == "" {
@@ -91,7 +105,7 @@ func (m *ODLMOperator) GetOperandRegistry(ctx context.Context, key types.Namespa
91105
reg.Spec.Operators[i].Namespace = key.Namespace
92106
}
93107
if o.SourceName == "" || o.SourceNamespace == "" {
94-
catalogSourceName, catalogSourceNs, err := m.GetCatalogSourceFromPackage(ctx, o.PackageName, reg.Spec.Operators[i].Namespace, o.Channel, key.Namespace, excludedCatalogSources)
108+
catalogSourceName, catalogSourceNs, err := m.GetCatalogSourceFromPackage(ctx, o.PackageName, reg.Spec.Operators[i].Namespace, o.Channel, key.Namespace, odlmCatalog, odlmCatalogNs, excludedCatalogSources)
95109
if err != nil {
96110
return nil, err
97111
}
@@ -107,11 +121,13 @@ func (m *ODLMOperator) GetOperandRegistry(ctx context.Context, key types.Namespa
107121
}
108122

109123
type CatalogSource struct {
110-
Name string
111-
Namespace string
112-
OpNamespace string
113-
RegistryNamespace string
114-
Priority int
124+
Name string
125+
Namespace string
126+
OpNamespace string
127+
RegistryNamespace string
128+
Priority int
129+
ODLMCatalog string
130+
ODLMCatalogNamespace string
115131
}
116132

117133
type sortableCatalogSource []CatalogSource
@@ -120,27 +136,40 @@ func (s sortableCatalogSource) Len() int { return len(s) }
120136
func (s sortableCatalogSource) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
121137
func (s sortableCatalogSource) Less(i, j int) bool {
122138

123-
// Check if the catalogsource is in the same namespace as operator
139+
// Check if the catalogsource is in the same namespace as operator.
140+
// CatalogSources in operator namespace (private CatalogSource) have a higher priority than those in other namespaces (global CatalogSource).
124141
inOpNsI, inOpNsJ := s[i].Namespace == s[i].OpNamespace, s[j].Namespace == s[j].OpNamespace
125142
if inOpNsI && !inOpNsJ {
126143
return true
127144
}
128145
if !inOpNsI && inOpNsJ {
129146
return false
130147
}
148+
131149
// Compare catalogsource priorities first, higher priority comes first
132150
iPriority, jPriority := s[i].Priority, s[j].Priority
133151
if iPriority != jPriority {
134152
return iPriority > jPriority
135153
}
154+
155+
// Check if the catalogsource is in the same catalog as ODLM itself.
156+
// CatalogSources in the same catalog as ODLM have a higher priority than those in other catalogs.
157+
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
158+
if inODLMNsI && !inODLMNsJ {
159+
return true
160+
}
161+
if !inODLMNsI && inODLMNsJ {
162+
return false
163+
}
164+
136165
// If their namespaces are the same, then compare the name of the catalogsource
137166
if s[i].Namespace == s[j].Namespace {
138167
return s[i].Name < s[j].Name
139168
}
140169
return s[i].Namespace < s[j].Namespace
141170
}
142171

143-
func (m *ODLMOperator) GetCatalogSourceFromPackage(ctx context.Context, packageName, namespace, channel, registryNs string, excludedCatalogSources []string) (catalogSourceName string, catalogSourceNs string, err error) {
172+
func (m *ODLMOperator) GetCatalogSourceFromPackage(ctx context.Context, packageName, namespace, channel, registryNs, odlmCatalog, odlmCatalogNs string, excludedCatalogSources []string) (catalogSourceName string, catalogSourceNs string, err error) {
144173
packageManifestList := &operatorsv1.PackageManifestList{}
145174
opts := []client.ListOption{
146175
client.MatchingFields{"metadata.name": packageName},
@@ -172,7 +201,15 @@ func (m *ODLMOperator) GetCatalogSourceFromPackage(ctx context.Context, packageN
172201
klog.Warning(err)
173202
continue
174203
}
175-
catalogSourceCandidate = append(catalogSourceCandidate, CatalogSource{Name: pm.Status.CatalogSource, Namespace: pm.Status.CatalogSourceNamespace, OpNamespace: namespace, RegistryNamespace: registryNs, Priority: catalogsource.Spec.Priority})
204+
catalogSourceCandidate = append(catalogSourceCandidate, CatalogSource{
205+
Name: pm.Status.CatalogSource,
206+
Namespace: pm.Status.CatalogSourceNamespace,
207+
OpNamespace: namespace,
208+
RegistryNamespace: registryNs,
209+
Priority: catalogsource.Spec.Priority,
210+
ODLMCatalog: odlmCatalog,
211+
ODLMCatalogNamespace: odlmCatalogNs,
212+
})
176213
}
177214
if len(catalogSourceCandidate) == 0 {
178215
klog.Errorf("Not found PackageManifest %s in the namespace %s has channel %s", packageName, namespace, channel)

0 commit comments

Comments
 (0)