Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 20ec3f5

Browse files
committed
Link container logs
1 parent 3b34cb9 commit 20ec3f5

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

pkg/kubelet/hyper/hyper.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ const (
5656
hyperDefaultContainerCPU = 1
5757
hyperDefaultContainerMem = 128
5858
hyperPodSpecDir = "/var/lib/kubelet/hyper"
59+
hyperLogsDir = "/var/run/hyper/Pods"
5960
minimumGracePeriodInSeconds = 2
6061
)
6162

6263
// runtime implements the container runtime for hyper
6364
type runtime struct {
6465
hyperBinAbsPath string
6566
dockerKeyring credentialprovider.DockerKeyring
67+
containerLogsDir string
6668
containerRefManager *kubecontainer.RefManager
6769
generator kubecontainer.RunContainerOptionsGenerator
6870
recorder record.EventRecorder
@@ -72,6 +74,7 @@ type runtime struct {
7274
hyperClient *HyperClient
7375
kubeClient client.Interface
7476
imagePuller kubecontainer.ImagePuller
77+
os kubecontainer.OSInterface
7578
version kubecontainer.Version
7679

7780
// Disable the internal haproxy service in Hyper pods
@@ -99,6 +102,8 @@ func New(generator kubecontainer.RunContainerOptionsGenerator,
99102
serializeImagePulls bool,
100103
httpClient kubetypes.HttpGetter,
101104
disableHyperInternalService bool,
105+
containerLogsDir string,
106+
os kubecontainer.OSInterface,
102107
) (kubecontainer.Runtime, error) {
103108
// check hyper has already installed
104109
hyperBinAbsPath, err := exec.LookPath(hyperBinName)
@@ -110,9 +115,11 @@ func New(generator kubecontainer.RunContainerOptionsGenerator,
110115
hyper := &runtime{
111116
hyperBinAbsPath: hyperBinAbsPath,
112117
dockerKeyring: credentialprovider.NewDockerKeyring(),
118+
containerLogsDir: containerLogsDir,
113119
containerRefManager: containerRefManager,
114120
generator: generator,
115121
livenessManager: livenessManager,
122+
os: os,
116123
recorder: recorder,
117124
networkPlugin: networkPlugin,
118125
volumeGetter: volumeGetter,
@@ -731,13 +738,25 @@ func (r *runtime) RunPod(pod *api.Pod, restartCount int, pullSecrets []api.Secre
731738
}
732739
}
733740

741+
// Update container references
734742
ref, err := kubecontainer.GenerateContainerRef(pod, &container)
735743
if err != nil {
736744
glog.Errorf("Couldn't make a ref to pod %q, container %v: '%v'", pod.Name, container.Name, err)
737745
} else {
738746
r.containerRefManager.SetRef(containerID, ref)
739747
}
740748

749+
// Create a symbolic link to the Hyper container log file using a name
750+
// which captures the full pod name, the container name and the
751+
// container ID. Cluster level logging will capture these symbolic
752+
// filenames which can be used for search terms in Elasticsearch or for
753+
// labels for Cloud Logging.
754+
containerLogFile := path.Join(hyperLogsDir, podID, fmt.Sprintf("%s-json.log", containerID.ID))
755+
symlinkFile := LogSymlink(r.containerLogsDir, podFullName, container.Name, containerID.ID)
756+
if err = r.os.Symlink(containerLogFile, symlinkFile); err != nil {
757+
glog.Errorf("Failed to create symbolic link to the log file of pod %q container %q: %v", podFullName, container.Name, err)
758+
}
759+
741760
if container.Lifecycle != nil && container.Lifecycle.PostStart != nil {
742761
handlerErr := r.runner.Run(containerID, pod, &container, container.Lifecycle.PostStart)
743762
if handlerErr != nil {
@@ -901,6 +920,20 @@ func (r *runtime) KillPod(pod *api.Pod, runningPod kubecontainer.Pod) error {
901920
for _, podInfo := range podInfos {
902921
if podInfo.PodName == podName {
903922
podID = podInfo.PodID
923+
924+
// Remove log links
925+
for _, c := range podInfo.PodInfo.Status.Status {
926+
_, _, _, containerName, _, _, err := r.parseHyperContainerFullName(c.Name)
927+
if err != nil {
928+
continue
929+
}
930+
symlinkFile := LogSymlink(r.containerLogsDir, podName, containerName, c.ContainerID)
931+
err = os.Remove(symlinkFile)
932+
if err != nil && !os.IsNotExist(err) {
933+
glog.Warningf("Failed to remove container log symlink %q: %v", symlinkFile, err)
934+
}
935+
}
936+
904937
break
905938
}
906939
}
@@ -1267,6 +1300,20 @@ func (r *runtime) GarbageCollect(gcPolicy kubecontainer.ContainerGCPolicy) error
12671300
}
12681301

12691302
if lastTime.Before(time.Now().Add(-gcPolicy.MinAge)) {
1303+
// Remove log links
1304+
for _, c := range pod.PodInfo.Status.Status {
1305+
_, _, _, containerName, _, _, err := r.parseHyperContainerFullName(c.Name)
1306+
if err != nil {
1307+
continue
1308+
}
1309+
symlinkFile := LogSymlink(r.containerLogsDir, pod.PodName, containerName, c.ContainerID)
1310+
err = os.Remove(symlinkFile)
1311+
if err != nil && !os.IsNotExist(err) {
1312+
glog.Warningf("Failed to remove container log symlink %q: %v", symlinkFile, err)
1313+
}
1314+
}
1315+
1316+
// Remove the pod
12701317
cmds := append([]string{}, "rm", pod.PodID)
12711318
_, err = r.runCommand(cmds...)
12721319
if err != nil {
@@ -1290,3 +1337,8 @@ func (r *runtime) GetPodStatusAndAPIPodStatus(pod *api.Pod) (*kubecontainer.PodS
12901337
apiPodStatus, err := r.ConvertPodStatusToAPIPodStatus(pod, podStatus)
12911338
return podStatus, apiPodStatus, err
12921339
}
1340+
1341+
// LogSymlink generates symlink file path for specified container
1342+
func LogSymlink(containerLogsDir, podFullName, containerName, containerID string) string {
1343+
return path.Join(containerLogsDir, fmt.Sprintf("%s_%s-%s.log", podFullName, containerName, containerID))
1344+
}

pkg/kubelet/kubelet.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,8 @@ func NewMainKubelet(
406406
serializeImagePulls,
407407
klet.httpClient,
408408
klet.disableHyperInternalService,
409+
containerLogsDir,
410+
osInterface,
409411
)
410412
if err != nil {
411413
return nil, err

0 commit comments

Comments
 (0)