Skip to content

Commit 447a1a0

Browse files
committed
fix validating webhook url port
1 parent 735f13a commit 447a1a0

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

cmd/project-workspace-operator/app/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func (o *InitOptions) Run(ctx context.Context) error {
223223
}
224224
if o.PlatformCluster.RESTConfig().Host != onboardingCluster.RESTConfig().Host {
225225
// create a URL-based webhook otherwise
226-
opts = append(opts, webhooks.WithCustomBaseURL(fmt.Sprintf("https://%s:%d", gatewayResult.HostName, WebhookPortSvc)))
226+
opts = append(opts, webhooks.WithCustomBaseURL(fmt.Sprintf("https://%s:%d", gatewayResult.HostName, gatewayResult.TLSPort)))
227227
}
228228

229229
// webhook options we might or might not support at a later time

internal/dns/dns.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
"github.com/openmcp-project/controller-utils/pkg/clusters"
9+
"github.com/openmcp-project/controller-utils/pkg/collections/filters"
910
"github.com/openmcp-project/controller-utils/pkg/logging"
1011
"k8s.io/apimachinery/pkg/api/errors"
1112
"k8s.io/utils/ptr"
@@ -46,6 +47,8 @@ type Instance struct {
4647
type GatewayReconcileResult struct {
4748
// HostName is the hostname that was created for the instance and can be used for DNS records.
4849
HostName string
50+
// TLSPort is the port under which the gateway accepts TLS traffic.
51+
TLSPort int32
4952
// Result is the result of the reconciliation.
5053
reconcile.Result
5154
}
@@ -92,10 +95,18 @@ func (r *Reconciler) ReconcileGateway(ctx context.Context, instance *Instance, t
9295

9396
log.Debug("Base domain found", "baseDomain", baseDomain)
9497

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+
95105
hostName := getHostName(baseDomain, instance)
96106

97107
return GatewayReconcileResult{
98108
HostName: hostName,
109+
TLSPort: tlsPort,
99110
Result: reconcile.Result{},
100111
}, nil
101112
}
@@ -227,3 +238,27 @@ func getBaseDomain(gateway *gatewayv1.Gateway) (string, bool) {
227238
func getHostName(baseDomain string, instance *Instance) string {
228239
return fmt.Sprintf("%s.%s", instance.SubDomainPrefix, baseDomain)
229240
}
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

Comments
 (0)