istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/echo/server/endpoint/instance.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package endpoint
    16  
    17  import (
    18  	"fmt"
    19  	"io"
    20  
    21  	"istio.io/istio/pkg/config/protocol"
    22  	"istio.io/istio/pkg/test/echo/common"
    23  )
    24  
    25  // IsServerReadyFunc is a function that indicates whether the server is currently ready to handle traffic.
    26  type IsServerReadyFunc func() bool
    27  
    28  // OnReadyFunc is a callback function that informs the server that the endpoint is ready.
    29  type OnReadyFunc func()
    30  
    31  // Config for a single endpoint Instance.
    32  type Config struct {
    33  	IsServerReady IsServerReadyFunc
    34  	Version       string
    35  	Cluster       string
    36  	TLSCert       string
    37  	TLSKey        string
    38  	UDSServer     string
    39  	Dialer        common.Dialer
    40  	Port          *common.Port
    41  	ListenerIP    string
    42  	IstioVersion  string
    43  	Namespace     string
    44  	DisableALPN   bool
    45  }
    46  
    47  // Instance of an endpoint that serves the Echo application on a single port/protocol.
    48  type Instance interface {
    49  	io.Closer
    50  	Start(onReady OnReadyFunc) error
    51  	GetConfig() Config
    52  }
    53  
    54  // New creates a new endpoint Instance.
    55  func New(cfg Config) (Instance, error) {
    56  	if cfg.Port != nil {
    57  		switch cfg.Port.Protocol {
    58  		case protocol.HBONE:
    59  			return newHBONE(cfg), nil
    60  		case protocol.HTTP, protocol.HTTPS:
    61  			return newHTTP(cfg), nil
    62  		case protocol.HTTP2, protocol.GRPC:
    63  			return newGRPC(cfg), nil
    64  		case protocol.TCP:
    65  			return newTCP(cfg), nil
    66  		case protocol.UDP:
    67  			return newUDP(cfg), nil
    68  		default:
    69  			return nil, fmt.Errorf("unsupported protocol: %s", cfg.Port.Protocol)
    70  		}
    71  	}
    72  
    73  	if len(cfg.UDSServer) > 0 {
    74  		return newHTTP(cfg), nil
    75  	}
    76  
    77  	return nil, fmt.Errorf("either port or UDS must be specified")
    78  }