...

Source file src/github.com/datawire/ambassador/v2/pkg/envoy-control-plane/ttl/v3/ttl.go

Documentation: github.com/datawire/ambassador/v2/pkg/envoy-control-plane/ttl/v3

     1  package ttl
     2  
     3  import (
     4  	discovery "github.com/datawire/ambassador/v2/pkg/api/envoy/service/discovery/v3"
     5  	"github.com/datawire/ambassador/v2/pkg/envoy-control-plane/cache/types"
     6  	"github.com/golang/protobuf/proto"
     7  	"github.com/golang/protobuf/ptypes"
     8  	"github.com/golang/protobuf/ptypes/any"
     9  )
    10  
    11  var deltaResourceTypeURL = "type.googleapis.com/" + proto.MessageName(&discovery.Resource{})
    12  
    13  // Helper functions for interacting with TTL resources for xDS V3. A resource will be wrapped in a discovery.Resource in order
    14  // to allow specifying a TTL. If the resource is meant to be a heartbeat response, only the resource name and TTL will be set
    15  // to avoid having to send the entire resource down.
    16  
    17  func MaybeCreateTtlResourceIfSupported(resource types.ResourceWithTtl, name string, resourceTypeUrl string, heartbeat bool) (types.Resource, string, error) {
    18  	if resource.Ttl != nil {
    19  		wrappedResource := &discovery.Resource{
    20  			Name: name,
    21  			Ttl:  ptypes.DurationProto(*resource.Ttl),
    22  		}
    23  
    24  		if !heartbeat {
    25  			any, err := ptypes.MarshalAny(resource.Resource)
    26  			if err != nil {
    27  				return nil, "", err
    28  			}
    29  			any.TypeUrl = resourceTypeUrl
    30  			wrappedResource.Resource = any
    31  		}
    32  
    33  		return wrappedResource, deltaResourceTypeURL, nil
    34  	}
    35  
    36  	return resource.Resource, resourceTypeUrl, nil
    37  }
    38  
    39  func IsTTLResource(resource *any.Any) bool {
    40  	// This is only done in test, so no need to worry about the overhead of the marshalling.
    41  	wrappedResource := &discovery.Resource{}
    42  	err := ptypes.UnmarshalAny(resource, wrappedResource)
    43  	if err != nil {
    44  		return false
    45  	}
    46  
    47  	return wrappedResource.Resource == nil
    48  }
    49  

View as plain text