mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-12-24 11:51:13 +08:00
191 lines
5.1 KiB
Go
191 lines
5.1 KiB
Go
package resources
|
|
|
|
import (
|
|
etmv3 "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
|
|
"google.golang.org/protobuf/types/known/anypb"
|
|
"google.golang.org/protobuf/types/known/durationpb"
|
|
"time"
|
|
|
|
cluster "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
|
|
core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
|
|
endpoint "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
|
|
listener "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
|
|
route "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
|
|
hcm "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3"
|
|
"github.com/envoyproxy/go-control-plane/pkg/resource/v3"
|
|
"github.com/envoyproxy/go-control-plane/pkg/wellknown"
|
|
)
|
|
|
|
func MakeCluster(clusterName string) *cluster.Cluster {
|
|
return &cluster.Cluster{
|
|
Name: clusterName,
|
|
ConnectTimeout: durationpb.New(5 * time.Second),
|
|
ClusterDiscoveryType: &cluster.Cluster_Type{Type: cluster.Cluster_EDS},
|
|
LbPolicy: cluster.Cluster_ROUND_ROBIN,
|
|
//LoadAssignment: makeEndpoint(clusterName, UpstreamHost),
|
|
DnsLookupFamily: cluster.Cluster_V4_ONLY,
|
|
EdsClusterConfig: makeEDSCluster(),
|
|
}
|
|
}
|
|
|
|
func makeEDSCluster() *cluster.Cluster_EdsClusterConfig {
|
|
return &cluster.Cluster_EdsClusterConfig{
|
|
EdsConfig: makeConfigSource(),
|
|
}
|
|
}
|
|
|
|
func MakeEndpoint(clusterName string, eps []Endpoint) *endpoint.ClusterLoadAssignment {
|
|
var endpoints []*endpoint.LbEndpoint
|
|
|
|
for _, e := range eps {
|
|
endpoints = append(endpoints, &endpoint.LbEndpoint{
|
|
HostIdentifier: &endpoint.LbEndpoint_Endpoint{
|
|
Endpoint: &endpoint.Endpoint{
|
|
Address: &core.Address{
|
|
Address: &core.Address_SocketAddress{
|
|
SocketAddress: &core.SocketAddress{
|
|
Protocol: core.SocketAddress_TCP,
|
|
Address: e.UpstreamHost,
|
|
PortSpecifier: &core.SocketAddress_PortValue{
|
|
PortValue: e.UpstreamPort,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
return &endpoint.ClusterLoadAssignment{
|
|
ClusterName: clusterName,
|
|
Endpoints: []*endpoint.LocalityLbEndpoints{{
|
|
LbEndpoints: endpoints,
|
|
}},
|
|
}
|
|
}
|
|
|
|
func MakeRoute(routes []Route) *route.RouteConfiguration {
|
|
var rts []*route.Route
|
|
|
|
for _, r := range routes {
|
|
var rr []*route.HeaderMatcher
|
|
for _, header := range r.Headers {
|
|
rr = append(rr, &route.HeaderMatcher{
|
|
Name: header.Key,
|
|
HeaderMatchSpecifier: &route.HeaderMatcher_StringMatch{
|
|
StringMatch: &etmv3.StringMatcher{
|
|
MatchPattern: &etmv3.StringMatcher_Contains{
|
|
Contains: header.Value,
|
|
},
|
|
IgnoreCase: true,
|
|
},
|
|
},
|
|
})
|
|
}
|
|
rts = append(rts, &route.Route{
|
|
//Name: r.Name,
|
|
Match: &route.RouteMatch{
|
|
PathSpecifier: &route.RouteMatch_Prefix{
|
|
Prefix: "/",
|
|
},
|
|
Headers: rr,
|
|
},
|
|
Action: &route.Route_Route{
|
|
Route: &route.RouteAction{
|
|
ClusterSpecifier: &route.RouteAction_Cluster{
|
|
Cluster: r.Cluster,
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
//rts = append(rts, &route.Route{
|
|
// //Name: r.Name,
|
|
// Match: &route.RouteMatch{
|
|
// PathSpecifier: &route.RouteMatch_Prefix{
|
|
// Prefix: "/",
|
|
// },
|
|
// },
|
|
// Action: &route.Route_Route{
|
|
// Route: &route.RouteAction{
|
|
// ClusterSpecifier: &route.RouteAction_Cluster{
|
|
// Cluster: "default_cluster",
|
|
// },
|
|
// },
|
|
// },
|
|
//})
|
|
|
|
return &route.RouteConfiguration{
|
|
Name: "listener_0",
|
|
VirtualHosts: []*route.VirtualHost{{
|
|
Name: "local_service",
|
|
Domains: []string{"*"},
|
|
Routes: rts,
|
|
}},
|
|
}
|
|
}
|
|
|
|
func MakeHTTPListener(listenerName, route, address string, port uint32) *listener.Listener {
|
|
// HTTP filter configuration
|
|
manager := &hcm.HttpConnectionManager{
|
|
CodecType: hcm.HttpConnectionManager_AUTO,
|
|
StatPrefix: "http",
|
|
RouteSpecifier: &hcm.HttpConnectionManager_Rds{
|
|
Rds: &hcm.Rds{
|
|
ConfigSource: makeConfigSource(),
|
|
RouteConfigName: "listener_0",
|
|
},
|
|
},
|
|
HttpFilters: []*hcm.HttpFilter{{
|
|
Name: wellknown.Router,
|
|
}},
|
|
}
|
|
pbst, err := anypb.New(manager)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return &listener.Listener{
|
|
Name: listenerName,
|
|
Address: &core.Address{
|
|
Address: &core.Address_SocketAddress{
|
|
SocketAddress: &core.SocketAddress{
|
|
Protocol: core.SocketAddress_TCP,
|
|
Address: address,
|
|
PortSpecifier: &core.SocketAddress_PortValue{
|
|
PortValue: port,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
FilterChains: []*listener.FilterChain{{
|
|
Filters: []*listener.Filter{{
|
|
Name: wellknown.HTTPConnectionManager,
|
|
ConfigType: &listener.Filter_TypedConfig{
|
|
TypedConfig: pbst,
|
|
},
|
|
}},
|
|
}},
|
|
}
|
|
}
|
|
|
|
func makeConfigSource() *core.ConfigSource {
|
|
source := &core.ConfigSource{}
|
|
source.ResourceApiVersion = resource.DefaultAPIVersion
|
|
source.ConfigSourceSpecifier = &core.ConfigSource_ApiConfigSource{
|
|
ApiConfigSource: &core.ApiConfigSource{
|
|
TransportApiVersion: resource.DefaultAPIVersion,
|
|
ApiType: core.ApiConfigSource_GRPC,
|
|
SetNodeOnFirstMessageOnly: true,
|
|
GrpcServices: []*core.GrpcService{{
|
|
TargetSpecifier: &core.GrpcService_EnvoyGrpc_{
|
|
EnvoyGrpc: &core.GrpcService_EnvoyGrpc{ClusterName: "xds_cluster"},
|
|
},
|
|
}},
|
|
},
|
|
}
|
|
return source
|
|
}
|