From 2d31d8ed4c6738136f7473d181ee1f396f6bbb22 Mon Sep 17 00:00:00 2001 From: Random-Liu Date: Wed, 13 Jan 2016 15:08:18 -0800 Subject: [PATCH] Add PodSyncResult --- pkg/kubelet/container/runtime.go | 27 ------ pkg/kubelet/container/sync_result.go | 135 +++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 27 deletions(-) create mode 100644 pkg/kubelet/container/sync_result.go diff --git a/pkg/kubelet/container/runtime.go b/pkg/kubelet/container/runtime.go index f1ba8b30a08..dd7130cbcb1 100644 --- a/pkg/kubelet/container/runtime.go +++ b/pkg/kubelet/container/runtime.go @@ -17,7 +17,6 @@ limitations under the License. package container import ( - "errors" "fmt" "io" "reflect" @@ -31,32 +30,6 @@ import ( "k8s.io/kubernetes/pkg/volume" ) -// Container Terminated and Kubelet is backing off the restart -var ErrCrashLoopBackOff = errors.New("CrashLoopBackOff") - -var ( - // Container image pull failed, kubelet is backing off image pull - ErrImagePullBackOff = errors.New("ImagePullBackOff") - - // Unable to inspect image - ErrImageInspect = errors.New("ImageInspectError") - - // General image pull error - ErrImagePull = errors.New("ErrImagePull") - - // Required Image is absent on host and PullPolicy is NeverPullImage - ErrImageNeverPull = errors.New("ErrImageNeverPull") - - // ErrContainerNotFound returned when a container in the given pod with the - // given container name was not found, amongst those managed by the kubelet. - ErrContainerNotFound = errors.New("no matching container") - - // Get http error when pulling image from registry - RegistryUnavailable = errors.New("RegistryUnavailable") -) - -var ErrRunContainer = errors.New("RunContainerError") - type Version interface { // Compare compares two versions of the runtime. On success it returns -1 // if the version is less than the other, 1 if it is greater than the other, diff --git a/pkg/kubelet/container/sync_result.go b/pkg/kubelet/container/sync_result.go new file mode 100644 index 00000000000..0faf701d7ae --- /dev/null +++ b/pkg/kubelet/container/sync_result.go @@ -0,0 +1,135 @@ +/* +Copyright 2015 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package container + +import ( + "errors" + "fmt" + + utilerrors "k8s.io/kubernetes/pkg/util/errors" +) + +// TODO(random-liu): We need to better organize runtime errors for introspection. + +// Container Terminated and Kubelet is backing off the restart +var ErrCrashLoopBackOff = errors.New("CrashLoopBackOff") + +var ( + // Container image pull failed, kubelet is backing off image pull + ErrImagePullBackOff = errors.New("ImagePullBackOff") + + // Unable to inspect image + ErrImageInspect = errors.New("ImageInspectError") + + // General image pull error + ErrImagePull = errors.New("ErrImagePull") + + // Required Image is absent on host and PullPolicy is NeverPullImage + ErrImageNeverPull = errors.New("ErrImageNeverPull") + + // ErrContainerNotFound returned when a container in the given pod with the + // given container name was not found, amongst those managed by the kubelet. + ErrContainerNotFound = errors.New("no matching container") + + // Get http error when pulling image from registry + RegistryUnavailable = errors.New("RegistryUnavailable") +) + +var ( + ErrRunContainer = errors.New("RunContainerError") + ErrKillContainer = errors.New("KillContainerError") + ErrVerifyNonRoot = errors.New("VerifyNonRootError") +) + +var ( + ErrSetupNetwork = errors.New("SetupNetworkError") + ErrTeardownNetwork = errors.New("TeardownNetworkError") +) + +// SyncAction indicates different kind of actions in SyncPod() and KillPod(). Now there are only actions +// about start/kill container and setup/teardown network. +type SyncAction string + +const ( + StartContainer SyncAction = "StartContainer" + KillContainer SyncAction = "KillContainer" + SetupNetwork SyncAction = "SetupNetwork" + TeardownNetwork SyncAction = "TeardownNetwork" +) + +// SyncResult is the result of sync action. +type SyncResult struct { + // The associated action of the result + Action SyncAction + // The target of the action, now the target can only be: + // * Container: Target should be container name + // * Network: Target is useless now, we just set it as pod full name now + Target interface{} + // Brief error reason + Error error + // Human readable error reason + Message string +} + +// NewSyncResult generates new SyncResult with specific Action and Target +func NewSyncResult(action SyncAction, target interface{}) *SyncResult { + return &SyncResult{Action: action, Target: target} +} + +// Fail fails the SyncResult with specific error and message +func (r *SyncResult) Fail(err error, msg string) { + r.Error, r.Message = err, msg +} + +// PodSyncResult is the summary result of SyncPod() and KillPod() +type PodSyncResult struct { + // Result of different sync actions + SyncResults []*SyncResult + // Error encountered in SyncPod() and KillPod() that is not already included in SyncResults + SyncError error +} + +// AddSyncResult adds multiple SyncResult to current PodSyncResult +func (p *PodSyncResult) AddSyncResult(result ...*SyncResult) { + p.SyncResults = append(p.SyncResults, result...) +} + +// AddPodSyncResult merges a PodSyncResult to current one +func (p *PodSyncResult) AddPodSyncResult(result PodSyncResult) { + p.AddSyncResult(result.SyncResults...) + p.SyncError = result.SyncError +} + +// Fail fails the PodSyncResult with an error occured in SyncPod() and KillPod() itself +func (p *PodSyncResult) Fail(err error) { + p.SyncError = err +} + +// Error returns an error summarizing all the errors in PodSyncResult +func (p *PodSyncResult) Error() error { + errlist := []error{} + if p.SyncError != nil { + errlist = append(errlist, fmt.Errorf("failed to SyncPod: %v\n", p.SyncError)) + } + for _, result := range p.SyncResults { + if result.Error != nil { + errlist = append(errlist, fmt.Errorf("failed to %q for %q with %v: %q\n", result.Action, result.Target, + result.Error, result.Message)) + } + } + return utilerrors.NewAggregate(errlist) +}