github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/go-control-plane/pkg/cache/v3/resource.go (about) 1 // Copyright 2018 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 cache 16 17 import ( 18 "crypto/sha256" 19 "encoding/hex" 20 "fmt" 21 22 "github.com/golang/protobuf/proto" 23 24 cluster "github.com/hxx258456/ccgo/go-control-plane/envoy/config/cluster/v3" 25 core "github.com/hxx258456/ccgo/go-control-plane/envoy/config/core/v3" 26 endpoint "github.com/hxx258456/ccgo/go-control-plane/envoy/config/endpoint/v3" 27 listener "github.com/hxx258456/ccgo/go-control-plane/envoy/config/listener/v3" 28 route "github.com/hxx258456/ccgo/go-control-plane/envoy/config/route/v3" 29 hcm "github.com/hxx258456/ccgo/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3" 30 auth "github.com/hxx258456/ccgo/go-control-plane/envoy/extensions/transport_sockets/tls/v3" 31 runtime "github.com/hxx258456/ccgo/go-control-plane/envoy/service/runtime/v3" 32 "github.com/hxx258456/ccgo/go-control-plane/pkg/cache/types" 33 "github.com/hxx258456/ccgo/go-control-plane/pkg/resource/v3" 34 "github.com/hxx258456/ccgo/go-control-plane/pkg/wellknown" 35 ) 36 37 // GetResponseType returns the enumeration for a valid xDS type URL 38 func GetResponseType(typeURL resource.Type) types.ResponseType { 39 switch typeURL { 40 case resource.EndpointType: 41 return types.Endpoint 42 case resource.ClusterType: 43 return types.Cluster 44 case resource.RouteType: 45 return types.Route 46 case resource.ListenerType: 47 return types.Listener 48 case resource.SecretType: 49 return types.Secret 50 case resource.RuntimeType: 51 return types.Runtime 52 case resource.ExtensionConfigType: 53 return types.ExtensionConfig 54 } 55 return types.UnknownType 56 } 57 58 // GetResponseTypeURL returns the type url for a valid enum 59 func GetResponseTypeURL(responseType types.ResponseType) (string, error) { 60 switch responseType { 61 case types.Endpoint: 62 return resource.EndpointType, nil 63 case types.Cluster: 64 return resource.ClusterType, nil 65 case types.Route: 66 return resource.RouteType, nil 67 case types.Listener: 68 return resource.ListenerType, nil 69 case types.Secret: 70 return resource.SecretType, nil 71 case types.Runtime: 72 return resource.RuntimeType, nil 73 case types.ExtensionConfig: 74 return resource.ExtensionConfigType, nil 75 } 76 77 return "", fmt.Errorf("couldn't map response type to known resource type") 78 } 79 80 // GetResourceName returns the resource name for a valid xDS response type. 81 func GetResourceName(res types.Resource) string { 82 switch v := res.(type) { 83 case *endpoint.ClusterLoadAssignment: 84 return v.GetClusterName() 85 case *cluster.Cluster: 86 return v.GetName() 87 case *route.RouteConfiguration: 88 return v.GetName() 89 case *listener.Listener: 90 return v.GetName() 91 case *auth.Secret: 92 return v.GetName() 93 case *runtime.Runtime: 94 return v.GetName() 95 case *core.TypedExtensionConfig: 96 return v.GetName() 97 default: 98 return "" 99 } 100 } 101 102 // MarshalResource converts the Resource to MarshaledResource 103 func MarshalResource(resource types.Resource) (types.MarshaledResource, error) { 104 b := proto.NewBuffer(nil) 105 b.SetDeterministic(true) 106 err := b.Marshal(resource) 107 if err != nil { 108 return nil, err 109 } 110 111 return b.Bytes(), nil 112 } 113 114 // GetResourceReferences returns the names for dependent resources (EDS cluster 115 // names for CDS, RDS routes names for LDS). 116 func GetResourceReferences(resources map[string]types.ResourceWithTTL) map[string]bool { 117 out := make(map[string]bool) 118 for _, res := range resources { 119 if res.Resource == nil { 120 continue 121 } 122 switch v := res.Resource.(type) { 123 case *endpoint.ClusterLoadAssignment: 124 // no dependencies 125 case *cluster.Cluster: 126 // for EDS type, use cluster name or ServiceName override 127 switch typ := v.ClusterDiscoveryType.(type) { 128 case *cluster.Cluster_Type: 129 if typ.Type == cluster.Cluster_EDS { 130 if v.EdsClusterConfig != nil && v.EdsClusterConfig.ServiceName != "" { 131 out[v.EdsClusterConfig.ServiceName] = true 132 } else { 133 out[v.Name] = true 134 } 135 } 136 } 137 case *route.RouteConfiguration: 138 // References to clusters in both routes (and listeners) are not included 139 // in the result, because the clusters are retrieved in bulk currently, 140 // and not by name. 141 case *listener.Listener: 142 // extract route configuration names from HTTP connection manager 143 for _, chain := range v.FilterChains { 144 for _, filter := range chain.Filters { 145 if filter.Name != wellknown.HTTPConnectionManager { 146 continue 147 } 148 149 config := resource.GetHTTPConnectionManager(filter) 150 151 if config == nil { 152 continue 153 } 154 155 if rds, ok := config.RouteSpecifier.(*hcm.HttpConnectionManager_Rds); ok && rds != nil && rds.Rds != nil { 156 out[rds.Rds.RouteConfigName] = true 157 } 158 } 159 } 160 case *runtime.Runtime: 161 // no dependencies 162 } 163 } 164 return out 165 } 166 167 // HashResource will take a resource and create a SHA256 hash sum out of the marshaled bytes 168 func HashResource(resource []byte) string { 169 hasher := sha256.New() 170 hasher.Write(resource) 171 172 return hex.EncodeToString(hasher.Sum(nil)) 173 }