github.com/midokura/kubeedge@v1.2.0-mido.0/tests/stubs/cloud/controllerstub/upstream.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 controllerstub 18 19 import ( 20 "encoding/json" 21 22 "k8s.io/klog" 23 24 beehiveContext "github.com/kubeedge/beehive/pkg/core/context" 25 "github.com/kubeedge/beehive/pkg/core/model" 26 "github.com/kubeedge/kubeedge/tests/stubs/common/constants" 27 "github.com/kubeedge/kubeedge/tests/stubs/common/types" 28 "github.com/kubeedge/kubeedge/tests/stubs/common/utils" 29 ) 30 31 // NewUpstreamController creates a upstream controller 32 func NewUpstreamController(pm *PodManager) (*UpstreamController, error) { 33 // New upstream controller 34 uc := &UpstreamController{podManager: pm} 35 return uc, nil 36 } 37 38 // UpstreamController subscribe messages from edge 39 type UpstreamController struct { 40 podManager *PodManager 41 podStatusChan chan model.Message 42 } 43 44 // Start UpstreamController 45 func (uc *UpstreamController) Start() error { 46 klog.Infof("Start upstream controller") 47 uc.podStatusChan = make(chan model.Message, 1024) 48 49 go uc.WaitforMessage() 50 go uc.UpdatePodStatus() 51 52 return nil 53 } 54 55 // WaitforMessage from cloudhub 56 func (uc *UpstreamController) WaitforMessage() { 57 for { 58 select { 59 case <-beehiveContext.Done(): 60 klog.Infof("Stop waiting for message") 61 return 62 default: 63 64 } 65 // Receive message from cloudhub 66 msg, err := beehiveContext.Receive(constants.ControllerStub) 67 if err != nil { 68 klog.Errorf("Receive message failed: %v", err) 69 continue 70 } 71 klog.V(4).Infof("Receive message: %v", msg) 72 73 // Get resource type in message 74 resourceType, err := utils.GetResourceType(msg) 75 if err != nil { 76 klog.Errorf("Get message: %s resource type with error: %v", msg.GetID(), err) 77 continue 78 } 79 klog.Infof("Message: %s resource type: %s", msg.GetID(), resourceType) 80 81 switch resourceType { 82 case model.ResourceTypePodStatus: 83 uc.podStatusChan <- msg 84 default: 85 klog.V(4).Infof("Message: %s, resource type: %s unsupported", msg.GetID(), resourceType) 86 } 87 } 88 } 89 90 // UpdatePodStatus is used to update pod status in cache map 91 func (uc *UpstreamController) UpdatePodStatus() { 92 for { 93 select { 94 case <-beehiveContext.Done(): 95 klog.Infof("Stop updatePodStatus") 96 return 97 case msg := <-uc.podStatusChan: 98 klog.Infof("Message: %s operation: %s resource: %s", 99 msg.GetID(), msg.GetOperation(), msg.GetResource()) 100 switch msg.GetOperation() { 101 case model.UpdateOperation: 102 // Marshal message content 103 var data []byte 104 switch msg.Content.(type) { 105 case []byte: 106 data = msg.GetContent().([]byte) 107 default: 108 var err error 109 data, err = json.Marshal(msg.GetContent()) 110 if err != nil { 111 klog.Warningf("message: %s process failure, marshal content failed with error: %s", msg.GetID(), err) 112 continue 113 } 114 } 115 116 // Get pod 117 var pod types.FakePod 118 if err := json.Unmarshal(data, &pod); err != nil { 119 klog.Errorf("Unmarshal content failed with error: %s", msg.GetID(), err) 120 continue 121 } 122 123 // Update pod status in cache 124 uc.podManager.UpdatePodStatus(pod.Namespace+"/"+pod.Name, pod.Status) 125 126 klog.Infof("Pod namespace: %s name: %s status: %s", 127 pod.Namespace, pod.Name, pod.Status) 128 default: 129 klog.V(4).Infof("Pod operation: %s unsupported", msg.GetOperation()) 130 } 131 klog.V(4).Infof("Message: %s process successfully", msg.GetID()) 132 } 133 } 134 }