sync_result.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Copyright 2015 The Kubernetes 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 container
  14. import (
  15. "errors"
  16. "fmt"
  17. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  18. )
  19. // TODO(random-liu): We need to better organize runtime errors for introspection.
  20. // Container Terminated and Kubelet is backing off the restart
  21. var ErrCrashLoopBackOff = errors.New("CrashLoopBackOff")
  22. var (
  23. // ErrContainerNotFound returned when a container in the given pod with the
  24. // given container name was not found, amongst those managed by the kubelet.
  25. ErrContainerNotFound = errors.New("no matching container")
  26. )
  27. var (
  28. ErrRunContainer = errors.New("RunContainerError")
  29. ErrKillContainer = errors.New("KillContainerError")
  30. ErrVerifyNonRoot = errors.New("VerifyNonRootError")
  31. ErrRunInitContainer = errors.New("RunInitContainerError")
  32. ErrCreatePodSandbox = errors.New("CreatePodSandboxError")
  33. ErrConfigPodSandbox = errors.New("ConfigPodSandboxError")
  34. ErrKillPodSandbox = errors.New("KillPodSandboxError")
  35. )
  36. var (
  37. ErrSetupNetwork = errors.New("SetupNetworkError")
  38. ErrTeardownNetwork = errors.New("TeardownNetworkError")
  39. )
  40. // SyncAction indicates different kind of actions in SyncPod() and KillPod(). Now there are only actions
  41. // about start/kill container and setup/teardown network.
  42. type SyncAction string
  43. const (
  44. StartContainer SyncAction = "StartContainer"
  45. KillContainer SyncAction = "KillContainer"
  46. SetupNetwork SyncAction = "SetupNetwork"
  47. TeardownNetwork SyncAction = "TeardownNetwork"
  48. InitContainer SyncAction = "InitContainer"
  49. CreatePodSandbox SyncAction = "CreatePodSandbox"
  50. ConfigPodSandbox SyncAction = "ConfigPodSandbox"
  51. KillPodSandbox SyncAction = "KillPodSandbox"
  52. )
  53. // SyncResult is the result of sync action.
  54. type SyncResult struct {
  55. // The associated action of the result
  56. Action SyncAction
  57. // The target of the action, now the target can only be:
  58. // * Container: Target should be container name
  59. // * Network: Target is useless now, we just set it as pod full name now
  60. Target interface{}
  61. // Brief error reason
  62. Error error
  63. // Human readable error reason
  64. Message string
  65. }
  66. // NewSyncResult generates new SyncResult with specific Action and Target
  67. func NewSyncResult(action SyncAction, target interface{}) *SyncResult {
  68. return &SyncResult{Action: action, Target: target}
  69. }
  70. // Fail fails the SyncResult with specific error and message
  71. func (r *SyncResult) Fail(err error, msg string) {
  72. r.Error, r.Message = err, msg
  73. }
  74. // PodSyncResult is the summary result of SyncPod() and KillPod()
  75. type PodSyncResult struct {
  76. // Result of different sync actions
  77. SyncResults []*SyncResult
  78. // Error encountered in SyncPod() and KillPod() that is not already included in SyncResults
  79. SyncError error
  80. }
  81. // AddSyncResult adds multiple SyncResult to current PodSyncResult
  82. func (p *PodSyncResult) AddSyncResult(result ...*SyncResult) {
  83. p.SyncResults = append(p.SyncResults, result...)
  84. }
  85. // AddPodSyncResult merges a PodSyncResult to current one
  86. func (p *PodSyncResult) AddPodSyncResult(result PodSyncResult) {
  87. p.AddSyncResult(result.SyncResults...)
  88. p.SyncError = result.SyncError
  89. }
  90. // Fail fails the PodSyncResult with an error occurred in SyncPod() and KillPod() itself
  91. func (p *PodSyncResult) Fail(err error) {
  92. p.SyncError = err
  93. }
  94. // Error returns an error summarizing all the errors in PodSyncResult
  95. func (p *PodSyncResult) Error() error {
  96. errlist := []error{}
  97. if p.SyncError != nil {
  98. errlist = append(errlist, fmt.Errorf("failed to SyncPod: %v", p.SyncError))
  99. }
  100. for _, result := range p.SyncResults {
  101. if result.Error != nil {
  102. errlist = append(errlist, fmt.Errorf("failed to %q for %q with %v: %q", result.Action, result.Target,
  103. result.Error, result.Message))
  104. }
  105. }
  106. return utilerrors.NewAggregate(errlist)
  107. }