sync_result.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/kubernetes/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. )
  33. var (
  34. ErrSetupNetwork = errors.New("SetupNetworkError")
  35. ErrTeardownNetwork = errors.New("TeardownNetworkError")
  36. )
  37. // SyncAction indicates different kind of actions in SyncPod() and KillPod(). Now there are only actions
  38. // about start/kill container and setup/teardown network.
  39. type SyncAction string
  40. const (
  41. StartContainer SyncAction = "StartContainer"
  42. KillContainer SyncAction = "KillContainer"
  43. SetupNetwork SyncAction = "SetupNetwork"
  44. TeardownNetwork SyncAction = "TeardownNetwork"
  45. InitContainer SyncAction = "InitContainer"
  46. )
  47. // SyncResult is the result of sync action.
  48. type SyncResult struct {
  49. // The associated action of the result
  50. Action SyncAction
  51. // The target of the action, now the target can only be:
  52. // * Container: Target should be container name
  53. // * Network: Target is useless now, we just set it as pod full name now
  54. Target interface{}
  55. // Brief error reason
  56. Error error
  57. // Human readable error reason
  58. Message string
  59. }
  60. // NewSyncResult generates new SyncResult with specific Action and Target
  61. func NewSyncResult(action SyncAction, target interface{}) *SyncResult {
  62. return &SyncResult{Action: action, Target: target}
  63. }
  64. // Fail fails the SyncResult with specific error and message
  65. func (r *SyncResult) Fail(err error, msg string) {
  66. r.Error, r.Message = err, msg
  67. }
  68. // PodSyncResult is the summary result of SyncPod() and KillPod()
  69. type PodSyncResult struct {
  70. // Result of different sync actions
  71. SyncResults []*SyncResult
  72. // Error encountered in SyncPod() and KillPod() that is not already included in SyncResults
  73. SyncError error
  74. }
  75. // AddSyncResult adds multiple SyncResult to current PodSyncResult
  76. func (p *PodSyncResult) AddSyncResult(result ...*SyncResult) {
  77. p.SyncResults = append(p.SyncResults, result...)
  78. }
  79. // AddPodSyncResult merges a PodSyncResult to current one
  80. func (p *PodSyncResult) AddPodSyncResult(result PodSyncResult) {
  81. p.AddSyncResult(result.SyncResults...)
  82. p.SyncError = result.SyncError
  83. }
  84. // Fail fails the PodSyncResult with an error occurred in SyncPod() and KillPod() itself
  85. func (p *PodSyncResult) Fail(err error) {
  86. p.SyncError = err
  87. }
  88. // Error returns an error summarizing all the errors in PodSyncResult
  89. func (p *PodSyncResult) Error() error {
  90. errlist := []error{}
  91. if p.SyncError != nil {
  92. errlist = append(errlist, fmt.Errorf("failed to SyncPod: %v\n", p.SyncError))
  93. }
  94. for _, result := range p.SyncResults {
  95. if result.Error != nil {
  96. errlist = append(errlist, fmt.Errorf("failed to %q for %q with %v: %q\n", result.Action, result.Target,
  97. result.Error, result.Message))
  98. }
  99. }
  100. return utilerrors.NewAggregate(errlist)
  101. }