github.com/google/cadvisor@v0.49.1/container/podman/podman.go (about) 1 // Copyright 2021 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package podman 16 17 import ( 18 "context" 19 "encoding/json" 20 "fmt" 21 "io" 22 "net/http" 23 "time" 24 25 dockertypes "github.com/docker/docker/api/types" 26 "github.com/pkg/errors" 27 28 "github.com/google/cadvisor/container/docker" 29 "github.com/google/cadvisor/container/docker/utils" 30 v1 "github.com/google/cadvisor/info/v1" 31 ) 32 33 const ( 34 Namespace = "podman" 35 ) 36 37 var timeout = 10 * time.Second 38 39 func validateResponse(gotError error, response *http.Response) error { 40 var err error 41 switch { 42 case response == nil: 43 err = fmt.Errorf("response not present") 44 case response.StatusCode == http.StatusNotFound: 45 err = fmt.Errorf("item not found") 46 case response.StatusCode == http.StatusNotImplemented: 47 err = fmt.Errorf("query not implemented") 48 default: 49 return gotError 50 } 51 52 if gotError != nil { 53 err = errors.Wrap(gotError, err.Error()) 54 } 55 56 return err 57 } 58 59 func apiGetRequest(url string, item interface{}) error { 60 ctx, cancel := context.WithTimeout(context.Background(), timeout) 61 defer cancel() 62 63 conn, err := client(&ctx) 64 if err != nil { 65 return err 66 } 67 68 req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) 69 if err != nil { 70 return err 71 } 72 73 resp, err := conn.Client.Do(req) 74 err = validateResponse(err, resp) 75 if err != nil { 76 return err 77 } 78 defer resp.Body.Close() 79 80 data, err := io.ReadAll(resp.Body) 81 if err != nil { 82 return err 83 84 } 85 86 err = json.Unmarshal(data, item) 87 if err != nil { 88 return err 89 } 90 91 return ctx.Err() 92 } 93 94 func Images() ([]v1.DockerImage, error) { 95 var summaries []dockertypes.ImageSummary 96 err := apiGetRequest("http://d/v1.0.0/images/json", &summaries) 97 if err != nil { 98 return nil, err 99 } 100 return utils.SummariesToImages(summaries) 101 } 102 103 func Status() (v1.DockerStatus, error) { 104 podmanInfo, err := GetInfo() 105 if err != nil { 106 return v1.DockerStatus{}, err 107 } 108 109 return docker.StatusFromDockerInfo(*podmanInfo) 110 } 111 112 func GetInfo() (*dockertypes.Info, error) { 113 var info dockertypes.Info 114 err := apiGetRequest("http://d/v1.0.0/info", &info) 115 return &info, err 116 } 117 118 func VersionString() (string, error) { 119 var version dockertypes.Version 120 err := apiGetRequest("http://d/v1.0.0/version", &version) 121 if err != nil { 122 return "Unknown", err 123 } 124 125 return version.Version, nil 126 } 127 128 func InspectContainer(id string) (dockertypes.ContainerJSON, error) { 129 var data dockertypes.ContainerJSON 130 err := apiGetRequest(fmt.Sprintf("http://d/v1.0.0/containers/%s/json", id), &data) 131 return data, err 132 }