github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/stream/server.go (about)

     1  /*
     2  Copyright 2017 Mirantis
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package stream
    18  
    19  import (
    20  	"fmt"
    21  	"net"
    22  	"os"
    23  	"strconv"
    24  	"syscall"
    25  
    26  	"github.com/Mirantis/virtlet/pkg/metadata"
    27  
    28  	"github.com/golang/glog"
    29  	knet "k8s.io/apimachinery/pkg/util/net"
    30  	"k8s.io/kubernetes/pkg/kubelet/server/streaming"
    31  )
    32  
    33  // Server implements streaming.Runtime
    34  type Server struct {
    35  	DeadlineSeconds int
    36  	exitMonitorChan chan struct{}
    37  
    38  	unixServer *UnixServer
    39  
    40  	streamServer        streaming.Server
    41  	streamServerCloseCh chan struct{}
    42  	streaming.Runtime
    43  
    44  	metadataStore metadata.Store //required for port-forward
    45  }
    46  
    47  var _ streaming.Runtime = (*Server)(nil)
    48  
    49  // NewServer creates a new Server
    50  func NewServer(socketPath string, metadataStore metadata.Store, iStreamPort int) (*Server, error) {
    51  	s := &Server{DeadlineSeconds: 10}
    52  
    53  	// Prepare unix server
    54  	s.unixServer = NewUnixServer(socketPath)
    55  
    56  	bindAddress, err := knet.ChooseBindAddress(net.IP{0, 0, 0, 0})
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  	streamPort := strconv.Itoa(iStreamPort)
    61  
    62  	streamServerConfig := streaming.DefaultConfig
    63  	streamServerConfig.Addr = net.JoinHostPort(bindAddress.String(), streamPort)
    64  	s.streamServer, err = streaming.NewServer(streamServerConfig, s)
    65  	if err != nil {
    66  		return nil, fmt.Errorf("unable to create streaming server")
    67  	}
    68  
    69  	s.metadataStore = metadataStore
    70  
    71  	return s, nil
    72  }
    73  
    74  // Start starts streaming server gorutine and unixServer gorutine
    75  func (s *Server) Start() error {
    76  	if err := syscall.Unlink(s.unixServer.SocketPath); err != nil && !os.IsNotExist(err) {
    77  		return err
    78  	}
    79  	// start http server
    80  	go func() {
    81  		if err := s.streamServer.Start(true); err != nil {
    82  			glog.Fatalf("Failed to start streaming server: %v", err)
    83  		}
    84  	}()
    85  	// start socket server
    86  	go s.unixServer.Listen()
    87  	return nil
    88  }
    89  
    90  // Stop stops all goroutines
    91  func (s *Server) Stop() {
    92  	// in k8s 1.7 Stop() does nothing, starting from 1.8 it will stop streaming server
    93  	s.streamServer.Stop()
    94  	s.unixServer.Stop()
    95  }