summaryrefslogtreecommitdiff
path: root/tests/stubs/cloud/controllerstub/podmanager.go
blob: 33888a0206b58cd7ab8633382b9c8b5c84e43a73 (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
/*
Copyright 2019 The KubeEdge Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controllerstub

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

	"k8s.io/klog"

	"github.com/kubeedge/beehive/pkg/core/model"
	"github.com/kubeedge/kubeedge/tests/stubs/common/constants"
	"github.com/kubeedge/kubeedge/tests/stubs/common/types"
	"github.com/kubeedge/kubeedge/tests/stubs/common/utils"
)

// NewPodManager creates pod manger
func NewPodManager() (*PodManager, error) {
	event := make(chan *model.Message, 1024)
	pm := &PodManager{event: event}
	return pm, nil
}

// PodManager is a manager watch pod change event
type PodManager struct {
	// event
	event chan *model.Message
	// pods map
	pods sync.Map
}

// GetEvent return a channel which receives event
func (pm *PodManager) GetEvent() chan *model.Message {
	return pm.event
}

// AddPod adds pod in cache
func (pm *PodManager) AddPod(k string, v types.FakePod) {
	pm.pods.Store(k, v)
}

// DeletePod deletes pod in cache
func (pm *PodManager) DeletePod(k string) {
	pm.pods.Delete(k)
}

// UpdatePodStatus update pod status in cache
func (pm *PodManager) UpdatePodStatus(k string, s string) {
	v, ok := pm.pods.Load(k)
	if ok {
		pod := v.(types.FakePod)
		// Status becomes running in the first time
		if pod.Status != s && s == constants.PodRunning {
			pod.RunningTime = time.Now().UnixNano()
		}
		pod.Status = s
		pm.pods.Store(k, pod)
	}
}

// GetPod gets pod from cache
func (pm *PodManager) GetPod(key string) types.FakePod {
	v, ok := pm.pods.Load(key)
	if ok {
		return v.(types.FakePod)
	}
	return types.FakePod{}
}

// ListPods lists all pods in cache
func (pm *PodManager) ListPods() []types.FakePod {
	pods := make([]types.FakePod, 0)
	pm.pods.Range(func(k, v interface{}) bool {
		pods = append(pods, v.(types.FakePod))
		return true
	})
	return pods
}

// PodHandlerFunc is used to receive and process message
func (pm *PodManager) PodHandlerFunc(w http.ResponseWriter, req *http.Request) {
	switch req.Method {
	case http.MethodGet:
		// List Pod
		klog.V(4).Infof("Receive list pod request")
		pods := pm.ListPods()
		klog.V(4).Infof("Current pods number: %v", len(pods))
		rspBodyBytes := new(bytes.Buffer)
		json.NewEncoder(rspBodyBytes).Encode(pods)
		w.Write(rspBodyBytes.Bytes())
		klog.V(4).Infof("Finish list pod request")
	case http.MethodPost:
		klog.V(4).Infof("Receive add pod request")
		var p types.FakePod
		// Get request body
		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"))
				return
			}
			klog.V(4).Infof("Request body is %s", 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"))
				return
			}
		}
		// Add Pod
		ns := constants.NamespaceDefault
		if p.Namespace != "" {
			ns = p.Namespace
		}

		// Build Add message
		msg := model.NewMessage("")
		resource, err := utils.BuildResource(p.NodeName, p.Namespace, model.ResourceTypePod, p.Name)
		if err != nil {
			klog.Errorf("Build message resource failed with error: %s", err)
			w.Write([]byte("Build message resource failed with error"))
			return
		}
		msg.Content = p
		msg.BuildRouter(constants.ControllerStub, constants.GroupResource, resource, model.InsertOperation)

		// Add pod in cache
		p.CreateTime = time.Now().UnixNano()
		pm.AddPod(ns+"/"+p.Name, p)

		// Send msg
		pm.event <- msg
		klog.V(4).Infof("Finish add pod request")

	case http.MethodDelete:
		// Delete Pod
		klog.V(4).Infof("Receive delete pod request")
		params := req.URL.Query()
		ns := params.Get("namespace")
		if ns == "" {
			ns = constants.NamespaceDefault
		}
		nodename := params.Get("nodename")
		name := params.Get("name")
		klog.V(4).Infof("Pod Namespace: %s NodeName: %s Name: %s", ns, nodename, name)

		// Build delete message
		msg := model.NewMessage("")
		resource, err := utils.BuildResource(nodename, ns, model.ResourceTypePod, name)
		if err != nil {
			klog.Errorf("Build message resource failed with error: %s", err)
			w.Write([]byte("Build message resource failed with error"))
			return
		}
		msg.Content = pm.GetPod(ns + "/" + name)
		msg.BuildRouter(constants.ControllerStub, constants.GroupResource, resource, model.DeleteOperation)

		// Delete pod in cache
		pm.DeletePod(ns + "/" + name)

		// Send msg
		pm.event <- msg
		klog.V(4).Infof("Finish delete pod request")

	default:
		klog.Errorf("Http type: %s unsupported", req.Method)
	}
}