diff --git a/pkg/kubelet/rkt/log.go b/pkg/kubelet/rkt/log.go new file mode 100644 index 00000000000..29888661946 --- /dev/null +++ b/pkg/kubelet/rkt/log.go @@ -0,0 +1,50 @@ +/* +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 rkt + +import ( + "io" + "os/exec" + "strconv" + + kubecontainer "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container" +) + +// GetContainerLogs uses journalctl to get the logs of the container. +// By default, it returns a snapshot of the container log. Set |follow| to true to +// stream the log. Set |follow| to false and specify the number of lines (e.g. +// "100" or "all") to tail the log. +// TODO(yifan): Currently, it fetches all the containers' log within a pod. We will +// be able to fetch individual container's log once https://github.com/coreos/rkt/pull/841 +// landed. +func (r *Runtime) GetContainerLogs(pod kubecontainer.Pod, tail string, follow bool, stdout, stderr io.Writer) error { + unitName := makePodServiceFileName(pod.ID) + cmd := exec.Command("journalctl", "-u", unitName) + if follow { + cmd.Args = append(cmd.Args, "-f") + } + if tail == "all" { + cmd.Args = append(cmd.Args, "-a") + } else { + _, err := strconv.Atoi(tail) + if err == nil { + cmd.Args = append(cmd.Args, "-n", tail) + } + } + cmd.Stdout, cmd.Stderr = stdout, stderr + return cmd.Start() +} diff --git a/pkg/kubelet/rkt/rkt.go b/pkg/kubelet/rkt/rkt.go index 2a4ac3e2589..078351c06cd 100644 --- a/pkg/kubelet/rkt/rkt.go +++ b/pkg/kubelet/rkt/rkt.go @@ -22,6 +22,7 @@ import ( "strings" "github.com/GoogleCloudPlatform/kubernetes/pkg/credentialprovider" + "github.com/GoogleCloudPlatform/kubernetes/pkg/types" "github.com/coreos/go-systemd/dbus" "github.com/golang/glog" ) @@ -142,3 +143,9 @@ func (r *Runtime) runCommand(args ...string) ([]string, error) { } return strings.Split(strings.TrimSpace(string(output)), "\n"), nil } + +// makePodServiceFileName constructs the unit file name for a pod using its UID. +func makePodServiceFileName(uid types.UID) string { + // TODO(yifan): Revisit this later, decide whether we want to use UID. + return fmt.Sprintf("%s_%s.service", kubernetesUnitPrefix, uid) +}