forked from GoogleContainerTools/skaffold
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
154 lines (131 loc) · 4.24 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Copyright 2019 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package server
import (
"context"
"fmt"
"net"
"net/http"
"sync"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/proto"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
var srv *server
type server struct {
buildIntentCallback func()
syncIntentCallback func()
deployIntentCallback func()
}
func SetBuildCallback(callback func()) {
if srv != nil {
srv.buildIntentCallback = callback
}
}
func SetDeployCallback(callback func()) {
if srv != nil {
srv.deployIntentCallback = callback
}
}
func SetSyncCallback(callback func()) {
if srv != nil {
srv.syncIntentCallback = callback
}
}
// Initialize creates the gRPC and HTTP servers for serving the state and event log.
// It returns a shutdown callback for tearing down the grpc server,
// which the runner is responsible for calling.
func Initialize(opts *config.SkaffoldOptions) (func() error, error) {
if !opts.EnableRPC {
return func() error { return nil }, nil
}
originalRPCPort := opts.RPCPort
if originalRPCPort == -1 {
return func() error { return nil }, nil
}
rpcPort := util.GetAvailablePort(originalRPCPort, &sync.Map{})
if rpcPort != originalRPCPort && originalRPCPort != constants.DefaultRPCPort {
logrus.Warnf("provided port %d already in use: using %d instead", originalRPCPort, rpcPort)
}
grpcCallback, err := newGRPCServer(rpcPort)
if err != nil {
return grpcCallback, errors.Wrap(err, "starting gRPC server")
}
m := &sync.Map{}
m.Store(rpcPort, true)
originalHTTPPort := opts.RPCHTTPPort
httpPort := util.GetAvailablePort(originalHTTPPort, m)
if httpPort != originalHTTPPort && originalHTTPPort != constants.DefaultRPCHTTPPort {
logrus.Warnf("provided port %d already in use: using %d instead", originalHTTPPort, httpPort)
}
httpCallback, err := newHTTPServer(httpPort, rpcPort)
callback := func() error {
httpErr := httpCallback()
grpcErr := grpcCallback()
errStr := ""
if grpcErr != nil {
errStr += fmt.Sprintf("grpc callback error: %s\n", grpcErr.Error())
}
if httpErr != nil {
errStr += fmt.Sprintf("http callback error: %s\n", httpErr.Error())
}
return errors.New(errStr)
}
if err != nil {
return callback, errors.Wrap(err, "starting HTTP server")
}
return callback, nil
}
func newGRPCServer(port int) (func() error, error) {
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", util.Loopback, port))
if err != nil {
return func() error { return nil }, errors.Wrap(err, "creating listener")
}
logrus.Infof("starting gRPC server on port %d", port)
s := grpc.NewServer()
srv = &server{
buildIntentCallback: func() {},
deployIntentCallback: func() {},
syncIntentCallback: func() {},
}
proto.RegisterSkaffoldServiceServer(s, srv)
go func() {
if err := s.Serve(l); err != nil {
logrus.Errorf("failed to start grpc server: %s", err)
}
}()
return func() error {
s.Stop()
return l.Close()
}, nil
}
func newHTTPServer(port, proxyPort int) (func() error, error) {
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
err := proto.RegisterSkaffoldServiceHandlerFromEndpoint(context.Background(), mux, fmt.Sprintf("%s:%d", util.Loopback, proxyPort), opts)
if err != nil {
return func() error { return nil }, err
}
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", util.Loopback, port))
if err != nil {
return func() error { return nil }, errors.Wrap(err, "creating listener")
}
logrus.Infof("starting gRPC HTTP server on port %d", port)
go http.Serve(l, mux)
return l.Close, nil
}