github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/go-control-plane/internal/example/resource.go (about)

     1  // Copyright 2020 Envoyproxy 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  package example
    15  
    16  import (
    17  	"time"
    18  
    19  	"github.com/golang/protobuf/ptypes"
    20  
    21  	cluster "github.com/hxx258456/ccgo/go-control-plane/envoy/config/cluster/v3"
    22  	core "github.com/hxx258456/ccgo/go-control-plane/envoy/config/core/v3"
    23  	endpoint "github.com/hxx258456/ccgo/go-control-plane/envoy/config/endpoint/v3"
    24  	listener "github.com/hxx258456/ccgo/go-control-plane/envoy/config/listener/v3"
    25  	route "github.com/hxx258456/ccgo/go-control-plane/envoy/config/route/v3"
    26  	hcm "github.com/hxx258456/ccgo/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3"
    27  	"github.com/hxx258456/ccgo/go-control-plane/pkg/cache/types"
    28  	"github.com/hxx258456/ccgo/go-control-plane/pkg/cache/v3"
    29  	"github.com/hxx258456/ccgo/go-control-plane/pkg/resource/v3"
    30  	"github.com/hxx258456/ccgo/go-control-plane/pkg/wellknown"
    31  )
    32  
    33  const (
    34  	ClusterName  = "example_proxy_cluster"
    35  	RouteName    = "local_route"
    36  	ListenerName = "listener_0"
    37  	ListenerPort = 10000
    38  	UpstreamHost = "www.envoyproxy.io"
    39  	UpstreamPort = 80
    40  )
    41  
    42  func makeCluster(clusterName string) *cluster.Cluster {
    43  	return &cluster.Cluster{
    44  		Name:                 clusterName,
    45  		ConnectTimeout:       ptypes.DurationProto(5 * time.Second),
    46  		ClusterDiscoveryType: &cluster.Cluster_Type{Type: cluster.Cluster_LOGICAL_DNS},
    47  		LbPolicy:             cluster.Cluster_ROUND_ROBIN,
    48  		LoadAssignment:       makeEndpoint(clusterName),
    49  		DnsLookupFamily:      cluster.Cluster_V4_ONLY,
    50  	}
    51  }
    52  
    53  func makeEndpoint(clusterName string) *endpoint.ClusterLoadAssignment {
    54  	return &endpoint.ClusterLoadAssignment{
    55  		ClusterName: clusterName,
    56  		Endpoints: []*endpoint.LocalityLbEndpoints{{
    57  			LbEndpoints: []*endpoint.LbEndpoint{{
    58  				HostIdentifier: &endpoint.LbEndpoint_Endpoint{
    59  					Endpoint: &endpoint.Endpoint{
    60  						Address: &core.Address{
    61  							Address: &core.Address_SocketAddress{
    62  								SocketAddress: &core.SocketAddress{
    63  									Protocol: core.SocketAddress_TCP,
    64  									Address:  UpstreamHost,
    65  									PortSpecifier: &core.SocketAddress_PortValue{
    66  										PortValue: UpstreamPort,
    67  									},
    68  								},
    69  							},
    70  						},
    71  					},
    72  				},
    73  			}},
    74  		}},
    75  	}
    76  }
    77  
    78  func makeRoute(routeName string, clusterName string) *route.RouteConfiguration {
    79  	return &route.RouteConfiguration{
    80  		Name: routeName,
    81  		VirtualHosts: []*route.VirtualHost{{
    82  			Name:    "local_service",
    83  			Domains: []string{"*"},
    84  			Routes: []*route.Route{{
    85  				Match: &route.RouteMatch{
    86  					PathSpecifier: &route.RouteMatch_Prefix{
    87  						Prefix: "/",
    88  					},
    89  				},
    90  				Action: &route.Route_Route{
    91  					Route: &route.RouteAction{
    92  						ClusterSpecifier: &route.RouteAction_Cluster{
    93  							Cluster: clusterName,
    94  						},
    95  						HostRewriteSpecifier: &route.RouteAction_HostRewriteLiteral{
    96  							HostRewriteLiteral: UpstreamHost,
    97  						},
    98  					},
    99  				},
   100  			}},
   101  		}},
   102  	}
   103  }
   104  
   105  func makeHTTPListener(listenerName string, route string) *listener.Listener {
   106  	// HTTP filter configuration
   107  	manager := &hcm.HttpConnectionManager{
   108  		CodecType:  hcm.HttpConnectionManager_AUTO,
   109  		StatPrefix: "http",
   110  		RouteSpecifier: &hcm.HttpConnectionManager_Rds{
   111  			Rds: &hcm.Rds{
   112  				ConfigSource:    makeConfigSource(),
   113  				RouteConfigName: route,
   114  			},
   115  		},
   116  		HttpFilters: []*hcm.HttpFilter{{
   117  			Name: wellknown.Router,
   118  		}},
   119  	}
   120  	pbst, err := ptypes.MarshalAny(manager)
   121  	if err != nil {
   122  		panic(err)
   123  	}
   124  
   125  	return &listener.Listener{
   126  		Name: listenerName,
   127  		Address: &core.Address{
   128  			Address: &core.Address_SocketAddress{
   129  				SocketAddress: &core.SocketAddress{
   130  					Protocol: core.SocketAddress_TCP,
   131  					Address:  "0.0.0.0",
   132  					PortSpecifier: &core.SocketAddress_PortValue{
   133  						PortValue: ListenerPort,
   134  					},
   135  				},
   136  			},
   137  		},
   138  		FilterChains: []*listener.FilterChain{{
   139  			Filters: []*listener.Filter{{
   140  				Name: wellknown.HTTPConnectionManager,
   141  				ConfigType: &listener.Filter_TypedConfig{
   142  					TypedConfig: pbst,
   143  				},
   144  			}},
   145  		}},
   146  	}
   147  }
   148  
   149  func makeConfigSource() *core.ConfigSource {
   150  	source := &core.ConfigSource{}
   151  	source.ResourceApiVersion = resource.DefaultAPIVersion
   152  	source.ConfigSourceSpecifier = &core.ConfigSource_ApiConfigSource{
   153  		ApiConfigSource: &core.ApiConfigSource{
   154  			TransportApiVersion:       resource.DefaultAPIVersion,
   155  			ApiType:                   core.ApiConfigSource_GRPC,
   156  			SetNodeOnFirstMessageOnly: true,
   157  			GrpcServices: []*core.GrpcService{{
   158  				TargetSpecifier: &core.GrpcService_EnvoyGrpc_{
   159  					EnvoyGrpc: &core.GrpcService_EnvoyGrpc{ClusterName: "xds_cluster"},
   160  				},
   161  			}},
   162  		},
   163  	}
   164  	return source
   165  }
   166  
   167  func GenerateSnapshot() cache.Snapshot {
   168  	snap, _ := cache.NewSnapshot("1",
   169  		map[resource.Type][]types.Resource{
   170  			resource.ClusterType:  {makeCluster(ClusterName)},
   171  			resource.RouteType:    {makeRoute(RouteName, ClusterName)},
   172  			resource.ListenerType: {makeHTTPListener(ListenerName, RouteName)},
   173  		},
   174  	)
   175  	return snap
   176  }