Skip to content

Commit ecef072

Browse files
author
Robin Sonefors
committed
Fix events view to take clusters into account
We should only get events for the current cluster - we used to search all clusters, and if another cluster had the same object then we'd show incorrect events. This makes the new, correct object ref kind introduced in #2765 the only object ref used in our APIs, and then copy-pastes the client creation code from c3a407a.
1 parent 2ef84dc commit ecef072

File tree

13 files changed

+427
-505
lines changed

13 files changed

+427
-505
lines changed

api/core/core.proto

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ message ListEventsResponse {
239239
}
240240

241241
message SyncFluxObjectRequest {
242-
repeated ClusteredObjRef objects = 1;
243-
bool withSource = 2;
242+
repeated ObjectRef objects = 1;
243+
bool withSource = 2;
244244
}
245245

246246
message SyncFluxObjectResponse {
@@ -264,8 +264,8 @@ message GetFeatureFlagsResponse {
264264
}
265265

266266
message ToggleSuspendResourceRequest {
267-
repeated ClusteredObjRef objects = 1;
268-
bool suspend = 2;
267+
repeated ObjectRef objects = 1;
268+
bool suspend = 2;
269269
}
270270

271271
message ToggleSuspendResourceResponse {

api/core/core.swagger.json

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@
8686
"in": "query",
8787
"required": false,
8888
"type": "string"
89+
},
90+
{
91+
"name": "involvedObject.clusterName",
92+
"in": "query",
93+
"required": false,
94+
"type": "string"
8995
}
9096
],
9197
"tags": [
@@ -493,23 +499,6 @@
493499
}
494500
}
495501
},
496-
"v1ClusteredObjRef": {
497-
"type": "object",
498-
"properties": {
499-
"kind": {
500-
"type": "string"
501-
},
502-
"name": {
503-
"type": "string"
504-
},
505-
"namespace": {
506-
"type": "string"
507-
},
508-
"clusterName": {
509-
"type": "string"
510-
}
511-
}
512-
},
513502
"v1Condition": {
514503
"type": "object",
515504
"properties": {
@@ -888,6 +877,9 @@
888877
},
889878
"namespace": {
890879
"type": "string"
880+
},
881+
"clusterName": {
882+
"type": "string"
891883
}
892884
}
893885
},
@@ -897,7 +889,7 @@
897889
"objects": {
898890
"type": "array",
899891
"items": {
900-
"$ref": "#/definitions/v1ClusteredObjRef"
892+
"$ref": "#/definitions/v1ObjectRef"
901893
}
902894
},
903895
"withSource": {
@@ -914,7 +906,7 @@
914906
"objects": {
915907
"type": "array",
916908
"items": {
917-
"$ref": "#/definitions/v1ClusteredObjRef"
909+
"$ref": "#/definitions/v1ObjectRef"
918910
}
919911
},
920912
"suspend": {

api/core/types.proto

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ message ObjectRef {
3232
string kind = 1;
3333
string name = 2;
3434
string namespace = 3;
35-
}
36-
37-
message ClusteredObjRef {
38-
string kind = 1;
39-
string name = 2;
40-
string namespace = 3;
4135
string clusterName = 4;
4236
}
4337

core/server/events.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,24 @@ import (
1616
)
1717

1818
func (cs *coreServer) ListEvents(ctx context.Context, msg *pb.ListEventsRequest) (*pb.ListEventsResponse, error) {
19-
k8s, err := cs.clustersManager.GetImpersonatedClient(ctx, auth.Principal(ctx))
20-
if err != nil {
21-
return nil, doClientError(err)
22-
}
23-
2419
if msg.InvolvedObject == nil {
2520
return nil, status.Errorf(codes.InvalidArgument, "bad request: no object was specified")
2621
}
2722

23+
var clustersClient clustersmngr.Client
24+
25+
var err error
26+
27+
if msg.InvolvedObject.ClusterName != "" {
28+
clustersClient, err = cs.clustersManager.GetImpersonatedClientForCluster(ctx, auth.Principal(ctx), msg.InvolvedObject.ClusterName)
29+
} else {
30+
clustersClient, err = cs.clustersManager.GetImpersonatedClient(ctx, auth.Principal(ctx))
31+
}
32+
33+
if err != nil {
34+
return nil, doClientError(err)
35+
}
36+
2837
clist := clustersmngr.NewClusteredList(func() client.ObjectList {
2938
return &corev1.EventList{}
3039
})
@@ -42,7 +51,7 @@ func (cs *coreServer) ListEvents(ctx context.Context, msg *pb.ListEventsRequest)
4251
"involvedObject.namespace": msg.InvolvedObject.Namespace,
4352
}
4453

45-
if err := list(ctx, k8s, temporarilyEmptyAppName, msg.InvolvedObject.Namespace, clist, fields); err != nil {
54+
if err := list(ctx, clustersClient, temporarilyEmptyAppName, msg.InvolvedObject.Namespace, clist, fields); err != nil {
4655
return nil, fmt.Errorf("could not get events: %w", err)
4756
}
4857

core/server/events_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,21 @@ func TestListEvents(t *testing.T) {
102102

103103
g.Expect(res.Events[0].Component).To(Equal(kustomizeEvent.Source.Component))
104104

105+
// Get kustomization events, explicit cluster
106+
res, err = c.ListEvents(ctx, &pb.ListEventsRequest{
107+
InvolvedObject: &pb.ObjectRef{
108+
Name: kustomizationObjectName,
109+
Namespace: ns.Name,
110+
Kind: kustomizev1.KustomizationKind,
111+
ClusterName: "Default",
112+
},
113+
})
114+
g.Expect(err).NotTo(HaveOccurred())
115+
116+
g.Expect(res.Events).To(HaveLen(1))
117+
118+
g.Expect(res.Events[0].Component).To(Equal(kustomizeEvent.Source.Component))
119+
105120
// Get helmrelease events
106121
res, err = c.ListEvents(ctx, &pb.ListEventsRequest{
107122
InvolvedObject: &pb.ObjectRef{

core/server/suspend_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,20 @@ func TestSuspend_Suspend(t *testing.T) {
5757
},
5858
}
5959

60-
requestObjects := []*api.ClusteredObjRef{}
60+
requestObjects := []*api.ObjectRef{}
6161

6262
for _, tt := range tests {
6363
t.Run(tt.kind, func(t *testing.T) {
6464
g := NewGomegaWithT(t)
6565
g.Expect(k.Create(ctx, tt.obj)).Should(Succeed())
66-
object := &api.ClusteredObjRef{
66+
object := &api.ObjectRef{
6767
Kind: tt.kind,
6868
Name: tt.obj.GetName(),
6969
Namespace: tt.obj.GetNamespace(),
7070
ClusterName: "Default",
7171
}
7272
req := &api.ToggleSuspendResourceRequest{
73-
Objects: []*api.ClusteredObjRef{object},
73+
Objects: []*api.ObjectRef{object},
7474
Suspend: true,
7575
}
7676
_, err = c.ToggleSuspendResource(ctx, req)
@@ -100,7 +100,7 @@ func TestSuspend_Suspend(t *testing.T) {
100100

101101
_, err = c.ToggleSuspendResource(ctx, &api.ToggleSuspendResourceRequest{
102102

103-
Objects: []*api.ClusteredObjRef{{
103+
Objects: []*api.ObjectRef{{
104104
Kind: sourcev1.GitRepositoryKind,
105105
Name: "fakeName",
106106
Namespace: "fakeNamespace",

core/server/sync_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ func TestSync(t *testing.T) {
6161
}{{
6262
name: "kustomization no source",
6363
msg: &pb.SyncFluxObjectRequest{
64-
Objects: []*pb.ClusteredObjRef{{ClusterName: "Default",
64+
Objects: []*pb.ObjectRef{{ClusterName: "Default",
6565
Kind: kustomizev1.KustomizationKind}},
6666
WithSource: false,
6767
},
6868
automation: fluxsync.KustomizationAdapter{Kustomization: kust},
6969
}, {
7070
name: "kustomization with source",
7171
msg: &pb.SyncFluxObjectRequest{
72-
Objects: []*pb.ClusteredObjRef{{ClusterName: "Default",
72+
Objects: []*pb.ObjectRef{{ClusterName: "Default",
7373
Kind: kustomizev1.KustomizationKind}},
7474
WithSource: true,
7575
},
@@ -78,15 +78,15 @@ func TestSync(t *testing.T) {
7878
}, {
7979
name: "helm release no source",
8080
msg: &pb.SyncFluxObjectRequest{
81-
Objects: []*pb.ClusteredObjRef{{ClusterName: "Default",
81+
Objects: []*pb.ObjectRef{{ClusterName: "Default",
8282
Kind: helmv2.HelmReleaseKind}},
8383
WithSource: false,
8484
},
8585
automation: fluxsync.HelmReleaseAdapter{HelmRelease: hr},
8686
}, {
8787
name: "helm release with source",
8888
msg: &pb.SyncFluxObjectRequest{
89-
Objects: []*pb.ClusteredObjRef{{ClusterName: "Default",
89+
Objects: []*pb.ObjectRef{{ClusterName: "Default",
9090
Kind: helmv2.HelmReleaseKind}},
9191
WithSource: true,
9292
},
@@ -96,7 +96,7 @@ func TestSync(t *testing.T) {
9696
{
9797
name: "multiple objects",
9898
msg: &pb.SyncFluxObjectRequest{
99-
Objects: []*pb.ClusteredObjRef{{ClusterName: "Default",
99+
Objects: []*pb.ObjectRef{{ClusterName: "Default",
100100
Kind: helmv2.HelmReleaseKind}, {ClusterName: "Default",
101101
Kind: helmv2.HelmReleaseKind}},
102102
WithSource: true,

0 commit comments

Comments
 (0)