Skip to content

Commit

Permalink
add uid per host
Browse files Browse the repository at this point in the history
Signed-off-by: Kuat Yessenov <[email protected]>
  • Loading branch information
kyessenov committed May 22, 2018
1 parent d9121f0 commit 15253ed
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 89 deletions.
2 changes: 1 addition & 1 deletion envoy.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ function(services=import 'testdata/services.json',
},
},
},
metadata: {},
[if 'uid' in endpoint then 'metadata']: { filter_metadata: { mixer: { 'destination.uid': endpoint.uid } } },
} for endpoint in instances[service_name]],
}] else [],
}
Expand Down
12 changes: 8 additions & 4 deletions kube/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,16 @@ func (c *Controller) Instances() map[string][]model.Endpoint {
for _, ss := range ep.Subsets {
for _, ea := range ss.Addresses {
for _, port := range ss.Ports {
key := svc + ":" + port.Name
out[key] = append(out[key], model.Endpoint{
endpoint := model.Endpoint{
IP: ea.IP,
Port: int(port.Port),
})

}
pod, exists := c.pods.getPodByIP(ea.IP)
if exists {
endpoint.UID = pod.Namespace + "/" + pod.Name
}
key := svc + ":" + port.Name
out[key] = append(out[key], endpoint)
}
}
}
Expand Down
86 changes: 3 additions & 83 deletions model/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ type Endpoint struct {
IP string `json:"ip"`
Port int `json:"port"`
Protocol Protocol `json:"protocol"`

// Used by EDS
UID string
}

// Instance is a workload descriptor
Expand Down Expand Up @@ -238,89 +241,6 @@ func (s *Service) External() bool {
return s.ExternalName != ""
}

// Key generates a unique string referencing service instances for a given port and labels.
// The separator character must be exclusive to the regular expressions allowed in the
// service declaration.
func (s *Service) Key(port *Port, tag Labels) string {
// TODO: check port is non nil and membership of port in service
return ServiceKey(s.Hostname, PortList{port}, LabelsCollection{tag})
}

// ServiceKey generates a service key for a collection of ports and labels
func ServiceKey(hostname string, servicePorts PortList, labelsList LabelsCollection) string {
// example: name.namespace|http|env=prod;env=test,version=my-v1
var buffer bytes.Buffer
buffer.WriteString(hostname)
np := len(servicePorts)
nt := len(labelsList)

if nt == 1 && labelsList[0] == nil {
nt = 0
}

if np == 0 && nt == 0 {
return buffer.String()
} else if np == 1 && nt == 0 && servicePorts[0].Name == "" {
return buffer.String()
} else {
buffer.WriteString("|")
}

if np > 0 {
ports := make([]string, np)
for i := 0; i < np; i++ {
ports[i] = servicePorts[i].Name
}
sort.Strings(ports)
for i := 0; i < np; i++ {
if i > 0 {
buffer.WriteString(",")
}
buffer.WriteString(ports[i])
}
}

if nt > 0 {
buffer.WriteString("|")
labels := make([]string, nt)
for i := 0; i < nt; i++ {
labels[i] = labelsList[i].String()
}
sort.Strings(labels)
for i := 0; i < nt; i++ {
if i > 0 {
buffer.WriteString(";")
}
buffer.WriteString(labels[i])
}
}
return buffer.String()
}

// ParseServiceKey is the inverse of the Service.String() method
func ParseServiceKey(s string) (hostname string, ports PortList, labels LabelsCollection) {
parts := strings.Split(s, "|")
hostname = parts[0]

var names []string
if len(parts) > 1 {
names = strings.Split(parts[1], ",")
} else {
names = []string{""}
}

for _, name := range names {
ports = append(ports, &Port{Name: name})
}

if len(parts) > 2 && len(parts[2]) > 0 {
for _, tag := range strings.Split(parts[2], ";") {
labels = append(labels, ParseLabelsString(tag))
}
}
return
}

func (t Labels) String() string {
labels := make([]string, 0, len(t))
for k, v := range t {
Expand Down
3 changes: 2 additions & 1 deletion testdata/instances.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"hello.default.svc.cluster.local:http": [
{
"ip":"10.0.0.1",
"port": 8080
"port": 8080,
"uid": "pod2.ns3"
}
]
}

0 comments on commit 15253ed

Please sign in to comment.