Merge pull request #2086 from markturansky/v1beta3_refactor

Refactor internal API for Services to match v1beta3
This commit is contained in:
bgrant0607
2014-11-04 21:48:02 -08:00
26 changed files with 466 additions and 340 deletions

View File

@@ -397,16 +397,16 @@ func ValidateService(service *api.Service, lister ServiceLister, ctx api.Context
if !util.IsDNSSubdomain(service.Namespace) {
allErrs = append(allErrs, errs.NewFieldInvalid("namespace", service.Namespace))
}
if !util.IsValidPortNum(service.Port) {
allErrs = append(allErrs, errs.NewFieldInvalid("port", service.Port))
if !util.IsValidPortNum(service.Spec.Port) {
allErrs = append(allErrs, errs.NewFieldInvalid("spec.port", service.Spec.Port))
}
if len(service.Protocol) == 0 {
service.Protocol = "TCP"
} else if !supportedPortProtocols.Has(strings.ToUpper(string(service.Protocol))) {
allErrs = append(allErrs, errs.NewFieldNotSupported("protocol", service.Protocol))
if len(service.Spec.Protocol) == 0 {
service.Spec.Protocol = "TCP"
} else if !supportedPortProtocols.Has(strings.ToUpper(string(service.Spec.Protocol))) {
allErrs = append(allErrs, errs.NewFieldNotSupported("spec.protocol", service.Spec.Protocol))
}
if labels.Set(service.Selector).AsSelector().Empty() {
allErrs = append(allErrs, errs.NewFieldRequired("selector", service.Selector))
if labels.Set(service.Spec.Selector).AsSelector().Empty() {
allErrs = append(allErrs, errs.NewFieldRequired("spec.selector", service.Spec.Selector))
}
if service.CreateExternalLoadBalancer {
services, err := lister.ListServices(ctx)
@@ -422,7 +422,7 @@ func ValidateService(service *api.Service, lister ServiceLister, ctx api.Context
}
}
allErrs = append(allErrs, validateLabels(service.Labels)...)
allErrs = append(allErrs, validateLabels(service.Selector)...)
allErrs = append(allErrs, validateLabels(service.Spec.Selector)...)
return allErrs
}

View File

@@ -635,8 +635,10 @@ func TestValidateService(t *testing.T) {
name: "missing id",
svc: api.Service{
ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault},
Port: 8675,
Selector: map[string]string{"foo": "bar"},
Spec: api.ServiceSpec{
Port: 8675,
Selector: map[string]string{"foo": "bar"},
},
},
// Should fail because the ID is missing.
numErrs: 1,
@@ -645,8 +647,10 @@ func TestValidateService(t *testing.T) {
name: "missing namespace",
svc: api.Service{
ObjectMeta: api.ObjectMeta{Name: "foo"},
Port: 8675,
Selector: map[string]string{"foo": "bar"},
Spec: api.ServiceSpec{
Port: 8675,
Selector: map[string]string{"foo": "bar"},
},
},
// Should fail because the Namespace is missing.
numErrs: 1,
@@ -655,8 +659,10 @@ func TestValidateService(t *testing.T) {
name: "invalid id",
svc: api.Service{
ObjectMeta: api.ObjectMeta{Name: "123abc", Namespace: api.NamespaceDefault},
Port: 8675,
Selector: map[string]string{"foo": "bar"},
Spec: api.ServiceSpec{
Port: 8675,
Selector: map[string]string{"foo": "bar"},
},
},
// Should fail because the ID is invalid.
numErrs: 1,
@@ -665,7 +671,9 @@ func TestValidateService(t *testing.T) {
name: "missing port",
svc: api.Service{
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
Selector: map[string]string{"foo": "bar"},
Spec: api.ServiceSpec{
Selector: map[string]string{"foo": "bar"},
},
},
// Should fail because the port number is missing/invalid.
numErrs: 1,
@@ -674,8 +682,10 @@ func TestValidateService(t *testing.T) {
name: "invalid port",
svc: api.Service{
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
Port: 65536,
Selector: map[string]string{"foo": "bar"},
Spec: api.ServiceSpec{
Port: 66536,
Selector: map[string]string{"foo": "bar"},
},
},
// Should fail because the port number is invalid.
numErrs: 1,
@@ -684,9 +694,11 @@ func TestValidateService(t *testing.T) {
name: "invalid protocol",
svc: api.Service{
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
Port: 8675,
Protocol: "INVALID",
Selector: map[string]string{"foo": "bar"},
Spec: api.ServiceSpec{
Port: 8675,
Selector: map[string]string{"foo": "bar"},
Protocol: "INVALID",
},
},
// Should fail because the protocol is invalid.
numErrs: 1,
@@ -695,7 +707,9 @@ func TestValidateService(t *testing.T) {
name: "missing selector",
svc: api.Service{
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: api.NamespaceDefault},
Port: 8675,
Spec: api.ServiceSpec{
Port: 8675,
},
},
// Should fail because the selector is missing.
numErrs: 1,
@@ -704,9 +718,11 @@ func TestValidateService(t *testing.T) {
name: "valid 1",
svc: api.Service{
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
Port: 1,
Protocol: "TCP",
Selector: map[string]string{"foo": "bar"},
Spec: api.ServiceSpec{
Port: 8675,
Selector: map[string]string{"foo": "bar"},
Protocol: "TCP",
},
},
numErrs: 0,
},
@@ -714,9 +730,11 @@ func TestValidateService(t *testing.T) {
name: "valid 2",
svc: api.Service{
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
Port: 65535,
Protocol: "UDP",
Selector: map[string]string{"foo": "bar"},
Spec: api.ServiceSpec{
Port: 8675,
Selector: map[string]string{"foo": "bar"},
Protocol: "UDP",
},
},
numErrs: 0,
},
@@ -724,8 +742,10 @@ func TestValidateService(t *testing.T) {
name: "valid 3",
svc: api.Service{
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
Port: 80,
Selector: map[string]string{"foo": "bar"},
Spec: api.ServiceSpec{
Port: 8675,
Selector: map[string]string{"foo": "bar"},
},
},
numErrs: 0,
},
@@ -797,8 +817,10 @@ func TestValidateService(t *testing.T) {
"NoUppercaseOrSpecialCharsLike=Equals": "bar",
},
},
Port: 80,
Selector: map[string]string{"foo": "bar", "NoUppercaseOrSpecialCharsLike=Equals": "bar"},
Spec: api.ServiceSpec{
Port: 8675,
Selector: map[string]string{"foo": "bar", "NoUppercaseOrSpecialCharsLike=Equals": "bar"},
},
},
numErrs: 2,
},
@@ -814,15 +836,17 @@ func TestValidateService(t *testing.T) {
}
svc := api.Service{
Port: 6502,
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: api.NamespaceDefault},
Selector: map[string]string{"foo": "bar"},
Spec: api.ServiceSpec{
Port: 8675,
Selector: map[string]string{"foo": "bar"},
},
}
errs := ValidateService(&svc, registrytest.NewServiceRegistry(), api.NewDefaultContext())
if len(errs) != 0 {
t.Errorf("Unexpected non-zero error list: %#v", errs)
}
if svc.Protocol != "TCP" {
if svc.Spec.Protocol != "TCP" {
t.Errorf("Expected default protocol of 'TCP': %#v", errs)
}
}