Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pkg/olm/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/openshift/console/pkg/auth"
"github.com/openshift/console/pkg/serverutils"
"github.com/operator-framework/kubectl-operator/pkg/action"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"

Expand Down Expand Up @@ -70,6 +71,20 @@ func (o *OLMHandler) OperandsList(user *auth.User, w http.ResponseWriter, r *htt
serverutils.SendResponse(w, http.StatusBadGateway, serverutils.ApiError{Err: errMsg})
return
}
// Deduplicate operands by UID to prevent duplicate CRs
if operandsList != nil && len(operandsList.Items) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add head comment to explain how this work would help.

For example:
// Deduplicate operands by UID to prevent duplicate CRs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good, thanks so much @Leo6Leo 🚀

seen := make(map[string]bool)
uniqueOperands := make([]unstructured.Unstructured, 0, len(operandsList.Items))

for _, operand := range operandsList.Items {
uid := string(operand.GetUID())
if !seen[uid] {
seen[uid] = true
uniqueOperands = append(uniqueOperands, operand)
}
}
operandsList.Items = uniqueOperands
}

w.Header().Set("Content-Type", "application/json")
resp, err := json.Marshal(operandsList)
Expand Down