Merge pull request #25567 from gmarek/validate

Automatic merge from submit-queue

Add Controller field to OwnerReference

cc @davidopp
This commit is contained in:
k8s-merge-robot
2016-05-31 14:21:38 -07:00
31 changed files with 503 additions and 94 deletions

View File

@@ -160,8 +160,17 @@ func validateOwnerReference(ownerReference api.OwnerReference, fldPath *field.Pa
func ValidateOwnerReferences(ownerReferences []api.OwnerReference, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
controllerName := ""
for _, ref := range ownerReferences {
allErrs = append(allErrs, validateOwnerReference(ref, fldPath)...)
if ref.Controller != nil && *ref.Controller {
if controllerName != "" {
allErrs = append(allErrs, field.Invalid(fldPath, ownerReferences,
fmt.Sprintf("Only one reference can have Controller set to true. Found \"true\" in references for %v and %v", controllerName, ref.Name)))
} else {
controllerName = ref.Name
}
}
}
return allErrs
}

View File

@@ -99,12 +99,17 @@ func TestValidateObjectMetaNamespaces(t *testing.T) {
}
func TestValidateObjectMetaOwnerReferences(t *testing.T) {
trueVar := true
falseVar := false
testCases := []struct {
ownerReferences []api.OwnerReference
expectError bool
description string
ownerReferences []api.OwnerReference
expectError bool
expectedErrorMessage string
}{
{
[]api.OwnerReference{
description: "simple success - third party extension.",
ownerReferences: []api.OwnerReference{
{
APIVersion: "thirdpartyVersion",
Kind: "thirdpartyKind",
@@ -112,11 +117,12 @@ func TestValidateObjectMetaOwnerReferences(t *testing.T) {
UID: "1",
},
},
false,
expectError: false,
expectedErrorMessage: "",
},
{
// event shouldn't be set as an owner
[]api.OwnerReference{
description: "simple failures - event shouldn't be set as an owner",
ownerReferences: []api.OwnerReference{
{
APIVersion: "v1",
Kind: "Event",
@@ -124,7 +130,76 @@ func TestValidateObjectMetaOwnerReferences(t *testing.T) {
UID: "1",
},
},
true,
expectError: true,
expectedErrorMessage: "is disallowed from being an owner",
},
{
description: "simple controller ref success - one reference with Controller set",
ownerReferences: []api.OwnerReference{
{
APIVersion: "thirdpartyVersion",
Kind: "thirdpartyKind",
Name: "name",
UID: "1",
Controller: &falseVar,
},
{
APIVersion: "thirdpartyVersion",
Kind: "thirdpartyKind",
Name: "name",
UID: "2",
Controller: &trueVar,
},
{
APIVersion: "thirdpartyVersion",
Kind: "thirdpartyKind",
Name: "name",
UID: "3",
Controller: &falseVar,
},
{
APIVersion: "thirdpartyVersion",
Kind: "thirdpartyKind",
Name: "name",
UID: "4",
},
},
expectError: false,
expectedErrorMessage: "",
},
{
description: "simple controller ref failure - two references with Controller set",
ownerReferences: []api.OwnerReference{
{
APIVersion: "thirdpartyVersion",
Kind: "thirdpartyKind",
Name: "name",
UID: "1",
Controller: &falseVar,
},
{
APIVersion: "thirdpartyVersion",
Kind: "thirdpartyKind",
Name: "name",
UID: "2",
Controller: &trueVar,
},
{
APIVersion: "thirdpartyVersion",
Kind: "thirdpartyKind",
Name: "name",
UID: "3",
Controller: &trueVar,
},
{
APIVersion: "thirdpartyVersion",
Kind: "thirdpartyKind",
Name: "name",
UID: "4",
},
},
expectError: true,
expectedErrorMessage: "Only one reference can have Controller set to true",
},
}
@@ -137,13 +212,13 @@ func TestValidateObjectMetaOwnerReferences(t *testing.T) {
},
field.NewPath("field"))
if len(errs) != 0 && !tc.expectError {
t.Errorf("unexpected error: %v", errs)
t.Errorf("unexpected error: %v in test case %v", errs, tc.description)
}
if len(errs) == 0 && tc.expectError {
t.Errorf("expect error")
t.Errorf("expect error in test case %v", tc.description)
}
if len(errs) != 0 && !strings.Contains(errs[0].Error(), "is disallowed from being an owner") {
t.Errorf("unexpected error message: %v", errs)
if len(errs) != 0 && !strings.Contains(errs[0].Error(), tc.expectedErrorMessage) {
t.Errorf("unexpected error message: %v in test case %v", errs, tc.description)
}
}
}