forked from google/cadvisor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpodman.go
163 lines (134 loc) · 3.65 KB
/
podman.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
// Copyright 2021 Google Inc. All Rights Reserved.
//
// 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 podman
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
dockertypes "github.com/docker/docker/api/types"
dockerimage "github.com/docker/docker/api/types/image"
dockersystem "github.com/docker/docker/api/types/system"
"github.com/pkg/errors"
"github.com/google/cadvisor/container/docker"
"github.com/google/cadvisor/container/docker/utils"
v1 "github.com/google/cadvisor/info/v1"
)
const (
Namespace = "podman"
)
var timeout = 10 * time.Second
func validateResponse(gotError error, response *http.Response) error {
var err error
switch {
case response == nil:
err = fmt.Errorf("response not present")
case response.StatusCode == http.StatusNotFound:
err = fmt.Errorf("item not found")
case response.StatusCode == http.StatusNotImplemented:
err = fmt.Errorf("query not implemented")
default:
return gotError
}
if gotError != nil {
err = errors.Wrap(gotError, err.Error())
}
return err
}
func apiGetRequest(url string, item interface{}) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
conn, err := client(&ctx)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return err
}
resp, err := conn.Client.Do(req)
err = validateResponse(err, resp)
if err != nil {
return err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(data, item)
if err != nil {
return err
}
return ctx.Err()
}
func Images() ([]v1.DockerImage, error) {
var summaries []dockerimage.Summary
err := apiGetRequest("http://d/v1.0.0/images/json", &summaries)
if err != nil {
return nil, err
}
return utils.SummariesToImages(summaries)
}
func Status() (v1.DockerStatus, error) {
podmanInfo, err := GetInfo()
if err != nil {
return v1.DockerStatus{}, err
}
status, err := docker.StatusFromDockerInfo(*podmanInfo)
if err != nil {
return v1.DockerStatus{}, err
}
podmanVersion, err := VersionString()
if err != nil {
// status.Version will be "Unknown"
return status, err
}
status.Version = podmanVersion
podmanAPIVersion, err := APIVersionString()
if err != nil {
// status.APIVersion will be "Unknown"
return status, err
}
status.APIVersion = podmanAPIVersion
return status, nil
}
func GetInfo() (*dockersystem.Info, error) {
var info dockersystem.Info
err := apiGetRequest("http://d/v1.0.0/info", &info)
return &info, err
}
func VersionString() (string, error) {
var version dockertypes.Version
err := apiGetRequest("http://d/v1.0.0/version", &version)
if err != nil {
return "Unknown", err
}
return version.Version, nil
}
func APIVersionString() (string, error) {
var version dockertypes.Version
err := apiGetRequest("http://d/v1.0.0/version", &version)
if err != nil {
return "Unknown", err
}
return version.APIVersion, nil
}
func InspectContainer(id string) (dockertypes.ContainerJSON, error) {
var data dockertypes.ContainerJSON
err := apiGetRequest(fmt.Sprintf("http://d/v1.0.0/containers/%s/json", id), &data)
return data, err
}