status_manager.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package status
  15. import (
  16. "context"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/onecloud/pkg/apis"
  22. computeapi "yunion.io/x/onecloud/pkg/apis/compute"
  23. "yunion.io/x/onecloud/pkg/hostman/container/prober/results"
  24. "yunion.io/x/onecloud/pkg/hostman/guestman/pod/statusman"
  25. "yunion.io/x/onecloud/pkg/hostman/hostutils"
  26. )
  27. type Manager interface {
  28. // SetContainerStartup updates the container status with the given startup
  29. // and triggers a status update.
  30. SetContainerStartup(podId string, containerId string, started bool, result results.ProbeResult, pod results.IPod) error
  31. }
  32. type manager struct{}
  33. func NewManager() Manager {
  34. return &manager{}
  35. }
  36. func (m *manager) SetContainerStartup(podId string, containerId string, started bool, result results.ProbeResult, pod results.IPod) error {
  37. status := computeapi.CONTAINER_STATUS_PROBE_FAILED
  38. if started {
  39. status = computeapi.CONTAINER_STATUS_RUNNING
  40. } else {
  41. if result.IsNetFailedError() && pod.IsRunning() {
  42. status = computeapi.CONTAINER_STATUS_NET_FAILED
  43. }
  44. }
  45. input := &statusman.PodStatusUpdateRequest{
  46. Id: podId,
  47. Pod: pod.(statusman.IPod),
  48. Status: computeapi.VM_RUNNING,
  49. Reason: result.Reason,
  50. ContainerStatuses: map[string]*statusman.ContainerStatus{
  51. containerId: {Status: status},
  52. },
  53. }
  54. if err := statusman.GetManager().UpdateStatus(input); err != nil {
  55. err = errors.Wrapf(err, "set container(%s/%s) status failed, input: %s", podId, containerId, jsonutils.Marshal(input.ToServerPerformStatusInput()))
  56. log.Warningf("%s", err.Error())
  57. errMsg := []string{
  58. "can't set container status",
  59. }
  60. for _, msg := range errMsg {
  61. if strings.Contains(err.Error(), msg) {
  62. return nil
  63. }
  64. }
  65. return errors.Wrap(err, "update container status")
  66. } else {
  67. log.Infof("set container(%s/%s) status to %s", podId, containerId, jsonutils.Marshal(input).String())
  68. }
  69. return nil
  70. }
  71. func (m *manager) SetContainerStartupOld(podId string, containerId string, started bool, result results.ProbeResult, pod results.IPod) error {
  72. status := computeapi.CONTAINER_STATUS_PROBE_FAILED
  73. if started {
  74. status = computeapi.CONTAINER_STATUS_RUNNING
  75. } else {
  76. if result.IsNetFailedError() && pod.IsRunning() {
  77. status = computeapi.CONTAINER_STATUS_NET_FAILED
  78. }
  79. }
  80. input := &computeapi.ContainerPerformStatusInput{
  81. PerformStatusInput: apis.PerformStatusInput{
  82. Status: status,
  83. Reason: result.Reason,
  84. },
  85. }
  86. if _, err := hostutils.UpdateContainerStatus(context.Background(), containerId, input); err != nil {
  87. err = errors.Wrapf(err, "set container(%s/%s) status failed, input: %s", podId, containerId, jsonutils.Marshal(input))
  88. log.Warningf("%s", err.Error())
  89. errMsg := []string{
  90. "can't set container status",
  91. }
  92. for _, msg := range errMsg {
  93. if strings.Contains(err.Error(), msg) {
  94. return nil
  95. }
  96. }
  97. return errors.Wrap(err, "update container status")
  98. } else {
  99. log.Infof("set container(%s/%s) status to %s", podId, containerId, jsonutils.Marshal(input).String())
  100. }
  101. return nil
  102. }