|
6 | 6 | "time" |
7 | 7 |
|
8 | 8 | "github.com/openmcp-project/controller-utils/pkg/clusters" |
| 9 | + "github.com/openmcp-project/controller-utils/pkg/collections/filters" |
9 | 10 | "github.com/openmcp-project/controller-utils/pkg/logging" |
10 | 11 | "k8s.io/apimachinery/pkg/api/errors" |
11 | 12 | "k8s.io/utils/ptr" |
@@ -46,6 +47,8 @@ type Instance struct { |
46 | 47 | type GatewayReconcileResult struct { |
47 | 48 | // HostName is the hostname that was created for the instance and can be used for DNS records. |
48 | 49 | HostName string |
| 50 | + // TLSPort is the port under which the gateway accepts TLS traffic. |
| 51 | + TLSPort int32 |
49 | 52 | // Result is the result of the reconciliation. |
50 | 53 | reconcile.Result |
51 | 54 | } |
@@ -92,10 +95,18 @@ func (r *Reconciler) ReconcileGateway(ctx context.Context, instance *Instance, t |
92 | 95 |
|
93 | 96 | log.Debug("Base domain found", "baseDomain", baseDomain) |
94 | 97 |
|
| 98 | + tlsPort, hasTLSPort := getTLSPort(gateway) |
| 99 | + if !hasTLSPort { |
| 100 | + return GatewayReconcileResult{Result: reconcile.Result{}}, fmt.Errorf("gateway either does not have any listeners with TLS protocol or it has multiple ones and none is named 'tls'") |
| 101 | + } |
| 102 | + |
| 103 | + log.Debug("TLS port found", "tlsPort", tlsPort) |
| 104 | + |
95 | 105 | hostName := getHostName(baseDomain, instance) |
96 | 106 |
|
97 | 107 | return GatewayReconcileResult{ |
98 | 108 | HostName: hostName, |
| 109 | + TLSPort: tlsPort, |
99 | 110 | Result: reconcile.Result{}, |
100 | 111 | }, nil |
101 | 112 | } |
@@ -227,3 +238,27 @@ func getBaseDomain(gateway *gatewayv1.Gateway) (string, bool) { |
227 | 238 | func getHostName(baseDomain string, instance *Instance) string { |
228 | 239 | return fmt.Sprintf("%s.%s", instance.SubDomainPrefix, baseDomain) |
229 | 240 | } |
| 241 | + |
| 242 | +// retrieves the TLS port from the gateway and a boolean indicating whether a TLS port was found |
| 243 | +// logic as follows: |
| 244 | +// - if the gateway has a single listener with TLS protocol, its port (and true) is returned |
| 245 | +// - if the gateway has multiple TLS listeners and one is named "tls", its port (and true) is returned |
| 246 | +// - in all other cases, (0, false) is returned |
| 247 | +func getTLSPort(gateway *gatewayv1.Gateway) (int32, bool) { |
| 248 | + tlsListeners := filters.FilterSlice(gateway.Spec.Listeners, func(args ...any) bool { |
| 249 | + elem := args[0].(gatewayv1.Listener) |
| 250 | + return elem.Protocol == gatewayv1.TLSProtocolType |
| 251 | + }) |
| 252 | + if len(tlsListeners) == 0 { |
| 253 | + return 0, false |
| 254 | + } |
| 255 | + if len(tlsListeners) == 1 { |
| 256 | + return tlsListeners[0].Port, true |
| 257 | + } |
| 258 | + for _, listener := range tlsListeners { |
| 259 | + if listener.Name == "tls" { |
| 260 | + return listener.Port, true |
| 261 | + } |
| 262 | + } |
| 263 | + return 0, false |
| 264 | +} |
0 commit comments