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