/
app.go
195 lines (168 loc) · 5.74 KB
/
app.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2022 OnMetal 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 app
import (
"context"
"errors"
goflag "flag"
"fmt"
"net"
"net/http"
"net/url"
"github.com/go-logr/logr"
"github.com/onmetal/controller-utils/configutils"
"github.com/onmetal/onmetal-api/broker/common"
commongrpc "github.com/onmetal/onmetal-api/broker/common/grpc"
machinebrokerhttp "github.com/onmetal/onmetal-api/broker/machinebroker/http"
"github.com/onmetal/onmetal-api/broker/machinebroker/server"
ori "github.com/onmetal/onmetal-api/ori/apis/machine/v1alpha1"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
type Options struct {
Kubeconfig string
Address string
StreamingAddress string
BaseURL string
Namespace string
MachinePoolName string
MachinePoolSelector map[string]string
}
func (o *Options) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.Kubeconfig, "kubeconfig", o.Kubeconfig, "Path pointing to a kubeconfig file to use.")
fs.StringVar(&o.Address, "address", "/var/run/ori-machinebroker.sock", "Address to listen on.")
fs.StringVar(&o.StreamingAddress, "streaming-address", "127.0.0.1:20251", "Address to run the streaming server on")
fs.StringVar(&o.BaseURL, "base-url", "", "The base url to construct urls for streaming from. If empty it will be "+
"constructed from the streaming-address")
fs.StringVar(&o.Namespace, "namespace", o.Namespace, "Target Kubernetes namespace to use.")
fs.StringVar(&o.MachinePoolName, "machine-pool-name", o.MachinePoolName, "Name of the target machine pool to pin machines to, if any.")
fs.StringToStringVar(&o.MachinePoolSelector, "machine-pool-selector", o.MachinePoolSelector, "Selector of the target machine pools to pin machines to, if any.")
}
func Command() *cobra.Command {
var (
zapOpts = zap.Options{Development: true}
opts Options
)
cmd := &cobra.Command{
Use: "machinebroker",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
logger := zap.New(zap.UseFlagOptions(&zapOpts))
ctrl.SetLogger(logger)
cmd.SetContext(ctrl.LoggerInto(cmd.Context(), ctrl.Log))
},
RunE: func(cmd *cobra.Command, args []string) error {
return Run(cmd.Context(), opts)
},
}
goFlags := goflag.NewFlagSet("", 0)
zapOpts.BindFlags(goFlags)
cmd.PersistentFlags().AddGoFlagSet(goFlags)
opts.AddFlags(cmd.Flags())
return cmd
}
func Run(ctx context.Context, opts Options) error {
log := ctrl.LoggerFrom(ctx)
setupLog := log.WithName("setup")
cfg, err := configutils.GetConfig(configutils.Kubeconfig(opts.Kubeconfig))
if err != nil {
return err
}
if opts.Namespace == "" {
return fmt.Errorf("must specify namespace")
}
baseURL := opts.BaseURL
if baseURL == "" {
u := &url.URL{
Scheme: "http",
Host: opts.StreamingAddress,
}
baseURL = u.String()
}
log.V(1).Info("Creating server",
"Namespace", opts.Namespace,
"MachinePoolName", opts.MachinePoolName,
"MachinePoolSelector", opts.MachinePoolSelector,
"BaseURL", baseURL,
)
srv, err := server.New(cfg, opts.Namespace, server.Options{
BaseURL: baseURL,
MachinePoolName: opts.MachinePoolName,
MachinePoolSelector: opts.MachinePoolSelector,
})
if err != nil {
return fmt.Errorf("error creating server: %w", err)
}
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
return runGRPCServer(ctx, setupLog, log, srv, opts)
})
g.Go(func() error {
return runStreamingServer(ctx, setupLog, log, srv, opts)
})
return g.Wait()
}
func runGRPCServer(ctx context.Context, setupLog logr.Logger, log logr.Logger, srv *server.Server, opts Options) error {
log.V(1).Info("Cleaning up any previous socket")
if err := common.CleanupSocketIfExists(opts.Address); err != nil {
return fmt.Errorf("error cleaning up socket: %w", err)
}
grpcSrv := grpc.NewServer(
grpc.ChainUnaryInterceptor(
commongrpc.InjectLogger(log),
commongrpc.LogRequest,
),
)
ori.RegisterMachineRuntimeServer(grpcSrv, srv)
log.V(1).Info("Start listening on unix socket", "Address", opts.Address)
l, err := net.Listen("unix", opts.Address)
if err != nil {
return fmt.Errorf("failed to listen: %w", err)
}
setupLog.Info("Starting server", "Address", l.Addr().String())
go func() {
<-ctx.Done()
setupLog.Info("Shutting down server")
grpcSrv.GracefulStop()
setupLog.Info("Shut down server")
}()
if err := grpcSrv.Serve(l); err != nil {
return fmt.Errorf("error serving: %w", err)
}
return nil
}
func runStreamingServer(ctx context.Context, setupLog, log logr.Logger, srv *server.Server, opts Options) error {
httpHandler := machinebrokerhttp.NewHandler(srv, machinebrokerhttp.HandlerOptions{
Log: log.WithName("server"),
})
httpSrv := &http.Server{
Addr: opts.StreamingAddress,
Handler: httpHandler,
}
go func() {
<-ctx.Done()
setupLog.Info("Shutting down http server")
_ = httpSrv.Close()
setupLog.Info("Shut down http server")
}()
log.V(1).Info("Starting streaming server", "Address", opts.StreamingAddress)
if err := httpSrv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
return fmt.Errorf("error listening / serving streaming server: %w", err)
}
log.V(1).Info("Server finished")
return nil
}