Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using unix domain sockets for docker syslog collector #897

Merged
merged 1 commit into from
Mar 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/driver/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (d *DockerDriver) createContainer(ctx *ExecContext, task *structs.Task,
LogConfig: docker.LogConfig{
Type: "syslog",
Config: map[string]string{
"syslog-address": fmt.Sprintf("tcp://%v", syslogAddr),
"syslog-address": syslogAddr,
},
},
}
Expand Down
34 changes: 33 additions & 1 deletion client/driver/logging/universal_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ package logging
import (
"fmt"
"io"
"io/ioutil"
"log"
"log/syslog"
"net"
"os"
"runtime"

"github.com/hashicorp/nomad/client/allocdir"
cstructs "github.com/hashicorp/nomad/client/driver/structs"
Expand Down Expand Up @@ -105,7 +108,8 @@ func (s *SyslogCollector) LaunchCollector(ctx *LogCollectorContext) (*SyslogColl
s.lre = lre

go s.collectLogs(lre, lro)
return &SyslogCollectorState{Addr: l.Addr().String()}, nil
syslogAddr := fmt.Sprintf("%s://%s", l.Addr().Network(), l.Addr().String())
return &SyslogCollectorState{Addr: syslogAddr}, nil
}

func (s *SyslogCollector) collectLogs(we io.Writer, wo io.Writer) {
Expand Down Expand Up @@ -160,6 +164,16 @@ func (s *SyslogCollector) configureTaskDir() error {
// getFreePort returns a free port ready to be listened on between upper and
// lower bounds
func (s *SyslogCollector) getListener(lowerBound uint, upperBound uint) (net.Listener, error) {
if runtime.GOOS == "windows" {
return s.listenerTCP(lowerBound, upperBound)
}

return s.listenerUnix()
}

// listenerTCP creates a TCP listener using an unused port between an upper and
// lower bound
func (s *SyslogCollector) listenerTCP(lowerBound uint, upperBound uint) (net.Listener, error) {
for i := lowerBound; i <= upperBound; i++ {
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("localhost:%v", i))
if err != nil {
Expand All @@ -173,3 +187,21 @@ func (s *SyslogCollector) getListener(lowerBound uint, upperBound uint) (net.Lis
}
return nil, fmt.Errorf("No free port found")
}

// listenerUnix creates a Unix domain socket
func (s *SyslogCollector) listenerUnix() (net.Listener, error) {
f, err := ioutil.TempFile("", "plugin")
if err != nil {
return nil, err
}
path := f.Name()

if err := f.Close(); err != nil {
return nil, err
}
if err := os.Remove(path); err != nil {
return nil, err
}

return net.Listen("unix", path)
}
9 changes: 6 additions & 3 deletions command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"os"
"path/filepath"
"runtime"
"sync"
"time"

Expand Down Expand Up @@ -260,9 +261,11 @@ func (a *Agent) setupClient() error {
return fmt.Errorf("client setup failed: %v", err)
}

// Reserve some ports for the plugins
if err := a.reservePortsForClient(conf); err != nil {
return err
// Reserve some ports for the plugins if we are on Windows
if runtime.GOOS == "windows" {
if err := a.reservePortsForClient(conf); err != nil {
return err
}
}

// Create the client
Expand Down