github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/edge/test/test.go (about) 1 package test 2 3 import ( 4 "encoding/json" 5 "io" 6 "io/ioutil" 7 "net/http" 8 "sync" 9 "time" 10 11 "k8s.io/api/core/v1" 12 "k8s.io/klog" 13 14 "github.com/kubeedge/beehive/pkg/core" 15 beehiveContext "github.com/kubeedge/beehive/pkg/core/context" 16 "github.com/kubeedge/beehive/pkg/core/model" 17 "github.com/kubeedge/kubeedge/edge/pkg/common/message" 18 "github.com/kubeedge/kubeedge/edge/pkg/common/modules" 19 "github.com/kubeedge/kubeedge/pkg/apis/componentconfig/edgecore/v1alpha1" 20 ) 21 22 const ( 23 name = "testManager" 24 edgedEndPoint = "http://127.0.0.1:10255" 25 EdgedPodHandler = "/pods" 26 ) 27 28 // TODO move this files into /edge/pkg/dbtest @kadisi 29 func Register(t *v1alpha1.DBTest) { 30 core.Register(&testManager{enable: t.Enable}) 31 } 32 33 type testManager struct { 34 moduleWait *sync.WaitGroup 35 enable bool 36 } 37 38 func (tm *testManager) Name() string { 39 return name 40 } 41 42 func (tm *testManager) Group() string { 43 //return core.MetaGroup 44 return modules.MetaGroup 45 } 46 47 func (tm *testManager) Enable() bool { 48 return tm.enable 49 } 50 51 //Function to get the pods from Edged 52 func GetPodListFromEdged(w http.ResponseWriter) error { 53 var pods v1.PodList 54 var bytes io.Reader 55 client := &http.Client{} 56 t := time.Now() 57 req, err := http.NewRequest(http.MethodGet, edgedEndPoint+EdgedPodHandler, bytes) 58 req.Header.Set("Content-Type", "application/json; charset=utf-8") 59 if err != nil { 60 klog.Errorf("Frame HTTP request failed: %v", err) 61 return err 62 } 63 resp, err := client.Do(req) 64 if err != nil { 65 klog.Errorf("Sending HTTP request failed: %v", err) 66 return err 67 } 68 klog.Infof("%s %s %v in %v", req.Method, req.URL, resp.Status, time.Now().Sub(t)) 69 defer resp.Body.Close() 70 contents, err := ioutil.ReadAll(resp.Body) 71 if err != nil { 72 klog.Errorf("HTTP Response reading has failed: %v", err) 73 return err 74 } 75 err = json.Unmarshal(contents, &pods) 76 if err != nil { 77 klog.Errorf("Json Unmarshal has failed: %v", err) 78 return err 79 } 80 respBody, err := json.Marshal(pods) 81 if err != nil { 82 klog.Errorf("Json Marshal failed: %v", err) 83 return err 84 } 85 w.Header().Set("Content-Type", "application/json") 86 w.Write(respBody) 87 88 return nil 89 } 90 91 //Function to handle Get/Add/Delete deployment list. 92 func (tm *testManager) podHandler(w http.ResponseWriter, req *http.Request) { 93 var operation string 94 var p v1.Pod 95 if req.Method == http.MethodGet { 96 err := GetPodListFromEdged(w) 97 if err != nil { 98 klog.Errorf("Get podlist from Edged has failed: %v", err) 99 } 100 } else if req.Body != nil { 101 body, err := ioutil.ReadAll(req.Body) 102 if err != nil { 103 klog.Errorf("read body error %v", err) 104 w.Write([]byte("read request body error")) 105 } 106 klog.Infof("request body is %s\n", string(body)) 107 if err = json.Unmarshal(body, &p); err != nil { 108 klog.Errorf("unmarshal request body error %v", err) 109 w.Write([]byte("unmarshal request body error")) 110 } 111 112 switch req.Method { 113 case "POST": 114 operation = model.InsertOperation 115 case "DELETE": 116 operation = model.DeleteOperation 117 case "PUT": 118 operation = model.UpdateOperation 119 } 120 121 ns := v1.NamespaceDefault 122 if p.Namespace != "" { 123 ns = p.Namespace 124 } 125 msgReq := message.BuildMsg("resource", string(p.UID), "edgecontroller", ns+"/pod/"+string(p.Name), operation, p) 126 beehiveContext.Send("metaManager", *msgReq) 127 klog.Infof("send message to metaManager is %+v\n", msgReq) 128 } 129 } 130 131 //Function to handle device addition and removal from the edgenode 132 func (tm *testManager) deviceHandler(w http.ResponseWriter, req *http.Request) { 133 var operation string 134 var Content interface{} 135 136 if req.Body != nil { 137 body, err := ioutil.ReadAll(req.Body) 138 if err != nil { 139 klog.Errorf("read body error %v", err) 140 w.Write([]byte("read request body error")) 141 } 142 klog.Infof("request body is %s\n", string(body)) 143 err = json.Unmarshal(body, &Content) 144 if err != nil { 145 klog.Errorf("unmarshal request body error %v", err) 146 w.Write([]byte("unmarshal request body error")) 147 } 148 switch req.Method { 149 case "POST": 150 operation = model.InsertOperation 151 case "DELETE": 152 operation = model.DeleteOperation 153 case "PUT": 154 operation = model.UpdateOperation 155 } 156 msgReq := message.BuildMsg("edgehub", "", "edgemgr", "membership", operation, Content) 157 beehiveContext.Send("twin", *msgReq) 158 klog.Infof("send message to twingrp is %+v\n", msgReq) 159 } 160 } 161 162 func (tm *testManager) secretHandler(w http.ResponseWriter, req *http.Request) { 163 var operation string 164 var p v1.Secret 165 if req.Body != nil { 166 body, err := ioutil.ReadAll(req.Body) 167 if err != nil { 168 klog.Errorf("read body error %v", err) 169 w.Write([]byte("read request body error")) 170 } 171 klog.Infof("request body is %s\n", string(body)) 172 if err = json.Unmarshal(body, &p); err != nil { 173 klog.Errorf("unmarshal request body error %v", err) 174 w.Write([]byte("unmarshal request body error")) 175 } 176 177 switch req.Method { 178 case "POST": 179 operation = model.InsertOperation 180 case "DELETE": 181 operation = model.DeleteOperation 182 case "PUT": 183 operation = model.UpdateOperation 184 } 185 186 msgReq := message.BuildMsg("edgehub", string(p.UID), "test", "fakeNamespace/secret/"+string(p.UID), operation, p) 187 beehiveContext.Send("metaManager", *msgReq) 188 klog.Infof("send message to metaManager is %+v\n", msgReq) 189 } 190 } 191 192 func (tm *testManager) configmapHandler(w http.ResponseWriter, req *http.Request) { 193 var operation string 194 var p v1.ConfigMap 195 if req.Body != nil { 196 body, err := ioutil.ReadAll(req.Body) 197 if err != nil { 198 klog.Errorf("read body error %v", err) 199 w.Write([]byte("read request body error")) 200 } 201 klog.Infof("request body is %s\n", string(body)) 202 if err = json.Unmarshal(body, &p); err != nil { 203 klog.Errorf("unmarshal request body error %v", err) 204 w.Write([]byte("unmarshal request body error")) 205 } 206 207 switch req.Method { 208 case "POST": 209 operation = model.InsertOperation 210 case "DELETE": 211 operation = model.DeleteOperation 212 case "PUT": 213 operation = model.UpdateOperation 214 } 215 216 msgReq := message.BuildMsg("edgehub", string(p.UID), "test", "fakeNamespace/configmap/"+string(p.UID), operation, p) 217 beehiveContext.Send("metaManager", *msgReq) 218 klog.Infof("send message to metaManager is %+v\n", msgReq) 219 } 220 } 221 222 func (tm *testManager) Start() { 223 224 http.HandleFunc("/pods", tm.podHandler) 225 http.HandleFunc("/configmap", tm.configmapHandler) 226 http.HandleFunc("/secret", tm.secretHandler) 227 http.HandleFunc("/devices", tm.deviceHandler) 228 err := http.ListenAndServe(":12345", nil) 229 if err != nil { 230 klog.Errorf("ListenAndServe: %v", err) 231 } 232 }