...

Source file src/github.com/datawire/ambassador/v2/pkg/envoy-control-plane/cache/v3/resource.go

Documentation: github.com/datawire/ambassador/v2/pkg/envoy-control-plane/cache/v3

     1  // Code generated by create_version. DO NOT EDIT.
     2  // Copyright 2018 Envoyproxy Authors
     3  //
     4  //   Licensed under the Apache License, Version 2.0 (the "License");
     5  //   you may not use this file except in compliance with the License.
     6  //   You may obtain a copy of the License at
     7  //
     8  //       http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  //   Unless required by applicable law or agreed to in writing, software
    11  //   distributed under the License is distributed on an "AS IS" BASIS,
    12  //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  //   See the License for the specific language governing permissions and
    14  //   limitations under the License.
    15  
    16  package cache
    17  
    18  import (
    19  	"github.com/golang/protobuf/proto"
    20  
    21  	cluster "github.com/datawire/ambassador/v2/pkg/api/envoy/config/cluster/v3"
    22  	core "github.com/datawire/ambassador/v2/pkg/api/envoy/config/core/v3"
    23  	endpoint "github.com/datawire/ambassador/v2/pkg/api/envoy/config/endpoint/v3"
    24  	listener "github.com/datawire/ambassador/v2/pkg/api/envoy/config/listener/v3"
    25  	route "github.com/datawire/ambassador/v2/pkg/api/envoy/config/route/v3"
    26  	hcm "github.com/datawire/ambassador/v2/pkg/api/envoy/extensions/filters/network/http_connection_manager/v3"
    27  	auth "github.com/datawire/ambassador/v2/pkg/api/envoy/extensions/transport_sockets/tls/v3"
    28  	runtime "github.com/datawire/ambassador/v2/pkg/api/envoy/service/runtime/v3"
    29  	"github.com/datawire/ambassador/v2/pkg/envoy-control-plane/cache/types"
    30  	"github.com/datawire/ambassador/v2/pkg/envoy-control-plane/resource/v3"
    31  	"github.com/datawire/ambassador/v2/pkg/envoy-control-plane/wellknown"
    32  )
    33  
    34  // GetResponseType returns the enumeration for a valid xDS type URL
    35  func GetResponseType(typeURL string) types.ResponseType {
    36  	switch typeURL {
    37  	case resource.EndpointType:
    38  		return types.Endpoint
    39  	case resource.ClusterType:
    40  		return types.Cluster
    41  	case resource.RouteType:
    42  		return types.Route
    43  	case resource.ListenerType:
    44  		return types.Listener
    45  	case resource.SecretType:
    46  		return types.Secret
    47  	case resource.RuntimeType:
    48  		return types.Runtime
    49  	}
    50  	return types.UnknownType
    51  }
    52  
    53  // GetResourceName returns the resource name for a valid xDS response type.
    54  func GetResourceName(res types.Resource) string {
    55  	switch v := res.(type) {
    56  	case *endpoint.ClusterLoadAssignment:
    57  		return v.GetClusterName()
    58  	case *cluster.Cluster:
    59  		return v.GetName()
    60  	case *route.RouteConfiguration:
    61  		return v.GetName()
    62  	case *listener.Listener:
    63  		return v.GetName()
    64  	case *auth.Secret:
    65  		return v.GetName()
    66  	case *runtime.Runtime:
    67  		return v.GetName()
    68  	case *core.TypedExtensionConfig:
    69  		// This is a V3 proto, but this is the easiest workaround for the fact that there is no V2 proto.
    70  		return v.GetName()
    71  	default:
    72  		return ""
    73  	}
    74  }
    75  
    76  // MarshalResource converts the Resource to MarshaledResource
    77  func MarshalResource(resource types.Resource) (types.MarshaledResource, error) {
    78  	b := proto.NewBuffer(nil)
    79  	b.SetDeterministic(true)
    80  	err := b.Marshal(resource)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	return b.Bytes(), nil
    86  }
    87  
    88  // GetResourceReferences returns the names for dependent resources (EDS cluster
    89  // names for CDS, RDS routes names for LDS).
    90  func GetResourceReferences(resources map[string]types.ResourceWithTtl) map[string]bool {
    91  	out := make(map[string]bool)
    92  	for _, res := range resources {
    93  		if res.Resource == nil {
    94  			continue
    95  		}
    96  		switch v := res.Resource.(type) {
    97  		case *endpoint.ClusterLoadAssignment:
    98  			// no dependencies
    99  		case *cluster.Cluster:
   100  			// for EDS type, use cluster name or ServiceName override
   101  			switch typ := v.ClusterDiscoveryType.(type) {
   102  			case *cluster.Cluster_Type:
   103  				if typ.Type == cluster.Cluster_EDS {
   104  					if v.EdsClusterConfig != nil && v.EdsClusterConfig.ServiceName != "" {
   105  						out[v.EdsClusterConfig.ServiceName] = true
   106  					} else {
   107  						out[v.Name] = true
   108  					}
   109  				}
   110  			}
   111  		case *route.RouteConfiguration:
   112  			// References to clusters in both routes (and listeners) are not included
   113  			// in the result, because the clusters are retrieved in bulk currently,
   114  			// and not by name.
   115  		case *listener.Listener:
   116  			// extract route configuration names from HTTP connection manager
   117  			for _, chain := range v.FilterChains {
   118  				for _, filter := range chain.Filters {
   119  					if filter.Name != wellknown.HTTPConnectionManager {
   120  						continue
   121  					}
   122  
   123  					config := resource.GetHTTPConnectionManager(filter)
   124  
   125  					if config == nil {
   126  						continue
   127  					}
   128  
   129  					if rds, ok := config.RouteSpecifier.(*hcm.HttpConnectionManager_Rds); ok && rds != nil && rds.Rds != nil {
   130  						out[rds.Rds.RouteConfigName] = true
   131  					}
   132  				}
   133  			}
   134  		case *runtime.Runtime:
   135  			// no dependencies
   136  		}
   137  	}
   138  	return out
   139  }
   140  

View as plain text