Merge pull request #3356 from mxpv/binary-io-path

BinaryIO/LogFile creator bug fixing
This commit is contained in:
Wei Fu 2019-06-20 10:25:47 +08:00 committed by GitHub
commit 111b082e20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 10 deletions

View File

@ -18,10 +18,13 @@ package cio
import (
"context"
"errors"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"github.com/containerd/containerd/defaults"
@ -242,17 +245,24 @@ 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 := &url.URL{
Scheme: "binary",
Host: binary,
Path: binary,
}
q := uri.Query()
for k, v := range args {
uri.Query().Set(k, v)
q.Set(k, v)
}
uri.RawQuery = q.Encode()
res := uri.String()
return &logURI{
config: Config{
Stdout: uri.String(),
Stderr: uri.String(),
Stdout: res,
Stderr: res,
},
}, nil
}
@ -262,14 +272,19 @@ 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",
Host: path,
Path: path,
}
res := uri.String()
return &logURI{
config: Config{
Stdout: uri.String(),
Stderr: uri.String(),
Stdout: res,
Stderr: res,
},
}, nil
}

View File

@ -23,6 +23,7 @@ import (
"context"
"io"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"runtime"
@ -153,3 +154,44 @@ func initProducers(t *testing.T, producers producers, stdout, stderr string) {
assert.NilError(t, err)
assert.NilError(t, producers.Stderr.Close())
}
func TestBinaryIOArgs(t *testing.T) {
res, err := BinaryIO("/file.bin", map[string]string{"id": "1"})("")
assert.NilError(t, err)
assert.Equal(t, "binary:///file.bin?id=1", res.Config().Stdout)
assert.Equal(t, "binary:///file.bin?id=1", res.Config().Stderr)
}
func TestBinaryIOAbsolutePath(t *testing.T) {
res, err := BinaryIO("/full/path/bin", nil)("!")
assert.NilError(t, err)
// Test parse back
parsed, err := url.Parse(res.Config().Stdout)
assert.NilError(t, err)
assert.Equal(t, "binary", parsed.Scheme)
assert.Equal(t, "/full/path/bin", parsed.Path)
}
func TestBinaryIOFailOnRelativePath(t *testing.T) {
_, err := BinaryIO("./bin", nil)("!")
assert.Error(t, err, "absolute path needed")
}
func TestLogFileAbsolutePath(t *testing.T) {
res, err := LogFile("/full/path/file.txt")("!")
assert.NilError(t, err)
assert.Equal(t, "file:///full/path/file.txt", res.Config().Stdout)
assert.Equal(t, "file:///full/path/file.txt", res.Config().Stderr)
// Test parse back
parsed, err := url.Parse(res.Config().Stdout)
assert.NilError(t, err)
assert.Equal(t, "file", parsed.Scheme)
assert.Equal(t, "/full/path/file.txt", parsed.Path)
}
func TestLogFileFailOnRelativePath(t *testing.T) {
_, err := LogFile("./file.txt")("!")
assert.Error(t, err, "absolute path needed")
}

View File

@ -103,11 +103,11 @@ func createIO(ctx context.Context, id string, ioUID, ioGID int, stdio proc.Stdio
case "binary":
pio.io, err = NewBinaryIO(ctx, id, u)
case "file":
if err := os.MkdirAll(filepath.Dir(u.Host), 0755); err != nil {
if err := os.MkdirAll(filepath.Dir(u.Path), 0755); err != nil {
return nil, err
}
var f *os.File
f, err = os.OpenFile(u.Host, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
f, err = os.OpenFile(u.Path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, err
}
@ -265,7 +265,7 @@ func NewBinaryIO(ctx context.Context, id string, uri *url.URL) (runc.IO, error)
}
}
ctx, cancel := context.WithCancel(ctx)
cmd := exec.CommandContext(ctx, uri.Host, args...)
cmd := exec.CommandContext(ctx, uri.Path, args...)
cmd.Env = append(cmd.Env,
"CONTAINER_ID="+id,
"CONTAINER_NAMESPACE="+ns,