Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2343 from mklanjsek/issue-1944
Browse files Browse the repository at this point in the history
Make Resource Viewer layout repeatable
  • Loading branch information
Sam Foo authored Apr 25, 2021
2 parents 45086ec + cfabfcc commit d0d2595
Show file tree
Hide file tree
Showing 26 changed files with 237 additions and 142 deletions.
Empty file.
7 changes: 4 additions & 3 deletions internal/objectvisitor/apiservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (p *APIService) Supports() schema.GroupVersionKind {
}

// Visit visits a apiservice. It looks for service accounts and services.
func (p *APIService) Visit(ctx context.Context, object *unstructured.Unstructured, handler ObjectHandler, visitor Visitor, visitDescendants bool) error {
func (p *APIService) Visit(ctx context.Context, object *unstructured.Unstructured, handler ObjectHandler, visitor Visitor, visitDescendants bool, level int) error {
ctx, span := trace.StartSpan(ctx, "visitAPIService")
defer span.End()

Expand All @@ -53,6 +53,7 @@ func (p *APIService) Visit(ctx context.Context, object *unstructured.Unstructure
if err := kubernetes.FromUnstructured(object, apiservice); err != nil {
return err
}
level = handler.SetLevel(apiservice.Kind, level)

var g errgroup.Group

Expand All @@ -72,12 +73,12 @@ func (p *APIService) Visit(ctx context.Context, object *unstructured.Unstructure
return err
}

if err := visitor.Visit(ctx, service, handler, true); err != nil {
if err := visitor.Visit(ctx, service, handler, true, level); err != nil {
return errors.Wrapf(err, "apiservice %s visit service %s",
kubernetes.PrintObject(apiservice), kubernetes.PrintObject(service))
}

return handler.AddEdge(ctx, object, service)
return handler.AddEdge(ctx, object, service, level)
})

return g.Wait()
Expand Down
23 changes: 13 additions & 10 deletions internal/objectvisitor/apiservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ func TestAPIService_Visit(t *testing.T) {
u := testutil.ToUnstructured(t, object)

handler := fake.NewMockObjectHandler(controller)
handler.EXPECT().SetLevel(gomock.Any(), 1).Return(2)
handler.EXPECT().
AddEdge(gomock.Any(), u, testutil.ToUnstructured(t, service)).
AddEdge(gomock.Any(), u, testutil.ToUnstructured(t, service), gomock.Any()).
Return(nil)

var visited []unstructured.Unstructured
visitor := fake.NewMockVisitor(controller)
visitor.EXPECT().
Visit(gomock.Any(), gomock.Any(), handler, true).
DoAndReturn(func(ctx context.Context, object *unstructured.Unstructured, handler objectvisitor.ObjectHandler, _ bool) error {
Visit(gomock.Any(), gomock.Any(), handler, true, gomock.Any()).
DoAndReturn(func(ctx context.Context, object *unstructured.Unstructured, handler objectvisitor.ObjectHandler, _ bool, _ int) error {
visited = append(visited, *object)
return nil
})
Expand All @@ -66,7 +67,7 @@ func TestAPIService_Visit(t *testing.T) {
apiService := objectvisitor.NewAPIService(objectStore)

ctx := context.Background()
err := apiService.Visit(ctx, u, handler, visitor, true)
err := apiService.Visit(ctx, u, handler, visitor, true, 1)

sortObjectsByName(t, visited)

Expand All @@ -92,9 +93,10 @@ func TestAPIService_Visit_notfound(t *testing.T) {

var visited []unstructured.Unstructured
visitor := fake.NewMockVisitor(controller)
handler.EXPECT().SetLevel(gomock.Any(), 1).Return(2)
visitor.EXPECT().
Visit(gomock.Any(), gomock.Any(), handler, true).
DoAndReturn(func(ctx context.Context, object *unstructured.Unstructured, handler objectvisitor.ObjectHandler, _ bool) error {
Visit(gomock.Any(), gomock.Any(), handler, true, gomock.Any()).
DoAndReturn(func(ctx context.Context, object *unstructured.Unstructured, handler objectvisitor.ObjectHandler, _ bool, _ int) error {
visited = append(visited, *object)
return nil
}).AnyTimes()
Expand All @@ -114,7 +116,7 @@ func TestAPIService_Visit_notfound(t *testing.T) {
apiService := objectvisitor.NewAPIService(objectStore)

ctx := context.Background()
err := apiService.Visit(ctx, u, handler, visitor, true)
err := apiService.Visit(ctx, u, handler, visitor, true, 1)

sortObjectsByName(t, visited)

Expand All @@ -134,9 +136,10 @@ func TestAPIService_Visit_local(t *testing.T) {

var visited []unstructured.Unstructured
visitor := fake.NewMockVisitor(controller)
handler.EXPECT().SetLevel(gomock.Any(), 1).Return(2)
visitor.EXPECT().
Visit(gomock.Any(), gomock.Any(), handler, true).
DoAndReturn(func(ctx context.Context, object *unstructured.Unstructured, handler objectvisitor.ObjectHandler, _ bool) error {
Visit(gomock.Any(), gomock.Any(), handler, true, gomock.Any()).
DoAndReturn(func(ctx context.Context, object *unstructured.Unstructured, handler objectvisitor.ObjectHandler, _ bool, _ int) error {
visited = append(visited, *object)
return nil
}).AnyTimes()
Expand All @@ -146,7 +149,7 @@ func TestAPIService_Visit_local(t *testing.T) {
apiService := objectvisitor.NewAPIService(objectStore)

ctx := context.Background()
err := apiService.Visit(ctx, u, handler, visitor, true)
err := apiService.Visit(ctx, u, handler, visitor, true, 1)

sortObjectsByName(t, visited)

Expand Down
8 changes: 4 additions & 4 deletions internal/objectvisitor/fake/mock_default_typed_visitor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 18 additions & 4 deletions internal/objectvisitor/fake/mock_object_handler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions internal/objectvisitor/fake/mock_typed_visitor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions internal/objectvisitor/fake/mock_visitor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions internal/objectvisitor/horizontalpodautoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ func (HorizontalPodAutoscaler) Supports() schema.GroupVersionKind {
}

// Visit visits a hpa. It looks for an associated scale target (replication controllers, deployments, and replica sets)
func (s *HorizontalPodAutoscaler) Visit(ctx context.Context, object *unstructured.Unstructured, handler ObjectHandler, visitor Visitor, visitDescendants bool) error {
func (s *HorizontalPodAutoscaler) Visit(ctx context.Context, object *unstructured.Unstructured, handler ObjectHandler, visitor Visitor, visitDescendants bool, level int) error {
ctx, span := trace.StartSpan(ctx, "visitHorizontalPodAutoscaler")
defer span.End()

hpa := &autoscalingv1.HorizontalPodAutoscaler{}
if err := kubernetes.FromUnstructured(object, hpa); err != nil {
return err
}
level = handler.SetLevel(hpa.Kind, level)

var g errgroup.Group

Expand All @@ -53,12 +54,12 @@ func (s *HorizontalPodAutoscaler) Visit(ctx context.Context, object *unstructure
if target != nil {
g.Go(func() error {
u := &unstructured.Unstructured{Object: target}
if err := visitor.Visit(ctx, u, handler, true); err != nil {
if err := visitor.Visit(ctx, u, handler, true, level); err != nil {
return errors.Wrapf(err, "horizontal pod scaler %s visit scale target",
kubernetes.PrintObject(hpa))
}

return handler.AddEdge(ctx, object, u)
return handler.AddEdge(ctx, object, u, level)
})
}

Expand Down
7 changes: 4 additions & 3 deletions internal/objectvisitor/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ func (i *Ingress) Supports() schema.GroupVersionKind {
}

// Visit visits an ingress. It looks for associated services.
func (i *Ingress) Visit(ctx context.Context, object *unstructured.Unstructured, handler ObjectHandler, visitor Visitor, visitDescendants bool) error {
func (i *Ingress) Visit(ctx context.Context, object *unstructured.Unstructured, handler ObjectHandler, visitor Visitor, visitDescendants bool, level int) error {
ctx, span := trace.StartSpan(ctx, "visitIngress")
defer span.End()

ingress := &networkingv1.Ingress{}
if err := kubernetes.FromUnstructured(object, ingress); err != nil {
return err
}
level = handler.SetLevel(ingress.Kind, level)

services, err := i.queryer.ServicesForIngress(ctx, ingress)
if err != nil {
Expand All @@ -52,11 +53,11 @@ func (i *Ingress) Visit(ctx context.Context, object *unstructured.Unstructured,
for i := range services.Items {
service := &services.Items[i]
g.Go(func() error {
if err := visitor.Visit(ctx, service, handler, true); err != nil {
if err := visitor.Visit(ctx, service, handler, true, level); err != nil {
return errors.Wrapf(err, "ingress %s visit service %s",
kubernetes.PrintObject(ingress), kubernetes.PrintObject(service))
}
return handler.AddEdge(ctx, object, service)
return handler.AddEdge(ctx, object, service, level)
})

}
Expand Down
12 changes: 7 additions & 5 deletions internal/objectvisitor/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,23 @@ func TestIngress_Visit(t *testing.T) {

handler := fake.NewMockObjectHandler(controller)
handler.EXPECT().
AddEdge(gomock.Any(), u, testutil.ToUnstructured(t, service)).
AddEdge(gomock.Any(), u, testutil.ToUnstructured(t, service), gomock.Any()).
Return(nil)

var visited []unstructured.Unstructured
visitor := fake.NewMockVisitor(controller)
handler.EXPECT().SetLevel(gomock.Any(), 1).Return(2)
visitor.EXPECT().
Visit(gomock.Any(), gomock.Any(), handler, true).
DoAndReturn(func(ctx context.Context, object *unstructured.Unstructured, handler objectvisitor.ObjectHandler, _ bool) error {
Visit(gomock.Any(), gomock.Any(), handler, true, gomock.Any()).
DoAndReturn(func(ctx context.Context, object *unstructured.Unstructured, handler objectvisitor.ObjectHandler, _ bool, _ int) error {
visited = append(visited, *object)
return nil
})

ingress := objectvisitor.NewIngress(q)

ctx := context.Background()
err := ingress.Visit(ctx, u, handler, visitor, true)
err := ingress.Visit(ctx, u, handler, visitor, true, 1)

sortObjectsByName(t, visited)

Expand All @@ -66,13 +67,14 @@ func TestIngress_Visit_invalid_service_name(t *testing.T) {
Return(testutil.ToUnstructuredList(t), nil)

handler := fake.NewMockObjectHandler(controller)
handler.EXPECT().SetLevel(gomock.Any(), 1).Return(2)

visitor := fake.NewMockVisitor(controller)

ingress := objectvisitor.NewIngress(q)

ctx := context.Background()
err := ingress.Visit(ctx, u, handler, visitor, true)
err := ingress.Visit(ctx, u, handler, visitor, true, 1)

assert.NoError(t, err)

Expand Down
7 changes: 4 additions & 3 deletions internal/objectvisitor/mutatingwebhookconfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (p *MutatingWebhookConfiguration) Supports() schema.GroupVersionKind {
}

// Visit visits a mutatingwebhookconfiguration. It looks for service accounts and services.
func (p *MutatingWebhookConfiguration) Visit(ctx context.Context, object *unstructured.Unstructured, handler ObjectHandler, visitor Visitor, visitDescendants bool) error {
func (p *MutatingWebhookConfiguration) Visit(ctx context.Context, object *unstructured.Unstructured, handler ObjectHandler, visitor Visitor, visitDescendants bool, level int) error {
ctx, span := trace.StartSpan(ctx, "visitMutatingWebhookConfiguration")
defer span.End()

Expand All @@ -53,6 +53,7 @@ func (p *MutatingWebhookConfiguration) Visit(ctx context.Context, object *unstru
if err := kubernetes.FromUnstructured(object, mutatingwebhookconfiguration); err != nil {
return err
}
level = handler.SetLevel(mutatingwebhookconfiguration.Kind, level)

var g errgroup.Group

Expand All @@ -75,13 +76,13 @@ func (p *MutatingWebhookConfiguration) Visit(ctx context.Context, object *unstru
}

if visitDescendants {
if err := visitor.Visit(ctx, service, handler, false); err != nil {
if err := visitor.Visit(ctx, service, handler, false, level); err != nil {
return errors.Wrapf(err, "mutatingwebhookconfiguration %s visit service %s",
kubernetes.PrintObject(mutatingwebhookconfiguration), kubernetes.PrintObject(service))
}
}

return handler.AddEdge(ctx, object, service)
return handler.AddEdge(ctx, object, service, level)
})
}

Expand Down
Loading

0 comments on commit d0d2595

Please sign in to comment.