summaryrefslogtreecommitdiff
path: root/edge/test/test.go
blob: ac9bf6ce8b160d5f1938a2b4204fe3e8df4df483 (about) (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package test

import (
	"encoding/json"
	"io"
	"io/ioutil"
	"net/http"
	"time"

	"k8s.io/api/core/v1"
	"k8s.io/klog/v2"

	"github.com/kubeedge/beehive/pkg/core"
	beehiveContext "github.com/kubeedge/beehive/pkg/core/context"
	"github.com/kubeedge/beehive/pkg/core/model"
	"github.com/kubeedge/kubeedge/edge/pkg/common/message"
	"github.com/kubeedge/kubeedge/edge/pkg/common/modules"
	"github.com/kubeedge/kubeedge/pkg/apis/componentconfig/edgecore/v1alpha1"
)

const (
	name            = "testManager"
	edgedEndPoint   = "http://127.0.0.1:10255"
	EdgedPodHandler = "/pods"
)

// TODO move this files into /edge/pkg/dbtest @kadisi
func Register(t *v1alpha1.DBTest) {
	core.Register(&testManager{enable: t.Enable})
}

type testManager struct {
	enable bool
}

func (tm *testManager) Name() string {
	return name
}

func (tm *testManager) Group() string {
	//return core.MetaGroup
	return modules.MetaGroup
}

func (tm *testManager) Enable() bool {
	return tm.enable
}

//Function to get the pods from Edged
func GetPodListFromEdged(w http.ResponseWriter) error {
	var pods v1.PodList
	var bytes io.Reader
	client := &http.Client{}
	t := time.Now()
	req, err := http.NewRequest(http.MethodGet, edgedEndPoint+EdgedPodHandler, bytes)
	req.Header.Set("Content-Type", "application/json; charset=utf-8")
	if err != nil {
		klog.Errorf("Frame HTTP request failed: %v", err)
		return err
	}
	resp, err := client.Do(req)
	if err != nil {
		klog.Errorf("Sending HTTP request failed: %v", err)
		return err
	}
	klog.Infof("%s %s %v in %v", req.Method, req.URL, resp.Status, time.Since(t))
	defer resp.Body.Close()
	contents, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		klog.Errorf("HTTP Response reading has failed: %v", err)
		return err
	}
	err = json.Unmarshal(contents, &pods)
	if err != nil {
		klog.Errorf("Json Unmarshal has failed: %v", err)
		return err
	}
	respBody, err := json.Marshal(pods)
	if err != nil {
		klog.Errorf("Json Marshal failed: %v", err)
		return err
	}
	w.Header().Set("Content-Type", "application/json")
	w.Write(respBody)

	return nil
}

//Function to handle Get/Add/Delete deployment list.
func (tm *testManager) podHandler(w http.ResponseWriter, req *http.Request) {
	var operation string
	var p v1.Pod
	if req.Method == http.MethodGet {
		err := GetPodListFromEdged(w)
		if err != nil {
			klog.Errorf("Get podlist from Edged has failed: %v", err)
		}
	} else if req.Body != nil {
		body, err := ioutil.ReadAll(req.Body)
		if err != nil {
			klog.Errorf("read body error %v", err)
			w.Write([]byte("read request body error"))
		}
		klog.Infof("request body is %s\n", string(body))
		if err = json.Unmarshal(body, &p); err != nil {
			klog.Errorf("unmarshal request body error %v", err)
			w.Write([]byte("unmarshal request body error"))
		}

		switch req.Method {
		case http.MethodPost:
			operation = model.InsertOperation
		case http.MethodDelete:
			operation = model.DeleteOperation
		case http.MethodPut:
			operation = model.UpdateOperation
		}

		ns := v1.NamespaceDefault
		if p.Namespace != "" {
			ns = p.Namespace
		}
		msgReq := message.BuildMsg("resource", string(p.UID), "edgecontroller", ns+"/pod/"+string(p.Name), operation, p)
		beehiveContext.Send("metaManager", *msgReq)
		klog.Infof("send message to metaManager is %+v\n", msgReq)
	}
}

//Function to handle device addition and removal from the edgenode
func (tm *testManager) deviceHandler(w http.ResponseWriter, req *http.Request) {
	var operation string
	var Content interface{}

	if req.Body != nil {
		body, err := ioutil.ReadAll(req.Body)
		if err != nil {
			klog.Errorf("read body error %v", err)
			w.Write([]byte("read request body error"))
		}
		klog.Infof("request body is %s\n", string(body))
		err = json.Unmarshal(body, &Content)
		if err != nil {
			klog.Errorf("unmarshal request body error %v", err)
			w.Write([]byte("unmarshal request body error"))
		}
		switch req.Method {
		case http.MethodPost:
			operation = model.InsertOperation
		case http.MethodDelete:
			operation = model.DeleteOperation
		case http.MethodPut:
			operation = model.UpdateOperation
		}
		msgReq := message.BuildMsg("edgehub", "", "edgemgr", "membership", operation, Content)
		beehiveContext.Send("twin", *msgReq)
		klog.Infof("send message to twingrp is %+v\n", msgReq)
	}
}

func (tm *testManager) secretHandler(w http.ResponseWriter, req *http.Request) {
	var operation string
	var p v1.Secret
	if req.Body != nil {
		body, err := ioutil.ReadAll(req.Body)
		if err != nil {
			klog.Errorf("read body error %v", err)
			w.Write([]byte("read request body error"))
		}
		klog.Infof("request body is %s\n", string(body))
		if err = json.Unmarshal(body, &p); err != nil {
			klog.Errorf("unmarshal request body error %v", err)
			w.Write([]byte("unmarshal request body error"))
		}

		switch req.Method {
		case http.MethodPost:
			operation = model.InsertOperation
		case http.MethodDelete:
			operation = model.DeleteOperation
		case http.MethodPut:
			operation = model.UpdateOperation
		}

		msgReq := message.BuildMsg("edgehub", string(p.UID), "test", "fakeNamespace/secret/"+string(p.UID), operation, p)
		beehiveContext.Send("metaManager", *msgReq)
		klog.Infof("send message to metaManager is %+v\n", msgReq)
	}
}

func (tm *testManager) configmapHandler(w http.ResponseWriter, req *http.Request) {
	var operation string
	var p v1.ConfigMap
	if req.Body != nil {
		body, err := ioutil.ReadAll(req.Body)
		if err != nil {
			klog.Errorf("read body error %v", err)
			w.Write([]byte("read request body error"))
		}
		klog.Infof("request body is %s\n", string(body))
		if err = json.Unmarshal(body, &p); err != nil {
			klog.Errorf("unmarshal request body error %v", err)
			w.Write([]byte("unmarshal request body error"))
		}

		switch req.Method {
		case http.MethodPost:
			operation = model.InsertOperation
		case http.MethodDelete:
			operation = model.DeleteOperation
		case http.MethodPut:
			operation = model.UpdateOperation
		}

		msgReq := message.BuildMsg("edgehub", string(p.UID), "test", "fakeNamespace/configmap/"+string(p.UID), operation, p)
		beehiveContext.Send("metaManager", *msgReq)
		klog.Infof("send message to metaManager is %+v\n", msgReq)
	}
}

func (tm *testManager) Start() {
	http.HandleFunc("/pods", tm.podHandler)
	http.HandleFunc("/configmap", tm.configmapHandler)
	http.HandleFunc("/secret", tm.secretHandler)
	http.HandleFunc("/devices", tm.deviceHandler)
	err := http.ListenAndServe(":12345", nil)
	if err != nil {
		klog.Errorf("ListenAndServe: %v", err)
	}
}