restart plugin: support binary log uri
Introduce LogURIGenerator helper function in cio package. It is used in the restart options, like WithBinaryLogURI and WithFileLogURI. And restart.LogPathLabel might be used in production and work well. In order to reduce breaking change, the LogPathLabel is still recognized if new LogURILabel is not set. In next release 1.5, the LogPathLabel will be removed. Signed-off-by: Wei Fu <fuweid89@gmail.com>
This commit is contained in:
51
cio/io.go
51
cio/io.go
@@ -245,19 +245,11 @@ func LogURI(uri *url.URL) Creator {
|
||||
// BinaryIO forwards container STDOUT|STDERR directly to a logging binary
|
||||
func BinaryIO(binary string, args map[string]string) Creator {
|
||||
return func(_ string) (IO, error) {
|
||||
binary = filepath.Clean(binary)
|
||||
if !strings.HasPrefix(binary, "/") {
|
||||
return nil, errors.New("absolute path needed")
|
||||
uri, err := LogURIGenerator("binary", binary, args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
uri := &url.URL{
|
||||
Scheme: "binary",
|
||||
Path: binary,
|
||||
}
|
||||
q := uri.Query()
|
||||
for k, v := range args {
|
||||
q.Set(k, v)
|
||||
}
|
||||
uri.RawQuery = q.Encode()
|
||||
|
||||
res := uri.String()
|
||||
return &logURI{
|
||||
config: Config{
|
||||
@@ -272,14 +264,11 @@ func BinaryIO(binary string, args map[string]string) Creator {
|
||||
// If the log file already exists, the logs will be appended to the file.
|
||||
func LogFile(path string) Creator {
|
||||
return func(_ string) (IO, error) {
|
||||
path = filepath.Clean(path)
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
return nil, errors.New("absolute path needed")
|
||||
}
|
||||
uri := &url.URL{
|
||||
Scheme: "file",
|
||||
Path: path,
|
||||
uri, err := LogURIGenerator("file", path, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := uri.String()
|
||||
return &logURI{
|
||||
config: Config{
|
||||
@@ -290,6 +279,30 @@ func LogFile(path string) Creator {
|
||||
}
|
||||
}
|
||||
|
||||
// LogURIGenerator is the helper to generate log uri with specific scheme.
|
||||
func LogURIGenerator(scheme string, path string, args map[string]string) (*url.URL, error) {
|
||||
path = filepath.Clean(path)
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
return nil, errors.New("absolute path needed")
|
||||
}
|
||||
|
||||
uri := &url.URL{
|
||||
Scheme: scheme,
|
||||
Path: path,
|
||||
}
|
||||
|
||||
if len(args) == 0 {
|
||||
return uri, nil
|
||||
}
|
||||
|
||||
q := uri.Query()
|
||||
for k, v := range args {
|
||||
q.Set(k, v)
|
||||
}
|
||||
uri.RawQuery = q.Encode()
|
||||
return uri, nil
|
||||
}
|
||||
|
||||
type logURI struct {
|
||||
config Config
|
||||
}
|
||||
|
||||
@@ -195,3 +195,47 @@ func TestLogFileFailOnRelativePath(t *testing.T) {
|
||||
_, err := LogFile("./file.txt")("!")
|
||||
assert.Error(t, err, "absolute path needed")
|
||||
}
|
||||
|
||||
func TestLogURIGenerator(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
scheme string
|
||||
path string
|
||||
args map[string]string
|
||||
expected string
|
||||
err string
|
||||
}{
|
||||
{
|
||||
scheme: "fifo",
|
||||
path: "/full/path/pipe.fifo",
|
||||
expected: "fifo:///full/path/pipe.fifo",
|
||||
},
|
||||
{
|
||||
scheme: "file",
|
||||
path: "/full/path/file.txt",
|
||||
args: map[string]string{
|
||||
"maxSize": "100MB",
|
||||
},
|
||||
expected: "file:///full/path/file.txt?maxSize=100MB",
|
||||
},
|
||||
{
|
||||
scheme: "binary",
|
||||
path: "/full/path/bin",
|
||||
args: map[string]string{
|
||||
"id": "testing",
|
||||
},
|
||||
expected: "binary:///full/path/bin?id=testing",
|
||||
},
|
||||
{
|
||||
scheme: "unknown",
|
||||
path: "nowhere",
|
||||
err: "absolute path needed",
|
||||
},
|
||||
} {
|
||||
uri, err := LogURIGenerator(tc.scheme, tc.path, tc.args)
|
||||
if err != nil {
|
||||
assert.Error(t, err, tc.err)
|
||||
continue
|
||||
}
|
||||
assert.Equal(t, tc.expected, uri.String())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user