|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +package loadbalancerclass |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + . "github.com/onsi/ginkgo/v2" |
| 12 | + . "github.com/onsi/gomega" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + core_v1 "k8s.io/api/core/v1" |
| 15 | + api_errors "k8s.io/apimachinery/pkg/api/errors" |
| 16 | + meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 17 | + "k8s.io/utils/ptr" |
| 18 | + |
| 19 | + "github.com/kube-vip/kube-vip-cloud-provider/pkg/provider" |
| 20 | + tu "github.com/kube-vip/kube-vip-cloud-provider/pkg/testutil" |
| 21 | + "github.com/kube-vip/kube-vip-cloud-provider/test/e2e" |
| 22 | +) |
| 23 | + |
| 24 | +// Each suite load default manifest from scratch, so that changes on manifest objects won't impact other tests suites. |
| 25 | +var f = e2e.NewFramework() |
| 26 | + |
| 27 | +func TestDeployWithLoadBalancerClass(t *testing.T) { |
| 28 | + RegisterFailHandler(Fail) |
| 29 | + RunSpecs(t, "deploy with loadbalancerclass") |
| 30 | +} |
| 31 | + |
| 32 | +var _ = BeforeSuite(func() { |
| 33 | + // By default, configMap only contains below 3 cidr: |
| 34 | + // cidr-default: 192.168.0.200/29 |
| 35 | + // cidr-plunder: 192.168.0.210/29 |
| 36 | + // cidr-testing: 192.168.0.220/29 |
| 37 | + |
| 38 | + // enable loadbalancerclass |
| 39 | + f.Deployment.Deployment.Spec.Template.Spec.Containers[0].Env = append(f.Deployment.Deployment.Spec.Template.Spec.Containers[0].Env, |
| 40 | + core_v1.EnvVar{ |
| 41 | + Name: provider.EnableLoadbalancerClassEnvKey, |
| 42 | + Value: "true", |
| 43 | + }, |
| 44 | + ) |
| 45 | + require.NoError(f.T(), f.Deployment.EnsureResources()) |
| 46 | +}) |
| 47 | + |
| 48 | +var _ = AfterSuite(func() { |
| 49 | + // Reset resource requests for other tests. |
| 50 | + require.NoError(f.T(), f.Deployment.DeleteResources()) |
| 51 | +}) |
| 52 | + |
| 53 | +var watchedNamespaces = []string{"default", "testing", "plunder"} |
| 54 | + |
| 55 | +var _ = Describe("Loadbalancerclass enabled", func() { |
| 56 | + Context("Deploy service with loadbalancerclass in different namespaces that kube-vip-cloud-provider is configured to watch and is not configured to watch", func() { |
| 57 | + f.NamespacedTest("create-services-in-different-namespace-with-loadblaancerclass", func(namespace string) { |
| 58 | + Specify("Service not be reconcile if namespace is not configured namespace testing, service in default namespace should be reconciled", func() { |
| 59 | + ctx := context.TODO() |
| 60 | + By("Create a service type LB in namespace that's not testing") |
| 61 | + svc := tu.NewService("test1", tu.TweakNamespace(namespace), tu.TweakAddLBClass(ptr.To(provider.LoadbalancerClass))) |
| 62 | + _, err := f.Client.CoreV1().Services(svc.Namespace).Create(ctx, svc, meta_v1.CreateOptions{}) |
| 63 | + require.NoError(f.T(), err) |
| 64 | + |
| 65 | + By("Service should not have IP assigned, it shouldn't have kube-vip annotations and labels") |
| 66 | + require.Eventually(f.T(), func() bool { |
| 67 | + svc, err = f.Client.CoreV1().Services(svc.Namespace).Get(ctx, svc.Name, meta_v1.GetOptions{}) |
| 68 | + if err != nil { |
| 69 | + return false |
| 70 | + } |
| 71 | + return !e2e.ServiceIsReconciled(svc) && !e2e.ServiceHasIPAssigned(svc) |
| 72 | + }, 30*time.Second, time.Second, fmt.Sprintf("Service is not supposed to have label or annotation %v, with error %v", svc, err)) |
| 73 | + |
| 74 | + for _, ns := range watchedNamespaces { |
| 75 | + By("Create a service type LB in watched namespace") |
| 76 | + svc = tu.NewService(fmt.Sprintf("test-%s", ns), tu.TweakNamespace(ns), tu.TweakAddLBClass(ptr.To(provider.LoadbalancerClass))) |
| 77 | + _, err = f.Client.CoreV1().Services(svc.Namespace).Create(ctx, svc, meta_v1.CreateOptions{}) |
| 78 | + require.NoError(f.T(), err) |
| 79 | + |
| 80 | + By("Service should have a valid IP assigned, and related kube-vip annotations and labels") |
| 81 | + require.Eventually(f.T(), func() bool { |
| 82 | + svc, err = f.Client.CoreV1().Services(svc.Namespace).Get(ctx, svc.Name, meta_v1.GetOptions{}) |
| 83 | + if err != nil { |
| 84 | + return false |
| 85 | + } |
| 86 | + return e2e.ServiceIsReconciled(svc) && e2e.ServiceHasIPAssigned(svc) |
| 87 | + }, 30*time.Second, time.Second, fmt.Sprintf("Service is not successfully reconciled %v, with error %v", svc, err)) |
| 88 | + |
| 89 | + By("Clean up the service") |
| 90 | + err = f.Client.CoreV1().Services(ns).Delete(context.TODO(), svc.Name, |
| 91 | + meta_v1.DeleteOptions{PropagationPolicy: ptr.To(meta_v1.DeletePropagationBackground)}) |
| 92 | + require.Eventually(f.T(), func() bool { |
| 93 | + svc, err = f.Client.CoreV1().Services(svc.Namespace).Get(ctx, svc.Name, meta_v1.GetOptions{}) |
| 94 | + if api_errors.IsNotFound(err) { |
| 95 | + return true |
| 96 | + } |
| 97 | + return false |
| 98 | + }, 30*time.Second, time.Second, fmt.Sprintf("Service is not successfully deleted %v, with error %v", svc, err)) |
| 99 | + } |
| 100 | + }) |
| 101 | + }, watchedNamespaces[1:]...) // Don't delete default namespace. |
| 102 | + }) |
| 103 | + |
| 104 | + Context("Deploy service without loadbalancerclass in different namespaces that kube-vip-cloud-provider is configured to watch", func() { |
| 105 | + f.NamespacedTest("create-services-namespace-without-loadblaancerclass", func(namespace string) { |
| 106 | + Specify("Service not be reconcile if service doesn't have loadbalancer class even though namespace is configured to be watched", func() { |
| 107 | + ctx := context.TODO() |
| 108 | + |
| 109 | + for _, ns := range watchedNamespaces { |
| 110 | + By("Create a service type LB in watched namespace") |
| 111 | + svc := tu.NewService("test1", tu.TweakNamespace(ns)) |
| 112 | + _, err := f.Client.CoreV1().Services(svc.Namespace).Create(ctx, svc, meta_v1.CreateOptions{}) |
| 113 | + require.NoError(f.T(), err) |
| 114 | + By("Service should not have IP assigned, it shouldn't have kube-vip annotations and labels") |
| 115 | + require.Eventually(f.T(), func() bool { |
| 116 | + svc, err = f.Client.CoreV1().Services(svc.Namespace).Get(ctx, svc.Name, meta_v1.GetOptions{}) |
| 117 | + if err != nil { |
| 118 | + return false |
| 119 | + } |
| 120 | + return !e2e.ServiceIsReconciled(svc) && !e2e.ServiceHasIPAssigned(svc) |
| 121 | + }, 30*time.Second, time.Second, fmt.Sprintf("Service is not supposed to have label or annotation %v, with error %v", svc, err)) |
| 122 | + |
| 123 | + By("Clean up the service") |
| 124 | + err = f.Client.CoreV1().Services(ns).Delete(context.TODO(), svc.Name, |
| 125 | + meta_v1.DeleteOptions{PropagationPolicy: ptr.To(meta_v1.DeletePropagationBackground)}) |
| 126 | + require.Eventually(f.T(), func() bool { |
| 127 | + svc, err = f.Client.CoreV1().Services(svc.Namespace).Get(ctx, svc.Name, meta_v1.GetOptions{}) |
| 128 | + if api_errors.IsNotFound(err) { |
| 129 | + return true |
| 130 | + } |
| 131 | + return false |
| 132 | + }, 30*time.Second, time.Second, fmt.Sprintf("Service is not successfully deleted %v, with error %v", svc, err)) |
| 133 | + } |
| 134 | + }) |
| 135 | + }, watchedNamespaces[1:]...) // Don't delete default namespace. |
| 136 | + }) |
| 137 | +}) |
0 commit comments