-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
345 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"k8s.io/api/networking/v1beta1" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
"k8s.io/ingress-gce/pkg/e2e" | ||
"k8s.io/ingress-gce/pkg/fuzz" | ||
"k8s.io/ingress-gce/pkg/utils/common" | ||
) | ||
|
||
// TestV2FrontendNamer tests ingress basic lifecycle with v2 frontend naming scheme. | ||
// This also tests the lifecycle an ingress with v1 naming scheme with v2 namer enabled. | ||
func TestV2FrontendNamer(t *testing.T) { | ||
t.Parallel() | ||
port80 := intstr.FromInt(80) | ||
svcName := "service-1" | ||
v1Ing := fuzz.NewIngressBuilder("", "ing-v1", ""). | ||
AddPath("foo.com", "/", svcName, port80). | ||
Build() | ||
v1Ing.SetFinalizers([]string{common.FinalizerKey}) | ||
v2Ing := fuzz.NewIngressBuilder("", "ing-v2", ""). | ||
AddPath("foo.com", "/", svcName, port80). | ||
Build() | ||
|
||
for _, tc := range []struct { | ||
desc string | ||
ings []*v1beta1.Ingress | ||
}{ | ||
{"v2 only", []*v1beta1.Ingress{v2Ing}}, | ||
{"v1 only", []*v1beta1.Ingress{v1Ing}}, | ||
{"both v1 and v2, delete v1 first", []*v1beta1.Ingress{v1Ing, v2Ing}}, | ||
{"both v1 and v2, delete v2 first", []*v1beta1.Ingress{v2Ing, v1Ing}}, | ||
} { | ||
tc := tc | ||
desc := fmt.Sprintf("v2 frontend namer %s", tc.desc) | ||
Framework.RunWithSandbox(desc, t, func(t *testing.T, s *e2e.Sandbox) { | ||
t.Parallel() | ||
ctx := context.Background() | ||
|
||
_, err := e2e.CreateEchoService(s, svcName, nil) | ||
if err != nil { | ||
t.Fatalf("CreateEchoService(_, %q, nil): %v, want nil", svcName, err) | ||
} | ||
t.Logf("Echo service created (%s/%s)", s.Namespace, svcName) | ||
|
||
crud := e2e.IngressCRUD{C: Framework.Clientset} | ||
var gclbs []*fuzz.GCLB | ||
var updatedIngs []*v1beta1.Ingress | ||
|
||
// Create Ingresses. | ||
for _, ing := range tc.ings { | ||
ing = ing.DeepCopy() | ||
ing.Namespace = s.Namespace | ||
if _, err := crud.Create(ing); err != nil { | ||
t.Fatalf("create(%s/%s) = %v, want nil; Ingress: %v", ing.Namespace, ing.Name, err, ing) | ||
} | ||
t.Logf("Ingress created (%s/%s)", ing.Namespace, ing.Name) | ||
} | ||
// Perform whitebox testing. | ||
for _, ing := range tc.ings { | ||
// Determine the expected finalizer after ingress creation. | ||
isV1Finalizer := false | ||
if len(ing.GetFinalizers()) > 0 { | ||
isV1Finalizer = true | ||
} | ||
ing = waitForStableIngress(true, ing, s, t) | ||
if isV1Finalizer { | ||
// Assert that v1 finalizer is added. | ||
if err := checkV1Finalizer(ing); err != nil { | ||
t.Fatalf("checkV1Finalizer(%s/%s) = %v, want nil", ing.Namespace, ing.Name, err) | ||
} | ||
} else { | ||
// Assert that v2 finalizer is added. | ||
if err := checkV2Finalizer(ing); err != nil { | ||
t.Fatalf("checkV2Finalizer(%s/%s) = %v, want nil", ing.Namespace, ing.Name, err) | ||
} | ||
} | ||
// Perform whitebox testing. This also tests naming scheme. | ||
gclbs = append(gclbs, whiteboxTest(ing, s, t, "")) | ||
updatedIngs = append(updatedIngs, ing) | ||
} | ||
|
||
// Delete the first ingress. | ||
deleteOptions := &fuzz.GCLBDeleteOptions{ | ||
SkipDefaultBackend: true, | ||
} | ||
// Determine if we need to skip backends. | ||
if len(updatedIngs) > 1 { | ||
deleteOptions.SkipBackends = true | ||
} | ||
if err := e2e.WaitForIngressDeletion(ctx, gclbs[0], s, updatedIngs[0], deleteOptions); err != nil { | ||
t.Errorf("e2e.WaitForIngressDeletion(..., %q, nil) = %v, want nil", tc.ings[0].Name, err) | ||
} | ||
|
||
if len(updatedIngs) > 1 { | ||
// Verify that GCE frontend resources of second ingress are intact. | ||
gclb := whiteboxTest(updatedIngs[1], s, t, "") | ||
// Delete the second ingress. | ||
deleteOptions.SkipBackends = false | ||
if err := e2e.WaitForIngressDeletion(ctx, gclb, s, updatedIngs[1], deleteOptions); err != nil { | ||
t.Errorf("e2e.WaitForIngressDeletion(..., %q, nil) = %v, want nil", tc.ings[1].Name, err) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// TestUpdateTo1dot8 asserts that v1 finalizer is retained/attached and ingress | ||
// continues to use v1 naming naming scheme when master is upgraded to a version that use v1.8. | ||
// Note: The test is named in such a way that it does run as a workflow test or an | ||
// upgrade test for other ingress versions. | ||
func TestUpdateTo1dot8(t *testing.T) { | ||
port80 := intstr.FromInt(80) | ||
svcName := "service-1" | ||
ing := fuzz.NewIngressBuilder("", "ingress-1", ""). | ||
AddPath("foo.com", "/", svcName, port80). | ||
SetIngressClass("gce"). | ||
Build() | ||
Framework.RunWithSandbox("v2namer-master-upgrade", t, func(t *testing.T, s *e2e.Sandbox) { | ||
t.Parallel() | ||
|
||
_, err := e2e.CreateEchoService(s, svcName, nil) | ||
if err != nil { | ||
t.Fatalf("CreateEchoService(_, %q, nil): %v, want nil", svcName, err) | ||
} | ||
t.Logf("Echo service created (%s/%s)", s.Namespace, svcName) | ||
|
||
crud := e2e.IngressCRUD{C: Framework.Clientset} | ||
ing.Namespace = s.Namespace | ||
if _, err := crud.Create(ing); err != nil { | ||
t.Fatalf("create(%s/%s) = %v, want nil; Ingress: %v", ing.Namespace, ing.Name, err, ing) | ||
} | ||
t.Logf("Ingress created (%s/%s)", s.Namespace, ing.Name) | ||
ing = waitForStableIngress(true, ing, s, t) | ||
|
||
// Assert that v1 finalizer is added. | ||
if err := checkV1Finalizer(ing); err != nil { | ||
t.Fatalf("checkV1Finalizer(%s/%s) = %v, want nil", ing.Namespace, ing.Name, err) | ||
} | ||
|
||
// Perform whitebox testing. | ||
whiteboxTest(ing, s, t, "") | ||
|
||
for { | ||
// While k8s master is upgrading, it will return a connection refused | ||
// error for any k8s resource we try to hit. We loop until the | ||
// master upgrade has finished. | ||
if s.MasterUpgrading() { | ||
continue | ||
} | ||
|
||
if s.MasterUpgraded() { | ||
t.Logf("Detected master upgrade, adding a path to Ingress to force Ingress update") | ||
newIng := fuzz.NewIngressBuilderFromExisting(ing). | ||
AddPath("bar.com", "/", "service-1", port80). | ||
Build() | ||
newIng.Namespace = s.Namespace | ||
if _, err := crud.Update(newIng); err != nil { | ||
t.Fatalf("error creating Ingress spec: %v", err) | ||
} else { | ||
// If Ingress upgrade succeeds, we update the status on this Ingress | ||
// to Unstable. It is set back to Stable after WaitForIngress below | ||
// finishes successfully. | ||
s.PutStatus(e2e.Unstable) | ||
} | ||
break | ||
} | ||
} | ||
|
||
// Wait for ingress to stabilize after the upgrade. | ||
ing = waitForStableIngress(false, ing, s, t) | ||
|
||
// Assert that v1 finalizer is retained. | ||
if err := checkV1Finalizer(ing); err != nil { | ||
t.Fatalf("checkV1Finalizer(%s/%s) = %v, want nil", ing.Namespace, ing.Name, err) | ||
} | ||
|
||
// Perform whitebox testing. | ||
gclb := whiteboxTest(ing, s, t, "") | ||
|
||
// If the Master has upgraded and the Ingress is stable, | ||
// we delete the Ingress and exit out of the loop to indicate that | ||
// the test is done. | ||
deleteOptions := &fuzz.GCLBDeleteOptions{ | ||
SkipDefaultBackend: true, | ||
} | ||
if err := e2e.WaitForIngressDeletion(context.Background(), gclb, s, ing, deleteOptions); err != nil { | ||
t.Errorf("e2e.WaitForIngressDeletion(..., %q, nil) = %v, want nil", ing.Name, err) | ||
} | ||
}) | ||
} | ||
|
||
// checkV1Finalizer asserts that v1 finalizer exists on Ingress. | ||
func checkV1Finalizer(ing *v1beta1.Ingress) error { | ||
ingFinalizers := ing.GetFinalizers() | ||
if l := len(ingFinalizers); l != 1 { | ||
return fmt.Errorf("expected 1 Finalizer but got %d", l) | ||
} | ||
if ingFinalizers[0] != common.FinalizerKey { | ||
return fmt.Errorf("expected Finalizer %q but got %q", common.FinalizerKey, ingFinalizers[0]) | ||
} | ||
return nil | ||
} | ||
|
||
// checkV2Finalizer asserts that v2 finalizer exists on Ingress. | ||
func checkV2Finalizer(ing *v1beta1.Ingress) error { | ||
ingFinalizers := ing.GetFinalizers() | ||
if l := len(ingFinalizers); l != 1 { | ||
return fmt.Errorf("expected 1 Finalizer but got %d", l) | ||
} | ||
if ingFinalizers[0] != common.FinalizerKeyV2 { | ||
return fmt.Errorf("expected Finalizer %q but got %q", common.FinalizerKeyV2, ingFinalizers[0]) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.