You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

context.go 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright 2021 The KubeEdge Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package messagelayer
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "strings"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/watch"
  20. "k8s.io/klog/v2"
  21. "github.com/kubeedge/sedna/pkg/globalmanager/messagelayer/model"
  22. wsContext "github.com/kubeedge/sedna/pkg/globalmanager/messagelayer/ws"
  23. )
  24. // MessageLayer define all functions that message layer must implement
  25. type MessageLayer interface {
  26. SendResourceObject(nodeName string, eventType watch.EventType, obj interface{}) error
  27. ReceiveResourceUpdate() (*ResourceUpdateSpec, error)
  28. Done() <-chan struct{}
  29. }
  30. // ContextMessageLayer build on context
  31. type ContextMessageLayer struct {
  32. }
  33. // ResourceUpdateSpec describes the resource update from upstream
  34. type ResourceUpdateSpec struct {
  35. Kind string
  36. Namespace string
  37. Name string
  38. Operation string
  39. Content []byte
  40. }
  41. // SendResourceObject message to the node with resource object and event type
  42. func (cml *ContextMessageLayer) SendResourceObject(nodeName string, eventType watch.EventType, obj interface{}) error {
  43. var operation string
  44. switch eventType {
  45. case watch.Added:
  46. operation = "insert"
  47. case watch.Modified:
  48. operation = "update"
  49. case watch.Deleted:
  50. operation = "delete"
  51. default:
  52. // should never get here
  53. return fmt.Errorf("event type: %s unsupported", eventType)
  54. }
  55. var msg model.Message
  56. payload, _ := json.Marshal(obj)
  57. msg.Content = (payload)
  58. // For code simplicity not to duplicate the code,
  59. // here just unmarshal to unifying struct type.
  60. var om metav1.PartialObjectMetadata
  61. err := json.Unmarshal(payload, &om)
  62. if err != nil {
  63. // impossible here, just for in case
  64. return fmt.Errorf("Unmarshal error for %v, err: %w", obj, err)
  65. }
  66. namespace := om.Namespace
  67. kind := strings.ToLower(om.Kind)
  68. name := om.Name
  69. msg.Namespace = namespace
  70. msg.ResourceKind = kind
  71. msg.ResourceName = name
  72. msg.Operation = operation
  73. klog.V(2).Infof("sending %s %s/%s to node(%s)", kind, namespace, name, nodeName)
  74. klog.V(4).Infof("sending %s %s/%s to node(%s), msg:%s", kind, namespace, name, nodeName, msg)
  75. // TODO: may need to guarantee message send to node
  76. return wsContext.SendToEdge(nodeName, &msg)
  77. }
  78. // ReceiveResourceUpdate receives and handles the update
  79. func (cml *ContextMessageLayer) ReceiveResourceUpdate() (*ResourceUpdateSpec, error) {
  80. nodeName, msg, err := wsContext.ReceiveFromEdge()
  81. if err != nil {
  82. return nil, err
  83. }
  84. klog.V(4).Infof("get message from nodeName %s:%s", nodeName, msg)
  85. namespace := msg.Namespace
  86. kind := strings.ToLower(msg.ResourceKind)
  87. name := msg.ResourceName
  88. operation := msg.Operation
  89. content := msg.Content
  90. return &ResourceUpdateSpec{
  91. Kind: kind,
  92. Namespace: namespace,
  93. Name: name,
  94. Operation: operation,
  95. Content: content,
  96. }, nil
  97. }
  98. // Done signals the message layer is done
  99. func (cml *ContextMessageLayer) Done() <-chan struct{} {
  100. return wsContext.Done()
  101. }
  102. // NewContextMessageLayer create a ContextMessageLayer
  103. func NewContextMessageLayer() MessageLayer {
  104. return &ContextMessageLayer{}
  105. }