2022-11-01 19:03:23 +00:00
package service
import (
"context"
"fmt"
"io"
2023-01-11 21:34:27 +00:00
"github.com/hashicorp/go-cleanhttp"
2023-01-20 22:02:44 +00:00
"github.com/hashicorp/consul/api"
2023-01-11 21:34:27 +00:00
libcluster "github.com/hashicorp/consul/test/integration/consul-container/libs/cluster"
"github.com/hashicorp/consul/test/integration/consul-container/libs/utils"
2022-11-01 19:03:23 +00:00
)
2023-01-20 22:02:44 +00:00
const (
StaticServerServiceName = "static-server"
StaticClientServiceName = "static-client"
)
2023-01-11 21:34:27 +00:00
func CreateAndRegisterStaticServerAndSidecar ( node libcluster . Agent ) ( Service , Service , error ) {
// Do some trickery to ensure that partial completion is correctly torn
// down, but successful execution is not.
var deferClean utils . ResettableDefer
defer deferClean . Execute ( )
2023-01-20 22:02:44 +00:00
// Register the static-server service and sidecar first to prevent race with sidecar
// trying to get xDS before it's ready
2022-11-01 19:03:23 +00:00
req := & api . AgentServiceRegistration {
2023-01-20 22:02:44 +00:00
Name : StaticServerServiceName ,
2023-01-11 21:34:27 +00:00
Port : 8080 ,
2022-11-01 19:03:23 +00:00
Connect : & api . AgentServiceConnect {
SidecarService : & api . AgentServiceRegistration {
2023-01-11 21:34:27 +00:00
Proxy : & api . AgentServiceConnectProxyConfig { } ,
2022-11-01 19:03:23 +00:00
} ,
} ,
Check : & api . AgentServiceCheck {
2022-11-08 01:10:42 +00:00
Name : "Static Server Listening" ,
2023-01-11 21:34:27 +00:00
TCP : fmt . Sprintf ( "127.0.0.1:%d" , 8080 ) ,
2022-11-01 19:03:23 +00:00
Interval : "10s" ,
2022-11-08 01:10:42 +00:00
Status : api . HealthPassing ,
2022-11-01 19:03:23 +00:00
} ,
}
2023-01-20 22:02:44 +00:00
if err := node . GetClient ( ) . Agent ( ) . ServiceRegister ( req ) ; err != nil {
return nil , nil , err
}
// Create a service and proxy instance
serverService , err := NewExampleService ( context . Background ( ) , StaticServerServiceName , 8080 , 8079 , node )
2022-11-01 19:03:23 +00:00
if err != nil {
2023-01-20 22:02:44 +00:00
return nil , nil , err
2022-11-01 19:03:23 +00:00
}
2023-01-20 22:02:44 +00:00
deferClean . Add ( func ( ) {
_ = serverService . Terminate ( )
} )
serverConnectProxy , err := NewConnectService ( context . Background ( ) , fmt . Sprintf ( "%s-sidecar" , StaticServerServiceName ) , StaticServerServiceName , 8080 , node ) // bindPort not used
if err != nil {
return nil , nil , err
}
deferClean . Add ( func ( ) {
_ = serverConnectProxy . Terminate ( )
} )
2022-11-01 19:03:23 +00:00
2023-01-11 21:34:27 +00:00
// disable cleanup functions now that we have an object with a Terminate() function
deferClean . Reset ( )
2022-11-01 19:03:23 +00:00
return serverService , serverConnectProxy , nil
}
2023-01-11 21:34:27 +00:00
func CreateAndRegisterStaticClientSidecar (
node libcluster . Agent ,
peerName string ,
localMeshGateway bool ,
) ( * ConnectContainer , error ) {
// Do some trickery to ensure that partial completion is correctly torn
// down, but successful execution is not.
var deferClean utils . ResettableDefer
defer deferClean . Execute ( )
2022-11-01 19:03:23 +00:00
mgwMode := api . MeshGatewayModeRemote
if localMeshGateway {
mgwMode = api . MeshGatewayModeLocal
}
2023-01-20 22:02:44 +00:00
// Register the static-client service and sidecar first to prevent race with sidecar
// trying to get xDS before it's ready
2022-11-01 19:03:23 +00:00
req := & api . AgentServiceRegistration {
2023-01-20 22:02:44 +00:00
Name : StaticClientServiceName ,
2022-11-01 19:03:23 +00:00
Port : 8080 ,
Connect : & api . AgentServiceConnect {
SidecarService : & api . AgentServiceRegistration {
Proxy : & api . AgentServiceConnectProxyConfig {
2023-01-11 21:34:27 +00:00
Upstreams : [ ] api . Upstream { {
2023-01-20 22:02:44 +00:00
DestinationName : StaticServerServiceName ,
2023-01-11 21:34:27 +00:00
DestinationPeer : peerName ,
LocalBindAddress : "0.0.0.0" ,
LocalBindPort : 5000 ,
MeshGateway : api . MeshGatewayConfig {
Mode : mgwMode ,
2022-11-01 19:03:23 +00:00
} ,
2023-01-11 21:34:27 +00:00
} } ,
2022-11-01 19:03:23 +00:00
} ,
} ,
} ,
}
2023-01-20 22:02:44 +00:00
if err := node . GetClient ( ) . Agent ( ) . ServiceRegister ( req ) ; err != nil {
return nil , err
}
// Create a service and proxy instance
clientConnectProxy , err := NewConnectService ( context . Background ( ) , fmt . Sprintf ( "%s-sidecar" , StaticClientServiceName ) , StaticClientServiceName , 5000 , node )
2022-11-01 19:03:23 +00:00
if err != nil {
2023-01-20 22:02:44 +00:00
return nil , err
2022-11-01 19:03:23 +00:00
}
2023-01-20 22:02:44 +00:00
deferClean . Add ( func ( ) {
_ = clientConnectProxy . Terminate ( )
} )
2022-11-01 19:03:23 +00:00
2023-01-11 21:34:27 +00:00
// disable cleanup functions now that we have an object with a Terminate() function
deferClean . Reset ( )
2022-11-01 19:03:23 +00:00
return clientConnectProxy , nil
}
2023-01-18 21:13:55 +00:00
func GetEnvoyConfigDump ( port int , filter string ) ( string , error ) {
2023-01-11 21:34:27 +00:00
client := cleanhttp . DefaultClient ( )
2023-01-18 21:13:55 +00:00
url := fmt . Sprintf ( "http://localhost:%d/config_dump?%s" , port , filter )
2022-11-01 19:03:23 +00:00
res , err := client . Get ( url )
if err != nil {
return "" , err
}
defer res . Body . Close ( )
body , err := io . ReadAll ( res . Body )
if err != nil {
return "" , err
}
return string ( body ) , nil
}
2023-01-25 17:27:36 +00:00
func GetEnvoyClusters ( port int ) ( string , error ) {
client := cleanhttp . DefaultClient ( )
url := fmt . Sprintf ( "http://localhost:%d/clusters?format=json" , port )
res , err := client . Get ( url )
if err != nil {
return "" , err
}
defer res . Body . Close ( )
body , err := io . ReadAll ( res . Body )
if err != nil {
return "" , err
}
return string ( body ) , nil
}