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

Commit 5291f33

Browse files
Merge pull request #241 from joshwget/convert-v1-lb-selector
Convert v1 selector based LB to v2
2 parents 1a5c272 + cc0ee98 commit 5291f33

File tree

3 files changed

+58
-21
lines changed

3 files changed

+58
-21
lines changed

rancher/configs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func createLaunchConfig(r *RancherService, name string, serviceConfig *config.Se
7070

7171
// Strip off legacy load balancer labels
7272
for k, v := range serviceConfig.Labels {
73-
if !strings.HasPrefix(k, "io.rancher.loadbalancer") {
73+
if !strings.HasPrefix(k, "io.rancher.loadbalancer") && !strings.HasPrefix(k, "io.rancher.service.selector") {
7474
newLabels[k] = v
7575
}
7676
}

rancher/convert_lb_test.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ func TestRewritePorts(t *testing.T) {
5252
testRewritePorts(t, "80:80/tcp", "80/tcp")
5353
}
5454

55-
func testConvertLb(t *testing.T, ports, links, externalLinks []string, expectedPortRules []PortRule) {
56-
portRules, err := convertLb(ports, links, externalLinks)
55+
func testConvertLb(t *testing.T, ports, links, externalLinks []string, selector string, expectedPortRules []PortRule) {
56+
portRules, err := convertLb(ports, links, externalLinks, selector)
5757
if err != nil {
5858
t.Fatal(err)
5959
}
@@ -67,7 +67,7 @@ func testConvertLb(t *testing.T, ports, links, externalLinks []string, expectedP
6767
func TestConvertLb(t *testing.T) {
6868
testConvertLb(t, []string{
6969
"8080:80",
70-
}, []string{"web1", "web2"}, []string{"external/web3"}, []PortRule{
70+
}, []string{"web1", "web2"}, []string{"external/web3"}, "", []PortRule{
7171
PortRule{
7272
SourcePort: 8080,
7373
TargetPort: 80,
@@ -90,7 +90,7 @@ func TestConvertLb(t *testing.T) {
9090

9191
testConvertLb(t, []string{
9292
"80",
93-
}, []string{"web1", "web2"}, []string{}, []PortRule{
93+
}, []string{"web1", "web2"}, []string{}, "", []PortRule{
9494
PortRule{
9595
SourcePort: 80,
9696
TargetPort: 80,
@@ -107,7 +107,7 @@ func TestConvertLb(t *testing.T) {
107107

108108
testConvertLb(t, []string{
109109
"80/tcp",
110-
}, []string{"web1", "web2"}, []string{}, []PortRule{
110+
}, []string{"web1", "web2"}, []string{}, "", []PortRule{
111111
PortRule{
112112
SourcePort: 80,
113113
TargetPort: 80,
@@ -121,6 +121,17 @@ func TestConvertLb(t *testing.T) {
121121
Protocol: "tcp",
122122
},
123123
})
124+
125+
testConvertLb(t, []string{
126+
"80/tcp",
127+
}, nil, nil, "foo=bar", []PortRule{
128+
PortRule{
129+
SourcePort: 80,
130+
TargetPort: 80,
131+
Selector: "foo=bar",
132+
Protocol: "tcp",
133+
},
134+
})
124135
}
125136

126137
func testConvertLabel(t *testing.T, label string, expectedPortRules []PortRule) {

rancher/lb_service.go

+41-15
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,33 @@ func populateLbFields(r *RancherService, launchConfig *client.LaunchConfig, serv
6565
Postonly: legacyStickinessPolicy.Postonly,
6666
}
6767
}
68-
portRules, err := convertLb(r.serviceConfig.Ports, r.serviceConfig.Links, r.serviceConfig.ExternalLinks)
68+
portRules, err := convertLb(r.serviceConfig.Ports, r.serviceConfig.Links, r.serviceConfig.ExternalLinks, "")
6969
if err != nil {
7070
return err
7171
}
72-
exposeRules, err := convertLb(r.serviceConfig.Expose, r.serviceConfig.Links, r.serviceConfig.ExternalLinks)
72+
exposeRules, err := convertLb(r.serviceConfig.Expose, r.serviceConfig.Links, r.serviceConfig.ExternalLinks, "")
7373
portRules = append(portRules, exposeRules...)
74+
labelName := "io.rancher.service.selector.link"
75+
if label, ok := r.serviceConfig.Labels[labelName]; ok {
76+
selectorPortRules, err := convertLb(r.serviceConfig.Ports, nil, nil, label)
77+
if err != nil {
78+
return err
79+
}
80+
portRules = append(portRules, selectorPortRules...)
81+
selectorExposeRules, err := convertLb(r.serviceConfig.Expose, nil, nil, label)
82+
if err != nil {
83+
return err
84+
}
85+
portRules = append(portRules, selectorExposeRules...)
86+
87+
}
88+
7489
links, err := r.getLinks()
7590
if err != nil {
7691
return err
7792
}
7893
for link := range links {
79-
labelName := "io.rancher.loadbalancer.target." + link.ServiceName
94+
labelName = "io.rancher.loadbalancer.target." + link.ServiceName
8095
if label, ok := r.serviceConfig.Labels[labelName]; ok {
8196
newPortRules, err := convertLbLabel(label)
8297
if err != nil {
@@ -88,7 +103,7 @@ func populateLbFields(r *RancherService, launchConfig *client.LaunchConfig, serv
88103
portRules = mergePortRules(portRules, newPortRules)
89104
}
90105
}
91-
labelName := "io.rancher.loadbalancer.ssl.ports"
106+
labelName = "io.rancher.loadbalancer.ssl.ports"
92107
if label, ok := r.serviceConfig.Labels[labelName]; ok {
93108
split := strings.Split(label, ",")
94109
for _, portString := range split {
@@ -113,24 +128,27 @@ frontend %s
113128
}
114129
}
115130
for _, portRule := range portRules {
116-
targetService, err := r.FindExisting(portRule.Service)
117-
if err != nil {
118-
return err
119-
}
120-
if targetService == nil {
121-
return fmt.Errorf("Failed to find existing service: %s", portRule.Service)
122-
}
123-
service.RealLbConfig.PortRules = append(service.RealLbConfig.PortRules, client.PortRule{
131+
finalPortRule := client.PortRule{
124132
SourcePort: int64(portRule.SourcePort),
125133
Protocol: portRule.Protocol,
126134
Path: portRule.Path,
127135
Hostname: portRule.Hostname,
128-
ServiceId: targetService.Id,
129136
TargetPort: int64(portRule.TargetPort),
130137
Priority: int64(portRule.Priority),
131138
BackendName: portRule.BackendName,
132139
Selector: portRule.Selector,
133-
})
140+
}
141+
if portRule.Service != "" {
142+
targetService, err := r.FindExisting(portRule.Service)
143+
if err != nil {
144+
return err
145+
}
146+
if targetService == nil {
147+
return fmt.Errorf("Failed to find existing service: %s", portRule.Service)
148+
}
149+
finalPortRule.ServiceId = targetService.Id
150+
}
151+
service.RealLbConfig.PortRules = append(service.RealLbConfig.PortRules, finalPortRule)
134152
}
135153

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

261-
func convertLb(ports, links, externalLinks []string) ([]PortRule, error) {
279+
func convertLb(ports, links, externalLinks []string, selector string) ([]PortRule, error) {
262280
portRules := []PortRule{}
263281

264282
for _, port := range ports {
@@ -317,6 +335,14 @@ func convertLb(ports, links, externalLinks []string) ([]PortRule, error) {
317335
Protocol: protocol,
318336
})
319337
}
338+
if selector != "" {
339+
portRules = append(portRules, PortRule{
340+
SourcePort: int(sourcePort),
341+
TargetPort: int(targetPort),
342+
Selector: selector,
343+
Protocol: protocol,
344+
})
345+
}
320346
}
321347

322348
return portRules, nil

0 commit comments

Comments
 (0)