mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Add RuntimeHooks interface and Kubelet implementation.
This interface will be used to inject functionality and logic into the runtimes that should be shared accross runtimes.
This commit is contained in:
parent
5f831666de
commit
61d51243c5
@ -81,6 +81,16 @@ type Runtime interface {
|
|||||||
GetContainerLogs(containerID, tail string, follow bool, stdout, stderr io.Writer) (err error)
|
GetContainerLogs(containerID, tail string, follow bool, stdout, stderr io.Writer) (err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Customizable hooks injected into container runtimes.
|
||||||
|
type RuntimeHooks interface {
|
||||||
|
// Determines whether the runtime should pull the specified container's image.
|
||||||
|
ShouldPullImage(pod *api.Pod, container *api.Container, imagePresent bool) bool
|
||||||
|
|
||||||
|
// Runs after an image is pulled reporting it's status. Error may be nil
|
||||||
|
// for a successful pull.
|
||||||
|
ReportImagePull(pod *api.Pod, container *api.Container, err error)
|
||||||
|
}
|
||||||
|
|
||||||
// Pod is a group of containers, with the status of the pod.
|
// Pod is a group of containers, with the status of the pod.
|
||||||
type Pod struct {
|
type Pod struct {
|
||||||
// The ID of the pod, which can be used to retrieve a particular pod
|
// The ID of the pod, which can be used to retrieve a particular pod
|
||||||
|
64
pkg/kubelet/runtime_hooks.go
Normal file
64
pkg/kubelet/runtime_hooks.go
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2015 Google Inc. 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 kubelet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/record"
|
||||||
|
kubecontainer "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
|
||||||
|
"github.com/golang/glog"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Kubelet-specific runtime hooks.
|
||||||
|
type kubeletRuntimeHooks struct {
|
||||||
|
recorder record.EventRecorder
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ kubecontainer.RuntimeHooks = &kubeletRuntimeHooks{}
|
||||||
|
|
||||||
|
func newKubeletRuntimeHooks(recorder record.EventRecorder) kubecontainer.RuntimeHooks {
|
||||||
|
return &kubeletRuntimeHooks{
|
||||||
|
recorder: recorder,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (kr *kubeletRuntimeHooks) ShouldPullImage(pod *api.Pod, container *api.Container, imagePresent bool) bool {
|
||||||
|
if container.ImagePullPolicy == api.PullNever {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if container.ImagePullPolicy == api.PullAlways ||
|
||||||
|
(container.ImagePullPolicy == api.PullIfNotPresent && (!imagePresent)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (kr *kubeletRuntimeHooks) ReportImagePull(pod *api.Pod, container *api.Container, err error) {
|
||||||
|
ref, err := kubecontainer.GenerateContainerRef(pod, container)
|
||||||
|
if err != nil {
|
||||||
|
glog.Errorf("Couldn't make a ref to pod %v, container %v: '%v'", pod.Name, container.Name, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
kr.recorder.Eventf(ref, "failed", "Failed to pull image %q: %v", container.Image, err)
|
||||||
|
} else {
|
||||||
|
kr.recorder.Eventf(ref, "pulled", "Successfully pulled image %q", container.Image)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user