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 2.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 jointinference
  14. import (
  15. "context"
  16. "encoding/json"
  17. "fmt"
  18. sednav1 "github.com/kubeedge/sedna/pkg/apis/sedna/v1alpha1"
  19. "github.com/kubeedge/sedna/pkg/globalmanager/runtime"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/klog/v2"
  22. )
  23. func (c *Controller) updateMetrics(name, namespace string, metrics []sednav1.Metric) error {
  24. client := c.client.JointInferenceServices(namespace)
  25. return runtime.RetryUpdateStatus(name, namespace, func() error {
  26. joint, err := client.Get(context.TODO(), name, metav1.GetOptions{})
  27. if err != nil {
  28. return err
  29. }
  30. joint.Status.Metrics = metrics
  31. _, err = client.UpdateStatus(context.TODO(), joint, metav1.UpdateOptions{})
  32. return err
  33. })
  34. }
  35. // updateFromEdge syncs the edge updates to k8s
  36. func (c *Controller) updateFromEdge(name, namespace, operation string, content []byte) error {
  37. // Output defines owner output information
  38. type Output struct {
  39. ServiceInfo map[string]interface{} `json:"ownerInfo"`
  40. }
  41. var status struct {
  42. // Phase always should be "inference"
  43. Phase string `json:"phase"`
  44. Status string `json:"status"`
  45. Output *Output `json:"output"`
  46. }
  47. err := json.Unmarshal(content, &status)
  48. if err != nil {
  49. return err
  50. }
  51. // TODO: propagate status.Status to k8s
  52. output := status.Output
  53. if output == nil || output.ServiceInfo == nil {
  54. // no output info
  55. klog.Warningf("empty status info for joint inference service %s/%s", namespace, name)
  56. return nil
  57. }
  58. info := output.ServiceInfo
  59. for _, ignoreTimeKey := range []string{
  60. "startTime",
  61. "updateTime",
  62. } {
  63. delete(info, ignoreTimeKey)
  64. }
  65. metrics := runtime.ConvertMapToMetrics(info)
  66. err = c.updateMetrics(name, namespace, metrics)
  67. if err != nil {
  68. return fmt.Errorf("failed to update metrics, err:%+w", err)
  69. }
  70. return nil
  71. }
  72. func (c *Controller) SetUpstreamHandler(addFunc runtime.UpstreamHandlerAddFunc) error {
  73. return addFunc(KindName, c.updateFromEdge)
  74. }