github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/agent/xds/response.go (about)

     1  package xds
     2  
     3  import (
     4  	envoy "github.com/envoyproxy/go-control-plane/envoy/api/v2"
     5  	envoycore "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
     6  	"github.com/gogo/protobuf/proto"
     7  	"github.com/gogo/protobuf/types"
     8  	prototypes "github.com/gogo/protobuf/types"
     9  )
    10  
    11  func createResponse(typeURL string, version, nonce string, resources []proto.Message) (*envoy.DiscoveryResponse, error) {
    12  	anys := make([]types.Any, 0, len(resources))
    13  	for _, r := range resources {
    14  		if r == nil {
    15  			continue
    16  		}
    17  		if any, ok := r.(*types.Any); ok {
    18  			anys = append(anys, *any)
    19  			continue
    20  		}
    21  		data, err := proto.Marshal(r)
    22  		if err != nil {
    23  			return nil, err
    24  		}
    25  		anys = append(anys, types.Any{
    26  			TypeUrl: typeURL,
    27  			Value:   data,
    28  		})
    29  	}
    30  	resp := &envoy.DiscoveryResponse{
    31  		VersionInfo: version,
    32  		Resources:   anys,
    33  		TypeUrl:     typeURL,
    34  		Nonce:       nonce,
    35  	}
    36  	return resp, nil
    37  }
    38  
    39  func makeAddress(ip string, port int) envoycore.Address {
    40  	return envoycore.Address{
    41  		Address: &envoycore.Address_SocketAddress{
    42  			SocketAddress: &envoycore.SocketAddress{
    43  				Address: ip,
    44  				PortSpecifier: &envoycore.SocketAddress_PortValue{
    45  					PortValue: uint32(port),
    46  				},
    47  			},
    48  		},
    49  	}
    50  }
    51  
    52  func makeAddressPtr(ip string, port int) *envoycore.Address {
    53  	a := makeAddress(ip, port)
    54  	return &a
    55  }
    56  
    57  func makeUint32Value(n int) *prototypes.UInt32Value {
    58  	return &prototypes.UInt32Value{Value: uint32(n)}
    59  }