Signed-off-by: Jess Frazelle <jess@mesosphere.com>
This commit is contained in:
Jess Frazelle 2016-04-16 18:23:42 -07:00
parent e5b1eddadb
commit 0e4e8593ca
No known key found for this signature in database
GPG key ID: 18F3685C0022BFF3
13 changed files with 90 additions and 112 deletions

View file

@ -31,18 +31,15 @@ type Formatter interface {
// It's not exported because it's still using Data in an opinionated way. It's to
// avoid code duplication between the two default formatters.
func prefixFieldClashes(data Fields) {
_, ok := data["time"]
if ok {
data["fields.time"] = data["time"]
if t, ok := data["time"]; ok {
data["fields.time"] = t
}
_, ok = data["msg"]
if ok {
data["fields.msg"] = data["msg"]
if m, ok := data["msg"]; ok {
data["fields.msg"] = m
}
_, ok = data["level"]
if ok {
data["fields.level"] = data["level"]
if l, ok := data["level"]; ok {
data["fields.level"] = l
}
}

View file

@ -24,11 +24,13 @@ func (f *LogstashFormatter) Format(entry *logrus.Entry) ([]byte, error) {
fields["@version"] = 1
if f.TimestampFormat == "" {
f.TimestampFormat = logrus.DefaultTimestampFormat
timeStampFormat := f.TimestampFormat
if timeStampFormat == "" {
timeStampFormat = logrus.DefaultTimestampFormat
}
fields["@timestamp"] = entry.Time.Format(f.TimestampFormat)
fields["@timestamp"] = entry.Time.Format(timeStampFormat)
// set message field
v, ok := entry.Data["message"]

View file

@ -128,10 +128,10 @@ func needsQuoting(text string) bool {
(ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9') ||
ch == '-' || ch == '.') {
return false
return true
}
}
return true
return false
}
func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key string, value interface{}) {
@ -141,14 +141,14 @@ func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key string, value interf
switch value := value.(type) {
case string:
if needsQuoting(value) {
if !needsQuoting(value) {
b.WriteString(value)
} else {
fmt.Fprintf(b, "%q", value)
}
case error:
errmsg := value.Error()
if needsQuoting(errmsg) {
if !needsQuoting(errmsg) {
b.WriteString(errmsg)
} else {
fmt.Fprintf(b, "%q", value)

View file

@ -7,18 +7,40 @@ import (
)
func (logger *Logger) Writer() *io.PipeWriter {
return logger.WriterLevel(InfoLevel)
}
func (logger *Logger) WriterLevel(level Level) *io.PipeWriter {
reader, writer := io.Pipe()
go logger.writerScanner(reader)
var printFunc func(args ...interface{})
switch level {
case DebugLevel:
printFunc = logger.Debug
case InfoLevel:
printFunc = logger.Info
case WarnLevel:
printFunc = logger.Warn
case ErrorLevel:
printFunc = logger.Error
case FatalLevel:
printFunc = logger.Fatal
case PanicLevel:
printFunc = logger.Panic
default:
printFunc = logger.Print
}
go logger.writerScanner(reader, printFunc)
runtime.SetFinalizer(writer, writerFinalizer)
return writer
}
func (logger *Logger) writerScanner(reader *io.PipeReader) {
func (logger *Logger) writerScanner(reader *io.PipeReader, printFunc func(args ...interface{})) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
logger.Print(scanner.Text())
printFunc(scanner.Text())
}
if err := scanner.Err(); err != nil {
logger.Errorf("Error while reading from Writer: %s", err)

View file

@ -15,7 +15,6 @@ import (
"github.com/Sirupsen/logrus"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/user"
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
)
@ -111,14 +110,11 @@ func (m *Manager) Apply(pid int) (err error) {
var c = m.Cgroups
logrus.Debugf("pre get cgroups data: %#v", c)
d, err := getCgroupData(m.Cgroups, pid)
if err != nil {
return err
}
logrus.Debugf("cgroups data: %#v, config: %#v", d, d.config)
if c.Paths != nil {
paths := make(map[string]string)
for name, path := range c.Paths {
@ -132,7 +128,6 @@ func (m *Manager) Apply(pid int) (err error) {
paths[name] = path
}
m.Paths = paths
logrus.Debugf("cgroups apply paths: %#v", m.Paths)
return cgroups.EnterPid(m.Paths, pid)
}
@ -140,7 +135,6 @@ func (m *Manager) Apply(pid int) (err error) {
defer m.mu.Unlock()
paths := make(map[string]string)
for _, sys := range subsystems {
logrus.Debugf("applying cgroups to subsystem %#v", sys)
if err := sys.Apply(d); err != nil {
return err
}
@ -358,25 +352,9 @@ func writeFile(dir, file, data string) error {
if dir == "" {
return fmt.Errorf("no such directory for %s", file)
}
// get the current user
u, err := user.CurrentUser()
if err != nil {
return err
}
if err := os.Lchown(dir, u.Uid, u.Gid); err != nil {
return fmt.Errorf("failed to chown to %d:%d -> %v", u.Uid, u.Gid, err)
}
logrus.Debugf("chown dir %s to %d:%d", dir, u.Uid, u.Gid)
if err := os.Lchown(filepath.Join(dir, file), u.Uid, u.Gid); err != nil {
return fmt.Errorf("failed to chown to %d:%d -> %v", u.Uid, u.Gid, err)
}
logrus.Debugf("chown %s to %d:%d", filepath.Join(dir, file), u.Uid, u.Gid)
if err := ioutil.WriteFile(filepath.Join(dir, file), []byte(data), 0700); err != nil {
//return fmt.Errorf("failed to write %v to %v: %v", data, file, err)
logrus.Debugf("failed to write %v to %v: %v", data, file, err)
//return err
}
return nil
}

View file

@ -10,7 +10,6 @@ import (
"path/filepath"
"strconv"
"github.com/Sirupsen/logrus"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/configs"
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
@ -28,7 +27,6 @@ func (s *CpusetGroup) Apply(d *cgroupData) error {
if err != nil && !cgroups.IsNotFound(err) {
return err
}
logrus.Debugf("apply cpuset dir is %s", dir)
return s.ApplyDir(dir, d.config, d.pid)
}
@ -67,7 +65,6 @@ func (s *CpusetGroup) ApplyDir(dir string, cgroup *configs.Cgroup, pid int) erro
if err := s.ensureParent(dir, root); err != nil {
return err
}
// because we are not using d.join we need to place the pid into the procs file
// unlike the other subsystems
if err := writeFile(dir, "cgroup.procs", strconv.Itoa(pid)); err != nil {

View file

@ -13,9 +13,7 @@ import (
"strings"
"time"
"github.com/Sirupsen/logrus"
"github.com/docker/go-units"
"github.com/opencontainers/runc/libcontainer/user"
)
const cgroupNamePrefix = "name="
@ -291,21 +289,10 @@ func PathExists(path string) bool {
}
func EnterPid(cgroupPaths map[string]string, pid int) error {
// get the current user
u, err := user.CurrentUser()
if err != nil {
return err
}
for _, path := range cgroupPaths {
if PathExists(path) {
cgtasks := filepath.Join(path, "tasks")
// Chown the tasks file so that the user/group can enter the container.
if err := os.Chown(cgtasks, u.Uid, u.Gid); err != nil {
return fmt.Errorf("failed to chown %s to %d:%d -> %v", cgtasks, u.Uid, u.Gid, err)
}
if err := ioutil.WriteFile(filepath.Join(path, "cgroup.procs"),
[]byte(strconv.Itoa(pid)), 0700); err != nil {
logrus.Debug("cgroups/utils.go: ", err)
return err
}
}

View file

@ -171,7 +171,6 @@ func (c *linuxContainer) Set(config configs.Config) error {
c.m.Lock()
defer c.m.Unlock()
c.config = &config
logrus.Debugf("setting cgroups")
return c.cgroupManager.Set(c.config)
}
@ -742,7 +741,6 @@ func (c *linuxContainer) Restore(process *Process, criuOpts *CriuOpts) error {
}
func (c *linuxContainer) criuApplyCgroups(pid int, req *criurpc.CriuReq) error {
logrus.Debugf("criu apply cgroups")
if err := c.cgroupManager.Apply(pid); err != nil {
return err
}

View file

@ -13,7 +13,6 @@ import (
"strconv"
"syscall"
"github.com/Sirupsen/logrus"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/system"
@ -248,9 +247,6 @@ func (p *initProcess) start() error {
return newSystemError(err)
}
p.setExternalDescriptors(fds)
logrus.Debugf("starting process apply cgroups")
// Do this before syncing with child so that no children
// can escape the cgroup
if err := p.manager.Apply(p.pid()); err != nil {