Skip to content

Commit

Permalink
pkg/envoy/cds: add error codes
Browse files Browse the repository at this point in the history
Adds error codes for errors related to Envoy CDS.

Part of openservicemesh#2866.

Signed-off-by: jaellio <jaellio@microsoft.com>
  • Loading branch information
jaellio committed Jul 10, 2021
1 parent 32b9a78 commit d354ce3
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 14 deletions.
10 changes: 7 additions & 3 deletions pkg/envoy/bootstrap/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
10 changes: 7 additions & 3 deletions pkg/envoy/cds/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -198,15 +200,17 @@ 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)
}
default:
// 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)
}
Expand Down
24 changes: 16 additions & 8 deletions pkg/envoy/cds/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
55 changes: 55 additions & 0 deletions pkg/errcode/errcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
`,
}

0 comments on commit d354ce3

Please sign in to comment.