Skip to content

Commit 529cf47

Browse files
committed
fix webhook configuration
1 parent 829f810 commit 529cf47

File tree

7 files changed

+19
-87
lines changed

7 files changed

+19
-87
lines changed

api/core/v1alpha1/pwconfig_types.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package v1alpha1
33
import (
44
rbacv1 "k8s.io/api/rbac/v1"
55
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
6-
"k8s.io/apimachinery/pkg/util/intstr"
76
)
87

98
// ProjectWorkspaceConfigSpec defines the desired state of ProjectWorkspaceConfig
@@ -54,12 +53,6 @@ type WebhookConfig struct {
5453
// Disabled specifies whether the webhooks should be disabled.
5554
// +optional
5655
Disabled bool `json:"disabled"`
57-
// TargetPort is the port of the pod the webhook server listens on.
58-
// May be a port number or a named port of the pod.
59-
// Defaults to 9443, if not specified.
60-
// +kubebuilder:validation:XIntOrString
61-
// +optional
62-
TargetPort *intstr.IntOrString `json:"targetPort,omitempty"`
6356
}
6457

6558
// +kubebuilder:object:root=true
@@ -76,12 +69,7 @@ func init() {
7669
}
7770

7871
// SetDefaults sets the default values for the project workspace configuration when not set.
79-
func (pwc *ProjectWorkspaceConfig) SetDefaults() {
80-
if pwc.Spec.Webhook.TargetPort == nil || pwc.Spec.Webhook.TargetPort.IntValue() == 0 {
81-
defaultPort := intstr.FromInt(9443)
82-
pwc.Spec.Webhook.TargetPort = &defaultPort
83-
}
84-
}
72+
func (pwc *ProjectWorkspaceConfig) SetDefaults() {}
8573

8674
// Validate validates the project workspace configuration.
8775
func (pwc *ProjectWorkspaceConfig) Validate() error {

api/core/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/crds/manifests/core.openmcp.cloud_projectworkspaceconfigs.yaml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,6 @@ spec:
132132
description: Disabled specifies whether the webhooks should be
133133
disabled.
134134
type: boolean
135-
targetPort:
136-
anyOf:
137-
- type: integer
138-
- type: string
139-
description: |-
140-
TargetPort is the port of the pod the webhook server listens on.
141-
May be a port number or a named port of the pod.
142-
Defaults to 9443, if not specified.
143-
x-kubernetes-int-or-string: true
144135
type: object
145136
workspace:
146137
description: WorkspaceConfig contains the configuration for workspaces.

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

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
package app
22

33
import (
4-
"context"
54
"fmt"
65
"os"
76

8-
corev1 "k8s.io/api/core/v1"
9-
"k8s.io/apimachinery/pkg/util/intstr"
107
ctrl "sigs.k8s.io/controller-runtime"
11-
"sigs.k8s.io/controller-runtime/pkg/client"
128

139
"github.com/spf13/cobra"
1410

1511
"github.com/openmcp-project/controller-utils/pkg/clusters"
1612
"github.com/openmcp-project/controller-utils/pkg/logging"
17-
openmcpconst "github.com/openmcp-project/openmcp-operator/api/constants"
13+
)
14+
15+
const (
16+
// WebhookPortPod is the port the webhook server listens on in the pod.
17+
WebhookPortPod = 9443
18+
// WebhookPortSvc is the port the webhook service exposes.
19+
WebhookPortSvc = 443
1820
)
1921

2022
func NewPlatformServiceProjectWorkspaceCommand() *cobra.Command {
@@ -85,39 +87,3 @@ func (o *SharedOptions) Complete() error {
8587

8688
return nil
8789
}
88-
89-
func resolveWebhookPort(ctx context.Context, platformClusterClient client.Client, targetPort intstr.IntOrString) (int, error) {
90-
log := logging.FromContextOrDiscard(ctx)
91-
webhookPort := targetPort.IntValue()
92-
if webhookPort == 0 {
93-
// this should only have happened if the user configured a named port
94-
portName := targetPort.StrVal
95-
if portName == "" {
96-
return 0, fmt.Errorf("invalid webhook target port configuration: %v", targetPort)
97-
}
98-
log.Info("Resolving webhook port from named port", "portName", portName)
99-
pod := &corev1.Pod{}
100-
pod.Name = os.Getenv(openmcpconst.EnvVariablePodName)
101-
pod.Namespace = os.Getenv(openmcpconst.EnvVariablePodNamespace)
102-
if pod.Name == "" || pod.Namespace == "" {
103-
return 0, fmt.Errorf("environment variables %s and %s must be set to resolve webhook port from named port", openmcpconst.EnvVariablePodName, openmcpconst.EnvVariablePodNamespace)
104-
}
105-
if err := platformClusterClient.Get(ctx, client.ObjectKey{Name: pod.Name, Namespace: pod.Namespace}, pod); err != nil {
106-
return 0, fmt.Errorf("unable to get pod '%s/%s' to resolve webhook port from named port: %w", pod.Namespace, pod.Name, err)
107-
}
108-
namedPorts := pod.Spec.Containers[0].Ports
109-
found := false
110-
for _, p := range namedPorts {
111-
if p.Name == portName {
112-
webhookPort = int(p.ContainerPort)
113-
found = true
114-
log.Info("Resolved webhook port from named port", "portName", portName, "port", webhookPort)
115-
break
116-
}
117-
}
118-
if !found {
119-
return 0, fmt.Errorf("unable to find named port '%s' in pod '%s/%s' to resolve webhook port", portName, pod.Namespace, pod.Name)
120-
}
121-
}
122-
return webhookPort, nil
123-
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/spf13/cobra"
1010
rbacv1 "k8s.io/api/rbac/v1"
1111
"k8s.io/apimachinery/pkg/runtime"
12+
"k8s.io/apimachinery/pkg/util/intstr"
1213
"k8s.io/apimachinery/pkg/util/wait"
1314
"sigs.k8s.io/controller-runtime/pkg/client"
1415

@@ -150,18 +151,13 @@ func (o *InitOptions) Run(ctx context.Context) error {
150151
return fmt.Errorf("unable to determine webhook secret name: %w", err)
151152
}
152153

153-
webhookPort, err := resolveWebhookPort(ctx, o.PlatformCluster.Client(), *pwc.Spec.Webhook.TargetPort)
154-
if err != nil {
155-
return err
156-
}
157-
158154
// setup gateway for webhooks
159155
dnsInstance := &dns.Instance{
160156
Name: whServiceName,
161157
Namespace: providerSystemNamespace,
162158
SubDomainPrefix: "pwo-webhooks",
163159
BackendName: whServiceName,
164-
BackendPort: int32(webhookPort),
160+
BackendPort: int32(WebhookPortSvc),
165161
}
166162
dnsReconciler := dns.NewReconciler()
167163
timeout := 3 * time.Minute
@@ -214,8 +210,9 @@ func (o *InitOptions) Run(ctx context.Context) error {
214210
webhooks.WithWebhookService{Name: whServiceName, Namespace: providerSystemNamespace},
215211
webhooks.WithWebhookSecret{Name: whSecretName, Namespace: providerSystemNamespace},
216212
webhooks.WithRemoteClient{Client: onboardingCluster.Client()},
213+
webhooks.WithWebhookServicePort(WebhookPortSvc),
217214
webhooks.WithManagedWebhookService{
218-
TargetPort: *pwc.Spec.Webhook.TargetPort,
215+
TargetPort: intstr.FromInt32(WebhookPortPod),
219216
SelectorLabels: map[string]string{
220217
"app.kubernetes.io/component": "controller",
221218
"app.kubernetes.io/managed-by": "openmcp-operator",
@@ -224,11 +221,14 @@ func (o *InitOptions) Run(ctx context.Context) error {
224221
},
225222
},
226223
}
224+
if o.PlatformCluster.RESTConfig().Host != onboardingCluster.RESTConfig().Host {
225+
// create a URL-based webhook otherwise
226+
opts = append(opts, webhooks.WithCustomBaseURL(fmt.Sprintf("https://%s:%d", gatewayResult.HostName, WebhookPortSvc)))
227+
}
227228

228229
// webhook options we might or might not support at a later time
229230
/*
230231
opts = append(opts, webhooks.WithoutCA)
231-
opts = append(opts, webhooks.WithCustomBaseURL("todo"))
232232
opts = append(opts, webhooks.WithCustomCA{todo})
233233
*/
234234

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,9 @@ func (o *RunOptions) Run(ctx context.Context) error {
309309
}
310310
}
311311

312-
webhookPort, err := resolveWebhookPort(ctx, o.PlatformCluster.Client(), *pwc.Spec.Webhook.TargetPort)
313-
if err != nil {
314-
return err
315-
}
316-
317312
webhookServer := webhook.NewServer(webhook.Options{
318313
TLSOpts: o.WebhookTLSOpts,
319-
Port: webhookPort,
314+
Port: WebhookPortPod,
320315
})
321316

322317
mgr, err := ctrl.NewManager(onboardingCluster.RESTConfig(), ctrl.Options{

internal/dns/dns.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"github.com/openmcp-project/controller-utils/pkg/clusters"
9-
"github.com/openmcp-project/controller-utils/pkg/controller"
109
"github.com/openmcp-project/controller-utils/pkg/logging"
1110
"k8s.io/apimachinery/pkg/api/errors"
1211
"k8s.io/utils/ptr"
@@ -226,6 +225,5 @@ func getBaseDomain(gateway *gatewayv1.Gateway) (string, bool) {
226225
}
227226

228227
func getHostName(baseDomain string, instance *Instance) string {
229-
subDomain := controller.NameHashSHAKE128Base32(instance.Name, instance.Namespace)
230-
return fmt.Sprintf("%s-%s.%s", instance.SubDomainPrefix, subDomain, baseDomain)
228+
return fmt.Sprintf("%s.%s", instance.SubDomainPrefix, baseDomain)
231229
}

0 commit comments

Comments
 (0)