forked from containers/conmon-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
1272 lines (1036 loc) · 32.6 KB
/
client.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package client
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"os"
"os/exec"
"path/filepath"
"strconv"
"sync"
"syscall"
"time"
"capnproto.org/go/capnp/v3/rpc"
"github.com/blang/semver/v4"
"github.com/containers/conmon-rs/internal/proto"
"github.com/containers/storage/pkg/idtools"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)
const (
binaryName = "conmonrs"
socketName = "conmon.sock"
pidFileName = "pidfile"
defaultTimeout = 10 * time.Second
)
var (
errRuntimeUnspecified = errors.New("runtime must be specified")
errRunDirUnspecified = errors.New("RunDir must be specified")
errInvalidValue = errors.New("invalid value")
errRunDirNotCreated = errors.New("could not create RunDir")
errTimeoutWaitForPid = errors.New("timed out waiting for server PID to disappear")
errUndefinedCgroupManager = errors.New("undefined cgroup manager")
)
// ConmonClient is the main client structure of this package.
type ConmonClient struct {
serverPID uint32
runDir string
logger *logrus.Logger
attachReaders *sync.Map // K: UUID string, V: *attachReaderValue
tracingEnabled bool
tracer trace.Tracer
serverVersion semver.Version
cgroupManager CgroupManager
}
// ConmonServerConfig is the configuration for the conmon server instance.
type ConmonServerConfig struct {
// ClientLogger can be set to use a custom logger rather than the
// logrus.StandardLogger.
ClientLogger *logrus.Logger
// ConmonServerPath is the binary path to the conmon server.
ConmonServerPath string
// LogLevel of the server to be used.
// Can be "trace", "debug", "info", "warn", "error" or "off".
LogLevel LogLevel
// LogDriver is the possible server logging driver.
// Can be "stdout" or "systemd".
LogDriver LogDriver
// Runtime is the binary path of the OCI runtime to use to operate on the
// containers.
Runtime string
// RuntimeRoot is the root directory used by the OCI runtime to operate on
// containers.
RuntimeRoot string
// ServerRunDir is the path of the directory for the server to hold files
// at runtime.
ServerRunDir string
// Stdout is the standard output stream of the server when the log driver
// "stdout" is being used (can be nil).
Stdout io.WriteCloser
// Stderr is the standard error stream of the server when the log driver
// "stdout" is being used (can be nil).
Stderr io.WriteCloser
// CgroupManager can be use to select the cgroup manager.
CgroupManager CgroupManager
// Tracing can be used to enable OpenTelemetry tracing.
Tracing *Tracing
}
// Tracing is the structure for managing server-side OpenTelemetry tracing.
type Tracing struct {
// Enabled tells the server to run with OpenTelemetry tracing.
Enabled bool
// Endpoint is the GRPC tracing endpoint for OLTP.
// Defaults to "http://localhost:4317"
Endpoint string
// Tracer allows the client to create additional spans if set.
Tracer trace.Tracer
}
// NewConmonServerConfig creates a new ConmonServerConfig instance for the
// required arguments. Optional arguments are pointing to their corresponding
// default values.
func NewConmonServerConfig(
runtime, runtimeRoot, serverRunDir string,
) *ConmonServerConfig {
return &ConmonServerConfig{
LogLevel: LogLevelDebug,
LogDriver: LogDriverSystemd,
Runtime: runtime,
RuntimeRoot: runtimeRoot,
ServerRunDir: serverRunDir,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
}
// FromLogrusLevel converts the logrus.Level to a conmon-rs server log level.
func FromLogrusLevel(level logrus.Level) LogLevel {
switch level {
case logrus.PanicLevel, logrus.FatalLevel:
return LogLevelOff
case logrus.ErrorLevel:
return LogLevelError
case logrus.WarnLevel:
return LogLevelWarn
case logrus.InfoLevel:
return LogLevelInfo
case logrus.DebugLevel:
return LogLevelDebug
case logrus.TraceLevel:
return LogLevelTrace
}
return LogLevelDebug
}
// New creates a new conmon server, starts it and connects a new client to it.
//
// If a server is already started with the same `ServerRunDir` specified
// the client connects to the existing server instead.
// Note: Other options from the `ConmonServerConfig` will be ignored
// and the settings of the existing server will remain unchanged.
func New(config *ConmonServerConfig) (client *ConmonClient, retErr error) {
cl, err := config.toClient()
if err != nil {
return nil, fmt.Errorf("convert config to client: %w", err)
}
// Check if the process has already started, and inherit that process instead.
ctx, cancel := defaultContext()
defer cancel()
ctx, span := cl.startSpan(ctx, "New")
if span != nil {
defer span.End()
}
if resp, err := cl.Version(ctx, &VersionConfig{}); err == nil {
cl.serverPID = resp.ProcessID
return cl, nil
}
if err := cl.startServer(config); err != nil {
return nil, fmt.Errorf("start server: %w", err)
}
pid, err := pidGivenFile(cl.pidFile())
if err != nil {
return nil, fmt.Errorf("get pid from env: %w", err)
}
cl.serverPID = pid
// Cleanup the background server process
// if we fail any of the next steps
defer func() {
if retErr != nil {
if err := cl.Shutdown(); err != nil {
cl.logger.Errorf("Unable to shutdown server: %v", err)
}
}
}()
if err := cl.waitUntilServerUp(); err != nil {
return nil, fmt.Errorf("wait until server is up: %w", err)
}
if err := os.Remove(cl.pidFile()); err != nil {
return nil, fmt.Errorf("remove pid file: %w", err)
}
return cl, nil
}
func (c *ConmonServerConfig) toClient() (*ConmonClient, error) {
const perm = 0o755
if err := os.MkdirAll(c.ServerRunDir, perm); err != nil && !os.IsExist(err) {
return nil, fmt.Errorf("%s: %w", c.ServerRunDir, errRunDirNotCreated)
}
if c.ClientLogger == nil {
c.ClientLogger = logrus.StandardLogger()
}
var tracer trace.Tracer
if c.Tracing != nil && c.Tracing.Tracer != nil {
tracer = c.Tracing.Tracer
}
return &ConmonClient{
runDir: c.ServerRunDir,
logger: c.ClientLogger,
attachReaders: &sync.Map{},
tracer: tracer,
cgroupManager: c.CgroupManager,
}, nil
}
//nolint:ireturn,nolintlint // Returning the interface is intentional
func (c *ConmonClient) startSpan(ctx context.Context, name string) (context.Context, trace.Span) {
if c.tracer == nil {
return ctx, nil
}
const prefix = "conmonrs-client: "
return c.tracer.Start(ctx, prefix+name, trace.WithSpanKind(trace.SpanKindClient))
}
func (c *ConmonClient) startServer(config *ConmonServerConfig) error {
_, span := c.startSpan(context.TODO(), "startServer")
if span != nil {
defer span.End()
}
entrypoint, args, err := c.toArgs(config)
if err != nil {
return fmt.Errorf("convert config to args: %w", err)
}
cmd := exec.Command(entrypoint, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
if config.LogDriver == LogDriverStdout {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if config.Stdout != nil {
cmd.Stdout = config.Stdout
}
if config.Stderr != nil {
cmd.Stderr = config.Stderr
}
}
if err := cmd.Run(); err != nil {
return fmt.Errorf("run server command: %w", err)
}
return nil
}
func (c *ConmonClient) toArgs(config *ConmonServerConfig) (entrypoint string, args []string, err error) {
if c == nil {
return "", args, nil
}
entrypoint = config.ConmonServerPath
if entrypoint == "" {
path, err := exec.LookPath(binaryName)
if err != nil {
return "", args, fmt.Errorf("finding path: %w", err)
}
entrypoint = path
}
if config.Runtime == "" {
return "", args, errRuntimeUnspecified
}
args = append(args, "--runtime", config.Runtime)
if config.ServerRunDir == "" {
return "", args, errRunDirUnspecified
}
args = append(args, "--runtime-dir", config.ServerRunDir)
if config.RuntimeRoot != "" {
args = append(args, "--runtime-root", config.RuntimeRoot)
}
if config.LogLevel != "" {
if err := validateLogLevel(config.LogLevel); err != nil {
return "", args, fmt.Errorf("validate log level: %w", err)
}
args = append(args, "--log-level", string(config.LogLevel))
}
if config.LogDriver != "" {
if err := validateLogDriver(config.LogDriver); err != nil {
return "", args, fmt.Errorf("validate log driver: %w", err)
}
args = append(args, "--log-driver", string(config.LogDriver))
}
const cgroupManagerFlag = "--cgroup-manager"
switch config.CgroupManager {
case CgroupManagerSystemd:
args = append(args, cgroupManagerFlag, "systemd")
case CgroupManagerCgroupfs:
args = append(args, cgroupManagerFlag, "cgroupfs")
case CgroupManagerPerCommand:
// nothing to do, will use the cgroup manager specified per command
default:
return "", args, errUndefinedCgroupManager
}
if config.Tracing != nil && config.Tracing.Enabled {
c.tracingEnabled = true
args = append(args, "--enable-tracing")
if config.Tracing.Endpoint != "" {
args = append(args, "--tracing-endpoint", config.Tracing.Endpoint)
}
}
return entrypoint, args, nil
}
func validateLogLevel(level LogLevel) error {
return validateStringSlice(
"log level",
string(level),
string(LogLevelTrace),
string(LogLevelDebug),
string(LogLevelInfo),
string(LogLevelWarn),
string(LogLevelError),
string(LogLevelOff),
)
}
func validateLogDriver(driver LogDriver) error {
return validateStringSlice(
"log driver",
string(driver),
string(LogDriverStdout),
string(LogDriverSystemd),
)
}
func validateStringSlice(typ, given string, possibleValues ...string) error {
for _, possibleValue := range possibleValues {
if given == possibleValue {
return nil
}
}
return fmt.Errorf("%w: %s %q", errInvalidValue, typ, given)
}
func pidGivenFile(file string) (uint32, error) {
pidBytes, err := os.ReadFile(file)
if err != nil {
return 0, fmt.Errorf("reading pid bytes: %w", err)
}
const (
base = 10
bitSize = 32
)
pidU64, err := strconv.ParseUint(string(pidBytes), base, bitSize)
if err != nil {
return 0, fmt.Errorf("parsing pid: %w", err)
}
return uint32(pidU64), nil
}
func (c *ConmonClient) waitUntilServerUp() (err error) {
_, span := c.startSpan(context.TODO(), "waitUntilServerUp")
if span != nil {
defer span.End()
}
for i := 0; i < 100; i++ {
ctx, cancel := defaultContext()
_, err = c.Version(ctx, &VersionConfig{})
if err == nil {
cancel()
break
}
cancel()
time.Sleep(1 * time.Millisecond)
}
return err
}
func defaultContext() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), defaultTimeout)
}
func (c *ConmonClient) newRPCConn() (*rpc.Conn, error) {
socketConn, err := DialLongSocket("unix", c.socket())
if err != nil {
return nil, fmt.Errorf("dial long socket: %w", err)
}
return rpc.NewConn(rpc.NewStreamTransport(socketConn), nil), nil
}
// DialLongSocket is a wrapper around net.DialUnix.
// Its purpose is to allow for an arbitrarily long socket.
// It does so by opening the parent directory of path, and using the
// `/proc/self/fd` entry of that parent (which is a symlink to the actual parent)
// to construct the path to the socket.
// It assumes a valid path, as well as a file name that doesn't exceed the unix max socket length.
func DialLongSocket(network, path string) (*net.UnixConn, error) {
parent := filepath.Dir(path)
f, err := os.Open(parent)
if err != nil {
return nil, fmt.Errorf("open socket parent: %w", err)
}
defer f.Close()
socketName := filepath.Base(path)
const procSelfFDPath = "/proc/self/fd"
socketPath := filepath.Join(procSelfFDPath, strconv.Itoa(int(f.Fd())), socketName)
conn, err := net.DialUnix(network, nil, &net.UnixAddr{
Name: socketPath, Net: network,
})
if err != nil {
return nil, fmt.Errorf("dial unix socket: %w", err)
}
return conn, nil
}
// VersionConfig is the configuration for calling the Version method.
type VersionConfig struct {
// Verbose specifies verbose version output.
Verbose bool
}
// VersionResponse is the response of the Version method.
type VersionResponse struct {
// ProcessID is the PID of the server.
ProcessID uint32
// Version is the actual version string of the server.
Version string
// Tag is the git tag of the server, empty if no tag is available.
Tag string
// Commit is git commit SHA of the build.
Commit string
// BuildDate is the date of build.
BuildDate string
// Target is the build triple.
Target string
// RustVersion is the used Rust version.
RustVersion string
// CargoVersion is the used Cargo version.
CargoVersion string
// CargoTree is the used dependency tree.
// Only set if request was in verbose mode.
CargoTree string
}
// Version can be used to retrieve all available version information.
func (c *ConmonClient) Version(
ctx context.Context, cfg *VersionConfig,
) (*VersionResponse, error) {
ctx, span := c.startSpan(ctx, "Version")
if span != nil {
defer span.End()
}
conn, err := c.newRPCConn()
if err != nil {
return nil, fmt.Errorf("create RPC connection: %w", err)
}
defer conn.Close()
client := proto.Conmon(conn.Bootstrap(ctx))
future, free := client.Version(ctx, func(p proto.Conmon_version_Params) error {
req, err := p.NewRequest()
if err != nil {
return fmt.Errorf("create request: %w", err)
}
if err := c.setMetadata(ctx, req); err != nil {
return err
}
verbose := false
if cfg != nil {
verbose = cfg.Verbose
}
req.SetVerbose(verbose)
return nil
})
defer free()
result, err := future.Struct()
if err != nil {
return nil, fmt.Errorf("create result: %w", err)
}
response, err := result.Response()
if err != nil {
return nil, fmt.Errorf("set response: %w", err)
}
version, err := response.Version()
if err != nil {
return nil, fmt.Errorf("set version: %w", err)
}
semverVersion, err := semver.Parse(version)
if err != nil {
return nil, fmt.Errorf("parse server version to semver: %w", err)
}
c.serverVersion = semverVersion
tag, err := response.Tag()
if err != nil {
return nil, fmt.Errorf("set tag: %w", err)
}
commit, err := response.Commit()
if err != nil {
return nil, fmt.Errorf("set commit: %w", err)
}
buildDate, err := response.BuildDate()
if err != nil {
return nil, fmt.Errorf("set build date: %w", err)
}
target, err := response.Target()
if err != nil {
return nil, fmt.Errorf("set target: %w", err)
}
rustVersion, err := response.RustVersion()
if err != nil {
return nil, fmt.Errorf("set rust version: %w", err)
}
cargoVersion, err := response.CargoVersion()
if err != nil {
return nil, fmt.Errorf("set cargo version: %w", err)
}
cargoTree, err := response.CargoTree()
if err != nil {
return nil, fmt.Errorf("set cargo version: %w", err)
}
return &VersionResponse{
ProcessID: response.ProcessId(),
Version: version,
Tag: tag,
Commit: commit,
BuildDate: buildDate,
Target: target,
RustVersion: rustVersion,
CargoVersion: cargoVersion,
CargoTree: cargoTree,
}, nil
}
func (c *ConmonClient) setCgroupManager(
cfg CgroupManager,
req interface {
SetCgroupManager(proto.Conmon_CgroupManager)
},
) {
cgroupManager := c.cgroupManager
if cgroupManager == CgroupManagerPerCommand {
cgroupManager = cfg
} else if cfg != CgroupManager(0) {
c.logger.Warnf(
"cgroup manager specified in per command config, but global cgroup manager is not set to CgroupManagerPerCommand",
)
}
req.SetCgroupManager(proto.Conmon_CgroupManager(cgroupManager))
}
// CreateContainerConfig is the configuration for calling the CreateContainer
// method.
type CreateContainerConfig struct {
// ID is the container identifier.
ID string
// BundlePath is the path to the filesystem bundle.
BundlePath string
// Terminal indicates if a tty should be used or not.
Terminal bool
// Stdin indicates if stdin should be available or not.
Stdin bool
// ExitPaths is a slice of paths to write the exit statuses.
ExitPaths []string
// OOMExitPaths is a slice of files that should be created if the given container is OOM killed.
OOMExitPaths []string
// LogDrivers is a slice of selected log drivers.
LogDrivers []ContainerLogDriver
// CleanupCmd is the command that will be executed once the container exits
CleanupCmd []string
// GlobalArgs are the additional arguments passed to the create runtime call
// before the command. e.g: crun --runtime-arg create
GlobalArgs []string
// CommandArgs are the additional arguments passed to the create runtime call
// after the command. e.g: crun create --runtime-opt
CommandArgs []string
// EnvVars are the environment variables passed to the create runtime call.
EnvVars map[string]string
// CgroupManager can be use to select the cgroup manager.
//
// To use this option set `ConmonServerConfig.CgroupManager` to `CgroupManagerPerCommand`.
CgroupManager CgroupManager
// AdditionalFDs can be used to pass additional file descriptors to the container.
AdditionalFDs []RemoteFD
// LeakFDs can be used to keep file descriptors open as long as the container is running.
LeakFDs []RemoteFD
}
// ContainerLogDriver specifies a selected logging mechanism.
type ContainerLogDriver struct {
// Type defines the log driver variant.
Type LogDriverType
// Path specifies the filesystem path of the log driver.
Path string
// MaxSize is the maximum amount of bytes to be written before rotation.
// 0 translates to an unlimited size.
MaxSize uint64
}
// LogDriverType specifies available log drivers.
type LogDriverType int
const (
// LogDriverTypeContainerRuntimeInterface is the Kubernetes CRI logger
// type.
LogDriverTypeContainerRuntimeInterface LogDriverType = iota
)
// CreateContainerResponse is the response of the CreateContainer method.
type CreateContainerResponse struct {
// PID is the container process identifier.
PID uint32
// NamespacesPath is the base path where the namespaces are mounted.
NamespacesPath string
}
// CreateContainer can be used to create a new running container instance.
func (c *ConmonClient) CreateContainer(
ctx context.Context, cfg *CreateContainerConfig,
) (*CreateContainerResponse, error) {
ctx, span := c.startSpan(ctx, "CreateContainer")
if span != nil {
defer span.End()
}
conn, err := c.newRPCConn()
if err != nil {
return nil, fmt.Errorf("create RPC connection: %w", err)
}
defer conn.Close()
client := proto.Conmon(conn.Bootstrap(ctx))
future, free := client.CreateContainer(ctx, func(p proto.Conmon_createContainer_Params) error {
req, err := p.NewRequest()
if err != nil {
return fmt.Errorf("create request: %w", err)
}
if err := c.setMetadata(ctx, req); err != nil {
return err
}
if err := req.SetId(cfg.ID); err != nil {
return fmt.Errorf("set ID: %w", err)
}
if err := req.SetBundlePath(cfg.BundlePath); err != nil {
return fmt.Errorf("set bundle path: %w", err)
}
req.SetTerminal(cfg.Terminal)
req.SetStdin(cfg.Stdin)
if err := stringSliceToTextList(cfg.ExitPaths, req.NewExitPaths); err != nil {
return fmt.Errorf("convert exit paths string slice to text list: %w", err)
}
if err := stringSliceToTextList(cfg.OOMExitPaths, req.NewOomExitPaths); err != nil {
return fmt.Errorf("convert oom exit paths string slice to text list: %w", err)
}
if err := c.initLogDrivers(&req, cfg.LogDrivers); err != nil {
return fmt.Errorf("init log drivers: %w", err)
}
if err := stringSliceToTextList(cfg.CleanupCmd, req.NewCleanupCmd); err != nil {
return fmt.Errorf("convert cleanup command string slice to text list: %w", err)
}
if err := stringSliceToTextList(cfg.GlobalArgs, req.NewGlobalArgs); err != nil {
return fmt.Errorf("convert global arguments string slice to text list: %w", err)
}
if err := stringSliceToTextList(cfg.CommandArgs, req.NewCommandArgs); err != nil {
return fmt.Errorf("convert command arguments string slice to text list: %w", err)
}
if err := stringStringMapToMapEntryList(cfg.EnvVars, req.NewEnvVars); err != nil {
return fmt.Errorf("convert environment variables string slice to text list: %w", err)
}
c.setCgroupManager(cfg.CgroupManager, req)
if err := remoteFDSliceToUInt64List(cfg.AdditionalFDs, req.NewAdditionalFds); err != nil {
return fmt.Errorf("convert file descriptor slice to slot list: %w", err)
}
if err := remoteFDSliceToUInt64List(cfg.LeakFDs, req.NewLeakFds); err != nil {
return fmt.Errorf("convert file descriptor slice to slot list: %w", err)
}
return nil
})
defer free()
result, err := future.Struct()
if err != nil {
return nil, fmt.Errorf("create result: %w", err)
}
response, err := result.Response()
if err != nil {
return nil, fmt.Errorf("set response: %w", err)
}
return &CreateContainerResponse{
PID: response.ContainerPid(),
}, nil
}
// ExecSyncConfig is the configuration for calling the ExecSyncContainer
// method.
type ExecSyncConfig struct {
// ID is the container identifier.
ID string
// Command is a slice of command line arguments.
Command []string
// Timeout is the maximum time the command can run in seconds.
Timeout uint64
// Terminal specifies if a tty should be used.
Terminal bool
// EnvVars are the environment variables passed to the exec runtime call.
EnvVars map[string]string
// CgroupManager can be use to select the cgroup manager.
//
// To use this option set `ConmonServerConfig.CgroupManager` to `CgroupManagerPerCommand`.
CgroupManager CgroupManager
}
// ExecContainerResult is the result for calling the ExecSyncContainer method.
type ExecContainerResult struct {
// ExitCode specifies the returned exit status.
ExitCode int32
// Stdout contains the stdout stream result.
Stdout []byte
// Stderr contains the stderr stream result.
Stderr []byte
// TimedOut is true if the command timed out.
TimedOut bool
}
// ExecSyncContainer can be used to execute a command within a running
// container.
func (c *ConmonClient) ExecSyncContainer(ctx context.Context, cfg *ExecSyncConfig) (*ExecContainerResult, error) {
ctx, span := c.startSpan(ctx, "ExecSyncContainer")
if span != nil {
defer span.End()
}
conn, err := c.newRPCConn()
if err != nil {
return nil, fmt.Errorf("create RPC connection: %w", err)
}
defer conn.Close()
client := proto.Conmon(conn.Bootstrap(ctx))
future, free := client.ExecSyncContainer(ctx, func(p proto.Conmon_execSyncContainer_Params) error {
req, err := p.NewRequest()
if err != nil {
return fmt.Errorf("create request: %w", err)
}
if err := c.setMetadata(ctx, req); err != nil {
return err
}
if err := req.SetId(cfg.ID); err != nil {
return fmt.Errorf("set ID: %w", err)
}
req.SetTimeoutSec(cfg.Timeout)
if err := stringSliceToTextList(cfg.Command, req.NewCommand); err != nil {
return err
}
req.SetTerminal(cfg.Terminal)
if err := stringStringMapToMapEntryList(cfg.EnvVars, req.NewEnvVars); err != nil {
return err
}
c.setCgroupManager(cfg.CgroupManager, req)
return nil
})
defer free()
result, err := future.Struct()
if err != nil {
return nil, fmt.Errorf("create result: %w", err)
}
resp, err := result.Response()
if err != nil {
return nil, fmt.Errorf("set response: %w", err)
}
stdout, err := resp.Stdout()
if err != nil {
return nil, fmt.Errorf("get stdout: %w", err)
}
stderr, err := resp.Stderr()
if err != nil {
return nil, fmt.Errorf("get stderr: %w", err)
}
execContainerResult := &ExecContainerResult{
ExitCode: resp.ExitCode(),
Stdout: stdout,
Stderr: stderr,
TimedOut: resp.TimedOut(),
}
return execContainerResult, nil
}
func (c *ConmonClient) initLogDrivers(req *proto.Conmon_CreateContainerRequest, logDrivers []ContainerLogDriver) error {
newLogDrivers, err := req.NewLogDrivers(int32(len(logDrivers)))
if err != nil {
return fmt.Errorf("create log drivers: %w", err)
}
for i, logDriver := range logDrivers {
n := newLogDrivers.At(i)
if logDriver.Type == LogDriverTypeContainerRuntimeInterface {
n.SetType(proto.Conmon_LogDriver_Type_containerRuntimeInterface)
}
if err := n.SetPath(logDriver.Path); err != nil {
return fmt.Errorf("set log driver path: %w", err)
}
n.SetMaxSize(logDriver.MaxSize)
}
return nil
}
// PID returns the server process ID.
func (c *ConmonClient) PID() uint32 {
return c.serverPID
}
// Shutdown kill the server via SIGINT. Waits up to 10 seconds for the server
// PID to be removed from the system.
func (c *ConmonClient) Shutdown() error {
_, span := c.startSpan(context.TODO(), "Shutdown")
if span != nil {
defer span.End()
}
c.attachReaders.Range(func(_, in any) bool {
c.closeAttachReader(in)
return true
})
pid := int(c.serverPID)
if err := syscall.Kill(pid, syscall.SIGINT); err != nil {
// Process does not exist any more, it might be manually killed.
if errors.Is(err, syscall.ESRCH) {
return nil
}
return fmt.Errorf("kill server PID: %w", err)
}
const (
waitInterval = 100 * time.Millisecond
waitCount = 100
)
for i := 0; i < waitCount; i++ {
if err := syscall.Kill(pid, 0); errors.Is(err, syscall.ESRCH) {
return nil
}
time.Sleep(waitInterval)
}
return errTimeoutWaitForPid
}
func (c *ConmonClient) pidFile() string {
return filepath.Join(c.runDir, pidFileName)
}
func (c *ConmonClient) socket() string {
return filepath.Join(c.runDir, socketName)
}
// ReopenLogContainerConfig is the configuration for calling the
// ReopenLogContainer method.
type ReopenLogContainerConfig struct {
// ID is the container identifier.
ID string
}
// ReopenLogContainer can be used to rotate all configured container log
// drivers.
func (c *ConmonClient) ReopenLogContainer(ctx context.Context, cfg *ReopenLogContainerConfig) error {
ctx, span := c.startSpan(ctx, "ReopenLogContainer")
if span != nil {
defer span.End()
}
conn, err := c.newRPCConn()
if err != nil {
return fmt.Errorf("create RPC connection: %w", err)
}
defer conn.Close()
client := proto.Conmon(conn.Bootstrap(ctx))
future, free := client.ReopenLogContainer(ctx, func(p proto.Conmon_reopenLogContainer_Params) error {
req, err := p.NewRequest()
if err != nil {
return fmt.Errorf("create request: %w", err)
}
if err := c.setMetadata(ctx, req); err != nil {
return err
}
if err := req.SetId(cfg.ID); err != nil {
return fmt.Errorf("set ID: %w", err)