|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package v1 |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "io" |
| 9 | + "slices" |
| 10 | + "strings" |
| 11 | + "sync" |
| 12 | + |
| 13 | + "github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/internal/converter" |
| 14 | + "github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/types" |
| 15 | + pb "github.com/NVIDIA/OpenShell/sdk/go/proto/openshellv1" |
| 16 | + "google.golang.org/grpc" |
| 17 | +) |
| 18 | + |
| 19 | +// ServiceRouteSnapshot is a complete replacement view of workspace installations. |
| 20 | +type ServiceRouteSnapshot = types.ServiceRouteSnapshot |
| 21 | + |
| 22 | +// DiscoveredServiceRoute identifies an installed service and its current supervisor. |
| 23 | +type DiscoveredServiceRoute = types.DiscoveredServiceRoute |
| 24 | + |
| 25 | +// ServiceRouteEndpoint is the trusted supervisor TLS endpoint. |
| 26 | +type ServiceRouteEndpoint = types.ServiceRouteEndpoint |
| 27 | + |
| 28 | +// ServiceRouteScope selects a nonempty workspace set or all active workspaces. |
| 29 | +type ServiceRouteScope = types.ServiceRouteScope |
| 30 | + |
| 31 | +// ServiceRouteWatch delivers full replacements. Call Recv from one goroutine; |
| 32 | +// Close is safe concurrently. EOF is normal bounded stream completion: reconnect |
| 33 | +// and relist. Errors are not empty replacements. Expire cached routes independently. |
| 34 | +type ServiceRouteWatch interface { |
| 35 | + Recv() (*ServiceRouteSnapshot, error) |
| 36 | + Close() |
| 37 | +} |
| 38 | + |
| 39 | +// ServiceDiscoveryInterface is a narrow interface for external discovery consumers. |
| 40 | +// Selection never grants authority: the gateway must authorize every requested workspace. |
| 41 | +type ServiceDiscoveryInterface interface { |
| 42 | + Snapshot(ctx context.Context, workspace string) (*ServiceRouteSnapshot, error) |
| 43 | + Watch(ctx context.Context, workspace string) (ServiceRouteWatch, error) |
| 44 | + SnapshotFor(ctx context.Context, scope ServiceRouteScope) (*ServiceRouteSnapshot, error) |
| 45 | + WatchFor(ctx context.Context, scope ServiceRouteScope) (ServiceRouteWatch, error) |
| 46 | +} |
| 47 | + |
| 48 | +type serviceDiscoveryClient struct{ client pb.OpenShellClient } |
| 49 | + |
| 50 | +func newServiceDiscoveryClient(conn grpc.ClientConnInterface) *serviceDiscoveryClient { |
| 51 | + return &serviceDiscoveryClient{client: pb.NewOpenShellClient(conn)} |
| 52 | +} |
| 53 | + |
| 54 | +func discoveryScope(scope ServiceRouteScope) (ServiceRouteScope, error) { |
| 55 | + invalid := func() (ServiceRouteScope, error) { |
| 56 | + return ServiceRouteScope{}, &StatusError{Code: ErrorInvalidArgument, Message: "choose up to 64 distinct workspaces or all workspaces"} |
| 57 | + } |
| 58 | + if scope.AllWorkspaces == (len(scope.Workspaces) > 0) || len(scope.Workspaces) > 64 { |
| 59 | + return invalid() |
| 60 | + } |
| 61 | + scope.Workspaces = slices.Clone(scope.Workspaces) |
| 62 | + slices.Sort(scope.Workspaces) |
| 63 | + for i, name := range scope.Workspaces { |
| 64 | + if name == "" || len(name) > 19 || strings.HasPrefix(name, "-") || strings.HasSuffix(name, "-") || (i > 0 && name == scope.Workspaces[i-1]) { |
| 65 | + return invalid() |
| 66 | + } |
| 67 | + for _, c := range name { |
| 68 | + if (c < 'a' || c > 'z') && (c < '0' || c > '9') && c != '-' { |
| 69 | + return invalid() |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + return scope, nil |
| 74 | +} |
| 75 | + |
| 76 | +func (s *serviceDiscoveryClient) Snapshot(ctx context.Context, workspace string) (*ServiceRouteSnapshot, error) { |
| 77 | + return s.SnapshotFor(ctx, ServiceRouteScope{Workspaces: []string{workspace}}) |
| 78 | +} |
| 79 | +func (s *serviceDiscoveryClient) Watch(ctx context.Context, workspace string) (ServiceRouteWatch, error) { |
| 80 | + return s.WatchFor(ctx, ServiceRouteScope{Workspaces: []string{workspace}}) |
| 81 | +} |
| 82 | + |
| 83 | +// SnapshotFor returns one complete replacement view for the selected discovery scope. |
| 84 | +func (s *serviceDiscoveryClient) SnapshotFor(ctx context.Context, requested ServiceRouteScope) (*ServiceRouteSnapshot, error) { |
| 85 | + scope, err := discoveryScope(requested) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + request := &pb.GetServiceRouteSnapshotRequest{} |
| 90 | + if scope.AllWorkspaces { |
| 91 | + request.WorkspaceScope = allWorkspacesScope() |
| 92 | + } else if len(scope.Workspaces) == 1 { |
| 93 | + request.WorkspaceScope = namedWorkspaceScope(scope.Workspaces[0]) |
| 94 | + } else { |
| 95 | + request.Workspaces = scope.Workspaces |
| 96 | + } |
| 97 | + response, err := s.client.GetServiceRouteSnapshot(ctx, request) |
| 98 | + if err != nil { |
| 99 | + return nil, converter.FromGRPCError(err) |
| 100 | + } |
| 101 | + return converter.ServiceRouteSnapshotForScopeFromProto(response.GetSnapshot(), scope) |
| 102 | +} |
| 103 | + |
| 104 | +// WatchFor watches complete replacements, leaving reconnect policy to the caller. |
| 105 | +func (s *serviceDiscoveryClient) WatchFor(ctx context.Context, requested ServiceRouteScope) (ServiceRouteWatch, error) { |
| 106 | + scope, err := discoveryScope(requested) |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + request := &pb.WatchServiceRoutesRequest{} |
| 111 | + if scope.AllWorkspaces { |
| 112 | + request.WorkspaceScope = allWorkspacesScope() |
| 113 | + } else if len(scope.Workspaces) == 1 { |
| 114 | + request.WorkspaceScope = namedWorkspaceScope(scope.Workspaces[0]) |
| 115 | + } else { |
| 116 | + request.Workspaces = scope.Workspaces |
| 117 | + } |
| 118 | + ctx, cancel := context.WithCancel(ctx) |
| 119 | + stream, err := s.client.WatchServiceRoutes(ctx, request) |
| 120 | + if err != nil { |
| 121 | + cancel() |
| 122 | + return nil, converter.FromGRPCError(err) |
| 123 | + } |
| 124 | + return &serviceRouteWatch{stream: stream, cancel: cancel, scope: scope}, nil |
| 125 | +} |
| 126 | + |
| 127 | +type serviceRouteWatch struct { |
| 128 | + stream grpc.ServerStreamingClient[pb.WatchServiceRoutesResponse] |
| 129 | + cancel context.CancelFunc |
| 130 | + scope ServiceRouteScope |
| 131 | + once sync.Once |
| 132 | +} |
| 133 | + |
| 134 | +func (w *serviceRouteWatch) Close() { w.once.Do(w.cancel) } |
| 135 | + |
| 136 | +func (w *serviceRouteWatch) Recv() (*ServiceRouteSnapshot, error) { |
| 137 | + response, err := w.stream.Recv() |
| 138 | + if err != nil { |
| 139 | + w.Close() |
| 140 | + if err == io.EOF { |
| 141 | + return nil, io.EOF |
| 142 | + } |
| 143 | + return nil, converter.FromGRPCError(err) |
| 144 | + } |
| 145 | + result, err := converter.ServiceRouteSnapshotForScopeFromProto(response.GetSnapshot(), w.scope) |
| 146 | + if err != nil { |
| 147 | + w.Close() |
| 148 | + } |
| 149 | + return result, err |
| 150 | +} |
0 commit comments