Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update e2e framework for ILB and add basic test #824

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions cmd/e2e-test/ilb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
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"
"k8s.io/api/networking/v1beta1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/ingress-gce/pkg/annotations"
"k8s.io/ingress-gce/pkg/e2e"
"k8s.io/ingress-gce/pkg/fuzz"
"k8s.io/ingress-gce/pkg/fuzz/features"
"testing"
)

func TestILBBasic(t *testing.T) {
t.Parallel()

port80 := intstr.FromInt(80)

for _, tc := range []struct {
desc string
ing *v1beta1.Ingress

numForwardingRules int
numBackendServices int
}{
{
desc: "http ILB default backend",
ing: fuzz.NewIngressBuilder("", "ingress-1", "").
DefaultBackend("service-1", port80).
ConfigureForILB().
Build(),
numForwardingRules: 1,
numBackendServices: 1,
},
{
desc: "http ILB one path",
ing: fuzz.NewIngressBuilder("", "ingress-1", "").
AddPath("test.com", "/", "service-1", port80).
ConfigureForILB().
Build(),
numForwardingRules: 1,
numBackendServices: 2,
},
{
desc: "http ILB multiple paths",
ing: fuzz.NewIngressBuilder("", "ingress-1", "").
AddPath("test.com", "/foo", "service-1", port80).
AddPath("test.com", "/bar", "service-1", port80).
ConfigureForILB().
Build(),
numForwardingRules: 1,
numBackendServices: 2,
},
} {
tc := tc // Capture tc as we are running this in parallel.
Framework.RunWithSandbox(tc.desc, t, func(t *testing.T, s *e2e.Sandbox) {
t.Parallel()

ctx := context.Background()

t.Logf("Ingress = %s", tc.ing.String())

negAnnotation := annotations.NegAnnotation{Ingress: true}
annotation := map[string]string{annotations.NEGAnnotationKey: negAnnotation.String()}

_, err := e2e.CreateEchoService(s, "service-1", annotation)
if err != nil {
t.Fatalf("error creating echo service: %v", err)
}
t.Logf("Echo service created (%s/%s)", s.Namespace, "service-1")

if _, err := Framework.Clientset.NetworkingV1beta1().Ingresses(s.Namespace).Create(tc.ing); err != nil {
t.Fatalf("error creating Ingress spec: %v", err)
}
t.Logf("Ingress created (%s/%s)", s.Namespace, tc.ing.Name)

ing, err := e2e.WaitForIngress(s, tc.ing, nil)
if err != nil {
t.Fatalf("error waiting for Ingress to stabilize: %v", err)
}
t.Logf("GCLB resources createdd (%s/%s)", s.Namespace, tc.ing.Name)

// Perform whitebox testing.
if len(ing.Status.LoadBalancer.Ingress) < 1 {
t.Fatalf("Ingress does not have an IP: %+v", ing.Status)
}

vip := ing.Status.LoadBalancer.Ingress[0].IP
t.Logf("Ingress %s/%s VIP = %s", s.Namespace, tc.ing.Name, vip)
gclb, err := fuzz.GCLBForVIP(context.Background(), Framework.Cloud, vip, fuzz.FeatureValidators(features.All))
if err != nil {
t.Fatalf("Error getting GCP resources for LB with IP = %q: %v", vip, err)
}

if err = e2e.CheckGCLB(gclb, tc.numForwardingRules, tc.numBackendServices); err != nil {
t.Error(err)
}

deleteOptions := &fuzz.GCLBDeleteOptions{
SkipDefaultBackend: true,
}
if err := e2e.WaitForIngressDeletion(ctx, gclb, s, ing, deleteOptions); err != nil {
t.Errorf("e2e.WaitForIngressDeletion(..., %q, nil) = %v, want nil", ing.Name, err)
}
})
}
}
5 changes: 5 additions & 0 deletions pkg/fuzz/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type FeatureValidator interface {
CheckResponse(host, path string, resp *http.Response, body []byte) (CheckResponseAction, error)

HasAlphaResource(resourceType string) bool
HasAlphaRegionResource(resourceType string) bool
HasBetaResource(resourceType string) bool
}

Expand Down Expand Up @@ -97,6 +98,10 @@ func (*NullValidator) HasAlphaResource(resourceType string) bool {
return false
}

func (*NullValidator) HasAlphaRegionResource(resourceType string) bool {
return false
}

// HasBetaResource implements Feature.
func (*NullValidator) HasBetaResource(resourceType string) bool {
return false
Expand Down
1 change: 1 addition & 0 deletions pkg/fuzz/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ var All = []fuzz.Feature{
Affinity,
NEG,
AppProtocol,
ILB,
}
69 changes: 69 additions & 0 deletions pkg/fuzz/features/ilb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
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 features

import (
"k8s.io/api/networking/v1beta1"
"k8s.io/ingress-gce/pkg/fuzz"
"net/http"
)

// ILB is an internal load balancer
var ILB = &ILBFeature{}

// ILBFeature implements the associated feature
type ILBFeature struct{}

// NewValidator implements fuzz.Feature.
func (*ILBFeature) NewValidator() fuzz.FeatureValidator {
return &ILBValidator{}
}

// Name implements fuzz.Feature.
func (*ILBFeature) Name() string {
return "ILB"
}

// ILBValidator is an example validator.
type ILBValidator struct {
fuzz.NullValidator

ing *v1beta1.Ingress
env fuzz.ValidatorEnv
}

// Name implements fuzz.FeatureValidator.
func (*ILBValidator) Name() string {
return "ILB"
}

// ConfigureAttributes implements fuzz.FeatureValidator.
func (v *ILBValidator) ConfigureAttributes(env fuzz.ValidatorEnv, ing *v1beta1.Ingress, a *fuzz.IngressValidatorAttributes) error {
// Capture the env for use later in CheckResponse.
v.ing = ing
v.env = env
return nil
}

// CheckResponse implements fuzz.FeatureValidator.
func (v *ILBValidator) CheckResponse(host, path string, resp *http.Response, body []byte) (fuzz.CheckResponseAction, error) {
return fuzz.CheckResponseContinue, nil
}

func (v *ILBValidator) HasAlphaRegionResource(resourceType string) bool {
return true
}
44 changes: 44 additions & 0 deletions pkg/fuzz/features/neg.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package features
import (
"context"
"fmt"
"k8s.io/klog"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -92,6 +93,9 @@ func (v *negValidator) CheckResponse(host, path string, resp *http.Response, bod

urlMapName := v.env.Namer().UrlMap(v.env.Namer().LoadBalancer(key))
if negEnabled {
if utils.IsGCEL7ILBIngress(v.ing) {
return fuzz.CheckResponseContinue, verifyNegRegionBackend(v.env, negName, negName, urlMapName)
}
return fuzz.CheckResponseContinue, verifyNegBackend(v.env, negName, urlMapName)
} else {
return fuzz.CheckResponseContinue, verifyIgBackend(v.env, v.env.Namer().IGBackend(int64(svcPort.NodePort)), urlMapName)
Expand Down Expand Up @@ -143,6 +147,7 @@ func verifyIgBackend(env fuzz.ValidatorEnv, bsName string, urlMapName string) er

// verifyBackend verifies the backend service and check if the corresponding backend group has the keyword
func verifyBackend(env fuzz.ValidatorEnv, bsName string, backendKeyword string, urlMapName string) error {
klog.V(3).Info("Verifying NEG Global Backend")
ctx := context.Background()
beService, err := env.Cloud().BackendServices().Get(ctx, &meta.Key{Name: bsName})
if err != nil {
Expand Down Expand Up @@ -178,3 +183,42 @@ func verifyBackend(env fuzz.ValidatorEnv, bsName string, backendKeyword string,

return fmt.Errorf("backend service %q is not used by UrlMap %q", bsName, urlMapName)
}

// verifyBackend verifies the backend service and check if the corresponding backend group has the keyword
func verifyNegRegionBackend(env fuzz.ValidatorEnv, bsName string, backendKeyword string, urlMapName string) error {
klog.V(3).Info("Verifying NEG Regional Backend")
ctx := context.Background()
beService, err := env.Cloud().AlphaRegionBackendServices().Get(ctx, &meta.Key{Name: bsName, Region: "us-central1"})
if err != nil {
return err
}

if beService == nil {
return fmt.Errorf("no backend service returned for name %s", bsName)
}

for _, be := range beService.Backends {
if !strings.Contains(be.Group, backendKeyword) {
return fmt.Errorf("backend group %q of backend service %q does not contain keyword %q", be.Group, bsName, backendKeyword)
}
}

// Examine if ingress url map is targeting the backend service
urlMap, err := env.Cloud().AlphaRegionUrlMaps().Get(ctx, &meta.Key{Name: urlMapName, Region: "us-central1"})
if err != nil {
return err
}

if strings.Contains(urlMap.DefaultService, beService.Name) {
return nil
}
for _, pathMatcher := range urlMap.PathMatchers {
for _, rule := range pathMatcher.PathRules {
if strings.Contains(rule.Service, beService.Name) {
return nil
}
}
}

return fmt.Errorf("backend service %q is not used by UrlMap %q", bsName, urlMapName)
}
Loading