Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime: Support path filter in ListResources #4651

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
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
1,553 changes: 783 additions & 770 deletions proto/gen/rill/runtime/v1/api.pb.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions proto/gen/rill/runtime/v1/api.pb.validate.go

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

7 changes: 7 additions & 0 deletions proto/gen/rill/runtime/v1/runtime.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2417,10 +2417,17 @@ paths:
$ref: '#/definitions/rpcStatus'
parameters:
- name: instanceId
description: Instance to list resources from.
in: path
required: true
type: string
- name: kind
description: Filter by resource kind (optional).
in: query
required: false
type: string
- name: path
description: Filter by resource path (optional).
in: query
required: false
type: string
Expand Down
4 changes: 4 additions & 0 deletions proto/rill/runtime/v1/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,12 @@ message WatchLogsResponse {
}

message ListResourcesRequest {
// Instance to list resources from.
string instance_id = 1;
// Filter by resource kind (optional).
string kind = 2;
// Filter by resource path (optional).
string path = 3;
}

message ListResourcesResponse {
Expand Down
83 changes: 41 additions & 42 deletions runtime/catalog_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,59 +154,58 @@ func (c *catalogCache) get(n *runtimev1.ResourceName, withDeleted, clone bool) (
}

// list returns a list of resources in the catalog.
// It optionally supports filtering by kind, path, and soft-deleted status.
// The returned list is not sorted.
// The returned list is always safe to manipulate (e.g. sort/filter), but the resource pointers must not be edited unless clone=true.
// Unlike other catalog functions, it is safe to call list concurrently with calls to get and flush (i.e. under a read lock).
func (c *catalogCache) list(kind string, withDeleted, clone bool) ([]*runtimev1.Resource, error) {
if kind != "" {
n := len(c.resources[kind])
res := make([]*runtimev1.Resource, 0, n)
if withDeleted {
for _, r := range c.resources[kind] {
if clone {
r = c.clone(r)
}
res = append(res, r)
}
} else {
for _, r := range c.resources[kind] {
if r.Meta.DeletedOn == nil {
if clone {
r = c.clone(r)
}
res = append(res, r)
}
}
}

return res, nil
}

func (c *catalogCache) list(kind, path string, withDeleted, clone bool) ([]*runtimev1.Resource, error) {
// Estimate number of resources to list
n := 0
for _, rs := range c.resources {
n += len(rs)
if path != "" {
n = 1
} else if kind == "" {
for _, rs := range c.resources {
n += len(rs)
}
} else {
n = len(c.resources[kind])
}

// Alloc slice for resources
res := make([]*runtimev1.Resource, 0, n)
if withDeleted {
for _, rs := range c.resources {
for _, r := range rs {
if clone {
r = c.clone(r)
}
res = append(res, r)
}

// Find resources matching the filters and append to slice
for k, rs := range c.resources {
// Skip if doesn't match kind filter
if kind != "" && k != kind {
continue
}
} else {
for _, rs := range c.resources {
for _, r := range rs {
if r.Meta.DeletedOn == nil {
if clone {
r = c.clone(r)

for _, r := range rs {
// Skip if doesn't match withDeleted filter
if !withDeleted && r.Meta.DeletedOn != nil {
continue
}

// Skip if doesn't match path filter
if path != "" {
found := false
for _, p := range r.Meta.FilePaths {
if p == path {
found = true
break
}
res = append(res, r)
}
if !found {
continue
}
}

// Append to res
if clone {
r = c.clone(r)
}
res = append(res, r)
}
}

Expand Down
4 changes: 2 additions & 2 deletions runtime/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ func (c *Controller) Get(ctx context.Context, name *runtimev1.ResourceName, clon
// List returns a list of resources of the specified kind.
// If kind is empty, all resources are returned.
// Soft-deleted resources (i.e. resources where DeletedOn != nil) are not returned.
func (c *Controller) List(ctx context.Context, kind string, clone bool) ([]*runtimev1.Resource, error) {
func (c *Controller) List(ctx context.Context, kind, path string, clone bool) ([]*runtimev1.Resource, error) {
ctx, span := tracer.Start(ctx, "Controller.List", trace.WithAttributes(attribute.String("instance_id", c.InstanceID), attribute.String("kind", kind)))
defer span.End()
if err := c.checkRunning(); err != nil {
Expand All @@ -404,7 +404,7 @@ func (c *Controller) List(ctx context.Context, kind string, clone bool) ([]*runt
}
c.lock(ctx, true)
defer c.unlock(ctx, true)
return c.catalog.list(kind, false, clone)
return c.catalog.list(kind, path, false, clone)
}

// SubscribeCallback is the callback type passed to Subscribe.
Expand Down
4 changes: 2 additions & 2 deletions runtime/reconcilers/project_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (r *ProjectParserReconciler) Reconcile(ctx context.Context, n *runtimev1.Re
r.C.Lock(ctx)
defer r.C.Unlock(ctx)

resources, err := r.C.List(ctx, "", false)
resources, err := r.C.List(ctx, "", "", false)
if err != nil {
return runtime.ReconcileResult{Err: err}
}
Expand Down Expand Up @@ -348,7 +348,7 @@ func (r *ProjectParserReconciler) reconcileResources(ctx context.Context, inst *
var deleteResources []*runtimev1.Resource

// Pass over all existing resources in the catalog.
resources, err := r.C.List(ctx, "", false)
resources, err := r.C.List(ctx, "", "", false)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/reconcilers/refresh_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (r *RefreshTriggerReconciler) Reconcile(ctx context.Context, n *runtimev1.R
r.C.Lock(ctx)
defer r.C.Unlock(ctx)

resources, err := r.C.List(ctx, "", false)
resources, err := r.C.List(ctx, "", "", false)
if err != nil {
return runtime.ReconcileResult{Err: err}
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/server/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func (s *Server) RefreshAndReconcile(ctx context.Context, req *runtimev1.Refresh
return nil, status.Error(codes.InvalidArgument, err.Error())
}

rs, err := ctrl.List(ctx, runtime.ResourceKindSource, false)
rs, err := ctrl.List(ctx, runtime.ResourceKindSource, "", false)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
Expand Down Expand Up @@ -528,7 +528,7 @@ func (s *Server) resourceToEntry(ctx context.Context, instanceID string, r *runt
}

func (s *Server) controllerToLegacyReconcileStatus(ctx context.Context, ctrl *runtime.Controller, since time.Time) (*runtimev1.ReconcileResponse, error) {
rs, err := ctrl.List(ctx, "", false)
rs, err := ctrl.List(ctx, "", "", false)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/server/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *Server) ListResources(ctx context.Context, req *runtimev1.ListResources
return nil, status.Error(codes.InvalidArgument, err.Error())
}

rs, err := ctrl.List(ctx, req.Kind, false)
rs, err := ctrl.List(ctx, req.Kind, req.Path, false)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
Expand Down Expand Up @@ -95,7 +95,7 @@ func (s *Server) WatchResources(req *runtimev1.WatchResourcesRequest, ss runtime
}

if req.Replay {
rs, err := ctrl.List(ss.Context(), req.Kind, false)
rs, err := ctrl.List(ss.Context(), req.Kind, "", false)
if err != nil {
return status.Error(codes.InvalidArgument, err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/testruntime/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func RequireReconcileState(t testing.TB, rt *runtime.Runtime, id string, lenReso
ctrl, err := rt.Controller(context.Background(), id)
require.NoError(t, err)

rs, err := ctrl.List(context.Background(), "", false)
rs, err := ctrl.List(context.Background(), "", "", false)
require.NoError(t, err)

var reconcileErrs, parseErrs []string
Expand Down Expand Up @@ -185,7 +185,7 @@ func DumpResources(t testing.TB, rt *runtime.Runtime, id string) {
ctrl, err := rt.Controller(context.Background(), id)
require.NoError(t, err)

rs, err := ctrl.List(context.Background(), "", false)
rs, err := ctrl.List(context.Background(), "", "", false)
require.NoError(t, err)

for _, r := range rs {
Expand Down
12 changes: 12 additions & 0 deletions web-common/src/proto/gen/rill/runtime/v1/api_pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2400,15 +2400,26 @@ export class WatchLogsResponse extends Message<WatchLogsResponse> {
*/
export class ListResourcesRequest extends Message<ListResourcesRequest> {
/**
* Instance to list resources from.
*
* @generated from field: string instance_id = 1;
*/
instanceId = "";

/**
* Filter by resource kind (optional).
*
* @generated from field: string kind = 2;
*/
kind = "";

/**
* Filter by resource path (optional).
*
* @generated from field: string path = 3;
*/
path = "";

constructor(data?: PartialMessage<ListResourcesRequest>) {
super();
proto3.util.initPartial(data, this);
Expand All @@ -2419,6 +2430,7 @@ export class ListResourcesRequest extends Message<ListResourcesRequest> {
static readonly fields: FieldList = proto3.util.newFieldList(() => [
{ no: 1, name: "instance_id", kind: "scalar", T: 9 /* ScalarType.STRING */ },
{ no: 2, name: "kind", kind: "scalar", T: 9 /* ScalarType.STRING */ },
{ no: 3, name: "path", kind: "scalar", T: 9 /* ScalarType.STRING */ },
]);

static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): ListResourcesRequest {
Expand Down
4 changes: 2 additions & 2 deletions web-common/src/proto/gen/rill/ui/v1/dashboard_pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class DashboardState extends Message<DashboardState> {
chartType?: string;

/**
*
* *
* Pivot related fields
*
* @generated from field: optional bool pivot_is_active = 22;
Expand Down Expand Up @@ -352,7 +352,7 @@ proto3.util.setEnumType(DashboardState_LeaderboardSortDirection, "rill.ui.v1.Das
]);

/**
*
* *
* SortType is used to determine how to sort the leaderboard
* and dimension detail table, as well as where to place the
* sort arrow.
Expand Down
5 changes: 4 additions & 1 deletion web-common/src/runtime-client/gen/index.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ export type RuntimeServiceWatchResourcesParams = {
level?: string;
};

export type RuntimeServiceListResourcesParams = { kind?: string };
export type RuntimeServiceListResourcesParams = {
kind?: string;
path?: string;
};

export type RuntimeServiceGetResourceParams = {
"name.kind"?: string;
Expand Down
Loading