Skip to content

Commit 5b3cafc

Browse files
Updated logic for sorting Operands with multiple versions
1 parent 03a35fe commit 5b3cafc

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

pkg/olm/handler.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/openshift/console/pkg/auth"
1010
"github.com/openshift/console/pkg/serverutils"
1111
"github.com/operator-framework/kubectl-operator/pkg/action"
12+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1213
"k8s.io/client-go/rest"
1314
"k8s.io/klog/v2"
1415

@@ -71,6 +72,8 @@ func (o *OLMHandler) OperandsList(user *auth.User, w http.ResponseWriter, r *htt
7172
return
7273
}
7374

75+
operandsList = deduplicateOperands(operandsList)
76+
7477
w.Header().Set("Content-Type", "application/json")
7578
resp, err := json.Marshal(operandsList)
7679
if err != nil {
@@ -150,3 +153,31 @@ func (o *OLMHandler) getClientWithScheme(user *auth.User) (client.Client, *runti
150153
}
151154
return client, scheme, nil
152155
}
156+
157+
func deduplicateOperands(operandsList *unstructured.UnstructuredList) *unstructured.UnstructuredList {
158+
if operandsList == nil || len(operandsList.Items) == 0 {
159+
return operandsList
160+
}
161+
162+
seen := make(map[string]bool)
163+
uniqueOperands := make([]unstructured.Unstructured, 0, len(operandsList.Items))
164+
165+
for _, operand := range operandsList.Items {
166+
key := fmt.Sprintf("%s-%s-%s",
167+
operand.GetKind(),
168+
operand.GetName(),
169+
operand.GetNamespace())
170+
171+
if !seen[key] {
172+
seen[key] = true
173+
uniqueOperands = append(uniqueOperands, operand)
174+
}
175+
}
176+
177+
result := &unstructured.UnstructuredList{
178+
Items: uniqueOperands,
179+
}
180+
result.SetGroupVersionKind(operandsList.GroupVersionKind())
181+
182+
return result
183+
}

0 commit comments

Comments
 (0)