From d354ce352898fb6ac1cad7522aba2717baab4282 Mon Sep 17 00:00:00 2001 From: jaellio Date: Sat, 3 Jul 2021 20:41:16 -0700 Subject: [PATCH] pkg/envoy/cds: add error codes Adds error codes for errors related to Envoy CDS. Part of #2866. Signed-off-by: jaellio --- pkg/envoy/bootstrap/config.go | 10 +++++-- pkg/envoy/cds/cluster.go | 10 +++++-- pkg/envoy/cds/response.go | 24 ++++++++++----- pkg/errcode/errcode.go | 55 +++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 14 deletions(-) diff --git a/pkg/envoy/bootstrap/config.go b/pkg/envoy/bootstrap/config.go index 430c6ab851..eac7c29698 100644 --- a/pkg/envoy/bootstrap/config.go +++ b/pkg/envoy/bootstrap/config.go @@ -17,6 +17,7 @@ import ( "github.com/openservicemesh/osm/pkg/constants" "github.com/openservicemesh/osm/pkg/envoy" + "github.com/openservicemesh/osm/pkg/errcode" ) // BuildFromConfig builds and returns an Envoy Bootstrap object from the given config @@ -30,14 +31,16 @@ func BuildFromConfig(config Config) (*xds_bootstrap.Bootstrap, error) { } pbHTTPProtocolOptions, err := ptypes.MarshalAny(httpProtocolOptions) if err != nil { - log.Error().Err(err).Msgf("Error marshaling HttpProtocolOptions struct into an anypb.Any message") + log.Error().Err(err).Str(errcode.Kind, errcode.ErrMarshallingXDSResource.String()). + Msgf("Error marshaling HttpProtocolOptions struct into an anypb.Any message") return nil, err } accessLogger := &xds_accesslog_stream.StdoutAccessLog{} pbAccessLog, err := ptypes.MarshalAny(accessLogger) if err != nil { - log.Error().Err(err).Msgf("Error marshaling StdoutAccessLog struct into an anypb.Any message") + log.Error().Err(err).Str(errcode.Kind, errcode.ErrMarshallingXDSResource.String()). + Msgf("Error marshaling StdoutAccessLog struct into an anypb.Any message") return nil, err } @@ -77,7 +80,8 @@ func BuildFromConfig(config Config) (*xds_bootstrap.Bootstrap, error) { } pbUpstreamTLSContext, err := ptypes.MarshalAny(upstreamTLSContext) if err != nil { - log.Error().Err(err).Msgf("Error marshaling UpstreamTlsContext struct into an anypb.Any message") + log.Error().Err(err).Str(errcode.Kind, errcode.ErrMarshallingXDSResource.String()). + Msgf("Error marshaling UpstreamTlsContext struct into an anypb.Any message") return nil, err } diff --git a/pkg/envoy/cds/cluster.go b/pkg/envoy/cds/cluster.go index 1010a9f665..5dcb344e1d 100644 --- a/pkg/envoy/cds/cluster.go +++ b/pkg/envoy/cds/cluster.go @@ -16,6 +16,7 @@ import ( "github.com/openservicemesh/osm/pkg/configurator" "github.com/openservicemesh/osm/pkg/constants" "github.com/openservicemesh/osm/pkg/envoy" + "github.com/openservicemesh/osm/pkg/errcode" "github.com/openservicemesh/osm/pkg/identity" "github.com/openservicemesh/osm/pkg/service" "github.com/openservicemesh/osm/pkg/trafficpolicy" @@ -127,7 +128,8 @@ func getLocalServiceCluster(catalog catalog.MeshCataloger, proxyServiceName serv ports, err := catalog.GetTargetPortToProtocolMappingForService(proxyServiceName) if err != nil { - log.Error().Err(err).Msgf("Failed to get ports for service %s", proxyServiceName) + log.Error().Err(err).Str(errcode.Kind, errcode.ErrGettingServicePorts.String()). + Msgf("Failed to get ports for service %s", proxyServiceName) return nil, err } @@ -198,7 +200,8 @@ func getEgressClusters(clusterConfigs []*trafficpolicy.EgressClusterConfig) []*x // Cluster config does not have a Host specified, route it to its original destination. // Used for TCP based clusters if originalDestinationEgressCluster, err := getOriginalDestinationEgressCluster(config.Name); err != nil { - log.Error().Err(err).Msg("Error building the original destination cluster for the given egress cluster config") + log.Error().Err(err).Str(errcode.Kind, errcode.ErrGettingOrgDstEgressCluster.String()). + Msg("Error building the original destination cluster for the given egress cluster config") } else { egressClusters = append(egressClusters, originalDestinationEgressCluster) } @@ -206,7 +209,8 @@ func getEgressClusters(clusterConfigs []*trafficpolicy.EgressClusterConfig) []*x // Cluster config has a Host specified, route it based on the Host resolved using DNS. // Used for HTTP based clusters if cluster, err := getDNSResolvableEgressCluster(config); err != nil { - log.Error().Err(err).Msg("Error building cluster for the given egress cluster config") + log.Error().Err(err).Str(errcode.Kind, errcode.ErrGettingDNSEgressCluster.String()). + Msg("Error building cluster for the given egress cluster config") } else { egressClusters = append(egressClusters, cluster) } diff --git a/pkg/envoy/cds/response.go b/pkg/envoy/cds/response.go index 8330bc775b..36047b83db 100644 --- a/pkg/envoy/cds/response.go +++ b/pkg/envoy/cds/response.go @@ -11,6 +11,7 @@ import ( "github.com/openservicemesh/osm/pkg/configurator" "github.com/openservicemesh/osm/pkg/envoy" "github.com/openservicemesh/osm/pkg/envoy/registry" + "github.com/openservicemesh/osm/pkg/errcode" ) // NewResponse creates a new Cluster Discovery Response. @@ -19,7 +20,8 @@ func NewResponse(meshCatalog catalog.MeshCataloger, proxy *envoy.Proxy, _ *xds_d proxyIdentity, err := envoy.GetServiceIdentityFromProxyCertificate(proxy.GetCertificateCommonName()) if err != nil { - log.Error().Err(err).Msgf("Error looking up identity for proxy %s", proxy.String()) + log.Error().Err(err).Str(errcode.Kind, errcode.ErrGettingServiceIdentity.String()). + Msgf("Error looking up identity for proxy %s", proxy.String()) return nil, err } @@ -28,8 +30,9 @@ func NewResponse(meshCatalog catalog.MeshCataloger, proxy *envoy.Proxy, _ *xds_d for _, dstService := range meshCatalog.ListOutboundServicesForIdentity(proxyIdentity) { cluster, err := getUpstreamServiceCluster(proxyIdentity, dstService, cfg) if err != nil { - log.Error().Err(err).Msgf("Failed to construct service cluster for service %s for proxy with XDS Certificate SerialNumber=%s on Pod with UID=%s", - dstService.Name, proxy.GetCertificateSerialNumber(), proxy.String()) + log.Error().Err(err).Str(errcode.Kind, errcode.ErrObtainingUpstreamServiceCluster.String()). + Msgf("Failed to construct service cluster for service %s for proxy with XDS Certificate SerialNumber=%s on Pod with UID=%s", + dstService.Name, proxy.GetCertificateSerialNumber(), proxy.String()) return nil, err } @@ -46,7 +49,8 @@ func NewResponse(meshCatalog catalog.MeshCataloger, proxy *envoy.Proxy, _ *xds_d } cluster, err := getUpstreamServiceCluster(proxyIdentity, dstService, cfg, opts...) if err != nil { - log.Error().Err(err).Msgf("Failed to construct service cluster for service %s for proxy %s", dstService.Name, proxy.String()) + log.Error().Err(err).Str(errcode.Kind, errcode.ErrObtainingUpstreamServiceCluster.String()). + Msgf("Failed to construct service cluster for service %s for proxy %s", dstService.Name, proxy.String()) return nil, err } @@ -55,7 +59,8 @@ func NewResponse(meshCatalog catalog.MeshCataloger, proxy *envoy.Proxy, _ *xds_d svcList, err := proxyRegistry.ListProxyServices(proxy) if err != nil { - log.Error().Err(err).Msgf("Error looking up MeshService for proxy %s", proxy.String()) + log.Error().Err(err).Str(errcode.Kind, errcode.ErrFetchingServiceList.String()). + Msgf("Error looking up MeshService for proxy %s", proxy.String()) return nil, err } @@ -65,7 +70,8 @@ func NewResponse(meshCatalog catalog.MeshCataloger, proxy *envoy.Proxy, _ *xds_d localClusterName := envoy.GetLocalClusterNameForService(proxyService) localCluster, err := getLocalServiceCluster(meshCatalog, proxyService, localClusterName) if err != nil { - log.Error().Err(err).Msgf("Failed to get local cluster config for proxy %s", proxyService) + log.Error().Err(err).Str(errcode.Kind, errcode.ErrGettingLocalServiceCluster.String()). + Msgf("Failed to get local cluster config for proxy %s", proxyService) return nil, err } clusters = append(clusters, localCluster) @@ -82,7 +88,8 @@ func NewResponse(meshCatalog catalog.MeshCataloger, proxy *envoy.Proxy, _ *xds_d outboundPassthroughCluser, err := getOriginalDestinationEgressCluster(envoy.OutboundPassthroughCluster) if err != nil { - log.Error().Err(err).Msgf("Failed to passthrough cluster for egress for proxy %s", envoy.OutboundPassthroughCluster) + log.Error().Err(err).Str(errcode.Kind, errcode.ErrGettingOrgDstEgressCluster.String()). + Msgf("Failed to passthrough cluster for egress for proxy %s", envoy.OutboundPassthroughCluster) return nil, err } @@ -111,7 +118,8 @@ func removeDups(clusters []*xds_cluster.Cluster) []types.Resource { var cdsResources []types.Resource for _, cluster := range clusters { if alreadyAdded.Contains(cluster.Name) { - log.Error().Msgf("Found duplicate clusters with name %s; duplicate will not be sent to proxy.", cluster.Name) + log.Error().Str(errcode.Kind, errcode.ErrDuplicateClusters.String()). + Msgf("Found duplicate clusters with name %s; duplicate will not be sent to proxy.", cluster.Name) continue } alreadyAdded.Add(cluster.Name) diff --git a/pkg/errcode/errcode.go b/pkg/errcode/errcode.go index 1c2c493a62..6451ae641c 100644 --- a/pkg/errcode/errcode.go +++ b/pkg/errcode/errcode.go @@ -218,6 +218,27 @@ const ( // ErrParsingDiscoveryReqVersion indicates the discovery request response version could not be parsed ErrParsingDiscoveryReqVersion + + // ErrGettingServicePorts indicates the mapping of a service's ports to their corresponding application protocol could not be obtained + ErrGettingServicePorts + + // ErrGettingOrgDstEgressCluster indicates that an Envoy egress cluster that routes traffic to its original destination could not be configured + ErrGettingOrgDstEgressCluster + + // ErrGettingDNSEgressCluster indicates that an Envoy egress cluster that routes traffic based on the specified Host resolved using DNS could not be configured + ErrGettingDNSEgressCluster + + // ErrObtainingUpstreamServiceCluster indicates an Envoy cluster corresponding to an upstream service could not be configured + ErrObtainingUpstreamServiceCluster + + // ErrFetchingServiceList indicates the services corresponding to a specified proxy could not be listed + ErrFetchingServiceList + + // ErrGettingLocalServiceCluster indicates that an Envoy cluster for a local service behind the Envoy proxy could not be configured + ErrGettingLocalServiceCluster + + // ErrDuplicateluster indicates Envoy clusters with the same name were found + ErrDuplicateClusters ) // String returns the error code as a string, ex. E1000 @@ -540,5 +561,39 @@ The TypeURL of the resource being requested in the DiscoveryRequest is invalid. ErrParsingDiscoveryReqVersion: ` The version of the DiscoveryRequest could not be parsed by ADS. +`, + + ErrGettingServicePorts: ` +The mapping of ports the application is exposing a service on to their corresponding +application protocol could not be obtained for a specified service. +`, + + ErrGettingOrgDstEgressCluster: ` +An Envoy egress cluster which routes traffic to its original destination could not +be configured. When a Host is not specified in the cluster config, the original +destination is used. +`, + + ErrGettingDNSEgressCluster: ` +An Envoy egress cluster that routes traffic based on the specified Host resolved +using DNS could not be configured. +`, + + ErrObtainingUpstreamServiceCluster: ` +An Envoy cluster that corresponds to a specified upstream service could not be +configured. +`, + + ErrFetchingServiceList: ` +The meshed services corresponding a specified Envoy proxy could not be listed. +`, + + ErrGettingLocalServiceCluster: ` +An Envoy cluster for a local service behind an Envoy proxy could not be configured. +`, + + ErrDuplicateClusters: ` +Multiple Envoy clusters with the same name were configured. The duplicate clusters +will not be sent to the Envoy proxy in a ClusterDiscovery response. `, }