mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 22:46:12 +00:00
Start putting event generation into kubelet (not enabled yet)
This commit is contained in:
parent
25bd151d86
commit
dcc111bf13
@ -30,6 +30,7 @@ import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/capabilities"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/record"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/health"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/dockertools"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
|
||||
@ -73,6 +74,7 @@ func NewMainKubelet(
|
||||
resyncInterval: ri,
|
||||
networkContainerImage: ni,
|
||||
podWorkers: newPodWorkers(),
|
||||
dockerIDToRef: map[dockertools.DockerID]*api.ObjectReference{},
|
||||
runner: dockertools.NewDockerContainerCommandRunner(dc),
|
||||
httpClient: &http.Client{},
|
||||
pullQPS: pullQPS,
|
||||
@ -93,6 +95,7 @@ func NewIntegrationTestKubelet(hn string, rd string, dc dockertools.DockerInterf
|
||||
networkContainerImage: NetworkContainerImage,
|
||||
resyncInterval: 3 * time.Second,
|
||||
podWorkers: newPodWorkers(),
|
||||
dockerIDToRef: map[dockertools.DockerID]*api.ObjectReference{},
|
||||
}
|
||||
}
|
||||
|
||||
@ -110,6 +113,11 @@ type Kubelet struct {
|
||||
resyncInterval time.Duration
|
||||
pods []api.BoundPod
|
||||
|
||||
// Needed to report events for containers belonging to deleted/modified pods.
|
||||
// Tracks references for reporting events
|
||||
dockerIDToRef map[dockertools.DockerID]*api.ObjectReference
|
||||
refLock sync.RWMutex
|
||||
|
||||
// Optional, no events will be sent without it
|
||||
etcdClient tools.EtcdClient
|
||||
// Optional, defaults to simple implementaiton
|
||||
@ -381,6 +389,57 @@ func (kl *Kubelet) runHandler(podFullName, uuid string, container *api.Container
|
||||
return actionHandler.Run(podFullName, uuid, container, handler)
|
||||
}
|
||||
|
||||
// fieldPath returns a fieldPath locating container within pod.
|
||||
// Returns an error if the container isn't part of the pod.
|
||||
func fieldPath(pod *api.BoundPod, container *api.Container) (string, error) {
|
||||
for i := range pod.Spec.Containers {
|
||||
here := &pod.Spec.Containers[i]
|
||||
if here == container {
|
||||
return fmt.Sprintf("spec.containers[%n]", i), nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("container %#v not found in pod %#v", container, pod)
|
||||
}
|
||||
|
||||
// containerRef returns an *api.ObjectReference which references the given container within the
|
||||
// given pod. Returns an error if the reference can't be constructed or the container doesn't
|
||||
// actually belong to the pod.
|
||||
func containerRef(pod *api.BoundPod, container *api.Container) (*api.ObjectReference, error) {
|
||||
fieldPath, err := fieldPath(pod, container)
|
||||
if err != nil {
|
||||
// TODO: figure out intelligent way to refer to containers that we implicitly
|
||||
// start (like the network container). This is not a good way, ugh.
|
||||
fieldPath = "implicitly required container " + container.Name
|
||||
}
|
||||
ref, err := api.GetPartialReference(pod, fieldPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ref, nil
|
||||
}
|
||||
|
||||
// setRef stores a reference to a pod's container, associating it with the given docker id.
|
||||
func (kl *Kubelet) setRef(id dockertools.DockerID, ref *api.ObjectReference) {
|
||||
kl.refLock.Lock()
|
||||
defer kl.refLock.Unlock()
|
||||
kl.dockerIDToRef[id] = ref
|
||||
}
|
||||
|
||||
// clearRef forgets the given docker id and its associated container reference.
|
||||
func (kl *Kubelet) clearRef(id dockertools.DockerID) {
|
||||
kl.refLock.Lock()
|
||||
defer kl.refLock.Unlock()
|
||||
delete(kl.dockerIDToRef, id)
|
||||
}
|
||||
|
||||
// getRef returns the container reference of the given id, or (nil, false) if none is stored.
|
||||
func (kl *Kubelet) getRef(id dockertools.DockerID) (ref *api.ObjectReference, ok bool) {
|
||||
kl.refLock.RLock()
|
||||
defer kl.refLock.RUnlock()
|
||||
ref, ok = kl.dockerIDToRef[id]
|
||||
return ref, ok
|
||||
}
|
||||
|
||||
// Run a single container from a pod. Returns the docker container ID
|
||||
func (kl *Kubelet) runContainer(pod *api.BoundPod, container *api.Container, podVolumes volumeMap, netMode string) (id dockertools.DockerID, err error) {
|
||||
envVariables := makeEnvironmentVariables(container)
|
||||
@ -416,7 +475,19 @@ func (kl *Kubelet) runContainer(pod *api.BoundPod, container *api.Container, pod
|
||||
NetworkMode: netMode,
|
||||
Privileged: privileged,
|
||||
})
|
||||
if err == nil && container.Lifecycle != nil && container.Lifecycle.PostStart != nil {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if ref, err := containerRef(pod, container); err != nil {
|
||||
glog.Errorf("Couldn't make a ref to pod %v, container %v: '%v'", pod.Name, container.Name, err)
|
||||
} else {
|
||||
// Remember this reference so we can report events about this container
|
||||
kl.setRef(dockertools.DockerID(dockerContainer.ID), ref)
|
||||
record.Eventf(ref, "", "running", "started", "Started with docker id %v", dockerContainer.ID)
|
||||
}
|
||||
|
||||
if container.Lifecycle != nil && container.Lifecycle.PostStart != nil {
|
||||
handlerErr := kl.runHandler(GetPodFullName(pod), pod.UID, container, container.Lifecycle.PostStart)
|
||||
if handlerErr != nil {
|
||||
kl.killContainerByID(dockerContainer.ID, "")
|
||||
@ -438,9 +509,13 @@ func (kl *Kubelet) killContainerByID(ID, name string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO(lavalamp): restore event logging:
|
||||
// podFullName, uuid, containerName, _ := dockertools.ParseDockerName(name)
|
||||
// kl.LogEvent(&api.Event{})
|
||||
ref, ok := kl.getRef(dockertools.DockerID(ID))
|
||||
if !ok {
|
||||
glog.Warningf("No ref for pod '%v' - '%v'", ID, name)
|
||||
} else {
|
||||
// TODO: pass reason down here, and state, or move this call up the stack.
|
||||
record.Eventf(ref, "", "killing", "Killing %v - %v", ID, name)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user