Skip to content

Commit

Permalink
Add v3 types (#138)
Browse files Browse the repository at this point in the history
This PR adds the v3 types into transport. Since the other parts of orchestrator were abstracted, most changes are in the transport package and also adds unit tests. The next PR will add integration tests using v3 flows.

Signed-off-by: Jyoti Mahapatra <[email protected]>
  • Loading branch information
jyotimahapatra authored Sep 9, 2020
1 parent ff2b887 commit 908106f
Show file tree
Hide file tree
Showing 31 changed files with 1,818 additions and 608 deletions.
137 changes: 111 additions & 26 deletions internal/app/admin/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ import (
"strings"
"time"

envoy_api_v2_core "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
clusterv3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
endpointv3 "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
listenerv3 "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
routev3 "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
secretv3 "github.com/envoyproxy/go-control-plane/envoy/extensions/transport_sockets/tls/v3"
runtimev3 "github.com/envoyproxy/go-control-plane/envoy/service/runtime/v3"
"github.com/envoyproxy/xds-relay/internal/pkg/log"
"github.com/envoyproxy/xds-relay/internal/pkg/log/zap"

envoy_api_v2_auth "github.com/envoyproxy/go-control-plane/envoy/api/v2/auth"
envoy_service_discovery_v2 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v2"
"github.com/envoyproxy/go-control-plane/pkg/cache/types"
resource2 "github.com/envoyproxy/go-control-plane/pkg/resource/v2"
resource3 "github.com/envoyproxy/go-control-plane/pkg/resource/v3"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"

Expand Down Expand Up @@ -106,8 +113,8 @@ func configDumpHandler(bootstrapConfig *bootstrapv1.Bootstrap) http.HandlerFunc
func cacheDumpHandler(o *orchestrator.Orchestrator) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
cacheKey := getParam(req.URL.Path)
cache := orchestrator.Orchestrator.GetReadOnlyCache(*o)

c := orchestrator.Orchestrator.GetReadOnlyCache(*o)
resp := marshallableCache{}
// If no key is provided, output the entire cache.
if cacheKey == "" {
keys, err := orchestrator.Orchestrator.GetDownstreamAggregatedKeys(*o)
Expand All @@ -116,25 +123,32 @@ func cacheDumpHandler(o *orchestrator.Orchestrator) http.HandlerFunc {
fmt.Fprintf(w, "error in getting cache keys: %s", err.Error())
return
}

for key := range keys {
resource, err := cache.FetchReadOnly(key)
if err == nil {
resourceString, err := resourceToString(resource)
if err == nil {
fmt.Fprintf(w, "%s: %s\n", key, resourceString)
}
resource, err := c.FetchReadOnly(key)
if err != nil {
continue
}
resp.Cache = append(resp.Cache, resourceToPayload(key, resource)...)
}
resourceString, err := stringify.InterfaceToString(resp)
if err != nil {
return
}
fmt.Fprintf(w, "%s\n", resourceString)
return
}

// Otherwise return the cache entry corresponding to the given key.
resource, err := cache.FetchReadOnly(cacheKey)
resource, err := c.FetchReadOnly(cacheKey)
if err != nil {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "no resource for key %s found in cache.\n", cacheKey)
return
}
resourceString, err := resourceToString(resource)

resp.Cache = append(resp.Cache, resourceToPayload(cacheKey, resource)...)
resourceString, err := stringify.InterfaceToString(resp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "unable to convert resource to string.\n")
Expand All @@ -145,27 +159,38 @@ func cacheDumpHandler(o *orchestrator.Orchestrator) http.HandlerFunc {
}

type marshallableResource struct {
Key string
Resp *marshalledDiscoveryResponse
Requests []*v2.DiscoveryRequest
Requests []types.Resource
ExpirationTime time.Time
}

type marshallableCache struct {
Cache []marshallableResource
}

// In order to marshal a Resource from the cache to JSON to be printed,
// the map of requests is converted to a slice of just the keys,
// since the bool value is meaningless.
func resourceToString(resource cache.Resource) (string, error) {
var requests []*v2.DiscoveryRequest
func resourceToPayload(key string, resource cache.Resource) []marshallableResource {
var marshallableResources []marshallableResource
var requests []types.Resource
for request := range resource.Requests {
requests = append(requests, request.GetRaw().V2)
if request.GetRaw().V2 != nil {
requests = append(requests, request.GetRaw().V2)
} else {
requests = append(requests, request.GetRaw().V3)
}
}

resourceString := &marshallableResource{
marshallableResources = append(marshallableResources, marshallableResource{
Key: key,
Resp: marshalDiscoveryResponse(resource.Resp),
Requests: requests,
ExpirationTime: resource.ExpirationTime,
}
})

return stringify.InterfaceToString(resourceString)
return marshallableResources
}

type marshalledDiscoveryResponse struct {
Expand All @@ -174,22 +199,34 @@ type marshalledDiscoveryResponse struct {
Canary bool
TypeURL string
Nonce string
ControlPlane *envoy_api_v2_core.ControlPlane
ControlPlane types.Resource
}

type xDSResources struct {
Endpoints []*v2.ClusterLoadAssignment
Clusters []*v2.Cluster
Routes []*v2.RouteConfiguration
Listeners []*v2.Listener
Secrets []*envoy_api_v2_auth.Secret
Runtimes []*envoy_service_discovery_v2.Runtime
Endpoints []types.Resource
Clusters []types.Resource
Routes []types.Resource
Listeners []types.Resource
Secrets []types.Resource
Runtimes []types.Resource
Unmarshalled []*any.Any
}

func marshalDiscoveryResponse(r transport.Response) *marshalledDiscoveryResponse {
if r != nil {
resp := r.Get().V2
if r.Get().V2 != nil {
resp := r.Get().V2
marshalledResp := marshalledDiscoveryResponse{
VersionInfo: resp.VersionInfo,
Canary: resp.Canary,
TypeURL: resp.TypeUrl,
Resources: marshalResources(resp.Resources),
Nonce: resp.Nonce,
ControlPlane: resp.ControlPlane,
}
return &marshalledResp
}
resp := r.Get().V3
marshalledResp := marshalledDiscoveryResponse{
VersionInfo: resp.VersionInfo,
Canary: resp.Canary,
Expand Down Expand Up @@ -255,6 +292,54 @@ func marshalResources(Resources []*any.Any) *xDSResources {
} else {
marshalledResources.Unmarshalled = append(marshalledResources.Unmarshalled, resource)
}
case resource3.EndpointType:
e := &endpointv3.ClusterLoadAssignment{}
err := ptypes.UnmarshalAny(resource, e)
if err == nil {
marshalledResources.Endpoints = append(marshalledResources.Endpoints, e)
} else {
marshalledResources.Unmarshalled = append(marshalledResources.Unmarshalled, resource)
}
case resource3.ClusterType:
c := &clusterv3.Cluster{}
err := ptypes.UnmarshalAny(resource, c)
if err == nil {
marshalledResources.Clusters = append(marshalledResources.Clusters, c)
} else {
marshalledResources.Unmarshalled = append(marshalledResources.Unmarshalled, resource)
}
case resource3.RouteType:
r := &routev3.RouteConfiguration{}
err := ptypes.UnmarshalAny(resource, r)
if err == nil {
marshalledResources.Routes = append(marshalledResources.Routes, r)
} else {
marshalledResources.Unmarshalled = append(marshalledResources.Unmarshalled, resource)
}
case resource3.ListenerType:
l := &listenerv3.Listener{}
err := ptypes.UnmarshalAny(resource, l)
if err == nil {
marshalledResources.Listeners = append(marshalledResources.Listeners, l)
} else {
marshalledResources.Unmarshalled = append(marshalledResources.Unmarshalled, resource)
}
case resource3.SecretType:
s := &secretv3.Secret{}
err := ptypes.UnmarshalAny(resource, s)
if err == nil {
marshalledResources.Secrets = append(marshalledResources.Secrets, s)
} else {
marshalledResources.Unmarshalled = append(marshalledResources.Unmarshalled, resource)
}
case resource3.RuntimeType:
r := &runtimev3.Runtime{}
err := ptypes.UnmarshalAny(resource, r)
if err == nil {
marshalledResources.Runtimes = append(marshalledResources.Runtimes, r)
} else {
marshalledResources.Unmarshalled = append(marshalledResources.Unmarshalled, resource)
}
default:
marshalledResources.Unmarshalled = append(marshalledResources.Unmarshalled, resource)
}
Expand Down
Loading

0 comments on commit 908106f

Please sign in to comment.