Skip to content

Commit

Permalink
Fix udp service access through k3d-proxy
Browse files Browse the repository at this point in the history
This commit separates PORTS and UDP_PORTS and configures
nginx appropriately.

Signed-off-by: Dinar Valeev <[email protected]>
  • Loading branch information
k0da committed Feb 8, 2021
1 parent 58d37be commit b454a5f
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 6 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/gogo/googleapis v1.4.0 // indirect
github.com/heroku/docker-registry-client v0.0.0-20190909225348-afc9e1acc3d5
github.com/imdario/mergo v0.3.9
github.com/iwilltry42/confd v0.16.1 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.3.3 // indirect
Expand Down
52 changes: 52 additions & 0 deletions go.sum

Large diffs are not rendered by default.

17 changes: 13 additions & 4 deletions pkg/client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,15 @@ ClusterCreatOpts:
}

// generate comma-separated list of extra ports to forward
ports := k3d.DefaultAPIPort
ports := []string{k3d.DefaultAPIPort}
var udp_ports []string
for exposedPort := range cluster.ServerLoadBalancer.Ports {
ports += "," + exposedPort.Port()
if exposedPort.Proto() == "udp" {
udp_ports = append(udp_ports, exposedPort.Port())
continue
}
ports = append(ports, exposedPort.Port())

}

if cluster.ServerLoadBalancer.Ports == nil {
Expand All @@ -523,14 +529,17 @@ ClusterCreatOpts:
Ports: cluster.ServerLoadBalancer.Ports,
Env: []string{
fmt.Sprintf("SERVERS=%s", servers),
fmt.Sprintf("PORTS=%s", ports),
fmt.Sprintf("WORKER_PROCESSES=%d", len(strings.Split(ports, ","))),
fmt.Sprintf("PORTS=%s", strings.Join(ports, ",")),
fmt.Sprintf("WORKER_PROCESSES=%d", len(ports)),
},
Role: k3d.LoadBalancerRole,
Labels: clusterCreateOpts.GlobalLabels, // TODO: createLoadBalancer: add more expressive labels
Networks: []string{cluster.Network.Name},
Restart: true,
}
if len(udp_ports) > 0 {
lbNode.Env = append(lbNode.Env, fmt.Sprintf("UDP_PORTS=%s", strings.Join(udp_ports, ",")))
}
cluster.Nodes = append(cluster.Nodes, lbNode) // append lbNode to list of cluster nodes, so it will be considered during rollback
log.Infof("Creating LoadBalancer '%s'", lbNode.Name)
if err := NodeCreate(clusterCreateCtx, runtime, lbNode, k3d.NodeCreateOpts{}); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion proxy/conf.d/nginx.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ dest = "/etc/nginx/nginx.conf"
keys = [
"SERVERS",
"PORTS",
]
]
19 changes: 18 additions & 1 deletion proxy/templates/nginx.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{{- $servers := split (getenv "SERVERS") "," -}}
{{- $ports := split (getenv "PORTS") "," -}}
{{- $udp_ports := split (getenv "UDP_PORTS") "," -}}
error_log stderr notice;

worker_processes auto;
Expand All @@ -24,4 +25,20 @@ stream {
proxy_connect_timeout 2s;
}
{{- end }}
}
{{- range $port := $udp_ports }}
{{- if $port }}
upstream server_nodes_{{ $port }} {
{{- range $server := $servers }}
server {{ $server }}:{{ $port }} max_fails=1 fail_timeout=10s;
{{- end }}
}

server {
listen {{ $port }} udp;
proxy_pass server_nodes_{{ $port }};
proxy_timeout 600;
proxy_connect_timeout 2s;
}
{{- end }}
{{- end }}
}
2 changes: 2 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ github.com/heroku/docker-registry-client/registry
github.com/imdario/mergo
# github.com/inconshreveable/mousetrap v1.0.0
github.com/inconshreveable/mousetrap
# github.com/iwilltry42/confd v0.16.1
## explicit
# github.com/json-iterator/go v1.1.8
github.com/json-iterator/go
# github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de
Expand Down

0 comments on commit b454a5f

Please sign in to comment.