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.

upstream.go 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 featureextraction
  14. import (
  15. "context"
  16. "encoding/json"
  17. "fmt"
  18. "k8s.io/klog/v2"
  19. sednav1 "github.com/kubeedge/sedna/pkg/apis/sedna/v1alpha1"
  20. "github.com/kubeedge/sedna/pkg/globalmanager/runtime"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. )
  23. // updateHandler handles the updates from LC(running at edge) to update the
  24. // corresponding resource
  25. type updateHandler func(namespace, name, operation string, content []byte) error
  26. const upstreamStatusUpdateRetries = 3
  27. // retryUpdateStatus simply retries to call the status update func
  28. func retryUpdateStatus(name, namespace string, updateStatusFunc func() error) error {
  29. var err error
  30. for retry := 0; retry <= upstreamStatusUpdateRetries; retry++ {
  31. err = updateStatusFunc()
  32. if err == nil {
  33. return nil
  34. }
  35. klog.Warningf("Error to update %s/%s status, retried %d times: %+v", namespace, name, retry, err)
  36. }
  37. return err
  38. }
  39. func newUnmarshalError(namespace, name, operation string, content []byte) error {
  40. return fmt.Errorf("Unable to unmarshal content for (%s/%s) operation: '%s', content: '%+v'", namespace, name, operation, string(content))
  41. }
  42. func checkUpstreamOperation(operation string) error {
  43. // current only support the 'status' operation
  44. if operation != "status" {
  45. return fmt.Errorf("unknown operation %s", operation)
  46. }
  47. return nil
  48. }
  49. // convertToMetrics converts the metrics from LCs to resource metrics
  50. func convertToMetrics(m map[string]interface{}) []sednav1.Metric {
  51. var l []sednav1.Metric
  52. for k, v := range m {
  53. var displayValue string
  54. switch t := v.(type) {
  55. case string:
  56. displayValue = t
  57. default:
  58. // ignore the json marshal error
  59. b, _ := json.Marshal(v)
  60. displayValue = string(b)
  61. }
  62. l = append(l, sednav1.Metric{Key: k, Value: displayValue})
  63. }
  64. return l
  65. }
  66. func (c *Controller) updateFeatureExtractionMetrics(name, namespace string, metrics []sednav1.Metric) error {
  67. client := c.client.FeatureExtractionServices(namespace)
  68. return retryUpdateStatus(name, namespace, func() error {
  69. joint, err := client.Get(context.TODO(), name, metav1.GetOptions{})
  70. if err != nil {
  71. return err
  72. }
  73. joint.Status.Metrics = metrics
  74. _, err = client.UpdateStatus(context.TODO(), joint, metav1.UpdateOptions{})
  75. return err
  76. })
  77. }
  78. func (c *Controller) updateFeatureExtractionFromEdge(name, namespace, operation string, content []byte) error {
  79. err := checkUpstreamOperation(operation)
  80. if err != nil {
  81. return err
  82. }
  83. // Output defines owner output information
  84. type Output struct {
  85. ServiceInfo map[string]interface{} `json:"ownerInfo"`
  86. }
  87. var status struct {
  88. // Phase always should be "inference"
  89. Phase string `json:"phase"`
  90. Status string `json:"status"`
  91. Output *Output `json:"output"`
  92. }
  93. err = json.Unmarshal(content, &status)
  94. if err != nil {
  95. return newUnmarshalError(namespace, name, operation, content)
  96. }
  97. // TODO: propagate status.Status to k8s
  98. output := status.Output
  99. if output == nil || output.ServiceInfo == nil {
  100. // no output info
  101. klog.Warningf("empty status info for feature extraction service %s/%s", namespace, name)
  102. return nil
  103. }
  104. info := output.ServiceInfo
  105. for _, ignoreTimeKey := range []string{
  106. "startTime",
  107. "updateTime",
  108. } {
  109. delete(info, ignoreTimeKey)
  110. }
  111. metrics := convertToMetrics(info)
  112. err = c.updateFeatureExtractionMetrics(name, namespace, metrics)
  113. if err != nil {
  114. return fmt.Errorf("failed to update metrics, err:%+w", err)
  115. }
  116. return nil
  117. }
  118. func (c *Controller) SetUpstreamHandler(addFunc runtime.UpstreamHandlerAddFunc) error {
  119. return addFunc(KindName, c.updateFeatureExtractionFromEdge)
  120. }