github.com/midokura/kubeedge@v1.2.0-mido.0/tests/stubs/edge/handlerstub/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 handlerstub 18 19 import ( 20 "time" 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 ) 28 29 // NewUpstreamController creates a upstream controller 30 func NewUpstreamController(pm *PodManager) (*UpstreamController, error) { 31 // New upstream controller 32 dc := &UpstreamController{podManager: pm} 33 return dc, nil 34 } 35 36 // UpstreamController sends message to edghub 37 type UpstreamController struct { 38 podManager *PodManager 39 } 40 41 // Start upstream 42 func (dc *UpstreamController) Start() error { 43 klog.Infof("Start upstream controller") 44 go dc.SyncPods() 45 return nil 46 } 47 48 // SyncPods is used to send simulation messages to edgehub periodically 49 func (dc *UpstreamController) SyncPods() { 50 for { 51 select { 52 case <-beehiveContext.Done(): 53 klog.Infof("Stop sync pods") 54 return 55 default: 56 57 } 58 pods := dc.podManager.ListPods() 59 klog.V(4).Infof("Current pods number is: %v", len(pods)) 60 for _, pod := range pods { 61 // Periodic sync message 62 msg := model.NewMessage("") 63 resource := pod.Namespace + "/" + model.ResourceTypePodStatus + "/" + pod.Name 64 msg.Content = pod 65 msg.BuildRouter(constants.HandlerStub, constants.GroupResource, resource, model.UpdateOperation) 66 67 klog.V(4).Infof("Begin to sync message: %v", *msg) 68 beehiveContext.SendToGroup(constants.HubGroup, *msg) 69 klog.V(4).Infof("End to sync message: %v", *msg) 70 } 71 time.Sleep(5 * time.Second) 72 } 73 }