Skip to content

Commit c73f6bb

Browse files
authored
Merge pull request #12 from cloudscale-ch/denis/service-port-mapping
Use a more flexible port to pool mapping
2 parents 1b1def8 + cb05334 commit c73f6bb

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

examples/nginx-hello.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ spec:
4242
- port: 80
4343
protocol: TCP
4444
targetPort: 80
45-
name: primary
45+
name: http
4646
selector:
4747
app: hello
4848
type: LoadBalancer

pkg/cloudscale_ccm/reconcile.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func desiredLbState(
146146
}
147147

148148
pool := cloudscale.LoadBalancerPool{
149-
Name: poolName(port.Protocol, port.Port),
149+
Name: poolName(port.Protocol, port.Name),
150150
Algorithm: algorithm,
151151
Protocol: protocol,
152152
}
@@ -954,10 +954,20 @@ func (l *lbState) poolsByName() map[string]*cloudscale.LoadBalancerPool {
954954
// poolName produces the name of the pool for the given service port (the port
955955
// that is bound on the load balancer and reachable from outside of it).
956956
//
957+
// We use the name of the port (may be empty, but is enforced to be unqiue
958+
// for each service).
959+
//
957960
// Warning: This named is used to compare desired pools to actual pools.
958961
// Any change to it causes pools to be rebuilt, which must be avoided!
959-
func poolName(protocol v1.Protocol, port int32) string {
960-
return strings.ToLower(fmt.Sprintf("%s/%d", protocol, port))
962+
func poolName(protocol v1.Protocol, name string) string {
963+
p := strings.ToLower(string(protocol))
964+
965+
// By default, the port has no name (required with more than 1 port)
966+
if name == "" {
967+
return p
968+
}
969+
970+
return fmt.Sprintf("%s/%s", p, name)
961971
}
962972

963973
// poolMemberName produces the name of the pool member for the given node

pkg/cloudscale_ccm/reconcile_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import (
1313
)
1414

1515
func TestPoolName(t *testing.T) {
16-
assert.Equal(t, "tcp/80", poolName(v1.ProtocolTCP, 80))
17-
assert.Equal(t, "udp/443", poolName(v1.ProtocolUDP, 443))
16+
assert.Equal(t, "tcp", poolName(v1.ProtocolTCP, ""))
17+
assert.Equal(t, "udp/foo", poolName(v1.ProtocolUDP, "foo"))
18+
assert.Equal(t, "udp/FOO", poolName(v1.ProtocolUDP, "FOO"))
1819
}
1920

2021
func TestPoolMemberName(t *testing.T) {
@@ -126,11 +127,13 @@ func TestDesiredService(t *testing.T) {
126127
Protocol: "TCP",
127128
Port: 80,
128129
NodePort: 8080,
130+
Name: "http",
129131
},
130132
{
131133
Protocol: "TCP",
132134
Port: 443,
133135
NodePort: 8443,
136+
Name: "https",
134137
},
135138
}
136139

@@ -143,10 +146,10 @@ func TestDesiredService(t *testing.T) {
143146

144147
// Have one pool per service port
145148
assert.Len(t, desired.pools, 2)
146-
assert.Equal(t, desired.pools[0].Name, "tcp/80")
149+
assert.Equal(t, desired.pools[0].Name, "tcp/http")
147150
assert.Equal(t, desired.pools[0].Protocol, "tcp")
148151
assert.Equal(t, desired.pools[0].Algorithm, "round_robin")
149-
assert.Equal(t, desired.pools[1].Name, "tcp/443")
152+
assert.Equal(t, desired.pools[1].Name, "tcp/https")
150153
assert.Equal(t, desired.pools[0].Protocol, "tcp")
151154
assert.Equal(t, desired.pools[0].Algorithm, "round_robin")
152155

0 commit comments

Comments
 (0)