Merge pull request #3356 from mxpv/binary-io-path
BinaryIO/LogFile creator bug fixing
This commit is contained in:
commit
111b082e20
29
cio/io.go
29
cio/io.go
@ -18,10 +18,13 @@ package cio
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/containerd/containerd/defaults"
|
"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
|
// BinaryIO forwards container STDOUT|STDERR directly to a logging binary
|
||||||
func BinaryIO(binary string, args map[string]string) Creator {
|
func BinaryIO(binary string, args map[string]string) Creator {
|
||||||
return func(_ string) (IO, error) {
|
return func(_ string) (IO, error) {
|
||||||
|
binary = filepath.Clean(binary)
|
||||||
|
if !strings.HasPrefix(binary, "/") {
|
||||||
|
return nil, errors.New("absolute path needed")
|
||||||
|
}
|
||||||
uri := &url.URL{
|
uri := &url.URL{
|
||||||
Scheme: "binary",
|
Scheme: "binary",
|
||||||
Host: binary,
|
Path: binary,
|
||||||
}
|
}
|
||||||
|
q := uri.Query()
|
||||||
for k, v := range args {
|
for k, v := range args {
|
||||||
uri.Query().Set(k, v)
|
q.Set(k, v)
|
||||||
}
|
}
|
||||||
|
uri.RawQuery = q.Encode()
|
||||||
|
res := uri.String()
|
||||||
return &logURI{
|
return &logURI{
|
||||||
config: Config{
|
config: Config{
|
||||||
Stdout: uri.String(),
|
Stdout: res,
|
||||||
Stderr: uri.String(),
|
Stderr: res,
|
||||||
},
|
},
|
||||||
}, nil
|
}, 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.
|
// If the log file already exists, the logs will be appended to the file.
|
||||||
func LogFile(path string) Creator {
|
func LogFile(path string) Creator {
|
||||||
return func(_ string) (IO, error) {
|
return func(_ string) (IO, error) {
|
||||||
|
path = filepath.Clean(path)
|
||||||
|
if !strings.HasPrefix(path, "/") {
|
||||||
|
return nil, errors.New("absolute path needed")
|
||||||
|
}
|
||||||
uri := &url.URL{
|
uri := &url.URL{
|
||||||
Scheme: "file",
|
Scheme: "file",
|
||||||
Host: path,
|
Path: path,
|
||||||
}
|
}
|
||||||
|
res := uri.String()
|
||||||
return &logURI{
|
return &logURI{
|
||||||
config: Config{
|
config: Config{
|
||||||
Stdout: uri.String(),
|
Stdout: res,
|
||||||
Stderr: uri.String(),
|
Stderr: res,
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -153,3 +154,44 @@ func initProducers(t *testing.T, producers producers, stdout, stderr string) {
|
|||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
assert.NilError(t, producers.Stderr.Close())
|
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")
|
||||||
|
}
|
||||||
|
@ -103,11 +103,11 @@ func createIO(ctx context.Context, id string, ioUID, ioGID int, stdio proc.Stdio
|
|||||||
case "binary":
|
case "binary":
|
||||||
pio.io, err = NewBinaryIO(ctx, id, u)
|
pio.io, err = NewBinaryIO(ctx, id, u)
|
||||||
case "file":
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
var f *os.File
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
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)
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
cmd := exec.CommandContext(ctx, uri.Host, args...)
|
cmd := exec.CommandContext(ctx, uri.Path, args...)
|
||||||
cmd.Env = append(cmd.Env,
|
cmd.Env = append(cmd.Env,
|
||||||
"CONTAINER_ID="+id,
|
"CONTAINER_ID="+id,
|
||||||
"CONTAINER_NAMESPACE="+ns,
|
"CONTAINER_NAMESPACE="+ns,
|
||||||
|
Loading…
Reference in New Issue
Block a user