github.com/midokura/kubeedge@v1.2.0-mido.0/tests/stubs/example/main.go (about) 1 /* 2 Copyright 2019 The KubeEdge Authors. 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 17 package main 18 19 import ( 20 "bytes" 21 "encoding/json" 22 "flag" 23 "io" 24 "io/ioutil" 25 "net/http" 26 "time" 27 28 "github.com/spf13/pflag" 29 "k8s.io/klog" 30 31 "github.com/kubeedge/kubeedge/tests/stubs/common/constants" 32 "github.com/kubeedge/kubeedge/tests/stubs/common/types" 33 ) 34 35 const ( 36 // Modules 37 ControllerHubURL = "http://127.0.0.1:54321" 38 ) 39 40 func main() { 41 var pod types.FakePod 42 43 klog.InitFlags(nil) 44 pflag.CommandLine.AddGoFlagSet(flag.CommandLine) 45 pflag.Parse() 46 47 pod.Name = "TestPod" 48 pod.Namespace = constants.NamespaceDefault 49 pod.NodeName = "edgenode1" 50 pod.Status = constants.PodPending 51 52 AddPod(pod) 53 ListPods() 54 55 time.Sleep(10 * time.Second) 56 ListPods() 57 58 DeletePod(pod) 59 60 time.Sleep(10 * time.Second) 61 ListPods() 62 } 63 64 // AddPod adds a fake pod 65 func AddPod(pod types.FakePod) { 66 reqBody, err := json.Marshal(pod) 67 if err != nil { 68 klog.Errorf("Unmarshal HTTP Response has failed: %v", err) 69 } 70 71 err, resp := SendHttpRequest(http.MethodPost, 72 ControllerHubURL+constants.PodResource, 73 bytes.NewBuffer(reqBody)) 74 if err != nil { 75 klog.Errorf("Frame HTTP request failed: %v", err) 76 } 77 defer resp.Body.Close() 78 79 contents, err := ioutil.ReadAll(resp.Body) 80 if err != nil { 81 klog.Errorf("HTTP Response reading has failed: %v", err) 82 } 83 84 klog.V(4).Infof("AddPod response: %v", contents) 85 } 86 87 // DeletePod deletes a fake pod 88 func DeletePod(pod types.FakePod) { 89 err, resp := SendHttpRequest(http.MethodDelete, 90 ControllerHubURL+constants.PodResource+ 91 "?name="+pod.Name+"&namespace="+pod.Namespace+"&nodename="+pod.NodeName, 92 nil) 93 if err != nil { 94 klog.Errorf("Frame HTTP request failed: %v", err) 95 } 96 defer resp.Body.Close() 97 98 contents, err := ioutil.ReadAll(resp.Body) 99 if err != nil { 100 klog.Errorf("HTTP Response reading has failed: %v", err) 101 } 102 103 klog.V(4).Infof("DeletePod response: %v", contents) 104 } 105 106 // ListPods lists all pods 107 func ListPods() { 108 err, resp := SendHttpRequest(http.MethodGet, ControllerHubURL+constants.PodResource, nil) 109 if err != nil { 110 klog.Errorf("Frame HTTP request failed: %v", err) 111 } 112 defer resp.Body.Close() 113 114 contents, err := ioutil.ReadAll(resp.Body) 115 if err != nil { 116 klog.Errorf("HTTP Response reading has failed: %v", err) 117 } 118 119 pods := []types.FakePod{} 120 err = json.Unmarshal(contents, &pods) 121 if err != nil { 122 klog.Errorf("Unmarshal message content with error: %s", err) 123 } 124 125 klog.V(4).Infof("ListPods result: %v", pods) 126 } 127 128 // SendHttpRequest launches a http request 129 func SendHttpRequest(method, reqApi string, body io.Reader) (error, *http.Response) { 130 var resp *http.Response 131 client := &http.Client{} 132 req, err := http.NewRequest(method, reqApi, body) 133 if err != nil { 134 klog.Errorf("Frame HTTP request failed: %v", err) 135 return err, resp 136 } 137 req.Header.Set("Content-Type", "application/json") 138 t := time.Now() 139 resp, err = client.Do(req) 140 klog.V(4).Infof("%s %s %v in %v", req.Method, req.URL, resp.Status, time.Now().Sub(t)) 141 if err != nil { 142 klog.Errorf("HTTP request is failed :%v", err) 143 return err, resp 144 } 145 return nil, resp 146 }