@@ -65,18 +65,33 @@ func populateLbFields(r *RancherService, launchConfig *client.LaunchConfig, serv
65
65
Postonly : legacyStickinessPolicy .Postonly ,
66
66
}
67
67
}
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 , "" )
69
69
if err != nil {
70
70
return err
71
71
}
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 , "" )
73
73
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
+
74
89
links , err := r .getLinks ()
75
90
if err != nil {
76
91
return err
77
92
}
78
93
for link := range links {
79
- labelName : = "io.rancher.loadbalancer.target." + link .ServiceName
94
+ labelName = "io.rancher.loadbalancer.target." + link .ServiceName
80
95
if label , ok := r .serviceConfig .Labels [labelName ]; ok {
81
96
newPortRules , err := convertLbLabel (label )
82
97
if err != nil {
@@ -88,7 +103,7 @@ func populateLbFields(r *RancherService, launchConfig *client.LaunchConfig, serv
88
103
portRules = mergePortRules (portRules , newPortRules )
89
104
}
90
105
}
91
- labelName : = "io.rancher.loadbalancer.ssl.ports"
106
+ labelName = "io.rancher.loadbalancer.ssl.ports"
92
107
if label , ok := r .serviceConfig .Labels [labelName ]; ok {
93
108
split := strings .Split (label , "," )
94
109
for _ , portString := range split {
@@ -113,24 +128,27 @@ frontend %s
113
128
}
114
129
}
115
130
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 {
124
132
SourcePort : int64 (portRule .SourcePort ),
125
133
Protocol : portRule .Protocol ,
126
134
Path : portRule .Path ,
127
135
Hostname : portRule .Hostname ,
128
- ServiceId : targetService .Id ,
129
136
TargetPort : int64 (portRule .TargetPort ),
130
137
Priority : int64 (portRule .Priority ),
131
138
BackendName : portRule .BackendName ,
132
139
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 )
134
152
}
135
153
136
154
// Strip target ports from lb service config
@@ -258,7 +276,7 @@ func rewritePorts(ports []string) ([]string, error) {
258
276
return updatedPorts , nil
259
277
}
260
278
261
- func convertLb (ports , links , externalLinks []string ) ([]PortRule , error ) {
279
+ func convertLb (ports , links , externalLinks []string , selector string ) ([]PortRule , error ) {
262
280
portRules := []PortRule {}
263
281
264
282
for _ , port := range ports {
@@ -317,6 +335,14 @@ func convertLb(ports, links, externalLinks []string) ([]PortRule, error) {
317
335
Protocol : protocol ,
318
336
})
319
337
}
338
+ if selector != "" {
339
+ portRules = append (portRules , PortRule {
340
+ SourcePort : int (sourcePort ),
341
+ TargetPort : int (targetPort ),
342
+ Selector : selector ,
343
+ Protocol : protocol ,
344
+ })
345
+ }
320
346
}
321
347
322
348
return portRules , nil
0 commit comments