-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
libpod_client.go
168 lines (143 loc) · 3.66 KB
/
libpod_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
// Copyright 2020 OpenTelemetry 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.
//go:build !windows
// +build !windows
package podmanreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/podmanreceiver"
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"go.uber.org/zap"
)
var (
errNoStatsFound = fmt.Errorf("No stats found")
)
type libpodClient struct {
conn *http.Client
endpoint string
}
func newLibpodClient(logger *zap.Logger, cfg *Config) (PodmanClient, error) {
connection, err := newPodmanConnection(logger, cfg.Endpoint, cfg.SSHKey, cfg.SSHPassphrase)
if err != nil {
return nil, err
}
c := &libpodClient{
conn: connection,
endpoint: fmt.Sprintf("http://d/v%s/libpod", cfg.APIVersion),
}
return c, nil
}
func (c *libpodClient) request(ctx context.Context, path string, params url.Values) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, "GET", c.endpoint+path, nil)
if err != nil {
return nil, err
}
if len(params) > 0 {
req.URL.RawQuery = params.Encode()
}
return c.conn.Do(req)
}
func (c *libpodClient) stats(ctx context.Context, options url.Values) ([]containerStats, error) {
resp, err := c.request(ctx, "/containers/stats", options)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
report := &containerStatsReport{}
err = json.Unmarshal(bytes, report)
if err != nil {
return nil, err
}
if report.Error.Message != "" {
return nil, errors.New(report.Error.Message)
} else if report.Stats == nil {
return nil, errNoStatsFound
}
return report.Stats, nil
}
func (c *libpodClient) list(ctx context.Context, options url.Values) ([]container, error) {
resp, err := c.request(ctx, "/containers/json", options)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var report []container
err = json.Unmarshal(bytes, &report)
if err != nil {
return nil, err
}
return report, nil
}
func (c *libpodClient) ping(ctx context.Context) error {
resp, err := c.request(ctx, "/_ping", nil)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("ping response was %d", resp.StatusCode)
}
return nil
}
// events returns a stream of events. It's up to the caller to close the stream by canceling the context.
func (c *libpodClient) events(ctx context.Context, options url.Values) (<-chan event, <-chan error) {
events := make(chan event)
errs := make(chan error, 1)
started := make(chan struct{})
go func() {
defer close(errs)
resp, err := c.request(ctx, "/events", options)
if err != nil {
close(started)
errs <- err
return
}
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
close(started)
for {
var e event
select {
case <-ctx.Done():
errs <- ctx.Err()
return
default:
err := dec.Decode(&e)
if err != nil {
errs <- err
return
}
select {
case events <- e:
case <-ctx.Done():
errs <- ctx.Err()
return
}
}
}
}()
<-started
return events, errs
}