-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Most of the tests in l4_test.go are from gce_loadbalancer_internal_test.go
- Loading branch information
Showing
7 changed files
with
939 additions
and
2 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,200 @@ | ||
/* | ||
Copyright 2020 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 controller | ||
|
||
import ( | ||
"k8s.io/ingress-gce/pkg/loadbalancers" | ||
"k8s.io/ingress-gce/pkg/neg/types" | ||
"testing" | ||
"time" | ||
|
||
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud" | ||
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta" | ||
api_v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1" | ||
//"k8s.io/ingress-gce/pkg/loadbalancers" | ||
"k8s.io/client-go/kubernetes/fake" | ||
"k8s.io/ingress-gce/pkg/composite" | ||
"k8s.io/ingress-gce/pkg/context" | ||
"k8s.io/ingress-gce/pkg/test" | ||
"k8s.io/ingress-gce/pkg/utils/common" | ||
"k8s.io/ingress-gce/pkg/utils/namer" | ||
"k8s.io/legacy-cloud-providers/gce" | ||
) | ||
|
||
func newServiceController() *L4Controller { | ||
kubeClient := fake.NewSimpleClientset() | ||
fakeGCE := gce.NewFakeGCECloud(gce.DefaultTestClusterValues()) | ||
(fakeGCE.Compute().(*cloud.MockGCE)).MockForwardingRules.InsertHook = loadbalancers.InsertForwardingRuleHook | ||
|
||
namer := namer.NewNamer(clusterUID, "") | ||
|
||
stopCh := make(chan struct{}) | ||
ctxConfig := context.ControllerContextConfig{ | ||
Namespace: api_v1.NamespaceAll, | ||
ResyncPeriod: 1 * time.Minute, | ||
} | ||
ctx := context.NewControllerContext(nil, kubeClient, nil, nil, fakeGCE, namer, "" /*kubeSystemUID*/, ctxConfig) | ||
return NewL4Controller(ctx, stopCh) | ||
} | ||
|
||
func addILBService(l4c *L4Controller, svc *api_v1.Service) { | ||
l4c.ctx.KubeClient.CoreV1().Services(svc.Namespace).Create(svc) | ||
l4c.ctx.ServiceInformer.GetIndexer().Add(svc) | ||
} | ||
|
||
func updateILBService(l4c *L4Controller, svc *api_v1.Service) { | ||
l4c.ctx.KubeClient.CoreV1().Services(svc.Namespace).Update(svc) | ||
l4c.ctx.ServiceInformer.GetIndexer().Update(svc) | ||
} | ||
|
||
func deleteILBService(l4c *L4Controller, svc *api_v1.Service) { | ||
l4c.ctx.KubeClient.CoreV1().Services(svc.Namespace).Delete(svc.Name, &v1.DeleteOptions{}) | ||
l4c.ctx.ServiceInformer.GetIndexer().Delete(svc) | ||
} | ||
|
||
func addNEG(l4c *L4Controller, svc *api_v1.Service) { | ||
// Also create a fake NEG for this service since the sync code will try to link the backend service to NEG | ||
negName := l4c.ctx.ClusterNamer.PrimaryIPNEG(svc.Namespace, svc.Name) | ||
neg := &composite.NetworkEndpointGroup{Name: negName} | ||
key := meta.ZonalKey(negName, types.TestZone1) | ||
composite.CreateNetworkEndpointGroup(l4c.ctx.Cloud, key, neg) | ||
} | ||
|
||
func getKeyForSvc(svc *api_v1.Service, t *testing.T) string { | ||
key, err := common.KeyFunc(svc) | ||
if err != nil { | ||
t.Fatalf("Failed to get key for service %v, err : %v", svc, err) | ||
} | ||
return key | ||
} | ||
|
||
func validateSvcStatus(svc *api_v1.Service, expectStatus bool, t *testing.T) { | ||
if common.HasGivenFinalizer(svc.ObjectMeta, common.ILBFinalizerV2) != expectStatus { | ||
t.Fatalf("Expected L4 finalizer present to be %v, but it was %v", expectStatus, !expectStatus) | ||
} | ||
if len(svc.Status.LoadBalancer.Ingress) == 0 || svc.Status.LoadBalancer.Ingress[0].IP == "" { | ||
if expectStatus { | ||
t.Fatalf("Invalid LoadBalancer status field in service - %+v", svc.Status.LoadBalancer) | ||
} | ||
} | ||
if len(svc.Status.LoadBalancer.Ingress) > 0 && !expectStatus { | ||
t.Fatalf("Expected LoadBalancer status to be empty, Got %v", svc.Status.LoadBalancer) | ||
} | ||
} | ||
|
||
// TestProcessCreateOrUpdate verifies the processing loop in L4Controller. | ||
// This test adds a new service, then performs a valid update and then modifies the service type to External and ensures | ||
// that the status field is as expected in each case. | ||
func TestProcessCreateOrUpdate(t *testing.T) { | ||
l4c := newServiceController() | ||
newSvc := test.NewL4ILBService(false, 8080) | ||
addILBService(l4c, newSvc) | ||
addNEG(l4c, newSvc) | ||
err := l4c.sync(getKeyForSvc(newSvc, t)) | ||
if err != nil { | ||
t.Errorf("Failed to sync newly added service %s, err %v", newSvc.Name, err) | ||
} | ||
// List the service and ensure that it contains the finalizer as well as Status field. | ||
svc, err := l4c.client.CoreV1().Services(newSvc.Namespace).Get(newSvc.Name, v1.GetOptions{}) | ||
if err != nil { | ||
t.Errorf("Failed to lookup service %s, err: %v", newSvc.Name, err) | ||
} | ||
validateSvcStatus(svc, true, t) | ||
|
||
// set the TrafficPolicy of the service to Local | ||
newSvc.Spec.ExternalTrafficPolicy = api_v1.ServiceExternalTrafficPolicyTypeLocal | ||
updateILBService(l4c, newSvc) | ||
err = l4c.sync(getKeyForSvc(newSvc, t)) | ||
if err != nil { | ||
t.Errorf("Failed to sync updated service %s, err %v", newSvc.Name, err) | ||
} | ||
// List the service and ensure that it contains the finalizer as well as Status field. | ||
svc, err = l4c.client.CoreV1().Services(newSvc.Namespace).Get(newSvc.Name, v1.GetOptions{}) | ||
if err != nil { | ||
t.Errorf("Failed to lookup service %s, err: %v", newSvc.Name, err) | ||
} | ||
validateSvcStatus(svc, true, t) | ||
|
||
// Remove the Internal LoadBalancer annotation, this should trigger a cleanup. | ||
delete(newSvc.Annotations, gce.ServiceAnnotationLoadBalancerType) | ||
updateILBService(l4c, newSvc) | ||
err = l4c.sync(getKeyForSvc(newSvc, t)) | ||
if err != nil { | ||
t.Errorf("Failed to sync updated service %s, err %v", newSvc.Name, err) | ||
} | ||
// List the service and ensure that it contains the finalizer as well as Status field. | ||
svc, err = l4c.client.CoreV1().Services(newSvc.Namespace).Get(newSvc.Name, v1.GetOptions{}) | ||
if err != nil { | ||
t.Errorf("Failed to lookup service %s, err: %v", newSvc.Name, err) | ||
} | ||
validateSvcStatus(svc, false, t) | ||
} | ||
|
||
func TestProcessDeletion(t *testing.T) { | ||
l4c := newServiceController() | ||
newSvc := test.NewL4ILBService(false, 8080) | ||
addILBService(l4c, newSvc) | ||
addNEG(l4c, newSvc) | ||
err := l4c.sync(getKeyForSvc(newSvc, t)) | ||
if err != nil { | ||
t.Errorf("Failed to sync newly added service %s, err %v", newSvc.Name, err) | ||
} | ||
// List the service and ensure that it contains the finalizer as well as Status field. | ||
svc, err := l4c.client.CoreV1().Services(newSvc.Namespace).Get(newSvc.Name, v1.GetOptions{}) | ||
if err != nil { | ||
t.Errorf("Failed to lookup service %s, err: %v", newSvc.Name, err) | ||
} | ||
validateSvcStatus(svc, true, t) | ||
|
||
// Mark the service for deletion by updating timestamp. Use svc instead of newSvc since that has the finalizer. | ||
// TODO use patch instead of update here as well. | ||
svc.DeletionTimestamp = &v1.Time{} | ||
updateILBService(l4c, svc) | ||
err = l4c.sync(getKeyForSvc(svc, t)) | ||
if err != nil { | ||
t.Errorf("Failed to sync updated service %s, err %v", svc.Name, err) | ||
} | ||
svc, err = l4c.client.CoreV1().Services(svc.Namespace).Get(svc.Name, v1.GetOptions{}) | ||
if err != nil { | ||
t.Errorf("Failed to lookup service %s, err: %v", newSvc.Name, err) | ||
} | ||
validateSvcStatus(svc, false, t) | ||
deleteILBService(l4c, svc) | ||
svc, err = l4c.client.CoreV1().Services(newSvc.Namespace).Get(svc.Name, v1.GetOptions{}) | ||
if svc != nil { | ||
t.Errorf("Expected service to be deleted, but was found - %v", svc) | ||
} | ||
} | ||
|
||
func TestProcessCreateLegacyService(t *testing.T) { | ||
l4c := newServiceController() | ||
newSvc := test.NewL4ILBService(false, 8080) | ||
// Set the legacy finalizer | ||
newSvc.Finalizers = append(newSvc.Finalizers, common.LegacyILBFinalizer) | ||
addILBService(l4c, newSvc) | ||
err := l4c.sync(getKeyForSvc(newSvc, t)) | ||
if err != nil { | ||
t.Errorf("Failed to sync newly added service %s, err %v", newSvc.Name, err) | ||
} | ||
// List the service and ensure that the status field is not updated. | ||
svc, err := l4c.client.CoreV1().Services(newSvc.Namespace).Get(newSvc.Name, v1.GetOptions{}) | ||
if err != nil { | ||
t.Errorf("Failed to lookup service %s, err: %v", newSvc.Name, err) | ||
} | ||
validateSvcStatus(svc, false, t) | ||
} |
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.