Skip to content

Commit

Permalink
Merge pull request #1060 from Random-Liu/support-stream-idle-timeout
Browse files Browse the repository at this point in the history
Support stream idle timeout.
  • Loading branch information
Random-Liu committed Feb 28, 2019
2 parents c0f4836 + 8222da7 commit f2f90f6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
6 changes: 6 additions & 0 deletions cri.go
Expand Up @@ -19,6 +19,7 @@ package cri
import (
"flag"
"path/filepath"
"time"

"github.com/containerd/containerd"
"github.com/containerd/containerd/api/services/containers/v1"
Expand Down Expand Up @@ -118,6 +119,11 @@ func validateConfig(c *criconfig.Config) error {
}
}

if c.StreamIdleTimeout != "" {
if _, err := time.ParseDuration(c.StreamIdleTimeout); err != nil {
return errors.Wrap(err, "invalid stream idle timeout")
}
}
return nil
}

Expand Down
6 changes: 6 additions & 0 deletions docs/config.md
Expand Up @@ -17,6 +17,12 @@ The explanation and default value of each configuration item are as follows:
# stream_server_port is the port streaming server is listening on.
stream_server_port = "0"

# stream_idle_timeout is the maximum time a streaming connection can be
# idle before the connection is automatically closed.
# The string is in the golang duration format, see:
# https://golang.org/pkg/time/#ParseDuration
stream_idle_timeout = "4h"

# enable_selinux indicates to enable the selinux support.
enable_selinux = false

Expand Down
7 changes: 7 additions & 0 deletions pkg/config/config.go
Expand Up @@ -19,6 +19,7 @@ package config
import (
"github.com/BurntSushi/toml"
"github.com/containerd/containerd"
"k8s.io/kubernetes/pkg/kubelet/server/streaming"
)

// Runtime struct to contain the type(ID), engine, and root variables for a default runtime
Expand Down Expand Up @@ -124,6 +125,11 @@ type PluginConfig struct {
StreamServerAddress string `toml:"stream_server_address" json:"streamServerAddress"`
// StreamServerPort is the port streaming server is listening on.
StreamServerPort string `toml:"stream_server_port" json:"streamServerPort"`
// StreamIdleTimeout is the maximum time a streaming connection
// can be idle before the connection is automatically closed.
// The string is in the golang duration format, see:
// https://golang.org/pkg/time/#ParseDuration
StreamIdleTimeout string `toml:"stream_idle_timeout" json:"streamIdleTimeout"`
// EnableSelinux indicates to enable the selinux support.
EnableSelinux bool `toml:"enable_selinux" json:"enableSelinux"`
// SandboxImage is the image used by sandbox container.
Expand Down Expand Up @@ -196,6 +202,7 @@ func DefaultConfig() PluginConfig {
},
StreamServerAddress: "127.0.0.1",
StreamServerPort: "0",
StreamIdleTimeout: streaming.DefaultConfig.StreamIdleTimeout.String(), // 4 hour
EnableSelinux: false,
EnableTLSStreaming: false,
X509KeyPairStreaming: X509KeyPairStreaming{
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/service.go
Expand Up @@ -159,7 +159,7 @@ func NewCRIService(config criconfig.Config, client *containerd.Client) (CRIServi
logrus.WithError(err).Error("Failed to load cni during init, please check CRI plugin status before setting up network for pods")
}
// prepare streaming server
c.streamServer, err = newStreamServer(c, config.StreamServerAddress, config.StreamServerPort)
c.streamServer, err = newStreamServer(c, config.StreamServerAddress, config.StreamServerPort, config.StreamIdleTimeout)
if err != nil {
return nil, errors.Wrap(err, "failed to create stream server")
}
Expand Down
10 changes: 9 additions & 1 deletion pkg/server/streaming.go
Expand Up @@ -22,6 +22,7 @@ import (
"math"
"net"
"os"
"time"

"github.com/pkg/errors"
k8snet "k8s.io/apimachinery/pkg/util/net"
Expand Down Expand Up @@ -64,7 +65,7 @@ func getStreamListenerMode(c *criService) (streamListenerMode, error) {
return withoutTLS, nil
}

func newStreamServer(c *criService, addr, port string) (streaming.Server, error) {
func newStreamServer(c *criService, addr, port, streamIdleTimeout string) (streaming.Server, error) {
if addr == "" {
a, err := k8snet.ChooseBindAddress(nil)
if err != nil {
Expand All @@ -73,6 +74,13 @@ func newStreamServer(c *criService, addr, port string) (streaming.Server, error)
addr = a.String()
}
config := streaming.DefaultConfig
if streamIdleTimeout != "" {
var err error
config.StreamIdleTimeout, err = time.ParseDuration(streamIdleTimeout)
if err != nil {
return nil, errors.Wrap(err, "invalid stream idle timeout")
}
}
config.Addr = net.JoinHostPort(addr, port)
run := newStreamRuntime(c)
tlsMode, err := getStreamListenerMode(c)
Expand Down

0 comments on commit f2f90f6

Please sign in to comment.