github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/client/userd/service.go (about) 1 package userd 2 3 import ( 4 "context" 5 6 "google.golang.org/grpc" 7 8 "github.com/datawire/dlib/dgroup" 9 "github.com/telepresenceio/telepresence/rpc/v2/manager" 10 "github.com/telepresenceio/telepresence/v2/pkg/client" 11 "github.com/telepresenceio/telepresence/v2/pkg/client/remotefs" 12 ) 13 14 const ProcessName = "connector" 15 16 // A Service is one that runs during the entire lifecycle of the daemon. 17 // This should be used to augment the daemon with GRPC services. 18 type Service interface { 19 // As will cast this instance to what the given ptr points to, and assign 20 // that to the pointer. It will panic if type is not implemented. 21 As(ptr any) 22 23 // ListenerAddress returns the address that this service is listening to. 24 ListenerAddress(ctx context.Context) string 25 26 Server() *grpc.Server 27 28 // SetManagerClient will assign the manager client that this Service will use when acting as 29 // a ManagerServer proxy 30 SetManagerClient(manager.ManagerClient, ...grpc.CallOption) 31 32 // FuseFTPMgr returns the manager responsible for creating a client that can connect to the FuseFTP service. 33 FuseFTPMgr() remotefs.FuseFTPManager 34 35 RootSessionInProcess() bool 36 WithSession(context.Context, string, func(context.Context, Session) error) error 37 38 ManageSessions(c context.Context) error 39 } 40 41 type NewServiceFunc func(context.Context, *dgroup.Group, client.Config, *grpc.Server) (Service, error) 42 43 type newServiceKey struct{} 44 45 func WithNewServiceFunc(ctx context.Context, f NewServiceFunc) context.Context { 46 return context.WithValue(ctx, newServiceKey{}, f) 47 } 48 49 func GetNewServiceFunc(ctx context.Context) NewServiceFunc { 50 if f, ok := ctx.Value(newServiceKey{}).(NewServiceFunc); ok { 51 return f 52 } 53 panic("No User daemon Service creator has been registered") 54 } 55 56 type serviceKey struct{} 57 58 func WithService(ctx context.Context, s Service) context.Context { 59 return context.WithValue(ctx, serviceKey{}, s) 60 } 61 62 func GetService(ctx context.Context) Service { 63 if f, ok := ctx.Value(serviceKey{}).(Service); ok { 64 return f 65 } 66 panic("No User daemon Service has been registered") 67 }