From acac7ee812515d2626780b668adad84e4aa5e361 Mon Sep 17 00:00:00 2001 From: Jana Radhakrishnan Date: Tue, 14 Jun 2016 16:40:54 -0700 Subject: [PATCH] Add service alias support Signed-off-by: Jana Radhakrishnan --- libnetwork/agent.go | 10 +-- libnetwork/agent.pb.go | 107 ++++++++++++++++++++++-------- libnetwork/agent.proto | 3 + libnetwork/endpoint.go | 13 +++- libnetwork/service_linux.go | 16 ++++- libnetwork/service_unsupported.go | 4 +- 6 files changed, 118 insertions(+), 35 deletions(-) diff --git a/libnetwork/agent.go b/libnetwork/agent.go index 7276beefe5..6215459d8d 100644 --- a/libnetwork/agent.go +++ b/libnetwork/agent.go @@ -353,7 +353,7 @@ func (ep *endpoint) addToCluster() error { ingressPorts = ep.ingressPorts } - if err := c.addServiceBinding(ep.svcName, ep.svcID, n.ID(), ep.ID(), ep.virtualIP, ingressPorts, ep.Iface().Address().IP); err != nil { + if err := c.addServiceBinding(ep.svcName, ep.svcID, n.ID(), ep.ID(), ep.virtualIP, ingressPorts, ep.svcAliases, ep.Iface().Address().IP); err != nil { return err } } @@ -364,6 +364,7 @@ func (ep *endpoint) addToCluster() error { ServiceID: ep.svcID, VirtualIP: ep.virtualIP.String(), IngressPorts: ingressPorts, + Aliases: ep.svcAliases, EndpointIP: ep.Iface().Address().IP.String(), }) @@ -399,7 +400,7 @@ func (ep *endpoint) deleteFromCluster() error { ingressPorts = ep.ingressPorts } - if err := c.rmServiceBinding(ep.svcName, ep.svcID, n.ID(), ep.ID(), ep.virtualIP, ingressPorts, ep.Iface().Address().IP); err != nil { + if err := c.rmServiceBinding(ep.svcName, ep.svcID, n.ID(), ep.ID(), ep.virtualIP, ingressPorts, ep.svcAliases, ep.Iface().Address().IP); err != nil { return err } } @@ -554,6 +555,7 @@ func (c *controller) handleEpTableEvent(ev events.Event) { vip := net.ParseIP(epRec.VirtualIP) ip := net.ParseIP(epRec.EndpointIP) ingressPorts := epRec.IngressPorts + aliases := epRec.Aliases if name == "" || ip == nil { logrus.Errorf("Invalid endpoint name/ip received while handling service table event %s", value) @@ -562,7 +564,7 @@ func (c *controller) handleEpTableEvent(ev events.Event) { if isAdd { if svcID != "" { - if err := c.addServiceBinding(svcName, svcID, nid, eid, vip, ingressPorts, ip); err != nil { + if err := c.addServiceBinding(svcName, svcID, nid, eid, vip, ingressPorts, aliases, ip); err != nil { logrus.Errorf("Failed adding service binding for value %s: %v", value, err) return } @@ -571,7 +573,7 @@ func (c *controller) handleEpTableEvent(ev events.Event) { n.addSvcRecords(name, ip, nil, true) } else { if svcID != "" { - if err := c.rmServiceBinding(svcName, svcID, nid, eid, vip, ingressPorts, ip); err != nil { + if err := c.rmServiceBinding(svcName, svcID, nid, eid, vip, ingressPorts, aliases, ip); err != nil { logrus.Errorf("Failed adding service binding for value %s: %v", value, err) return } diff --git a/libnetwork/agent.pb.go b/libnetwork/agent.pb.go index 19b30422ad..e9d2c8dee9 100644 --- a/libnetwork/agent.pb.go +++ b/libnetwork/agent.pb.go @@ -72,6 +72,8 @@ type EndpointRecord struct { EndpointIP string `protobuf:"bytes,5,opt,name=endpoint_ip,json=endpointIp,proto3" json:"endpoint_ip,omitempty"` // IngressPorts exposed by the service to which this endpoint belongs. IngressPorts []*PortConfig `protobuf:"bytes,6,rep,name=ingress_ports,json=ingressPorts" json:"ingress_ports,omitempty"` + // A list of aliases which are alternate names for the service + Aliases []string `protobuf:"bytes,7,rep,name=aliases" json:"aliases,omitempty"` } func (m *EndpointRecord) Reset() { *m = EndpointRecord{} } @@ -120,7 +122,7 @@ func (this *EndpointRecord) GoString() string { if this == nil { return "nil" } - s := make([]string, 0, 10) + s := make([]string, 0, 11) s = append(s, "&libnetwork.EndpointRecord{") s = append(s, "Name: "+fmt.Sprintf("%#v", this.Name)+",\n") s = append(s, "ServiceName: "+fmt.Sprintf("%#v", this.ServiceName)+",\n") @@ -130,6 +132,7 @@ func (this *EndpointRecord) GoString() string { if this.IngressPorts != nil { s = append(s, "IngressPorts: "+fmt.Sprintf("%#v", this.IngressPorts)+",\n") } + s = append(s, "Aliases: "+fmt.Sprintf("%#v", this.Aliases)+",\n") s = append(s, "}") return strings.Join(s, "") } @@ -228,6 +231,21 @@ func (m *EndpointRecord) MarshalTo(data []byte) (int, error) { i += n } } + if len(m.Aliases) > 0 { + for _, s := range m.Aliases { + data[i] = 0x3a + i++ + l = len(s) + for l >= 1<<7 { + data[i] = uint8(uint64(l)&0x7f | 0x80) + l >>= 7 + i++ + } + data[i] = uint8(l) + i++ + i += copy(data[i:], s) + } + } return i, nil } @@ -326,6 +344,12 @@ func (m *EndpointRecord) Size() (n int) { n += 1 + l + sovAgent(uint64(l)) } } + if len(m.Aliases) > 0 { + for _, s := range m.Aliases { + l = len(s) + n += 1 + l + sovAgent(uint64(l)) + } + } return n } @@ -372,6 +396,7 @@ func (this *EndpointRecord) String() string { `VirtualIP:` + fmt.Sprintf("%v", this.VirtualIP) + `,`, `EndpointIP:` + fmt.Sprintf("%v", this.EndpointIP) + `,`, `IngressPorts:` + strings.Replace(fmt.Sprintf("%v", this.IngressPorts), "PortConfig", "PortConfig", 1) + `,`, + `Aliases:` + fmt.Sprintf("%v", this.Aliases) + `,`, `}`, }, "") return s @@ -602,6 +627,35 @@ func (m *EndpointRecord) Unmarshal(data []byte) error { return err } iNdEx = postIndex + case 7: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Aliases", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAgent + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := data[iNdEx] + iNdEx++ + stringLen |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthAgent + } + postIndex := iNdEx + intStringLen + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Aliases = append(m.Aliases, string(data[iNdEx:postIndex])) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipAgent(data[iNdEx:]) @@ -865,29 +919,30 @@ var ( ) var fileDescriptorAgent = []byte{ - // 384 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x6c, 0x90, 0x3f, 0x6f, 0xda, 0x40, - 0x18, 0xc6, 0x31, 0xb8, 0x08, 0xbf, 0xc6, 0x2e, 0x3a, 0x55, 0x95, 0xc5, 0x60, 0x28, 0x52, 0x25, - 0x86, 0xca, 0x48, 0x74, 0x64, 0x03, 0x3a, 0x78, 0xa9, 0x2c, 0xf7, 0xcf, 0x8a, 0x0c, 0xbe, 0xba, - 0xa7, 0xba, 0x3e, 0xeb, 0x7c, 0xd0, 0xb5, 0x63, 0x94, 0x2d, 0x1f, 0x20, 0x53, 0xbe, 0x4c, 0xc6, - 0x8c, 0x99, 0xa2, 0xc0, 0x9a, 0x25, 0x1f, 0x21, 0x77, 0x67, 0x1b, 0x14, 0x89, 0xe1, 0x95, 0x4e, - 0xbf, 0xe7, 0xf7, 0x9e, 0x5e, 0x3d, 0x60, 0x46, 0x09, 0xce, 0xb8, 0x97, 0x33, 0xca, 0x29, 0x82, - 0x94, 0xac, 0x33, 0xcc, 0xff, 0x51, 0xf6, 0xa7, 0xff, 0x2e, 0xa1, 0x09, 0x55, 0x78, 0x22, 0x5f, - 0xa5, 0x31, 0xba, 0x6a, 0x82, 0xfd, 0x25, 0x8b, 0x73, 0x4a, 0x32, 0x1e, 0xe2, 0x0d, 0x65, 0x31, - 0x42, 0xa0, 0x67, 0xd1, 0x5f, 0xec, 0x68, 0x43, 0x6d, 0x6c, 0x84, 0xea, 0x8d, 0x3e, 0x40, 0xb7, - 0xc0, 0x6c, 0x47, 0x36, 0x78, 0xa5, 0xb2, 0xa6, 0xca, 0xcc, 0x8a, 0x7d, 0x95, 0xca, 0x27, 0x80, - 0x5a, 0x21, 0xb1, 0xd3, 0x92, 0xc2, 0xdc, 0x3a, 0x3c, 0x0c, 0x8c, 0x6f, 0x25,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0xde, 0xd5, 0xfe, 0x1f, 0x5c, 0xed, 0x56, 0xcc, 0x9d, 0x98, 0x47, 0x31, 0xeb, 0xb6, 0xba, - 0xf8, 0xf3, 0x4b, 0x00, 0x00, 0x00, 0xff, 0xff, 0x9e, 0x6d, 0x44, 0x68, 0x53, 0x02, 0x00, 0x00, + // 397 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x6c, 0x90, 0xbf, 0xae, 0xd3, 0x30, + 0x14, 0xc6, 0x9b, 0xdb, 0x70, 0x6f, 0x73, 0xd2, 0x84, 0xca, 0x42, 0x28, 0xea, 0x90, 0x96, 0x4a, + 0x48, 0x1d, 0x50, 0x2a, 0x95, 0xb1, 0x5b, 0x5b, 0x86, 0x2c, 0x28, 0x32, 0x7f, 0xd6, 0x2a, 0x6d, + 0x4c, 0xb0, 0x08, 0x71, 0x64, 0xbb, 0x65, 0x65, 0x44, 0xbc, 0x03, 0x13, 0x23, 0x2f, 0xc2, 0xc8, + 0xc8, 0x84, 0x68, 0x57, 0x16, 0x1e, 0x01, 0xdb, 0x49, 0x5a, 0x21, 0x75, 0xb0, 0xe4, 0xfc, 0xce, + 0xef, 0x4b, 0x4e, 0x3e, 0x70, 0xd3, 0x9c, 0x94, 0x32, 0xaa, 0x38, 0x93, 0x0c, 0x41, 0x41, 0xb7, + 0x25, 0x91, 0x1f, 0x18, 0x7f, 0x37, 0x7c, 0x90, 0xb3, 0x9c, 0x19, 0x3c, 0xd3, 0xb7, 0xda, 0x98, + 0x7c, 0xbb, 0x01, 0xff, 0x59, 0x99, 0x55, 0x8c, 0x96, 0x12, 0x93, 0x1d, 0xe3, 0x19, 0x42, 0x60, + 0x97, 0xe9, 0x7b, 0x12, 0x58, 0x63, 0x6b, 0xea, 0x60, 0x73, 0x47, 0x8f, 0xa0, 0x2f, 0x08, 0x3f, + 0xd0, 0x1d, 0xd9, 0x98, 0xd9, 0x8d, 0x99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0x4a, 0x06, 0x9d, + 0xe1, 0xfd, 0xcf, 0x5f, 0xc6, 0x6e, 0x8b, 0x15, 0xd2, 0x93, 0x57, 0xeb, 0x64, 0x60, 0xfd, 0x3f, + 0x51, 0x68, 0x68, 0x7f, 0xfa, 0x1a, 0x76, 0x96, 0xc1, 0xcf, 0x63, 0xd8, 0xf9, 0x7b, 0x0c, 0xad, + 0x8f, 0xa7, 0xd0, 0xfa, 0xae, 0xce, 0x0f, 0x75, 0x7e, 0xab, 0xb3, 0xbd, 0x35, 0x1b, 0x3f, 0xfd, + 0x17, 0x00, 0x00, 0xff, 0xff, 0xc5, 0x58, 0xc7, 0xbd, 0x6d, 0x02, 0x00, 0x00, } diff --git a/libnetwork/agent.proto b/libnetwork/agent.proto index 5d2b096755..2157ce40cd 100644 --- a/libnetwork/agent.proto +++ b/libnetwork/agent.proto @@ -31,6 +31,9 @@ message EndpointRecord { // IngressPorts exposed by the service to which this endpoint belongs. repeated PortConfig ingress_ports = 6; + + // A list of aliases which are alternate names for the service + repeated string aliases = 7; } // PortConfig specifies an exposed port which can be diff --git a/libnetwork/endpoint.go b/libnetwork/endpoint.go index 043c3f1643..1c9bf8291c 100644 --- a/libnetwork/endpoint.go +++ b/libnetwork/endpoint.go @@ -70,6 +70,7 @@ type endpoint struct { svcID string svcName string virtualIP net.IP + svcAliases []string ingressPorts []*PortConfig dbIndex uint64 dbExists bool @@ -98,6 +99,7 @@ func (ep *endpoint) MarshalJSON() ([]byte, error) { epMap["svcID"] = ep.svcID epMap["virtualIP"] = ep.virtualIP.String() epMap["ingressPorts"] = ep.ingressPorts + epMap["svcAliases"] = ep.svcAliases return json.Marshal(epMap) } @@ -198,6 +200,11 @@ func (ep *endpoint) UnmarshalJSON(b []byte) (err error) { ep.virtualIP = net.ParseIP(vip.(string)) } + sal, _ := json.Marshal(epMap["svcAliases"]) + var svcAliases []string + json.Unmarshal(sal, &svcAliases) + ep.svcAliases = svcAliases + pc, _ := json.Marshal(epMap["ingressPorts"]) var ingressPorts []*PortConfig json.Unmarshal(pc, &ingressPorts) @@ -231,6 +238,9 @@ func (ep *endpoint) CopyTo(o datastore.KVObject) error { dstEp.svcID = ep.svcID dstEp.virtualIP = ep.virtualIP + dstEp.svcAliases = make([]string, len(ep.svcAliases)) + copy(dstEp.svcAliases, ep.svcAliases) + dstEp.ingressPorts = make([]*PortConfig, len(ep.ingressPorts)) copy(dstEp.ingressPorts, ep.ingressPorts) @@ -935,12 +945,13 @@ func CreateOptionAlias(name string, alias string) EndpointOption { } // CreateOptionService function returns an option setter for setting service binding configuration -func CreateOptionService(name, id string, vip net.IP, ingressPorts []*PortConfig) EndpointOption { +func CreateOptionService(name, id string, vip net.IP, ingressPorts []*PortConfig, aliases []string) EndpointOption { return func(ep *endpoint) { ep.svcName = name ep.svcID = id ep.virtualIP = vip ep.ingressPorts = ingressPorts + ep.svcAliases = aliases } } diff --git a/libnetwork/service_linux.go b/libnetwork/service_linux.go index 204c59d9ff..1d36c4538d 100644 --- a/libnetwork/service_linux.go +++ b/libnetwork/service_linux.go @@ -37,7 +37,7 @@ func newService(name string, id string, ingressPorts []*PortConfig) *service { } } -func (c *controller) addServiceBinding(name, sid, nid, eid string, vip net.IP, ingressPorts []*PortConfig, ip net.IP) error { +func (c *controller) addServiceBinding(name, sid, nid, eid string, vip net.IP, ingressPorts []*PortConfig, aliases []string, ip net.IP) error { var ( s *service addService bool @@ -61,6 +61,9 @@ func (c *controller) addServiceBinding(name, sid, nid, eid string, vip net.IP, i // Add endpoint IP to special "tasks.svc_name" so that the // applications have access to DNS RR. n.(*network).addSvcRecords("tasks."+name, ip, nil, false) + for _, alias := range aliases { + n.(*network).addSvcRecords("tasks."+alias, ip, nil, false) + } // Add service name to vip in DNS, if vip is valid. Otherwise resort to DNS RR svcIP := vip @@ -68,6 +71,9 @@ func (c *controller) addServiceBinding(name, sid, nid, eid string, vip net.IP, i svcIP = ip } n.(*network).addSvcRecords(name, svcIP, nil, false) + for _, alias := range aliases { + n.(*network).addSvcRecords(alias, svcIP, nil, false) + } s.Lock() defer s.Unlock() @@ -107,7 +113,7 @@ func (c *controller) addServiceBinding(name, sid, nid, eid string, vip net.IP, i return nil } -func (c *controller) rmServiceBinding(name, sid, nid, eid string, vip net.IP, ingressPorts []*PortConfig, ip net.IP) error { +func (c *controller) rmServiceBinding(name, sid, nid, eid string, vip net.IP, ingressPorts []*PortConfig, aliases []string, ip net.IP) error { var rmService bool n, err := c.NetworkByID(nid) @@ -125,6 +131,9 @@ func (c *controller) rmServiceBinding(name, sid, nid, eid string, vip net.IP, in // Delete the special "tasks.svc_name" backend record. n.(*network).deleteSvcRecords("tasks."+name, ip, nil, false) + for _, alias := range aliases { + n.(*network).deleteSvcRecords("tasks."+alias, ip, nil, false) + } // Make sure to remove the right IP since if vip is // not valid we would have added a DNS RR record. @@ -133,6 +142,9 @@ func (c *controller) rmServiceBinding(name, sid, nid, eid string, vip net.IP, in svcIP = ip } n.(*network).deleteSvcRecords(name, svcIP, nil, false) + for _, alias := range aliases { + n.(*network).deleteSvcRecords(alias, svcIP, nil, false) + } s.Lock() defer s.Unlock() diff --git a/libnetwork/service_unsupported.go b/libnetwork/service_unsupported.go index 67984e2aba..9668dcc07e 100644 --- a/libnetwork/service_unsupported.go +++ b/libnetwork/service_unsupported.go @@ -7,11 +7,11 @@ import ( "net" ) -func (c *controller) addServiceBinding(name, sid, nid, eid string, vip net.IP, ingressPorts []*PortConfig, ip net.IP) error { +func (c *controller) addServiceBinding(name, sid, nid, eid string, vip net.IP, ingressPorts []*PortConfig, aliases []string, ip net.IP) error { return fmt.Errorf("not supported") } -func (c *controller) rmServiceBinding(name, sid, nid, eid string, vip net.IP, ingressPorts []*PortConfig, ip net.IP) error { +func (c *controller) rmServiceBinding(name, sid, nid, eid string, vip net.IP, ingressPorts []*PortConfig, aliases []string, ip net.IP) error { return fmt.Errorf("not supported") }