github.com/deemoprobe/k8s-first-commit@v0.0.0-20230430165612-a541f1982be3/pkg/client/container_info.go (about) 1 /* 2 Copyright 2014 Google Inc. All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 package client 17 18 import ( 19 "encoding/json" 20 "fmt" 21 "io/ioutil" 22 "net/http" 23 ) 24 25 type ContainerInfo interface { 26 GetContainerInfo(host, name string) (interface{}, error) 27 } 28 29 type HTTPContainerInfo struct { 30 Client *http.Client 31 Port uint 32 } 33 34 func (c *HTTPContainerInfo) GetContainerInfo(host, name string) (interface{}, error) { 35 request, err := http.NewRequest("GET", fmt.Sprintf("http://%s:%d/containerInfo?container=%s", host, c.Port, name), nil) 36 if err != nil { 37 return nil, err 38 } 39 response, err := c.Client.Do(request) 40 if err != nil { 41 return nil, err 42 } 43 defer response.Body.Close() 44 body, err := ioutil.ReadAll(response.Body) 45 if err != nil { 46 return nil, err 47 } 48 var data interface{} 49 err = json.Unmarshal(body, &data) 50 return data, err 51 } 52 53 // Useful for testing. 54 type FakeContainerInfo struct { 55 data interface{} 56 err error 57 } 58 59 func (c *FakeContainerInfo) GetContainerInfo(host, name string) (interface{}, error) { 60 return c.data, c.err 61 }