2015-04-22 18:55:04 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
2015-04-22 18:55:04 +00:00
|
|
|
|
|
|
|
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 (
|
2015-05-15 23:14:08 +00:00
|
|
|
"hash/adler32"
|
2015-10-26 07:18:45 +00:00
|
|
|
"strings"
|
2015-04-27 22:55:13 +00:00
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-10-26 07:18:45 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
|
|
|
"k8s.io/kubernetes/pkg/client/record"
|
2015-12-07 21:31:02 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/util/format"
|
2015-10-26 07:18:45 +00:00
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
2016-01-05 07:31:20 +00:00
|
|
|
hashutil "k8s.io/kubernetes/pkg/util/hash"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/third_party/golang/expansion"
|
2015-05-22 22:21:03 +00:00
|
|
|
|
2015-05-01 05:50:33 +00:00
|
|
|
"github.com/golang/glog"
|
2015-04-22 18:55:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// HandlerRunner runs a lifecycle handler for a container.
|
|
|
|
type HandlerRunner interface {
|
2015-10-07 17:58:05 +00:00
|
|
|
Run(containerID ContainerID, pod *api.Pod, container *api.Container, handler *api.Handler) error
|
2015-04-22 18:55:04 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 20:55:50 +00:00
|
|
|
// RunContainerOptionsGenerator generates the options that necessary for
|
|
|
|
// container runtime to run a container.
|
|
|
|
type RunContainerOptionsGenerator interface {
|
2015-05-12 21:18:00 +00:00
|
|
|
GenerateRunContainerOptions(pod *api.Pod, container *api.Container) (*RunContainerOptions, error)
|
2015-04-23 20:55:50 +00:00
|
|
|
}
|
2015-04-27 22:55:13 +00:00
|
|
|
|
2015-05-01 05:50:33 +00:00
|
|
|
// ShouldContainerBeRestarted checks whether a container needs to be restarted.
|
|
|
|
// TODO(yifan): Think about how to refactor this.
|
2015-12-05 00:06:25 +00:00
|
|
|
func ShouldContainerBeRestarted(container *api.Container, pod *api.Pod, podStatus *PodStatus) bool {
|
|
|
|
// Get all dead container status.
|
|
|
|
var resultStatus []*ContainerStatus
|
|
|
|
for _, containerStatus := range podStatus.ContainerStatuses {
|
|
|
|
if containerStatus.Name == container.Name && containerStatus.State == ContainerStateExited {
|
|
|
|
resultStatus = append(resultStatus, containerStatus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check RestartPolicy for dead container.
|
|
|
|
if len(resultStatus) > 0 {
|
|
|
|
if pod.Spec.RestartPolicy == api.RestartPolicyNever {
|
2015-12-07 21:31:02 +00:00
|
|
|
glog.V(4).Infof("Already ran container %q of pod %q, do nothing", container.Name, format.Pod(pod))
|
2015-12-05 00:06:25 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if pod.Spec.RestartPolicy == api.RestartPolicyOnFailure {
|
|
|
|
// Check the exit code of last run. Note: This assumes the result is sorted
|
|
|
|
// by the created time in reverse order.
|
|
|
|
if resultStatus[0].ExitCode == 0 {
|
2015-12-07 21:31:02 +00:00
|
|
|
glog.V(4).Infof("Already successfully ran container %q of pod %q, do nothing", container.Name, format.Pod(pod))
|
2015-12-05 00:06:25 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-12-31 08:41:05 +00:00
|
|
|
// TODO(random-liu): This should be removed soon after rkt implements GetPodStatus.
|
2015-12-05 00:06:25 +00:00
|
|
|
func ShouldContainerBeRestartedOldVersion(container *api.Container, pod *api.Pod, podStatus *api.PodStatus) bool {
|
2015-05-01 05:50:33 +00:00
|
|
|
// Get all dead container status.
|
|
|
|
var resultStatus []*api.ContainerStatus
|
|
|
|
for i, containerStatus := range podStatus.ContainerStatuses {
|
2015-05-27 22:02:11 +00:00
|
|
|
if containerStatus.Name == container.Name && containerStatus.State.Terminated != nil {
|
2015-05-01 05:50:33 +00:00
|
|
|
resultStatus = append(resultStatus, &podStatus.ContainerStatuses[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check RestartPolicy for dead container.
|
|
|
|
if len(resultStatus) > 0 {
|
|
|
|
if pod.Spec.RestartPolicy == api.RestartPolicyNever {
|
2015-12-07 21:31:02 +00:00
|
|
|
glog.V(4).Infof("Already ran container %q of pod %q, do nothing", container.Name, format.Pod(pod))
|
2015-05-01 05:50:33 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if pod.Spec.RestartPolicy == api.RestartPolicyOnFailure {
|
|
|
|
// Check the exit code of last run. Note: This assumes the result is sorted
|
|
|
|
// by the created time in reverse order.
|
2015-05-27 22:02:11 +00:00
|
|
|
if resultStatus[0].State.Terminated.ExitCode == 0 {
|
2015-12-07 21:31:02 +00:00
|
|
|
glog.V(4).Infof("Already successfully ran container %q of pod %q, do nothing", container.Name, format.Pod(pod))
|
2015-05-01 05:50:33 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2015-05-15 23:14:08 +00:00
|
|
|
|
2015-12-31 08:41:05 +00:00
|
|
|
// TODO(random-liu): Convert PodStatus to running Pod, should be deprecated soon
|
2015-12-05 00:06:25 +00:00
|
|
|
func ConvertPodStatusToRunningPod(podStatus *PodStatus) Pod {
|
|
|
|
runningPod := Pod{
|
|
|
|
ID: podStatus.ID,
|
|
|
|
Name: podStatus.Name,
|
|
|
|
Namespace: podStatus.Namespace,
|
|
|
|
}
|
|
|
|
for _, containerStatus := range podStatus.ContainerStatuses {
|
|
|
|
if containerStatus.State != ContainerStateRunning {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
container := &Container{
|
|
|
|
ID: containerStatus.ID,
|
|
|
|
Name: containerStatus.Name,
|
|
|
|
Image: containerStatus.Image,
|
|
|
|
Hash: containerStatus.Hash,
|
|
|
|
Created: containerStatus.CreatedAt.Unix(),
|
|
|
|
State: containerStatus.State,
|
|
|
|
}
|
|
|
|
runningPod.Containers = append(runningPod.Containers, container)
|
|
|
|
}
|
|
|
|
return runningPod
|
|
|
|
}
|
|
|
|
|
2015-05-15 23:14:08 +00:00
|
|
|
// HashContainer returns the hash of the container. It is used to compare
|
|
|
|
// the running container with its desired spec.
|
|
|
|
func HashContainer(container *api.Container) uint64 {
|
|
|
|
hash := adler32.New()
|
2016-01-05 07:31:20 +00:00
|
|
|
hashutil.DeepHashObject(hash, *container)
|
2015-05-15 23:14:08 +00:00
|
|
|
return uint64(hash.Sum32())
|
|
|
|
}
|
2015-05-22 22:21:03 +00:00
|
|
|
|
|
|
|
// EnvVarsToMap constructs a map of environment name to value from a slice
|
|
|
|
// of env vars.
|
|
|
|
func EnvVarsToMap(envs []EnvVar) map[string]string {
|
|
|
|
result := map[string]string{}
|
|
|
|
for _, env := range envs {
|
|
|
|
result[env.Name] = env.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExpandContainerCommandAndArgs(container *api.Container, envs []EnvVar) (command []string, args []string) {
|
|
|
|
mapping := expansion.MappingFuncFor(EnvVarsToMap(envs))
|
|
|
|
|
|
|
|
if len(container.Command) != 0 {
|
|
|
|
for _, cmd := range container.Command {
|
|
|
|
command = append(command, expansion.Expand(cmd, mapping))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(container.Args) != 0 {
|
|
|
|
for _, arg := range container.Args {
|
|
|
|
args = append(args, expansion.Expand(arg, mapping))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return command, args
|
|
|
|
}
|
2015-10-26 07:18:45 +00:00
|
|
|
|
|
|
|
// Create an event recorder to record object's event except implicitly required container's, like infra container.
|
|
|
|
func FilterEventRecorder(recorder record.EventRecorder) record.EventRecorder {
|
|
|
|
return &innerEventRecorder{
|
|
|
|
recorder: recorder,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type innerEventRecorder struct {
|
|
|
|
recorder record.EventRecorder
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irecorder *innerEventRecorder) shouldRecordEvent(object runtime.Object) (*api.ObjectReference, bool) {
|
|
|
|
if object == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if ref, ok := object.(*api.ObjectReference); ok {
|
|
|
|
if !strings.HasPrefix(ref.FieldPath, ImplicitContainerPrefix) {
|
|
|
|
return ref, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2015-11-13 22:30:01 +00:00
|
|
|
func (irecorder *innerEventRecorder) Event(object runtime.Object, eventtype, reason, message string) {
|
2015-10-26 07:18:45 +00:00
|
|
|
if ref, ok := irecorder.shouldRecordEvent(object); ok {
|
2015-11-13 22:30:01 +00:00
|
|
|
irecorder.recorder.Event(ref, eventtype, reason, message)
|
2015-10-26 07:18:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 22:30:01 +00:00
|
|
|
func (irecorder *innerEventRecorder) Eventf(object runtime.Object, eventtype, reason, messageFmt string, args ...interface{}) {
|
2015-10-26 07:18:45 +00:00
|
|
|
if ref, ok := irecorder.shouldRecordEvent(object); ok {
|
2015-11-13 22:30:01 +00:00
|
|
|
irecorder.recorder.Eventf(ref, eventtype, reason, messageFmt, args...)
|
2015-10-26 07:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-13 22:30:01 +00:00
|
|
|
func (irecorder *innerEventRecorder) PastEventf(object runtime.Object, timestamp unversioned.Time, eventtype, reason, messageFmt string, args ...interface{}) {
|
2015-10-26 07:18:45 +00:00
|
|
|
if ref, ok := irecorder.shouldRecordEvent(object); ok {
|
2015-11-13 22:30:01 +00:00
|
|
|
irecorder.recorder.PastEventf(ref, timestamp, eventtype, reason, messageFmt, args...)
|
2015-10-26 07:18:45 +00:00
|
|
|
}
|
|
|
|
}
|