|
4 | 4 | "bytes"
|
5 | 5 | "fmt"
|
6 | 6 | "os"
|
| 7 | + "strconv" |
7 | 8 | "strings"
|
8 | 9 |
|
9 | 10 | "github.com/golang/glog"
|
@@ -132,6 +133,8 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
|
132 | 133 | ProxyHideHeaders: ingCfg.ProxyHideHeaders,
|
133 | 134 | ProxyPassHeaders: ingCfg.ProxyPassHeaders,
|
134 | 135 | ServerSnippets: ingCfg.ServerSnippets,
|
| 136 | + Ports: ingCfg.Ports, |
| 137 | + SSLPorts: ingCfg.SSLPorts, |
135 | 138 | }
|
136 | 139 |
|
137 | 140 | if pemFile, ok := pems[serverName]; ok {
|
@@ -311,6 +314,15 @@ func (cnf *Configurator) createConfig(ingEx *IngressEx) Config {
|
311 | 314 | }
|
312 | 315 | }
|
313 | 316 |
|
| 317 | + ports, sslPorts := getServicesPorts(ingEx) |
| 318 | + if len(ports) > 0 { |
| 319 | + ingCfg.Ports = ports |
| 320 | + } |
| 321 | + |
| 322 | + if len(sslPorts) > 0 { |
| 323 | + ingCfg.SSLPorts = sslPorts |
| 324 | + } |
| 325 | + |
314 | 326 | return ingCfg
|
315 | 327 | }
|
316 | 328 |
|
@@ -405,6 +417,53 @@ func parseStickyService(service string) (serviceName string, stickyCookie string
|
405 | 417 | return svcNameParts[1], parts[1], nil
|
406 | 418 | }
|
407 | 419 |
|
| 420 | +func getServicesPorts(ingEx *IngressEx) ([]int, []int) { |
| 421 | + ports := map[string][]int{} |
| 422 | + |
| 423 | + annotations := []string{ |
| 424 | + "nginx.org/listen-ports", |
| 425 | + "nginx.org/listen-ports-ssl", |
| 426 | + } |
| 427 | + |
| 428 | + for _, annotation := range annotations { |
| 429 | + if values, exists := ingEx.Ingress.Annotations[annotation]; exists { |
| 430 | + for _, value := range strings.Split(values, ",") { |
| 431 | + if port, err := parsePort(value); err != nil { |
| 432 | + glog.Errorf( |
| 433 | + "In %v %s contains invalid declaration: %v, ignoring", |
| 434 | + ingEx.Ingress.Name, |
| 435 | + annotation, |
| 436 | + err, |
| 437 | + ) |
| 438 | + } else { |
| 439 | + ports[annotation] = append(ports[annotation], port) |
| 440 | + } |
| 441 | + } |
| 442 | + } |
| 443 | + } |
| 444 | + |
| 445 | + return ports[annotations[0]], ports[annotations[1]] |
| 446 | +} |
| 447 | + |
| 448 | +func parsePort(value string) (int, error) { |
| 449 | + port, err := strconv.ParseInt(value, 10, 16) |
| 450 | + if err != nil { |
| 451 | + return 0, fmt.Errorf( |
| 452 | + "Unable to parse port as integer: %s\n", |
| 453 | + err, |
| 454 | + ) |
| 455 | + } |
| 456 | + |
| 457 | + if port <= 0 { |
| 458 | + return 0, fmt.Errorf( |
| 459 | + "Port number should be greater than zero: %q", |
| 460 | + port, |
| 461 | + ) |
| 462 | + } |
| 463 | + |
| 464 | + return int(port), nil |
| 465 | +} |
| 466 | + |
408 | 467 | func createLocation(path string, upstream Upstream, cfg *Config, websocket bool, rewrite string, ssl bool) Location {
|
409 | 468 | loc := Location{
|
410 | 469 | Path: path,
|
|
0 commit comments