-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
external_process.go
159 lines (140 loc) · 4.44 KB
/
external_process.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
/*
Copyright 2023 Avi Zimmerman <avi.zimmerman@gmail.com>
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 clients
import (
"bufio"
"fmt"
"os"
"os/exec"
"strconv"
"sync"
"time"
v1 "github.com/webmeshproj/api/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/webmeshproj/webmesh/pkg/context"
)
// NewExternalProcessClient creates a new plugin client for an external plugin process.
func NewExternalProcessClient(ctx context.Context, path string) (PluginClient, error) {
p := &externalProcessPlugin{path: path}
return p, p.start(ctx)
}
type externalProcessPlugin struct {
path string
cmd *exec.Cmd
mux sync.Mutex
cli v1.PluginClient
conn *grpc.ClientConn
}
func (p *externalProcessPlugin) GetInfo(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*v1.PluginInfo, error) {
return p.cli.GetInfo(ctx, in)
}
func (p *externalProcessPlugin) Configure(ctx context.Context, in *v1.PluginConfiguration, opts ...grpc.CallOption) (*emptypb.Empty, error) {
return p.cli.Configure(ctx, in)
}
func (p *externalProcessPlugin) InjectQuerier(ctx context.Context, opts ...grpc.CallOption) (v1.Plugin_InjectQuerierClient, error) {
return p.cli.InjectQuerier(ctx, opts...)
}
func (p *externalProcessPlugin) Close(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*emptypb.Empty, error) {
p.mux.Lock()
defer p.mux.Unlock()
errs := make([]error, 0, 3)
if p.cli != nil {
if _, err := p.cli.Close(ctx, in); err != nil {
errs = append(errs, err)
}
}
if p.conn != nil {
if err := p.conn.Close(); err != nil {
errs = append(errs, err)
}
}
if p.cmd != nil && p.cmd.ProcessState == nil {
if err := p.cmd.Process.Kill(); err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
return nil, fmt.Errorf("close: %v", errs)
}
return &emptypb.Empty{}, nil
}
func (p *externalProcessPlugin) Storage() v1.StoragePluginClient {
return v1.NewStoragePluginClient(p.conn)
}
func (p *externalProcessPlugin) Auth() v1.AuthPluginClient {
return v1.NewAuthPluginClient(p.conn)
}
func (p *externalProcessPlugin) Events() v1.WatchPluginClient {
return v1.NewWatchPluginClient(p.conn)
}
func (p *externalProcessPlugin) IPAM() v1.IPAMPluginClient {
return v1.NewIPAMPluginClient(p.conn)
}
// checkProcess checks if the process is running and restarts it if it is not.
func (p *externalProcessPlugin) checkProcess(ctx context.Context) error {
p.mux.Lock()
defer p.mux.Unlock()
if p.cmd.ProcessState != nil {
if p.conn != nil {
_ = p.conn.Close()
}
_, ok := ctx.Deadline()
if !ok {
var cancel context.CancelFunc
ctx, cancel = context.WithDeadline(ctx, time.Now().Add(5*time.Second))
defer cancel()
}
return p.start(ctx)
}
return nil
}
// start starts the plugin server.
func (p *externalProcessPlugin) start(ctx context.Context) error {
r, w, err := os.Pipe()
if err != nil {
return fmt.Errorf("create pipe: %w", err)
}
defer r.Close()
defer w.Close()
p.cmd = exec.Command(p.path, "--broadcast-fd", strconv.Itoa(int(w.Fd())))
err = p.cmd.Start()
if err != nil {
return fmt.Errorf("start plugin: %w", err)
}
// Wait for the address to be written to the pipe.
b := bufio.NewReader(r)
if deadline, ok := ctx.Deadline(); ok {
err = r.SetReadDeadline(deadline)
if err != nil {
return fmt.Errorf("set read deadline: %w", err)
}
}
addr, err := b.ReadString('\n')
if err != nil {
return fmt.Errorf("read address: %w", err)
}
interceptor := func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
if err := p.checkProcess(ctx); err != nil {
return err
}
return invoker(ctx, method, req, reply, p.conn, opts...)
}
p.conn, err = grpc.DialContext(ctx, addr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithUnaryInterceptor(interceptor))
if err != nil {
return fmt.Errorf("dial: %w", err)
}
p.cli = v1.NewPluginClient(p.conn)
return nil
}