1 /* 2 Copyright 2015 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package container 18 19 import ( 20 "errors" 21 "fmt" 22 23 utilerrors "k8s.io/apimachinery/pkg/util/errors" 24 ) 25 26 // TODO(random-liu): We need to better organize runtime errors for introspection. 27 28 // ErrCrashLoopBackOff returned when a container Terminated and Kubelet is backing off the restart. 29 var ErrCrashLoopBackOff = errors.New("CrashLoopBackOff") 30 31 var ( 32 // ErrContainerNotFound returned when a container in the given pod with the 33 // given container name was not found, amongst those managed by the kubelet. 34 ErrContainerNotFound = errors.New("no matching container") 35 ) 36 37 var ( 38 // ErrRunContainer returned when runtime failed to start any of pod's container. 39 ErrRunContainer = errors.New("RunContainerError") 40 // ErrKillContainer returned when runtime failed to kill any of pod's containers. 41 ErrKillContainer = errors.New("KillContainerError") 42 // ErrCreatePodSandbox returned when runtime failed to create a sandbox for pod. 43 ErrCreatePodSandbox = errors.New("CreatePodSandboxError") 44 // ErrConfigPodSandbox returned when runetime failed to get pod sandbox config from pod. 45 ErrConfigPodSandbox = errors.New("ConfigPodSandboxError") 46 // ErrKillPodSandbox returned when runtime failed to stop pod's sandbox. 47 ErrKillPodSandbox = errors.New("KillPodSandboxError") 48 ) 49 50 // SyncAction indicates different kind of actions in SyncPod() and KillPod(). Now there are only actions 51 // about start/kill container and setup/teardown network. 52 type SyncAction string 53 54 const ( 55 // StartContainer action 56 StartContainer SyncAction = "StartContainer" 57 // KillContainer action 58 KillContainer SyncAction = "KillContainer" 59 // SetupNetwork action 60 SetupNetwork SyncAction = "SetupNetwork" 61 // TeardownNetwork action 62 TeardownNetwork SyncAction = "TeardownNetwork" 63 // InitContainer action 64 InitContainer SyncAction = "InitContainer" 65 // CreatePodSandbox action 66 CreatePodSandbox SyncAction = "CreatePodSandbox" 67 // ConfigPodSandbox action 68 ConfigPodSandbox SyncAction = "ConfigPodSandbox" 69 // KillPodSandbox action 70 KillPodSandbox SyncAction = "KillPodSandbox" 71 ) 72 73 // SyncResult is the result of sync action. 74 type SyncResult struct { 75 // The associated action of the result 76 Action SyncAction 77 // The target of the action, now the target can only be: 78 // * Container: Target should be container name 79 // * Network: Target is useless now, we just set it as pod full name now 80 Target interface{} 81 // Brief error reason 82 Error error 83 // Human readable error reason 84 Message string 85 } 86 87 // NewSyncResult generates new SyncResult with specific Action and Target 88 func NewSyncResult(action SyncAction, target interface{}) *SyncResult { 89 return &SyncResult{Action: action, Target: target} 90 } 91 92 // Fail fails the SyncResult with specific error and message 93 func (r *SyncResult) Fail(err error, msg string) { 94 r.Error, r.Message = err, msg 95 } 96 97 // PodSyncResult is the summary result of SyncPod() and KillPod() 98 type PodSyncResult struct { 99 // Result of different sync actions 100 SyncResults []*SyncResult 101 // Error encountered in SyncPod() and KillPod() that is not already included in SyncResults 102 SyncError error 103 } 104 105 // AddSyncResult adds multiple SyncResult to current PodSyncResult 106 func (p *PodSyncResult) AddSyncResult(result ...*SyncResult) { 107 p.SyncResults = append(p.SyncResults, result...) 108 } 109 110 // AddPodSyncResult merges a PodSyncResult to current one 111 func (p *PodSyncResult) AddPodSyncResult(result PodSyncResult) { 112 p.AddSyncResult(result.SyncResults...) 113 p.SyncError = result.SyncError 114 } 115 116 // Fail fails the PodSyncResult with an error occurred in SyncPod() and KillPod() itself 117 func (p *PodSyncResult) Fail(err error) { 118 p.SyncError = err 119 } 120 121 // Error returns an error summarizing all the errors in PodSyncResult 122 func (p *PodSyncResult) Error() error { 123 errlist := []error{} 124 if p.SyncError != nil { 125 errlist = append(errlist, fmt.Errorf("failed to SyncPod: %v", p.SyncError)) 126 } 127 for _, result := range p.SyncResults { 128 if result.Error != nil { 129 errlist = append(errlist, fmt.Errorf("failed to %q for %q with %v: %q", result.Action, result.Target, 130 result.Error, result.Message)) 131 } 132 } 133 return utilerrors.NewAggregate(errlist) 134 } 135