mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-30 06:54:01 +00:00
Update api/validation service validation with service fields moved by #2086
This commit is contained in:
parent
fc0dab630c
commit
535785e3b9
@ -408,14 +408,14 @@ func ValidateService(service *api.Service, lister ServiceLister, ctx api.Context
|
||||
if labels.Set(service.Spec.Selector).AsSelector().Empty() {
|
||||
allErrs = append(allErrs, errs.NewFieldRequired("spec.selector", service.Spec.Selector))
|
||||
}
|
||||
if service.CreateExternalLoadBalancer {
|
||||
if service.Spec.CreateExternalLoadBalancer {
|
||||
services, err := lister.ListServices(ctx)
|
||||
if err != nil {
|
||||
allErrs = append(allErrs, errs.NewInternalError(err))
|
||||
} else {
|
||||
for i := range services.Items {
|
||||
if services.Items[i].CreateExternalLoadBalancer && services.Items[i].Port == service.Port {
|
||||
allErrs = append(allErrs, errs.NewConflict("service", service.Namespace, fmt.Errorf("Port: %d is already in use", service.Port)))
|
||||
if services.Items[i].Spec.CreateExternalLoadBalancer && services.Items[i].Spec.Port == service.Spec.Port {
|
||||
allErrs = append(allErrs, errs.NewConflict("service", service.Namespace, fmt.Errorf("Port: %d is already in use", service.Spec.Port)))
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -753,13 +753,15 @@ func TestValidateService(t *testing.T) {
|
||||
name: "invalid port in use",
|
||||
svc: api.Service{
|
||||
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
|
||||
Port: 80,
|
||||
CreateExternalLoadBalancer: true,
|
||||
Selector: map[string]string{"foo": "bar"},
|
||||
Spec: api.ServiceSpec{
|
||||
Port: 80,
|
||||
CreateExternalLoadBalancer: true,
|
||||
Selector: map[string]string{"foo": "bar"},
|
||||
},
|
||||
},
|
||||
existing: api.ServiceList{
|
||||
Items: []api.Service{
|
||||
{Port: 80, CreateExternalLoadBalancer: true},
|
||||
{Spec: api.ServiceSpec{Port: 80, CreateExternalLoadBalancer: true}},
|
||||
},
|
||||
},
|
||||
numErrs: 1,
|
||||
@ -768,13 +770,15 @@ func TestValidateService(t *testing.T) {
|
||||
name: "same port in use, but not external",
|
||||
svc: api.Service{
|
||||
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
|
||||
Port: 80,
|
||||
CreateExternalLoadBalancer: true,
|
||||
Selector: map[string]string{"foo": "bar"},
|
||||
Spec: api.ServiceSpec{
|
||||
Port: 80,
|
||||
CreateExternalLoadBalancer: true,
|
||||
Selector: map[string]string{"foo": "bar"},
|
||||
},
|
||||
},
|
||||
existing: api.ServiceList{
|
||||
Items: []api.Service{
|
||||
{Port: 80},
|
||||
{Spec: api.ServiceSpec{Port: 80}},
|
||||
},
|
||||
},
|
||||
numErrs: 0,
|
||||
@ -783,12 +787,14 @@ func TestValidateService(t *testing.T) {
|
||||
name: "same port in use, but not external on input",
|
||||
svc: api.Service{
|
||||
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
|
||||
Port: 80,
|
||||
Selector: map[string]string{"foo": "bar"},
|
||||
Spec: api.ServiceSpec{
|
||||
Port: 80,
|
||||
Selector: map[string]string{"foo": "bar"},
|
||||
},
|
||||
},
|
||||
existing: api.ServiceList{
|
||||
Items: []api.Service{
|
||||
{Port: 80, CreateExternalLoadBalancer: true},
|
||||
{Spec: api.ServiceSpec{Port: 80, CreateExternalLoadBalancer: true}},
|
||||
},
|
||||
},
|
||||
numErrs: 0,
|
||||
@ -797,12 +803,14 @@ func TestValidateService(t *testing.T) {
|
||||
name: "same port in use, but neither external",
|
||||
svc: api.Service{
|
||||
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
|
||||
Port: 80,
|
||||
Selector: map[string]string{"foo": "bar"},
|
||||
Spec: api.ServiceSpec{
|
||||
Port: 80,
|
||||
Selector: map[string]string{"foo": "bar"},
|
||||
},
|
||||
},
|
||||
existing: api.ServiceList{
|
||||
Items: []api.Service{
|
||||
{Port: 80},
|
||||
{Spec: api.ServiceSpec{Port: 80}},
|
||||
},
|
||||
},
|
||||
numErrs: 0,
|
||||
|
Loading…
Reference in New Issue
Block a user