...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
70 return v.GetName()
71 default:
72 return ""
73 }
74 }
75
76
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
89
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
99 case *cluster.Cluster:
100
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
113
114
115 case *listener.Listener:
116
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
136 }
137 }
138 return out
139 }
140
View as plain text