Skip to content

Commit 11f37d7

Browse files
committed
Add port to ServiceResolvers
1 parent 404e2f7 commit 11f37d7

File tree

19 files changed

+78
-48
lines changed

19 files changed

+78
-48
lines changed

pkg/apis/admissionregistration/validation/validation_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ func TestValidateValidatingWebhookConfiguration(t *testing.T) {
377377
Service: &admissionregistration.ServiceReference{
378378
Namespace: "ns",
379379
Name: "n",
380+
Port: 443,
380381
},
381382
URL: strPtr("example.com/k8s/webhook"),
382383
},
@@ -478,6 +479,7 @@ func TestValidateValidatingWebhookConfiguration(t *testing.T) {
478479
Namespace: "ns",
479480
Name: "n",
480481
Path: strPtr("foo/"),
482+
Port: 443,
481483
},
482484
},
483485
},
@@ -494,6 +496,7 @@ func TestValidateValidatingWebhookConfiguration(t *testing.T) {
494496
Namespace: "ns",
495497
Name: "n",
496498
Path: strPtr("/"),
499+
Port: 443,
497500
},
498501
},
499502
},
@@ -510,6 +513,7 @@ func TestValidateValidatingWebhookConfiguration(t *testing.T) {
510513
Namespace: "ns",
511514
Name: "n",
512515
Path: strPtr("/foo"),
516+
Port: 443,
513517
},
514518
},
515519
},
@@ -526,6 +530,7 @@ func TestValidateValidatingWebhookConfiguration(t *testing.T) {
526530
Namespace: "ns",
527531
Name: "n",
528532
Path: strPtr("//"),
533+
Port: 443,
529534
},
530535
},
531536
},
@@ -542,6 +547,7 @@ func TestValidateValidatingWebhookConfiguration(t *testing.T) {
542547
Namespace: "ns",
543548
Name: "n",
544549
Path: strPtr("/foo//bar/"),
550+
Port: 443,
545551
},
546552
},
547553
},
@@ -557,6 +563,7 @@ func TestValidateValidatingWebhookConfiguration(t *testing.T) {
557563
Namespace: "ns",
558564
Name: "n",
559565
Path: strPtr("/foo/bar//"),
566+
Port: 443,
560567
},
561568
},
562569
},
@@ -573,6 +580,7 @@ func TestValidateValidatingWebhookConfiguration(t *testing.T) {
573580
Namespace: "ns",
574581
Name: "n",
575582
Path: strPtr("/apis/foo.bar/v1alpha1/--bad"),
583+
Port: 443,
576584
},
577585
},
578586
},
@@ -595,7 +603,7 @@ func TestValidateValidatingWebhookConfiguration(t *testing.T) {
595603
},
596604
},
597605
}, true),
598-
expectedError: `Invalid value: 0: port must be a valid number between 1 and 65535, inclusive`,
606+
expectedError: `Invalid value: 0: port is not valid: must be between 1 and 65535, inclusive`,
599607
},
600608
{
601609
name: "invalid port >65535",
@@ -613,7 +621,7 @@ func TestValidateValidatingWebhookConfiguration(t *testing.T) {
613621
},
614622
},
615623
}, true),
616-
expectedError: `Invalid value: 65536: port must be a valid number between 1 and 65535, inclusive`,
624+
expectedError: `Invalid value: 65536: port is not valid: must be between 1 and 65535, inclusive`,
617625
},
618626
{
619627
name: "timeout seconds cannot be greater than 30",

pkg/apis/auditregistration/validation/validation_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
155155
Service: &auditregistration.ServiceReference{
156156
Namespace: "ns",
157157
Name: "n",
158+
Port: 443,
158159
},
159160
URL: strPtr("example.com/k8s/webhook"),
160161
},
@@ -223,6 +224,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
223224
Namespace: "ns",
224225
Name: "n",
225226
Path: strPtr("foo/"),
227+
Port: 443,
226228
},
227229
},
228230
},
@@ -240,7 +242,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
240242
},
241243
},
242244
},
243-
expectedError: `Invalid value: 65536: port must be a valid number between 1 and 65535, inclusive`,
245+
expectedError: `Invalid value: 65536: port is not valid: must be between 1 and 65535, inclusive`,
244246
},
245247
{
246248
name: "invalid port 0",
@@ -254,7 +256,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
254256
},
255257
},
256258
},
257-
expectedError: `Invalid value: 0: port must be a valid number between 1 and 65535, inclusive`,
259+
expectedError: `Invalid value: 0: port is not valid: must be between 1 and 65535, inclusive`,
258260
},
259261
{
260262
name: "path accepts slash",
@@ -264,6 +266,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
264266
Namespace: "ns",
265267
Name: "n",
266268
Path: strPtr("/"),
269+
Port: 443,
267270
},
268271
},
269272
},
@@ -277,6 +280,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
277280
Namespace: "ns",
278281
Name: "n",
279282
Path: strPtr("/foo"),
283+
Port: 443,
280284
},
281285
},
282286
},
@@ -290,6 +294,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
290294
Namespace: "ns",
291295
Name: "n",
292296
Path: strPtr("//"),
297+
Port: 443,
293298
},
294299
},
295300
},
@@ -303,6 +308,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
303308
Namespace: "ns",
304309
Name: "n",
305310
Path: strPtr("/foo//bar/"),
311+
Port: 443,
306312
},
307313
},
308314
},
@@ -315,6 +321,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
315321
Namespace: "ns",
316322
Name: "n",
317323
Path: strPtr("/foo/bar//"),
324+
Port: 443,
318325
},
319326
},
320327
},
@@ -328,6 +335,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
328335
Namespace: "ns",
329336
Name: "n",
330337
Path: strPtr("/apis/foo.bar/v1alpha1/--bad"),
338+
Port: 443,
331339
},
332340
},
333341
},

staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/conversion/webhook_converter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func webhookClientConfigForCRD(crd *internal.CustomResourceDefinition) *webhook.
7575
ret.Service = &webhook.ClientConfigService{
7676
Name: apiConfig.Service.Name,
7777
Namespace: apiConfig.Service.Namespace,
78+
Port: apiConfig.Service.Port,
7879
}
7980
if apiConfig.Service.Path != nil {
8081
ret.Service.Path = *apiConfig.Service.Path

staging/src/k8s.io/apiextensions-apiserver/pkg/cmd/server/options/options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,6 @@ type serviceResolver struct {
129129
services v1.ServiceLister
130130
}
131131

132-
func (r *serviceResolver) ResolveEndpoint(namespace, name string) (*url.URL, error) {
133-
return proxy.ResolveCluster(r.services, namespace, name)
132+
func (r *serviceResolver) ResolveEndpoint(namespace, name string, port int32) (*url.URL, error) {
133+
return proxy.ResolveCluster(r.services, namespace, name, port)
134134
}

staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/initializer/initializer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type WantsServiceResolver interface {
3232
// ServiceResolver knows how to convert a service reference into an actual
3333
// location.
3434
type ServiceResolver interface {
35-
ResolveEndpoint(namespace, name string) (*url.URL, error)
35+
ResolveEndpoint(namespace, name string, port int32) (*url.URL, error)
3636
}
3737

3838
// WantsAuthenticationInfoResolverWrapper defines a function that wraps the standard AuthenticationInfoResolver

staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/initializer/initializer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (doNothingAdmission) Handles(o admission.Operation) bool { return false }
3030

3131
type fakeServiceResolver struct{}
3232

33-
func (f *fakeServiceResolver) ResolveEndpoint(namespace, name string) (*url.URL, error) {
33+
func (f *fakeServiceResolver) ResolveEndpoint(namespace, name string, port int32) (*url.URL, error) {
3434
return nil, nil
3535
}
3636

staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/testing/service_resolver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func NewServiceResolver(base url.URL) webhook.ServiceResolver {
3333
return &serviceResolver{base}
3434
}
3535

36-
func (f serviceResolver) ResolveEndpoint(namespace, name string) (*url.URL, error) {
36+
func (f serviceResolver) ResolveEndpoint(namespace, name string, port int32) (*url.URL, error) {
3737
if namespace == "failResolve" {
3838
return nil, fmt.Errorf("couldn't resolve service location")
3939
}

staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/util/client_config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ func HookClientConfigForWebhook(w *v1beta1.Webhook) webhook.ClientConfig {
3434
Name: w.ClientConfig.Service.Name,
3535
Namespace: w.ClientConfig.Service.Namespace,
3636
}
37+
if w.ClientConfig.Service.Port != nil {
38+
ret.Service.Port = *w.ClientConfig.Service.Port
39+
} else {
40+
ret.Service.Port = 443
41+
}
3742
if w.ClientConfig.Service.Path != nil {
3843
ret.Service.Path = *w.ClientConfig.Service.Path
3944
}

staging/src/k8s.io/apiserver/pkg/audit/util/conversion.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ func HookClientConfigForSink(a *v1alpha1.AuditSink) webhook.ClientConfig {
3535
Name: c.Service.Name,
3636
Namespace: c.Service.Namespace,
3737
}
38+
if c.Service.Port != nil {
39+
ret.Service.Port = *c.Service.Port
40+
} else {
41+
ret.Service.Port = 443
42+
}
43+
3844
if c.Service.Path != nil {
3945
ret.Service.Path = *c.Service.Path
4046
}

staging/src/k8s.io/apiserver/pkg/util/proxy/proxy.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,25 @@ import (
2626
"k8s.io/api/core/v1"
2727
"k8s.io/apimachinery/pkg/api/errors"
2828
listersv1 "k8s.io/client-go/listers/core/v1"
29-
30-
"k8s.io/apimachinery/pkg/util/intstr"
3129
)
3230

3331
// findServicePort finds the service port by name or numerically.
34-
func findServicePort(svc *v1.Service, port intstr.IntOrString) (*v1.ServicePort, error) {
32+
func findServicePort(svc *v1.Service, port int32) (*v1.ServicePort, error) {
3533
for _, svcPort := range svc.Spec.Ports {
36-
if (port.Type == intstr.Int && int32(svcPort.Port) == port.IntVal) || (port.Type == intstr.String && svcPort.Name == port.StrVal) {
34+
if svcPort.Port == port {
3735
return &svcPort, nil
3836
}
3937
}
40-
return nil, errors.NewServiceUnavailable(fmt.Sprintf("no service port %q found for service %q", port.String(), svc.Name))
38+
return nil, errors.NewServiceUnavailable(fmt.Sprintf("no service port %q found for service %q", port, svc.Name))
4139
}
4240

4341
// ResourceLocation returns a URL to which one can send traffic for the specified service.
44-
func ResolveEndpoint(services listersv1.ServiceLister, endpoints listersv1.EndpointsLister, namespace, id string) (*url.URL, error) {
42+
func ResolveEndpoint(services listersv1.ServiceLister, endpoints listersv1.EndpointsLister, namespace, id string, port int32) (*url.URL, error) {
4543
svc, err := services.Services(namespace).Get(id)
4644
if err != nil {
4745
return nil, err
4846
}
4947

50-
port := intstr.FromInt(443)
5148
svcPort, err := findServicePort(svc, port)
5249
if err != nil {
5350
return nil, err
@@ -92,14 +89,12 @@ func ResolveEndpoint(services listersv1.ServiceLister, endpoints listersv1.Endpo
9289
return nil, errors.NewServiceUnavailable(fmt.Sprintf("no endpoints available for service %q", id))
9390
}
9491

95-
func ResolveCluster(services listersv1.ServiceLister, namespace, id string) (*url.URL, error) {
92+
func ResolveCluster(services listersv1.ServiceLister, namespace, id string, port int32) (*url.URL, error) {
9693
svc, err := services.Services(namespace).Get(id)
9794
if err != nil {
9895
return nil, err
9996
}
10097

101-
port := intstr.FromInt(443)
102-
10398
switch {
10499
case svc.Spec.Type == v1.ServiceTypeClusterIP && svc.Spec.ClusterIP == v1.ClusterIPNone:
105100
return nil, fmt.Errorf(`cannot route to service with ClusterIP "None"`)
@@ -114,12 +109,9 @@ func ResolveCluster(services listersv1.ServiceLister, namespace, id string) (*ur
114109
Host: net.JoinHostPort(svc.Spec.ClusterIP, fmt.Sprintf("%d", svcPort.Port)),
115110
}, nil
116111
case svc.Spec.Type == v1.ServiceTypeExternalName:
117-
if port.Type != intstr.Int {
118-
return nil, fmt.Errorf("named ports not supported")
119-
}
120112
return &url.URL{
121113
Scheme: "https",
122-
Host: net.JoinHostPort(svc.Spec.ExternalName, port.String()),
114+
Host: net.JoinHostPort(svc.Spec.ExternalName, fmt.Sprintf("%d", port)),
123115
}, nil
124116
default:
125117
return nil, fmt.Errorf("unsupported service type %q", svc.Spec.Type)

0 commit comments

Comments
 (0)