github.com/midokura/kubeedge@v1.2.0-mido.0/tests/stubs/edge/handlerstub/message.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 handlerstub 18 19 import ( 20 "encoding/json" 21 22 "k8s.io/klog" 23 24 "github.com/kubeedge/beehive/pkg/common/util" 25 beehiveContext "github.com/kubeedge/beehive/pkg/core/context" 26 "github.com/kubeedge/beehive/pkg/core/model" 27 "github.com/kubeedge/kubeedge/tests/stubs/common/constants" 28 "github.com/kubeedge/kubeedge/tests/stubs/common/types" 29 ) 30 31 // WaitforMessage is used to receive and process message 32 func (hs *HandlerStub) WaitforMessage() { 33 go func() { 34 for { 35 select { 36 case <-beehiveContext.Done(): 37 klog.Warning("stop waiting for message") 38 return 39 default: 40 41 } 42 if msg, err := beehiveContext.Receive(hs.Name()); err == nil { 43 klog.V(4).Infof("Receive a message %v", msg) 44 hs.ProcessMessage(msg) 45 } else { 46 klog.Errorf("Failed to receive message %v with error: %v", msg, err) 47 } 48 } 49 }() 50 } 51 52 // ProcessMessage based on the operation type 53 func (hs *HandlerStub) ProcessMessage(msg model.Message) { 54 klog.V(4).Infof("Begin to process message %v", msg) 55 operation := msg.GetOperation() 56 switch operation { 57 case model.InsertOperation: 58 hs.ProcessInsert(msg) 59 case model.DeleteOperation: 60 hs.ProcessDelete(msg) 61 default: 62 klog.V(4).Infof("Unsupported message: %s operation: %s", msg.GetID(), operation) 63 } 64 klog.V(4).Infof("End to process message %v", msg) 65 } 66 67 // ProcessInsert message 68 func (hs *HandlerStub) ProcessInsert(msg model.Message) { 69 // Get resource type 70 _, resType, _, err := util.ParseResourceEdge(msg.GetResource(), msg.GetOperation()) 71 if err != nil { 72 klog.Errorf("failed to parse the Resource: %v", err) 73 return 74 } 75 76 if resType == model.ResourceTypePod { 77 // receive pod add event 78 klog.V(4).Infof("Message content: %v", msg) 79 80 // Marshal message content 81 var data []byte 82 switch msg.Content.(type) { 83 case []byte: 84 data = msg.GetContent().([]byte) 85 default: 86 var err error 87 data, err = json.Marshal(msg.GetContent()) 88 if err != nil { 89 klog.Warningf("message: %s process failure, marshal content failed with error: %s", msg.GetID(), err) 90 return 91 } 92 } 93 94 // Get pod 95 var pod types.FakePod 96 if err := json.Unmarshal(data, &pod); err != nil { 97 klog.Errorf("Unmarshal content failed with error: %s", msg.GetID(), err) 98 return 99 } 100 101 // Build Add message 102 pod.Status = constants.PodRunning 103 respMessage := model.NewMessage("") 104 resource := pod.Namespace + "/" + model.ResourceTypePodStatus + "/" + pod.Name 105 respMessage.Content = pod 106 respMessage.BuildRouter(constants.HandlerStub, constants.GroupResource, resource, model.UpdateOperation) 107 108 hs.SendToCloud(respMessage) 109 110 // Add pod in cache 111 hs.podManager.AddPod(pod.Namespace+"/"+pod.Name, pod) 112 } 113 } 114 115 // ProcessDelete message 116 func (hs *HandlerStub) ProcessDelete(msg model.Message) { 117 // Get resource type 118 _, resType, _, err := util.ParseResourceEdge(msg.GetResource(), msg.GetOperation()) 119 if err != nil { 120 klog.Errorf("failed to parse the Resource: %v", err) 121 return 122 } 123 124 if resType == model.ResourceTypePod { 125 // Receive pod delete event 126 klog.V(4).Infof("Message content: %v", msg) 127 128 // Marshal message content 129 var data []byte 130 switch msg.Content.(type) { 131 case []byte: 132 data = msg.GetContent().([]byte) 133 default: 134 var err error 135 data, err = json.Marshal(msg.GetContent()) 136 if err != nil { 137 klog.Warningf("message: %s process failure, marshal content failed with error: %s", msg.GetID(), err) 138 return 139 } 140 } 141 142 // Get pod 143 var pod types.FakePod 144 if err := json.Unmarshal(data, &pod); err != nil { 145 klog.Errorf("Unmarshal content failed with error: %s", msg.GetID(), err) 146 return 147 } 148 // Delete pod in cache 149 hs.podManager.DeletePod(pod.Namespace + "/" + pod.Name) 150 } 151 } 152 153 // SendToCloud sends message to cloudhub by edgehub 154 func (hs *HandlerStub) SendToCloud(msg *model.Message) { 155 klog.V(4).Infof("Begin to send message %v", *msg) 156 beehiveContext.SendToGroup(constants.HubGroup, *msg) 157 klog.V(4).Infof("End to send message %v", *msg) 158 }