This commit is contained in:
Lorenzo Fontana 2018-01-21 14:11:46 +00:00 committed by GitHub
commit e2e4107ee3
4 changed files with 33 additions and 25 deletions

View file

@ -1,5 +1,8 @@
package main package main
// #include "../../conmon/config.h"
import "C"
import ( import (
"context" "context"
"fmt" "fmt"
@ -40,9 +43,9 @@ func validateConfig(config *server.Config) error {
} }
// This needs to match the read buffer size in conmon bufSize := int64(C.BUF_SIZE)
if config.LogSizeMax >= 0 && config.LogSizeMax < 8192 { if config.LogSizeMax >= 0 && config.LogSizeMax < bufSize {
return fmt.Errorf("log size max should be negative or >= 8192") return fmt.Errorf("log size max should be negative or >= %d", bufSize)
} }
return nil return nil
} }

18
conmon/config.h Normal file
View file

@ -0,0 +1,18 @@
#pragma once
#if !defined(CONFIG_H)
#define CONFIG_H
#define BUF_SIZE 8192
#define STDIO_BUF_SIZE 8192
/* stdpipe_t represents one of the std pipes (or NONE). */
typedef enum {
NO_PIPE,
STDIN_PIPE, /* unused */
STDOUT_PIPE,
STDERR_PIPE,
} stdpipe_t;
#endif // CONFIG_H

View file

@ -26,6 +26,7 @@
#include <glib-unix.h> #include <glib-unix.h>
#include "cmsg.h" #include "cmsg.h"
#include "config.h"
#define pexit(fmt, ...) \ #define pexit(fmt, ...) \
do { \ do { \
@ -91,7 +92,6 @@ static inline void strv_cleanup(char ***strv)
#define _cleanup_gstring_ _cleanup_(gstring_free_cleanup) #define _cleanup_gstring_ _cleanup_(gstring_free_cleanup)
#define _cleanup_strv_ _cleanup_(strv_cleanup) #define _cleanup_strv_ _cleanup_(strv_cleanup)
#define BUF_SIZE 8192
#define CMD_SIZE 1024 #define CMD_SIZE 1024
#define MAX_EVENTS 10 #define MAX_EVENTS 10
@ -263,15 +263,6 @@ int set_k8s_timestamp(char *buf, ssize_t buflen, const char *pipename)
return err; return err;
} }
/* stdpipe_t represents one of the std pipes (or NONE).
* Sync with const in container_attach.go */
typedef enum {
NO_PIPE,
STDIN_PIPE, /* unused */
STDOUT_PIPE,
STDERR_PIPE,
} stdpipe_t;
const char *stdpipe_name(stdpipe_t pipe) const char *stdpipe_name(stdpipe_t pipe)
{ {
switch (pipe) { switch (pipe) {
@ -571,11 +562,10 @@ static gboolean tty_hup_timeout_cb (G_GNUC_UNUSED gpointer user_data)
static bool read_stdio(int fd, stdpipe_t pipe, bool *eof) static bool read_stdio(int fd, stdpipe_t pipe, bool *eof)
{ {
#define STDIO_BUF_SIZE 8192 /* Sync with redirectResponseToOutputStreams() */
/* We use one extra byte at the start, which we don't read into, instead /* We use one extra byte at the start, which we don't read into, instead
we use that for marking the pipe when we write to the attached socket */ we use that for marking the pipe when we write to the attached socket */
char real_buf[STDIO_BUF_SIZE + 1]; char real_buf[STDIO_BUF_SIZE + 1];
char *buf = real_buf + 1; char *buf = real_buf + 1;
ssize_t num_read = 0; ssize_t num_read = 0;
if (eof) if (eof)

View file

@ -1,5 +1,8 @@
package server package server
// #include "../conmon/config.h"
import "C"
import ( import (
"fmt" "fmt"
"io" "io"
@ -18,13 +21,6 @@ import (
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
) )
/* Sync with stdpipe_t in conmon.c */
const (
AttachPipeStdin = 1
AttachPipeStdout = 2
AttachPipeStderr = 3
)
// Attach prepares a streaming endpoint to attach to a running container. // Attach prepares a streaming endpoint to attach to a running container.
func (s *Server) Attach(ctx context.Context, req *pb.AttachRequest) (resp *pb.AttachResponse, err error) { func (s *Server) Attach(ctx context.Context, req *pb.AttachRequest) (resp *pb.AttachResponse, err error) {
const operation = "attach" const operation = "attach"
@ -113,16 +109,17 @@ func (ss streamService) Attach(containerID string, inputStream io.Reader, output
} }
func redirectResponseToOutputStreams(outputStream, errorStream io.Writer, conn io.Reader) error { func redirectResponseToOutputStreams(outputStream, errorStream io.Writer, conn io.Reader) error {
stdioBufSize := int(C.STDIO_BUF_SIZE)
var err error var err error
buf := make([]byte, 8192+1) /* Sync with conmon STDIO_BUF_SIZE */ buf := make([]byte, stdioBufSize+1)
for { for {
nr, er := conn.Read(buf) nr, er := conn.Read(buf)
if nr > 0 { if nr > 0 {
var dst io.Writer var dst io.Writer
if buf[0] == AttachPipeStdout { if buf[0] == byte(C.STDOUT_PIPE) {
dst = outputStream dst = outputStream
} else if buf[0] == AttachPipeStderr { } else if buf[0] == byte(C.STDERR_PIPE) {
dst = errorStream dst = errorStream
} else { } else {
logrus.Infof("Got unexpected attach type %+d", buf[0]) logrus.Infof("Got unexpected attach type %+d", buf[0])