Skip to content
This repository has been archived by the owner on Aug 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #241 from joshwget/convert-v1-lb-selector
Browse files Browse the repository at this point in the history
Convert v1 selector based LB to v2
  • Loading branch information
ibuildthecloud authored Jan 11, 2017
2 parents 1a5c272 + cc0ee98 commit 5291f33
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 21 deletions.
2 changes: 1 addition & 1 deletion rancher/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func createLaunchConfig(r *RancherService, name string, serviceConfig *config.Se

// Strip off legacy load balancer labels
for k, v := range serviceConfig.Labels {
if !strings.HasPrefix(k, "io.rancher.loadbalancer") {
if !strings.HasPrefix(k, "io.rancher.loadbalancer") && !strings.HasPrefix(k, "io.rancher.service.selector") {
newLabels[k] = v
}
}
Expand Down
21 changes: 16 additions & 5 deletions rancher/convert_lb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func TestRewritePorts(t *testing.T) {
testRewritePorts(t, "80:80/tcp", "80/tcp")
}

func testConvertLb(t *testing.T, ports, links, externalLinks []string, expectedPortRules []PortRule) {
portRules, err := convertLb(ports, links, externalLinks)
func testConvertLb(t *testing.T, ports, links, externalLinks []string, selector string, expectedPortRules []PortRule) {
portRules, err := convertLb(ports, links, externalLinks, selector)
if err != nil {
t.Fatal(err)
}
Expand All @@ -67,7 +67,7 @@ func testConvertLb(t *testing.T, ports, links, externalLinks []string, expectedP
func TestConvertLb(t *testing.T) {
testConvertLb(t, []string{
"8080:80",
}, []string{"web1", "web2"}, []string{"external/web3"}, []PortRule{
}, []string{"web1", "web2"}, []string{"external/web3"}, "", []PortRule{
PortRule{
SourcePort: 8080,
TargetPort: 80,
Expand All @@ -90,7 +90,7 @@ func TestConvertLb(t *testing.T) {

testConvertLb(t, []string{
"80",
}, []string{"web1", "web2"}, []string{}, []PortRule{
}, []string{"web1", "web2"}, []string{}, "", []PortRule{
PortRule{
SourcePort: 80,
TargetPort: 80,
Expand All @@ -107,7 +107,7 @@ func TestConvertLb(t *testing.T) {

testConvertLb(t, []string{
"80/tcp",
}, []string{"web1", "web2"}, []string{}, []PortRule{
}, []string{"web1", "web2"}, []string{}, "", []PortRule{
PortRule{
SourcePort: 80,
TargetPort: 80,
Expand All @@ -121,6 +121,17 @@ func TestConvertLb(t *testing.T) {
Protocol: "tcp",
},
})

testConvertLb(t, []string{
"80/tcp",
}, nil, nil, "foo=bar", []PortRule{
PortRule{
SourcePort: 80,
TargetPort: 80,
Selector: "foo=bar",
Protocol: "tcp",
},
})
}

func testConvertLabel(t *testing.T, label string, expectedPortRules []PortRule) {
Expand Down
56 changes: 41 additions & 15 deletions rancher/lb_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,33 @@ func populateLbFields(r *RancherService, launchConfig *client.LaunchConfig, serv
Postonly: legacyStickinessPolicy.Postonly,
}
}
portRules, err := convertLb(r.serviceConfig.Ports, r.serviceConfig.Links, r.serviceConfig.ExternalLinks)
portRules, err := convertLb(r.serviceConfig.Ports, r.serviceConfig.Links, r.serviceConfig.ExternalLinks, "")
if err != nil {
return err
}
exposeRules, err := convertLb(r.serviceConfig.Expose, r.serviceConfig.Links, r.serviceConfig.ExternalLinks)
exposeRules, err := convertLb(r.serviceConfig.Expose, r.serviceConfig.Links, r.serviceConfig.ExternalLinks, "")
portRules = append(portRules, exposeRules...)
labelName := "io.rancher.service.selector.link"
if label, ok := r.serviceConfig.Labels[labelName]; ok {
selectorPortRules, err := convertLb(r.serviceConfig.Ports, nil, nil, label)
if err != nil {
return err
}
portRules = append(portRules, selectorPortRules...)
selectorExposeRules, err := convertLb(r.serviceConfig.Expose, nil, nil, label)
if err != nil {
return err
}
portRules = append(portRules, selectorExposeRules...)

}

links, err := r.getLinks()
if err != nil {
return err
}
for link := range links {
labelName := "io.rancher.loadbalancer.target." + link.ServiceName
labelName = "io.rancher.loadbalancer.target." + link.ServiceName
if label, ok := r.serviceConfig.Labels[labelName]; ok {
newPortRules, err := convertLbLabel(label)
if err != nil {
Expand All @@ -88,7 +103,7 @@ func populateLbFields(r *RancherService, launchConfig *client.LaunchConfig, serv
portRules = mergePortRules(portRules, newPortRules)
}
}
labelName := "io.rancher.loadbalancer.ssl.ports"
labelName = "io.rancher.loadbalancer.ssl.ports"
if label, ok := r.serviceConfig.Labels[labelName]; ok {
split := strings.Split(label, ",")
for _, portString := range split {
Expand All @@ -113,24 +128,27 @@ frontend %s
}
}
for _, portRule := range portRules {
targetService, err := r.FindExisting(portRule.Service)
if err != nil {
return err
}
if targetService == nil {
return fmt.Errorf("Failed to find existing service: %s", portRule.Service)
}
service.RealLbConfig.PortRules = append(service.RealLbConfig.PortRules, client.PortRule{
finalPortRule := client.PortRule{
SourcePort: int64(portRule.SourcePort),
Protocol: portRule.Protocol,
Path: portRule.Path,
Hostname: portRule.Hostname,
ServiceId: targetService.Id,
TargetPort: int64(portRule.TargetPort),
Priority: int64(portRule.Priority),
BackendName: portRule.BackendName,
Selector: portRule.Selector,
})
}
if portRule.Service != "" {
targetService, err := r.FindExisting(portRule.Service)
if err != nil {
return err
}
if targetService == nil {
return fmt.Errorf("Failed to find existing service: %s", portRule.Service)
}
finalPortRule.ServiceId = targetService.Id
}
service.RealLbConfig.PortRules = append(service.RealLbConfig.PortRules, finalPortRule)
}

// Strip target ports from lb service config
Expand Down Expand Up @@ -258,7 +276,7 @@ func rewritePorts(ports []string) ([]string, error) {
return updatedPorts, nil
}

func convertLb(ports, links, externalLinks []string) ([]PortRule, error) {
func convertLb(ports, links, externalLinks []string, selector string) ([]PortRule, error) {
portRules := []PortRule{}

for _, port := range ports {
Expand Down Expand Up @@ -317,6 +335,14 @@ func convertLb(ports, links, externalLinks []string) ([]PortRule, error) {
Protocol: protocol,
})
}
if selector != "" {
portRules = append(portRules, PortRule{
SourcePort: int(sourcePort),
TargetPort: int(targetPort),
Selector: selector,
Protocol: protocol,
})
}
}

return portRules, nil
Expand Down

0 comments on commit 5291f33

Please sign in to comment.