Browse Source

libnet: add a new dnsNames property to Endpoint

This new property is meant to replace myAliases and anonymous
properties.

The end goal is to get rid of both properties by letting the daemon
determine what (non fully qualified) DNS names should be associated to
them.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
Albin Kerouanton 1 năm trước cách đây
mục cha
commit
dc1e73cbbf
2 tập tin đã thay đổi với 24 bổ sung9 xóa
  1. 21 8
      libnetwork/endpoint.go
  2. 3 1
      libnetwork/libnetwork_internal_test.go

+ 21 - 8
libnetwork/endpoint.go

@@ -23,14 +23,17 @@ type EndpointOption func(ep *Endpoint)
 
 // Endpoint represents a logical connection between a network and a sandbox.
 type Endpoint struct {
-	name              string
-	id                string
-	network           *Network
-	iface             *EndpointInterface
-	joinInfo          *endpointJoinInfo
-	sandboxID         string
-	exposedPorts      []types.TransportPort
-	anonymous         bool
+	name         string
+	id           string
+	network      *Network
+	iface        *EndpointInterface
+	joinInfo     *endpointJoinInfo
+	sandboxID    string
+	exposedPorts []types.TransportPort
+	anonymous    bool
+	// dnsNames holds all the non-fully qualified DNS names associated to this endpoint. Order matters: first entry
+	// will be used for the PTR records associated to the endpoint's IPv4 and IPv6 addresses.
+	dnsNames          []string
 	disableResolution bool
 	generic           map[string]interface{}
 	prefAddress       net.IP
@@ -65,6 +68,7 @@ func (ep *Endpoint) MarshalJSON() ([]byte, error) {
 	}
 	epMap["sandbox"] = ep.sandboxID
 	epMap["anonymous"] = ep.anonymous
+	epMap["dnsNames"] = ep.dnsNames
 	epMap["disableResolution"] = ep.disableResolution
 	epMap["myAliases"] = ep.myAliases
 	epMap["svcName"] = ep.svcName
@@ -193,6 +197,12 @@ func (ep *Endpoint) UnmarshalJSON(b []byte) (err error) {
 	var myAliases []string
 	json.Unmarshal(ma, &myAliases) //nolint:errcheck
 	ep.myAliases = myAliases
+
+	dn, _ := json.Marshal(epMap["dnsNames"])
+	var dnsNames []string
+	json.Unmarshal(dn, &dnsNames)
+	ep.dnsNames = dnsNames
+
 	return nil
 }
 
@@ -243,6 +253,9 @@ func (ep *Endpoint) CopyTo(o datastore.KVObject) error {
 	dstEp.myAliases = make([]string, len(ep.myAliases))
 	copy(dstEp.myAliases, ep.myAliases)
 
+	dstEp.dnsNames = make([]string, len(ep.dnsNames))
+	copy(dstEp.dnsNames, ep.dnsNames)
+
 	dstEp.generic = options.Generic{}
 	for k, v := range ep.generic {
 		dstEp.generic[k] = v

+ 3 - 1
libnetwork/libnetwork_internal_test.go

@@ -5,6 +5,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"net"
+	"reflect"
 	"runtime"
 	"testing"
 	"time"
@@ -205,6 +206,7 @@ func TestEndpointMarshalling(t *testing.T) {
 			v6PoolID:  "poolv6",
 			llAddrs:   lla,
 		},
+		dnsNames: []string{"test", "foobar", "baz"},
 	}
 
 	b, err := json.Marshal(e)
@@ -218,7 +220,7 @@ func TestEndpointMarshalling(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	if e.name != ee.name || e.id != ee.id || e.sandboxID != ee.sandboxID || !compareEndpointInterface(e.iface, ee.iface) || e.anonymous != ee.anonymous {
+	if e.name != ee.name || e.id != ee.id || e.sandboxID != ee.sandboxID || !reflect.DeepEqual(e.dnsNames, ee.dnsNames) || !compareEndpointInterface(e.iface, ee.iface) || e.anonymous != ee.anonymous {
 		t.Fatalf("JSON marsh/unmarsh failed.\nOriginal:\n%#v\nDecoded:\n%#v\nOriginal iface: %#v\nDecodediface:\n%#v", e, ee, e.iface, ee.iface)
 	}
 }