Skip to content

Commit 0a88eae

Browse files
Merge pull request #1012 from jhadvig/OCPBUGS-58434
OCPBUGS-58434: Console-operator should report plugin services as relatedObjects
2 parents c9eb614 + 4c82312 commit 0a88eae

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

pkg/console/starter/starter.go

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import (
6767

6868
telemetry "github.com/openshift/console-operator/pkg/console/telemetry"
6969

70+
"github.com/go-test/deep"
7071
consoleoperator "github.com/openshift/console-operator/pkg/console/operator"
7172
"github.com/openshift/library-go/pkg/operator/loglevel"
7273
)
@@ -456,21 +457,41 @@ func RunOperator(ctx context.Context, controllerContext *controllercmd.Controlle
456457
)
457458

458459
// Show all ConsolePlugin instances as related objects
460+
// Show all namespaces that are used by ConsolePlugin Backend Service as related objects
461+
// Show all namespaces that are used by Proxy Service configs as related objects
462+
// Deduplicate related objects
459463
clusterOperatorStatus.WithRelatedObjectsFunc(func() (bool, []configv1.ObjectReference) {
460464
relatedObjects := []configv1.ObjectReference{}
461465
consolePlugins, err := consoleClient.ConsoleV1().ConsolePlugins().List(ctx, metav1.ListOptions{})
462466
if err != nil {
463467
return false, nil
464468
}
465469
for _, plugin := range consolePlugins.Items {
466-
relatadPlugin := configv1.ObjectReference{
470+
relatedObjects = append(relatedObjects, configv1.ObjectReference{
467471
Group: "console.openshift.io",
468472
Resource: "consoleplugins",
469473
Name: plugin.GetName(),
474+
})
475+
if plugin.Spec.Backend.Service != nil {
476+
ns := plugin.Spec.Backend.Service.Namespace
477+
relatedObjects = append(relatedObjects, configv1.ObjectReference{
478+
Group: corev1.GroupName,
479+
Resource: "namespaces",
480+
Name: ns,
481+
})
482+
}
483+
for _, proxy := range plugin.Spec.Proxy {
484+
if proxy.Endpoint.Service != nil && proxy.Endpoint.Service.Namespace != "" {
485+
relatedObjects = append(relatedObjects, configv1.ObjectReference{
486+
Group: corev1.GroupName,
487+
Resource: "namespaces",
488+
Name: proxy.Endpoint.Service.Namespace,
489+
})
490+
}
470491
}
471-
relatedObjects = append(relatedObjects, relatadPlugin)
472492
}
473-
return true, relatedObjects
493+
494+
return true, deduplicateObjectReferences(relatedObjects)
474495
})
475496

476497
// NOTE: be sure to uncomment the .Run() below if using this
@@ -661,3 +682,20 @@ func extractStaticPodOperatorStatus(obj *unstructured.Unstructured, fieldManager
661682
}
662683
return &ret.Status.OperatorStatusApplyConfiguration, nil
663684
}
685+
686+
func deduplicateObjectReferences(objs []configv1.ObjectReference) []configv1.ObjectReference {
687+
result := make([]configv1.ObjectReference, 0, len(objs))
688+
for _, obj := range objs {
689+
duplicate := false
690+
for _, existing := range result {
691+
if len(deep.Equal(obj, existing)) == 0 {
692+
duplicate = true
693+
break
694+
}
695+
}
696+
if !duplicate {
697+
result = append(result, obj)
698+
}
699+
}
700+
return result
701+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package starter
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
configv1 "github.com/openshift/api/config/v1"
8+
)
9+
10+
func TestDeduplicateObjectReferences(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
input []configv1.ObjectReference
14+
expected []configv1.ObjectReference
15+
}{
16+
{
17+
name: "no duplicates",
18+
input: []configv1.ObjectReference{{Group: "g1", Resource: "r1", Name: "n1"}, {Group: "g2", Resource: "r2", Name: "n2"}},
19+
expected: []configv1.ObjectReference{{Group: "g1", Resource: "r1", Name: "n1"}, {Group: "g2", Resource: "r2", Name: "n2"}},
20+
},
21+
{
22+
name: "with duplicates",
23+
input: []configv1.ObjectReference{{Group: "g1", Resource: "r1", Name: "n1"}, {Group: "g1", Resource: "r1", Name: "n1"}},
24+
expected: []configv1.ObjectReference{{Group: "g1", Resource: "r1", Name: "n1"}},
25+
},
26+
{
27+
name: "different namespace not duplicate",
28+
input: []configv1.ObjectReference{{Group: "g1", Resource: "r1", Name: "n1", Namespace: "ns1"}, {Group: "g1", Resource: "r1", Name: "n1", Namespace: "ns2"}},
29+
expected: []configv1.ObjectReference{{Group: "g1", Resource: "r1", Name: "n1", Namespace: "ns1"}, {Group: "g1", Resource: "r1", Name: "n1", Namespace: "ns2"}},
30+
},
31+
{
32+
name: "all fields equal",
33+
input: []configv1.ObjectReference{{Group: "g1", Resource: "r1", Name: "n1", Namespace: "ns1"}, {Group: "g1", Resource: "r1", Name: "n1", Namespace: "ns1"}},
34+
expected: []configv1.ObjectReference{{Group: "g1", Resource: "r1", Name: "n1", Namespace: "ns1"}},
35+
},
36+
}
37+
38+
for _, tt := range tests {
39+
t.Run(tt.name, func(t *testing.T) {
40+
got := deduplicateObjectReferences(tt.input)
41+
if !reflect.DeepEqual(got, tt.expected) {
42+
t.Errorf("deduplicateObjectReferences() = %v, want %v", got, tt.expected)
43+
}
44+
})
45+
}
46+
}

0 commit comments

Comments
 (0)