@@ -7,33 +7,26 @@ package cmd
7
7
import (
8
8
"context"
9
9
"fmt"
10
- "net"
11
- "net/http"
12
- "os"
13
- "os/signal"
14
- "syscall"
10
+ "google.golang.org/grpc/credentials/insecure"
15
11
"time"
16
12
17
- grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
18
13
"github.com/heptiolabs/healthcheck"
19
14
"github.com/prometheus/client_golang/prometheus"
20
- "github.com/prometheus/client_golang/prometheus/collectors"
21
- "github.com/prometheus/client_golang/prometheus/promhttp"
22
15
"github.com/spf13/cobra"
23
- "golang.org/x/xerrors"
24
16
"google.golang.org/grpc"
25
17
"google.golang.org/grpc/credentials"
26
- "google.golang.org/grpc/health"
27
18
"google.golang.org/grpc/health/grpc_health_v1"
28
19
20
+ "github.com/gitpod-io/gitpod/common-go/baseserver"
29
21
common_grpc "github.com/gitpod-io/gitpod/common-go/grpc"
30
22
"github.com/gitpod-io/gitpod/common-go/log"
31
- "github.com/gitpod-io/gitpod/common-go/pprof"
32
23
"github.com/gitpod-io/gitpod/common-go/watch"
33
24
"github.com/gitpod-io/gitpod/ws-daemon/pkg/config"
34
25
"github.com/gitpod-io/gitpod/ws-daemon/pkg/daemon"
35
26
)
36
27
28
+ const grpcServerName = "wsdaemon"
29
+
37
30
// serveCmd represents the serve command
38
31
var runCmd = & cobra.Command {
39
32
Use : "run" ,
@@ -42,91 +35,31 @@ var runCmd = &cobra.Command{
42
35
Run : func (cmd * cobra.Command , args []string ) {
43
36
cfg , err := config .Read (configFile )
44
37
if err != nil {
45
- log .WithError (err ).Fatal ("cannot read configuration. Maybe missing --config?" )
46
- }
47
- reg := prometheus .NewRegistry ()
48
- dmn , err := daemon .NewDaemon (cfg .Daemon , prometheus .WrapRegistererWithPrefix ("gitpod_ws_daemon_" , reg ))
49
- if err != nil {
50
- log .WithError (err ).Fatal ("cannot create daemon" )
38
+ log .WithError (err ).Fatal ("Cannot read configuration. Maybe missing --config?" )
51
39
}
52
40
53
- common_grpc .SetupLogging ()
54
-
55
- grpcMetrics := grpc_prometheus .NewServerMetrics ()
56
- grpcMetrics .EnableHandlingTimeHistogram ()
57
- reg .MustRegister (grpcMetrics )
58
-
59
- grpcOpts := common_grpc .ServerOptionsWithInterceptors (
60
- []grpc.StreamServerInterceptor {grpcMetrics .StreamServerInterceptor ()},
61
- []grpc.UnaryServerInterceptor {grpcMetrics .UnaryServerInterceptor ()},
41
+ health := healthcheck .NewHandler ()
42
+ srv , err := baseserver .New (grpcServerName ,
43
+ baseserver .WithGRPC (& cfg .Service ),
44
+ baseserver .WithHealthHandler (health ),
62
45
)
63
- tlsOpt , err := cfg .Service .TLS .ServerOption ()
64
46
if err != nil {
65
- log .WithError (err ).Fatal ("cannot use TLS config " )
47
+ log .WithError (err ).Fatal ("Cannot set up server. " )
66
48
}
67
- if tlsOpt != nil {
68
- log .WithField ("crt" , cfg .Service .TLS .Certificate ).WithField ("key" , cfg .Service .TLS .PrivateKey ).Debug ("securing gRPC server with TLS" )
69
- grpcOpts = append (grpcOpts , tlsOpt )
70
- } else {
71
- log .Warn ("no TLS configured - gRPC server will be unsecured" )
72
- }
73
-
74
- healthServer := health .NewServer ()
75
49
76
- server := grpc .NewServer (grpcOpts ... )
77
- server .RegisterService (& grpc_health_v1 .Health_ServiceDesc , healthServer )
78
-
79
- dmn .Register (server )
80
- lis , err := net .Listen ("tcp" , cfg .Service .Addr )
50
+ dmn , err := daemon .NewDaemon (cfg .Daemon , prometheus .WrapRegistererWithPrefix ("gitpod_ws_daemon_" , srv .MetricsRegistry ()))
81
51
if err != nil {
82
- log .WithError (err ).Fatalf ( "cannot listen on %s" , cfg . Service . Addr )
52
+ log .WithError (err ).Fatal ( "Cannot create daemon." )
83
53
}
84
- go func () {
85
- err := server .Serve (lis )
86
- if err != nil {
87
- log .WithError (err ).Fatal ("cannot start server" )
88
- }
89
- }()
90
- log .WithField ("addr" , cfg .Service .Addr ).Info ("started gRPC server" )
91
-
92
- if cfg .Prometheus .Addr != "" {
93
- reg .MustRegister (
94
- collectors .NewGoCollector (),
95
- collectors .NewProcessCollector (collectors.ProcessCollectorOpts {}),
96
- )
97
-
98
- handler := http .NewServeMux ()
99
- handler .Handle ("/metrics" , promhttp .HandlerFor (reg , promhttp.HandlerOpts {}))
100
54
101
- go func () {
102
- err := http .ListenAndServe (cfg .Prometheus .Addr , handler )
103
- if err != nil {
104
- log .WithError (err ).Error ("Prometheus metrics server failed" )
105
- }
106
- }()
107
- log .WithField ("addr" , cfg .Prometheus .Addr ).Info ("started Prometheus metrics server" )
108
- }
109
-
110
- if cfg .PProf .Addr != "" {
111
- go pprof .Serve (cfg .PProf .Addr )
112
- }
55
+ health .AddReadinessCheck ("grpc-server" , grpcProbe (cfg .Service ))
56
+ health .AddReadinessCheck ("ws-daemon" , dmn .ReadinessProbe ())
113
57
114
- if cfg .ReadinessProbeAddr != "" {
115
- // Ensure we can access the GRPC server is healthy, the etc hosts file was updated and containerd is available.
116
- health := healthcheck .NewHandler ()
117
- health .AddReadinessCheck ("grpc-server" , grpcProbe (cfg .Service ))
118
- health .AddReadinessCheck ("ws-daemon" , dmn .ReadinessProbe ())
119
-
120
- go func () {
121
- if err := http .ListenAndServe (cfg .ReadinessProbeAddr , health ); err != nil && err != http .ErrServerClosed {
122
- log .WithError (err ).Panic ("error starting HTTP server" )
123
- }
124
- }()
125
- }
58
+ dmn .Register (srv .GRPC ())
126
59
127
60
err = dmn .Start ()
128
61
if err != nil {
129
- log .WithError (err ).Fatal ("cannot start daemon" )
62
+ log .WithError (err ).Fatal ("Cannot start daemon. " )
130
63
}
131
64
132
65
ctx , cancel := context .WithCancel (context .Background ())
@@ -138,57 +71,50 @@ var runCmd = &cobra.Command{
138
71
139
72
cfg , err := config .Read (configFile )
140
73
if err != nil {
141
- log .WithError (err ).Warn ("cannot reload configuration" )
74
+ log .WithError (err ).Warn ("Cannot reload configuration. " )
142
75
return
143
76
}
144
77
145
78
err = dmn .ReloadConfig (ctx , & cfg .Daemon )
146
79
if err != nil {
147
- log .WithError (err ).Warn ("cannot reload configuration" )
80
+ log .WithError (err ).Warn ("Cannot reload configuration. " )
148
81
}
149
82
})
150
83
if err != nil {
151
- log .WithError (err ).Fatal ("cannot start watch of configuration file" )
84
+ log .WithError (err ).Fatal ("Cannot start watch of configuration file. " )
152
85
}
153
86
154
- // run until we're told to stop
155
- sigChan := make (chan os.Signal , 1 )
156
- signal .Notify (sigChan , os .Interrupt , syscall .SIGTERM )
157
- log .Info ("🧫 ws-daemon is up and running. Stop with SIGINT or CTRL+C" )
158
- <- sigChan
159
- server .Stop ()
160
- err = dmn .Stop ()
87
+ err = srv .ListenAndServe ()
161
88
if err != nil {
162
- log .WithError (err ).Error ( "cannot shut down gracefully " )
89
+ log .WithError (err ).Fatal ( "Failed to listen and serve. " )
163
90
}
164
- log .Info ("Received SIGINT - shutting down" )
165
91
},
166
92
}
167
93
168
94
func init () {
169
95
rootCmd .AddCommand (runCmd )
170
96
}
171
97
172
- func grpcProbe (tlsConfig config. AddrTLS ) func () error {
98
+ func grpcProbe (cfg baseserver. ServerConfiguration ) func () error {
173
99
return func () error {
174
- secopt := grpc . WithInsecure ()
175
- if tlsConfig .TLS != nil && tlsConfig .TLS .Certificate != "" {
100
+ creds := insecure . NewCredentials ()
101
+ if cfg .TLS != nil && cfg .TLS .CertPath != "" {
176
102
tlsConfig , err := common_grpc .ClientAuthTLSConfig (
177
- tlsConfig .TLS .Authority , tlsConfig .TLS .Certificate , tlsConfig .TLS .PrivateKey ,
103
+ cfg .TLS .CAPath , cfg .TLS .CertPath , cfg .TLS .KeyPath ,
178
104
common_grpc .WithSetRootCAs (true ),
179
- common_grpc .WithServerName ("wsdaemon" ),
105
+ common_grpc .WithServerName (grpcServerName ),
180
106
)
181
107
if err != nil {
182
- return xerrors .Errorf ("cannot load ws-daemon certificate: %w" , err )
108
+ return fmt .Errorf ("cannot load ws-daemon certificate: %w" , err )
183
109
}
184
110
185
- secopt = grpc . WithTransportCredentials ( credentials .NewTLS (tlsConfig ) )
111
+ creds = credentials .NewTLS (tlsConfig )
186
112
}
187
113
188
114
ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
189
115
defer cancel ()
190
116
191
- conn , err := grpc .DialContext (ctx , tlsConfig . Addr , secopt )
117
+ conn , err := grpc .DialContext (ctx , cfg . Address , grpc . WithTransportCredentials ( creds ) )
192
118
if err != nil {
193
119
return err
194
120
}
0 commit comments