Fix podIP validation

This commit is contained in:
Jordan Liggitt
2020-04-29 22:25:03 -04:00
parent 7814f3aaf7
commit 23e9fb1bb5
7 changed files with 115 additions and 33 deletions

View File

@@ -4041,7 +4041,7 @@ func TestHugePagesIsolation(t *testing.T) {
for tcName, tc := range testCases {
t.Run(tcName, func(t *testing.T) {
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.HugePageStorageMediumSize, tc.enableHugePageStorageMediumSize)()
errs := ValidatePod(tc.pod, PodValidationOptions{tc.enableHugePageStorageMediumSize})
errs := ValidatePodCreate(tc.pod, PodValidationOptions{tc.enableHugePageStorageMediumSize})
if tc.expectError && len(errs) == 0 {
t.Errorf("Unexpected success")
}
@@ -7426,7 +7426,7 @@ func TestValidatePod(t *testing.T) {
},
}
for _, pod := range successCases {
if errs := ValidatePod(&pod, PodValidationOptions{}); len(errs) != 0 {
if errs := ValidatePodCreate(&pod, PodValidationOptions{}); len(errs) != 0 {
t.Errorf("expected success: %v", errs)
}
}
@@ -8276,7 +8276,7 @@ func TestValidatePod(t *testing.T) {
},
}
for k, v := range errorCases {
if errs := ValidatePod(&v.spec, PodValidationOptions{}); len(errs) == 0 {
if errs := ValidatePodCreate(&v.spec, PodValidationOptions{}); len(errs) == 0 {
t.Errorf("expected failure for %q", k)
} else if v.expectedError == "" {
t.Errorf("missing expectedError for %q, got %q", k, errs.ToAggregate().Error())
@@ -8485,7 +8485,10 @@ func TestValidatePodUpdate(t *testing.T) {
Spec: core.PodSpec{
Containers: []core.Container{
{
Image: "foo:V1",
Name: "container",
Image: "foo:V1",
TerminationMessagePolicy: "File",
ImagePullPolicy: "Always",
},
},
},
@@ -8495,7 +8498,10 @@ func TestValidatePodUpdate(t *testing.T) {
Spec: core.PodSpec{
Containers: []core.Container{
{
Image: "foo:V2",
Name: "container",
Image: "foo:V2",
TerminationMessagePolicy: "File",
ImagePullPolicy: "Always",
},
},
},
@@ -8509,7 +8515,10 @@ func TestValidatePodUpdate(t *testing.T) {
Spec: core.PodSpec{
InitContainers: []core.Container{
{
Image: "foo:V1",
Name: "container",
Image: "foo:V1",
TerminationMessagePolicy: "File",
ImagePullPolicy: "Always",
},
},
},
@@ -8519,7 +8528,10 @@ func TestValidatePodUpdate(t *testing.T) {
Spec: core.PodSpec{
InitContainers: []core.Container{
{
Image: "foo:V2",
Name: "container",
Image: "foo:V2",
TerminationMessagePolicy: "File",
ImagePullPolicy: "Always",
},
},
},
@@ -8532,7 +8544,11 @@ func TestValidatePodUpdate(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
Spec: core.PodSpec{
Containers: []core.Container{
{},
{
Name: "container",
TerminationMessagePolicy: "File",
ImagePullPolicy: "Always",
},
},
},
},
@@ -8541,7 +8557,10 @@ func TestValidatePodUpdate(t *testing.T) {
Spec: core.PodSpec{
Containers: []core.Container{
{
Image: "foo:V2",
Name: "container",
Image: "foo:V2",
TerminationMessagePolicy: "File",
ImagePullPolicy: "Always",
},
},
},
@@ -8554,7 +8573,11 @@ func TestValidatePodUpdate(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
Spec: core.PodSpec{
InitContainers: []core.Container{
{},
{
Name: "container",
TerminationMessagePolicy: "File",
ImagePullPolicy: "Always",
},
},
},
},
@@ -8563,7 +8586,10 @@ func TestValidatePodUpdate(t *testing.T) {
Spec: core.PodSpec{
InitContainers: []core.Container{
{
Image: "foo:V2",
Name: "container",
Image: "foo:V2",
TerminationMessagePolicy: "File",
ImagePullPolicy: "Always",
},
},
},
@@ -8690,7 +8716,7 @@ func TestValidatePodUpdate(t *testing.T) {
ActiveDeadlineSeconds: &activeDeadlineSecondsPositive,
},
},
"",
"spec.activeDeadlineSeconds",
"activeDeadlineSeconds change to zero from positive",
},
{
@@ -8700,7 +8726,7 @@ func TestValidatePodUpdate(t *testing.T) {
},
},
core.Pod{},
"",
"spec.activeDeadlineSeconds",
"activeDeadlineSeconds change to zero from nil",
},
{
@@ -9047,6 +9073,29 @@ func TestValidatePodUpdate(t *testing.T) {
for _, test := range tests {
test.new.ObjectMeta.ResourceVersion = "1"
test.old.ObjectMeta.ResourceVersion = "1"
// set required fields if old and new match and have no opinion on the value
if test.new.Name == "" && test.old.Name == "" {
test.new.Name = "name"
test.old.Name = "name"
}
if test.new.Namespace == "" && test.old.Namespace == "" {
test.new.Namespace = "namespace"
test.old.Namespace = "namespace"
}
if test.new.Spec.Containers == nil && test.old.Spec.Containers == nil {
test.new.Spec.Containers = []core.Container{{Name: "autoadded", Image: "image", TerminationMessagePolicy: "File", ImagePullPolicy: "Always"}}
test.old.Spec.Containers = []core.Container{{Name: "autoadded", Image: "image", TerminationMessagePolicy: "File", ImagePullPolicy: "Always"}}
}
if len(test.new.Spec.DNSPolicy) == 0 && len(test.old.Spec.DNSPolicy) == 0 {
test.new.Spec.DNSPolicy = core.DNSClusterFirst
test.old.Spec.DNSPolicy = core.DNSClusterFirst
}
if len(test.new.Spec.RestartPolicy) == 0 && len(test.old.Spec.RestartPolicy) == 0 {
test.new.Spec.RestartPolicy = "Always"
test.old.Spec.RestartPolicy = "Always"
}
errs := ValidatePodUpdate(&test.new, &test.old, PodValidationOptions{})
if test.err == "" {
if len(errs) != 0 {
@@ -15031,15 +15080,32 @@ func TestPodIPsValidation(t *testing.T) {
}
for _, testCase := range testCases {
errs := ValidatePod(&testCase.pod, PodValidationOptions{})
if len(errs) == 0 && testCase.expectError {
t.Errorf("expected failure for %s, but there were none", testCase.pod.Name)
return
}
if len(errs) != 0 && !testCase.expectError {
t.Errorf("expected success for %s, but there were errors: %v", testCase.pod.Name, errs)
return
}
t.Run(testCase.pod.Name, func(t *testing.T) {
for _, oldTestCase := range testCases {
newPod := testCase.pod.DeepCopy()
newPod.ResourceVersion = "1"
oldPod := oldTestCase.pod.DeepCopy()
oldPod.ResourceVersion = "1"
oldPod.Name = newPod.Name
errs := ValidatePodStatusUpdate(newPod, oldPod)
if oldTestCase.expectError {
// The old pod was invalid, tolerate invalid IPs in the new pod as well
if len(errs) > 0 {
t.Fatalf("expected success for update to pod with already-invalid IPs, got errors: %v", errs)
}
continue
}
if len(errs) == 0 && testCase.expectError {
t.Fatalf("expected failure for %s, but there were none", testCase.pod.Name)
}
if len(errs) != 0 && !testCase.expectError {
t.Fatalf("expected success for %s, but there were errors: %v", testCase.pod.Name, errs)
}
}
})
}
}