@@ -561,6 +561,20 @@ var ring1Cmd = &cobra.Command{
561
561
}
562
562
}()
563
563
564
+ socketPath := filepath .Join (ring2Root , ".supervisor" )
565
+ if _ , err = os .Stat (socketPath ); os .IsNotExist (err ) {
566
+ if err := os .MkdirAll (socketPath , 0644 ); err != nil {
567
+ log .Errorf ("failed to create dir %v" , err )
568
+ }
569
+ }
570
+
571
+ stopHook , err := startInfoService (socketPath )
572
+ if err != nil {
573
+ // workspace info is not critical, so we will not fail workspace start
574
+ log .Error ("failed to start workspace info service" )
575
+ }
576
+ defer stopHook ()
577
+
564
578
// run prestophook when ring1 exits. This is more reliable way of running this script then
565
579
// using PreStop Lifecycle Handler of the pod (it would not execute for prebuilds for example)
566
580
prestophookFunc := func () {
@@ -983,6 +997,68 @@ func connectToInWorkspaceDaemonService(ctx context.Context) (*inWorkspaceService
983
997
}, nil
984
998
}
985
999
1000
+ type workspaceInfoService struct {
1001
+ socket net.Listener
1002
+ server * grpc.Server
1003
+ api.UnimplementedWorkspaceInfoServiceServer
1004
+ }
1005
+
1006
+ func startInfoService (socketDir string ) (func (), error ) {
1007
+ socketFN := filepath .Join (socketDir , "info.sock" )
1008
+ if _ , err := os .Stat (socketFN ); err == nil {
1009
+ _ = os .Remove (socketFN )
1010
+ }
1011
+
1012
+ sckt , err := net .Listen ("unix" , socketFN )
1013
+ if err != nil {
1014
+ return nil , xerrors .Errorf ("cannot create info socket: %w" , err )
1015
+ }
1016
+
1017
+ err = os .Chmod (socketFN , 0777 )
1018
+ if err != nil {
1019
+ return nil , xerrors .Errorf ("cannot chmod info socket: %w" , err )
1020
+ }
1021
+
1022
+ infoSvc := workspaceInfoService {
1023
+ socket : sckt ,
1024
+ }
1025
+
1026
+ limiter := common_grpc .NewRatelimitingInterceptor (
1027
+ map [string ]common_grpc.RateLimit {
1028
+ "iws.WorkspaceInfoService/WorkspaceInfo" : {
1029
+ RefillInterval : 1500 ,
1030
+ BucketSize : 4 ,
1031
+ },
1032
+ })
1033
+
1034
+ infoSvc .server = grpc .NewServer (grpc .ChainUnaryInterceptor (limiter .UnaryInterceptor ()))
1035
+ api .RegisterWorkspaceInfoServiceServer (infoSvc .server , & infoSvc )
1036
+ go func () {
1037
+ err := infoSvc .server .Serve (sckt )
1038
+ if err != nil {
1039
+ log .WithError (err ).Error ("workspace info server failed" )
1040
+ }
1041
+ }()
1042
+
1043
+ return func () {
1044
+ infoSvc .server .Stop ()
1045
+ os .Remove (socketFN )
1046
+ }, nil
1047
+ }
1048
+
1049
+ func (svc * workspaceInfoService ) WorkspaceInfo (ctx context.Context , req * api.WorkspaceInfoRequest ) (* api.WorkspaceInfoResponse , error ) {
1050
+ client , err := connectToInWorkspaceDaemonService (ctx )
1051
+ if err != nil {
1052
+ return nil , err
1053
+ }
1054
+
1055
+ resp , err := client .WorkspaceInfo (ctx , & api.WorkspaceInfoRequest {})
1056
+ if err != nil {
1057
+ log .WithError (err ).Error ("could not get workspace info" )
1058
+ }
1059
+ return resp , nil
1060
+ }
1061
+
986
1062
func init () {
987
1063
rootCmd .AddCommand (ring0Cmd )
988
1064
rootCmd .AddCommand (ring1Cmd )
0 commit comments