diff --git a/cmd/controller/main.go b/cmd/controller/main.go index b40166131..027c9c3ef 100644 --- a/cmd/controller/main.go +++ b/cmd/controller/main.go @@ -137,7 +137,6 @@ func main() { BaseReconciler: dnsPolicyBaseReconciler, }, DNSProvider: provider.DNSProviderFactory, - Placer: placer, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "DNSPolicy") os.Exit(1) diff --git a/pkg/controllers/dnspolicy/dnspolicy_controller.go b/pkg/controllers/dnspolicy/dnspolicy_controller.go index 7a9d7e243..bd60af3aa 100644 --- a/pkg/controllers/dnspolicy/dnspolicy_controller.go +++ b/pkg/controllers/dnspolicy/dnspolicy_controller.go @@ -42,7 +42,6 @@ import ( "github.com/Kuadrant/multicluster-gateway-controller/pkg/_internal/conditions" "github.com/Kuadrant/multicluster-gateway-controller/pkg/apis/v1alpha1" "github.com/Kuadrant/multicluster-gateway-controller/pkg/controllers/events" - "github.com/Kuadrant/multicluster-gateway-controller/pkg/controllers/gateway" "github.com/Kuadrant/multicluster-gateway-controller/pkg/dns" ) @@ -64,7 +63,6 @@ type DNSPolicyReconciler struct { reconcilers.TargetRefReconciler DNSProvider dns.DNSProviderFactory dnsHelper dnsHelper - Placer gateway.GatewayPlacer } //+kubebuilder:rbac:groups=kuadrant.io,resources=dnspolicies,verbs=get;list;watch;create;update;patch;delete diff --git a/pkg/controllers/dnspolicy/dnspolicy_dnsrecords.go b/pkg/controllers/dnspolicy/dnspolicy_dnsrecords.go index e79cae733..0dd2487f0 100644 --- a/pkg/controllers/dnspolicy/dnspolicy_dnsrecords.go +++ b/pkg/controllers/dnspolicy/dnspolicy_dnsrecords.go @@ -3,6 +3,9 @@ package dnspolicy import ( "context" "fmt" + "strings" + + clusterv1 "open-cluster-management.io/api/cluster/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/labels" @@ -13,6 +16,7 @@ import ( "github.com/kuadrant/kuadrant-operator/pkg/reconcilers" "github.com/Kuadrant/multicluster-gateway-controller/pkg/apis/v1alpha1" + "github.com/Kuadrant/multicluster-gateway-controller/pkg/controllers/gateway" "github.com/Kuadrant/multicluster-gateway-controller/pkg/dns" ) @@ -47,14 +51,9 @@ func (r *DNSPolicyReconciler) reconcileGatewayDNSRecords(ctx context.Context, ga return err } - placed, err := r.Placer.GetPlacedClusters(ctx, gateway) - if err != nil { - log.V(3).Info("error getting placed clusters") - return err - } - clusters := placed.UnsortedList() + clusterAddresses := getClusterGatewayAddresses(gateway) - log.V(3).Info("checking gateway for attached routes ", "gateway", gateway.Name, "clusters", placed) + log.V(3).Info("checking gateway for attached routes ", "gateway", gateway.Name, "clusters", clusterAddresses) for _, listener := range gateway.Spec.Listeners { var clusterGateways []dns.ClusterGateway @@ -67,24 +66,23 @@ func (r *DNSPolicyReconciler) reconcileGatewayDNSRecords(ctx context.Context, ga log.Info("skipping listener no hostname assigned", listener.Name, "in ns ", gateway.Namespace) continue } - for _, downstreamCluster := range clusters { + for clusterName, clusterAddress := range clusterAddresses { // Only consider host for dns if there's at least 1 attached route to the listener for this host in *any* gateway - log.V(1).Info("checking downstream", "listener ", listener.Name) - attached, err := r.Placer.ListenerTotalAttachedRoutes(ctx, gateway, string(listener.Name), downstreamCluster) - if err != nil { - log.Error(err, "failed to get total attached routes for listener ", "listener", listener.Name) - continue - } + log.V(3).Info("checking downstream", "listener ", listener.Name) + attached := listenerTotalAttachedRoutes(gateway, clusterName, listener, clusterAddress) + if attached == 0 { - log.V(1).Info("no attached routes for ", "listener", listener.Name, "cluster ", downstreamCluster) + log.V(1).Info("no attached routes for ", "listener", listener, "cluster ", clusterName) continue } - log.V(1).Info("hostHasAttachedRoutes", "host", listener.Name, "hostHasAttachedRoutes", attached) - cg, err := r.Placer.GetClusterGateway(ctx, gateway, downstreamCluster) + log.V(3).Info("hostHasAttachedRoutes", "host", listener.Name, "hostHasAttachedRoutes", attached) + + cg, err := r.buildClusterGateway(ctx, clusterName, clusterAddress) if err != nil { return fmt.Errorf("get cluster gateway failed: %s", err) } + clusterGateways = append(clusterGateways, cg) } @@ -142,3 +140,81 @@ func (r *DNSPolicyReconciler) deleteGatewayDNSRecords(ctx context.Context, gatew } return nil } + +func (r *DNSPolicyReconciler) buildClusterGateway(ctx context.Context, downstreamClusterName string, clusterAddress []gatewayv1beta1.GatewayAddress) (dns.ClusterGateway, error) { + var target dns.ClusterGateway + singleClusterAddresses := make([]gatewayv1beta1.GatewayAddress, len(clusterAddress)) + + mc := &clusterv1.ManagedCluster{} + if err := r.Client().Get(ctx, client.ObjectKey{Name: downstreamClusterName}, mc, &client.GetOptions{}); err != nil { + return target, err + } + + for i, addr := range clusterAddress { + addrType := gatewayv1beta1.IPAddressType + if *addr.Type == gateway.MultiClusterHostnameAddressType { + addrType = gatewayv1beta1.HostnameAddressType + } + + singleClusterAddresses[i] = gatewayv1beta1.GatewayAddress{ + Type: &addrType, + Value: addr.Value, + } + } + + target = *dns.NewClusterGateway(mc, singleClusterAddresses) + + return target, nil +} + +func getClusterGatewayAddresses(gw *gatewayv1beta1.Gateway) map[string][]gatewayv1beta1.GatewayAddress { + clusterAddrs := make(map[string][]gatewayv1beta1.GatewayAddress, len(gw.Status.Addresses)) + + for _, address := range gw.Status.Addresses { + var gatewayAddresses []gatewayv1beta1.GatewayAddress + + //Default to Single Cluster (Normal Gateway Status) + cluster := "none" + addressValue := address.Value + + //Check for Multi Cluster (MGC Gateway Status) + if *address.Type == gateway.MultiClusterIPAddressType || *address.Type == gateway.MultiClusterHostnameAddressType { + tmpCluster, tmpAddress, found := strings.Cut(address.Value, "/") + //If this fails something is wrong and the value hasn't been set correctly + if found { + cluster = tmpCluster + addressValue = tmpAddress + } + } + + gatewayAddresses = append(gatewayAddresses, gatewayv1beta1.GatewayAddress{ + Type: address.Type, + Value: addressValue, + }) + clusterAddrs[cluster] = gatewayAddresses + } + + return clusterAddrs +} + +func listenerTotalAttachedRoutes(upstreamGateway *gatewayv1beta1.Gateway, downstreamCluster string, specListener gatewayv1beta1.Listener, addresses []gatewayv1beta1.GatewayAddress) int { + for _, statusListener := range upstreamGateway.Status.Listeners { + // assuming all adresses of the same type on the gateway + // for Multi Cluster (MGC Gateway Status) + if *addresses[0].Type == gateway.MultiClusterIPAddressType || *addresses[0].Type == gateway.MultiClusterHostnameAddressType { + clusterName, listenerName, found := strings.Cut(string(statusListener.Name), ".") + if !found { + return 0 + } + if clusterName == downstreamCluster && listenerName == string(specListener.Name) { + return int(statusListener.AttachedRoutes) + } + } + // Single Cluster (Normal Gateway Status) + if string(statusListener.Name) == string(specListener.Name) { + return int(statusListener.AttachedRoutes) + } + } + + return 0 +} diff --git a/pkg/controllers/dnspolicy/dnspolicy_healthchecks_test.go b/pkg/controllers/dnspolicy/dnspolicy_healthchecks_test.go index 4391921fe..3c676785e 100644 --- a/pkg/controllers/dnspolicy/dnspolicy_healthchecks_test.go +++ b/pkg/controllers/dnspolicy/dnspolicy_healthchecks_test.go @@ -342,7 +342,6 @@ func TestDNSPolicyReconciler_expectedProbesForGateway(t *testing.T) { TargetRefReconciler: tt.fields.TargetRefReconciler, DNSProvider: tt.fields.DNSProvider, dnsHelper: tt.fields.dnsHelper, - Placer: tt.fields.Placer, } got := r.expectedProbesForGateway(tt.args.ctx, tt.args.gw, tt.args.dnsPolicy) if !reflect.DeepEqual(got, tt.want) { diff --git a/test/integration/dnspolicy_controller_test.go b/test/integration/dnspolicy_controller_test.go index 6386a972e..f8d7fc803 100644 --- a/test/integration/dnspolicy_controller_test.go +++ b/test/integration/dnspolicy_controller_test.go @@ -9,6 +9,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + v1 "open-cluster-management.io/api/cluster/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" @@ -25,7 +26,7 @@ import ( . "github.com/Kuadrant/multicluster-gateway-controller/pkg/controllers/dnspolicy" mgcgateway "github.com/Kuadrant/multicluster-gateway-controller/pkg/controllers/gateway" "github.com/Kuadrant/multicluster-gateway-controller/pkg/dns" - . "github.com/Kuadrant/multicluster-gateway-controller/test/util" + testutil "github.com/Kuadrant/multicluster-gateway-controller/test/util" ) func testBuildManagedZone(domainName, ns string) *v1alpha1.ManagedZone { @@ -92,6 +93,34 @@ func testBuildGateway(gwName, gwClassName, hostname, ns, dnspolicy string) *gate } } +func testBuildGatewayAddresses() []gatewayv1beta1.GatewayAddress { + return []gatewayv1beta1.GatewayAddress{ + { + Type: testutil.Pointer(mgcgateway.MultiClusterIPAddressType), + Value: TestPlacedClusterControlName + "/" + TestAttachedRouteAddressOne, + }, + { + Type: testutil.Pointer(mgcgateway.MultiClusterIPAddressType), + Value: TestPlaceClusterWorkloadName + "/" + TestAttachedRouteAddressTwo, + }, + } +} + +func testBuildGatewayListenerStatus(names []string, numRoutes []int32) []gatewayv1beta1.ListenerStatus { + listeners := []gatewayv1beta1.ListenerStatus{} + + for i, name := range names { + listeners = append(listeners, gatewayv1beta1.ListenerStatus{ + AttachedRoutes: numRoutes[i], + Name: gatewayv1beta1.SectionName(name), + Conditions: make([]metav1.Condition, 0), + SupportedKinds: make([]gatewayv1beta1.RouteGroupKind, 0), + }) + } + + return listeners +} + func testBuildDNSPolicyWithHealthCheck(policyName, gwName, ns string, threshold *int) *v1alpha1.DNSPolicy { typedNamespace := gatewayv1beta1.Namespace(ns) protocol := v1alpha1.HttpProtocol @@ -238,7 +267,7 @@ var _ = Describe("DNSPolicy", Ordered, func() { It("should have ready condition with status true", func() { By("creating a valid Gateway") - gateway = NewTestGateway("test-gateway", gwClassName, testNamespace). + gateway = testutil.NewTestGateway("test-gateway", gwClassName, testNamespace). WithHTTPListener("test.example.com").Gateway Expect(k8sClient.Create(ctx, gateway)).To(BeNil()) Eventually(func() error { //gateway exists @@ -263,7 +292,7 @@ var _ = Describe("DNSPolicy", Ordered, func() { var lbHash, dnsRecordName, wildcardDNSRecordName string BeforeEach(func() { - gateway = testBuildGateway(TestPlacedGatewayName, gatewayClass.Name, TestAttachedRouteName, testNamespace, "test-dns-policy") + gateway = testBuildGateway(TestPlacedGatewayName, testutil.DummyCRName, TestAttachedRouteName, testNamespace, "test-dns-policy") lbHash = dns.ToBase36hash(fmt.Sprintf("%s-%s", gateway.Name, gateway.Namespace)) dnsRecordName = fmt.Sprintf("%s-%s", TestPlacedGatewayName, TestAttachedRouteName) wildcardDNSRecordName = fmt.Sprintf("%s-%s", TestPlacedGatewayName, TestWildCardListenerName) @@ -271,12 +300,48 @@ var _ = Describe("DNSPolicy", Ordered, func() { Eventually(func() error { //gateway exists return k8sClient.Get(ctx, client.ObjectKey{Name: gateway.Name, Namespace: gateway.Namespace}, gateway) }, TestTimeoutMedium, TestRetryIntervalMedium).ShouldNot(HaveOccurred()) + Eventually(func() error { // TODO remove the workaround during https://github.com/Kuadrant/multicluster-gateway-controller/issues/330 + // also use a proper gateway class + + if err := k8sClient.Create(ctx, &v1.ManagedCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: TestPlacedClusterControlName, + }, + }); err != nil && !k8serrors.IsAlreadyExists(err) { + return err + } + if err := k8sClient.Create(ctx, &v1.ManagedCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: TestPlaceClusterWorkloadName, + }, + }); err != nil && !k8serrors.IsAlreadyExists(err) { + return err + } + gateway.Status.Addresses = testBuildGatewayAddresses() + gateway.Status.Listeners = testBuildGatewayListenerStatus( + []string{ + TestPlacedClusterControlName + "." + TestAttachedRouteName, + TestPlaceClusterWorkloadName + "." + TestAttachedRouteName, + TestPlacedClusterControlName + "." + TestWildCardListenerName, + TestPlaceClusterWorkloadName + "." + TestWildCardListenerName, + }, + []int32{1, 1, 1, 1}) + return k8sClient.Status().Update(ctx, gateway) + }, TestTimeoutMedium, TestRetryIntervalMedium).ShouldNot(HaveOccurred()) // end of the workaround }) AfterEach(func() { err := k8sClient.Delete(ctx, gateway) // ignore not found err Expect(client.IgnoreNotFound(err)).ToNot(HaveOccurred()) + + dnsRecordList := &v1alpha1.DNSRecordList{} + err = k8sClient.List(ctx, dnsRecordList) + Expect(err).ToNot(HaveOccurred()) + + for _, record := range dnsRecordList.Items { + Expect(k8sClient.Delete(ctx, &record)).ToNot(HaveOccurred()) + } }) Context("weighted dnspolicy", func() { @@ -376,7 +441,7 @@ var _ = Describe("DNSPolicy", Ordered, func() { return nil }, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) Expect(createdDNSRecord.Spec.ManagedZoneRef.Name).To(Equal("example.com")) - Expect(createdDNSRecord.Spec.Endpoints).To(HaveLen(6)) + Expect(createdDNSRecord.Spec.Endpoints).To(HaveLen(len(expectedEndpoints))) Expect(createdDNSRecord.Spec.Endpoints).Should(ContainElements(expectedEndpoints)) }) It("should create a wildcard dns record", func() { @@ -465,7 +530,7 @@ var _ = Describe("DNSPolicy", Ordered, func() { return nil }, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) Expect(wildcardDNSRecord.Spec.ManagedZoneRef.Name).To(Equal("example.com")) - Expect(wildcardDNSRecord.Spec.Endpoints).To(HaveLen(6)) + Expect(wildcardDNSRecord.Spec.Endpoints).To(HaveLen(len(expectedEndpoints))) Expect(wildcardDNSRecord.Spec.Endpoints).Should(ContainElements(expectedEndpoints)) Expect(expectedEndpoints).Should(ContainElements(wildcardDNSRecord.Spec.Endpoints)) }) @@ -624,11 +689,6 @@ var _ = Describe("DNSPolicy", Ordered, func() { }, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) }) - AfterEach(func() { - err := k8sClient.Delete(ctx, gateway) - Expect(err).ToNot(HaveOccurred()) - }) - It("should create a dns record", func() { createdDNSRecord := &v1alpha1.DNSRecord{} expectedEndpoints := []*v1alpha1.Endpoint{ @@ -730,7 +790,7 @@ var _ = Describe("DNSPolicy", Ordered, func() { return nil }, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) Expect(createdDNSRecord.Spec.ManagedZoneRef.Name).To(Equal("example.com")) - Expect(createdDNSRecord.Spec.Endpoints).To(HaveLen(7)) + Expect(createdDNSRecord.Spec.Endpoints).To(HaveLen(len(expectedEndpoints))) Expect(createdDNSRecord.Spec.Endpoints).Should(ContainElements(expectedEndpoints)) Expect(expectedEndpoints).Should(ContainElements(createdDNSRecord.Spec.Endpoints)) @@ -838,11 +898,439 @@ var _ = Describe("DNSPolicy", Ordered, func() { return nil }, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) Expect(wildcardDNSRecord.Spec.ManagedZoneRef.Name).To(Equal("example.com")) - Expect(wildcardDNSRecord.Spec.Endpoints).To(HaveLen(7)) + Expect(wildcardDNSRecord.Spec.Endpoints).To(HaveLen(len(expectedEndpoints))) Expect(wildcardDNSRecord.Spec.Endpoints).Should(ContainElements(expectedEndpoints)) Expect(expectedEndpoints).Should(ContainElements(wildcardDNSRecord.Spec.Endpoints)) }) }) + Context("probes status impact DNS records", func() { + var dnsPolicy *v1alpha1.DNSPolicy + var unhealthy bool + + BeforeEach(func() { + dnsPolicy = testBuildDNSPolicyWithHealthCheck("test-dns-policy", TestPlacedGatewayName, testNamespace, testutil.Pointer(4)) + Expect(k8sClient.Create(ctx, dnsPolicy)).To(BeNil()) + Eventually(func() error { //dns policy exists + return k8sClient.Get(ctx, client.ObjectKey{Name: dnsPolicy.Name, Namespace: dnsPolicy.Namespace}, dnsPolicy) + }, TestTimeoutMedium, TestRetryIntervalMedium).ShouldNot(HaveOccurred()) + }) + + It("should create a dns record", func() { + createdDNSRecord := &v1alpha1.DNSRecord{} + Eventually(func() error { // DNS record exists + if err := k8sClient.Get(ctx, client.ObjectKey{Name: dnsRecordName, Namespace: testNamespace}, createdDNSRecord); err != nil { + return err + } + if len(createdDNSRecord.Spec.Endpoints) != 6 { + return fmt.Errorf("expected %v endpoints in DNSRecord, got %v", 6, len(createdDNSRecord.Spec.Endpoints)) + } + return nil + }, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) + Expect(createdDNSRecord.Spec.Endpoints).To(HaveLen(6)) + }) + It("should have probes that are healthy", func() { + probeList := &v1alpha1.DNSHealthCheckProbeList{} + Eventually(func() error { + Expect(k8sClient.List(ctx, probeList, &client.ListOptions{Namespace: testNamespace})).To(BeNil()) + if len(probeList.Items) != 2 { + return fmt.Errorf("expected %v probes, got %v", 2, len(probeList.Items)) + } + return nil + }, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) + Expect(len(probeList.Items)).To(Equal(2)) + }) + + Context("all unhealthy probes", func() { + It("should publish all dns records endpoints", func() { + lbHash = dns.ToBase36hash(fmt.Sprintf("%s-%s", gateway.Name, gateway.Namespace)) + + expectedEndpoints := []*v1alpha1.Endpoint{ + { + DNSName: "2w705o.lb-" + lbHash + ".test.example.com", + Targets: []string{ + TestAttachedRouteAddressTwo, + }, + RecordType: "A", + SetIdentifier: "", + RecordTTL: 60, + }, + { + DNSName: "s07c46.lb-" + lbHash + ".test.example.com", + Targets: []string{ + TestAttachedRouteAddressOne, + }, + RecordType: "A", + SetIdentifier: "", + RecordTTL: 60, + }, + { + DNSName: "default.lb-" + lbHash + ".test.example.com", + Targets: []string{ + "2w705o.lb-" + lbHash + ".test.example.com", + }, + RecordType: "CNAME", + SetIdentifier: "2w705o.lb-" + lbHash + ".test.example.com", + RecordTTL: 60, + ProviderSpecific: v1alpha1.ProviderSpecific{ + { + Name: "weight", + Value: "120", + }, + }, + }, + { + DNSName: "default.lb-" + lbHash + ".test.example.com", + Targets: []string{ + "s07c46.lb-" + lbHash + ".test.example.com", + }, + RecordType: "CNAME", + SetIdentifier: "s07c46.lb-" + lbHash + ".test.example.com", + RecordTTL: 60, + Labels: nil, + ProviderSpecific: v1alpha1.ProviderSpecific{ + { + Name: "weight", + Value: "120", + }, + }, + }, + { + DNSName: "lb-" + lbHash + ".test.example.com", + Targets: []string{ + "default.lb-" + lbHash + ".test.example.com", + }, + RecordType: "CNAME", + SetIdentifier: "default", + RecordTTL: 300, + ProviderSpecific: v1alpha1.ProviderSpecific{ + { + Name: "geo-code", + Value: "*", + }, + }, + }, + { + DNSName: "test.example.com", + Targets: []string{ + "lb-" + lbHash + ".test.example.com", + }, + RecordType: "CNAME", + SetIdentifier: "", + RecordTTL: 300, + }, + } + + probeList := &v1alpha1.DNSHealthCheckProbeList{} + Eventually(func() error { + Expect(k8sClient.List(ctx, probeList, &client.ListOptions{Namespace: testNamespace})).To(BeNil()) + if len(probeList.Items) != 2 { + return fmt.Errorf("expected %v probes, got %v", 2, len(probeList.Items)) + } + return nil + }, TestTimeoutLong, TestRetryIntervalMedium).Should(BeNil()) + + for _, probe := range probeList.Items { + Eventually(func() error { + if probe.Name == fmt.Sprintf("%s-%s-%s", TestAttachedRouteAddressTwo, TestPlacedGatewayName, TestAttachedRouteName) || + probe.Name == fmt.Sprintf("%s-%s-%s", TestAttachedRouteAddressOne, TestPlacedGatewayName, TestAttachedRouteName) { + getProbe := &v1alpha1.DNSHealthCheckProbe{} + if err := k8sClient.Get(ctx, client.ObjectKey{Name: probe.Name, Namespace: probe.Namespace}, getProbe); err != nil { + return err + } + patch := client.MergeFrom(getProbe.DeepCopy()) + unhealthy = false + getProbe.Status = v1alpha1.DNSHealthCheckProbeStatus{ + LastCheckedAt: metav1.NewTime(time.Now()), + ConsecutiveFailures: *getProbe.Spec.FailureThreshold + 1, + Healthy: &unhealthy, + } + if err := k8sClient.Status().Patch(ctx, getProbe, patch); err != nil { + return err + } + } + return nil + }, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) + } + createdDNSRecord := &v1alpha1.DNSRecord{} + Eventually(func() error { + + err := k8sClient.Get(ctx, client.ObjectKey{Name: dnsRecordName, Namespace: testNamespace}, createdDNSRecord) + if err != nil && k8serrors.IsNotFound(err) { + return err + } + if len(createdDNSRecord.Spec.Endpoints) != len(expectedEndpoints) { + return fmt.Errorf("expected %v endpoints in DNSRecord, got %v", len(expectedEndpoints), len(createdDNSRecord.Spec.Endpoints)) + } + return nil + }, TestTimeoutLong, TestRetryIntervalMedium).Should(BeNil()) + Expect(createdDNSRecord.Spec.Endpoints).To(HaveLen(len(expectedEndpoints))) + Expect(createdDNSRecord.Spec.Endpoints).Should(ContainElements(expectedEndpoints)) + Expect(expectedEndpoints).Should(ContainElements(createdDNSRecord.Spec.Endpoints)) + + }) + }) + Context("some unhealthy probes", func() { + It("should publish expected endpoints", func() { + lbHash = dns.ToBase36hash(fmt.Sprintf("%s-%s", gateway.Name, gateway.Namespace)) + + expectedEndpoints := []*v1alpha1.Endpoint{ + { + DNSName: "2w705o.lb-" + lbHash + ".test.example.com", + Targets: []string{ + TestAttachedRouteAddressTwo, + }, + RecordType: "A", + SetIdentifier: "", + RecordTTL: 60, + }, + { + DNSName: "s07c46.lb-" + lbHash + ".test.example.com", + Targets: []string{ + TestAttachedRouteAddressOne, + }, + RecordType: "A", + SetIdentifier: "", + RecordTTL: 60, + }, + { + DNSName: "default.lb-" + lbHash + ".test.example.com", + Targets: []string{ + "2w705o.lb-" + lbHash + ".test.example.com", + }, + RecordType: "CNAME", + SetIdentifier: "2w705o.lb-" + lbHash + ".test.example.com", + RecordTTL: 60, + ProviderSpecific: v1alpha1.ProviderSpecific{ + { + Name: "weight", + Value: "120", + }, + }, + }, + { + DNSName: "default.lb-" + lbHash + ".test.example.com", + Targets: []string{ + "s07c46.lb-" + lbHash + ".test.example.com", + }, + RecordType: "CNAME", + SetIdentifier: "s07c46.lb-" + lbHash + ".test.example.com", + RecordTTL: 60, + Labels: nil, + ProviderSpecific: v1alpha1.ProviderSpecific{ + { + Name: "weight", + Value: "120", + }, + }, + }, + { + DNSName: "lb-" + lbHash + ".test.example.com", + Targets: []string{ + "default.lb-" + lbHash + ".test.example.com", + }, + RecordType: "CNAME", + SetIdentifier: "default", + RecordTTL: 300, + ProviderSpecific: v1alpha1.ProviderSpecific{ + { + Name: "geo-code", + Value: "*", + }, + }, + }, + { + DNSName: "test.example.com", + Targets: []string{ + "lb-" + lbHash + ".test.example.com", + }, + RecordType: "CNAME", + SetIdentifier: "", + RecordTTL: 300, + }, + } + + probeList := &v1alpha1.DNSHealthCheckProbeList{} + Eventually(func() error { + Expect(k8sClient.List(ctx, probeList, &client.ListOptions{Namespace: testNamespace})).To(BeNil()) + if len(probeList.Items) != 2 { + return fmt.Errorf("expected %v probes, got %v", 2, len(probeList.Items)) + } + return nil + }, TestTimeoutLong, TestRetryIntervalMedium).Should(BeNil()) + Expect(probeList.Items).To(HaveLen(2)) + + Eventually(func() error { + getProbe := &v1alpha1.DNSHealthCheckProbe{} + if err := k8sClient.Get(ctx, client.ObjectKey{Name: fmt.Sprintf("%s-%s-%s", TestAttachedRouteAddressOne, TestPlacedGatewayName, TestAttachedRouteName), Namespace: testNamespace}, getProbe); err != nil { + return err + } + patch := client.MergeFrom(getProbe.DeepCopy()) + unhealthy = false + getProbe.Status = v1alpha1.DNSHealthCheckProbeStatus{ + LastCheckedAt: metav1.NewTime(time.Now()), + ConsecutiveFailures: *getProbe.Spec.FailureThreshold + 1, + Healthy: &unhealthy, + } + if err := k8sClient.Status().Patch(ctx, getProbe, patch); err != nil { + return err + } + return nil + }, TestTimeoutLong, TestRetryIntervalMedium).Should(BeNil()) + + // after that verify that in time the endpoints are 5 in the dnsrecord + createdDNSRecord := &v1alpha1.DNSRecord{} + Eventually(func() error { + err := k8sClient.Get(ctx, client.ObjectKey{Name: dnsRecordName, Namespace: testNamespace}, createdDNSRecord) + if err != nil && k8serrors.IsNotFound(err) { + return err + } + return nil + }, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) + Expect(createdDNSRecord.Spec.Endpoints).To(HaveLen(len(expectedEndpoints))) + Expect(createdDNSRecord.Spec.Endpoints).Should(ContainElements(expectedEndpoints)) + Expect(expectedEndpoints).Should(ContainElements(createdDNSRecord.Spec.Endpoints)) + }) + }) + Context("some unhealthy endpoints for other listener", func() { + It("should publish expected endpoints", func() { + lbHash = dns.ToBase36hash(fmt.Sprintf("%s-%s", gateway.Name, gateway.Namespace)) + + expectedEndpoints := []*v1alpha1.Endpoint{ + { + DNSName: "2w705o.lb-" + lbHash + ".test.example.com", + Targets: []string{ + TestAttachedRouteAddressTwo, + }, + RecordType: "A", + SetIdentifier: "", + RecordTTL: 60, + }, + { + DNSName: "s07c46.lb-" + lbHash + ".test.example.com", + Targets: []string{ + TestAttachedRouteAddressOne, + }, + RecordType: "A", + SetIdentifier: "", + RecordTTL: 60, + }, + { + DNSName: "default.lb-" + lbHash + ".test.example.com", + Targets: []string{ + "2w705o.lb-" + lbHash + ".test.example.com", + }, + RecordType: "CNAME", + SetIdentifier: "2w705o.lb-" + lbHash + ".test.example.com", + RecordTTL: 60, + ProviderSpecific: v1alpha1.ProviderSpecific{ + { + Name: "weight", + Value: "120", + }, + }, + }, + { + DNSName: "default.lb-" + lbHash + ".test.example.com", + Targets: []string{ + "s07c46.lb-" + lbHash + ".test.example.com", + }, + RecordType: "CNAME", + SetIdentifier: "s07c46.lb-" + lbHash + ".test.example.com", + RecordTTL: 60, + Labels: nil, + ProviderSpecific: v1alpha1.ProviderSpecific{ + { + Name: "weight", + Value: "120", + }, + }, + }, + { + DNSName: "lb-" + lbHash + ".test.example.com", + Targets: []string{ + "default.lb-" + lbHash + ".test.example.com", + }, + RecordType: "CNAME", + SetIdentifier: "default", + RecordTTL: 300, + ProviderSpecific: v1alpha1.ProviderSpecific{ + { + Name: "geo-code", + Value: "*", + }, + }, + }, + { + DNSName: "test.example.com", + Targets: []string{ + "lb-" + lbHash + ".test.example.com", + }, + RecordType: "CNAME", + SetIdentifier: "", + RecordTTL: 300, + }, + } + + err := k8sClient.Get(ctx, client.ObjectKey{Name: gateway.Name, Namespace: gateway.Namespace}, gateway) + Expect(err).NotTo(HaveOccurred()) + Expect(gateway.Spec.Listeners).NotTo(BeNil()) + // add another listener, should result in 4 probes + typedHostname := gatewayv1beta1.Hostname(OtherAttachedRouteName) + otherListener := gatewayv1beta1.Listener{ + Name: gatewayv1beta1.SectionName(OtherAttachedRouteName), + Hostname: &typedHostname, + Port: gatewayv1beta1.PortNumber(80), + Protocol: gatewayv1beta1.HTTPProtocolType, + } + + patch := client.MergeFrom(gateway.DeepCopy()) + gateway.Spec.Listeners = append(gateway.Spec.Listeners, otherListener) + Expect(k8sClient.Patch(ctx, gateway, patch)).To(BeNil()) + + probeList := &v1alpha1.DNSHealthCheckProbeList{} + Eventually(func() error { + Expect(k8sClient.List(ctx, probeList, &client.ListOptions{Namespace: testNamespace})).To(BeNil()) + if len(probeList.Items) != 4 { + return fmt.Errorf("expected %v probes, got %v", 4, len(probeList.Items)) + } + return nil + }, TestTimeoutLong, TestRetryIntervalMedium).Should(BeNil()) + Expect(len(probeList.Items)).To(Equal(4)) + + // + Eventually(func() error { + getProbe := &v1alpha1.DNSHealthCheckProbe{} + if err = k8sClient.Get(ctx, client.ObjectKey{Name: fmt.Sprintf("%s-%s-%s", TestAttachedRouteAddressOne, TestPlacedGatewayName, OtherAttachedRouteName), Namespace: testNamespace}, getProbe); err != nil { + return err + } + patch := client.MergeFrom(getProbe.DeepCopy()) + unhealthy = false + getProbe.Status = v1alpha1.DNSHealthCheckProbeStatus{ + LastCheckedAt: metav1.NewTime(time.Now()), + ConsecutiveFailures: *getProbe.Spec.FailureThreshold + 1, + Healthy: &unhealthy, + } + if err = k8sClient.Status().Patch(ctx, getProbe, patch); err != nil { + return err + } + return nil + }, TestTimeoutLong, TestRetryIntervalMedium).Should(BeNil()) + + // after that verify that in time the endpoints are 5 in the dnsrecord + createdDNSRecord := &v1alpha1.DNSRecord{} + Eventually(func() error { + err := k8sClient.Get(ctx, client.ObjectKey{Name: dnsRecordName, Namespace: testNamespace}, createdDNSRecord) + if err != nil && k8serrors.IsNotFound(err) { + return err + } + return nil + }, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) + Expect(createdDNSRecord.Spec.Endpoints).To(HaveLen(len(expectedEndpoints))) + Expect(createdDNSRecord.Spec.Endpoints).Should(ContainElements(expectedEndpoints)) + Expect(expectedEndpoints).Should(ContainElements(createdDNSRecord.Spec.Endpoints)) + }) + }) + }) }) Context("gateway not placed", func() { @@ -851,31 +1339,22 @@ var _ = Describe("DNSPolicy", Ordered, func() { testGatewayName := "test-not-placed-gateway" BeforeEach(func() { - gateway = testBuildGateway(testGatewayName, gatewayClass.Name, TestAttachedRouteName, testNamespace, "test-dns-policy") + gateway = testBuildGateway(testGatewayName, testutil.DummyCRName, TestAttachedRouteName, testNamespace, "test-dns-policy") dnsPolicy = testBuildDNSPolicyWithHealthCheck("test-dns-policy", testGatewayName, testNamespace, nil) Expect(k8sClient.Create(ctx, gateway)).To(BeNil()) Expect(k8sClient.Create(ctx, dnsPolicy)).To(BeNil()) Eventually(func() error { - if err := k8sClient.Get(ctx, client.ObjectKey{Name: dnsPolicy.Name, Namespace: dnsPolicy.Namespace}, dnsPolicy); err != nil { - return err - } - return nil + return k8sClient.Get(ctx, client.ObjectKey{Name: dnsPolicy.Name, Namespace: dnsPolicy.Namespace}, dnsPolicy) }, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) Eventually(func() error { //gateway exists - if err := k8sClient.Get(ctx, client.ObjectKey{Name: gateway.Name, Namespace: gateway.Namespace}, gateway); err != nil { - return err - } - return nil - }, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) + return k8sClient.Get(ctx, client.ObjectKey{Name: gateway.Name, Namespace: gateway.Namespace}, gateway) + }, TestTimeoutMedium, TestRetryIntervalMedium).ShouldNot(HaveOccurred()) Eventually(func() error { //dns policy exists - if err := k8sClient.Get(ctx, client.ObjectKey{Name: dnsPolicy.Name, Namespace: dnsPolicy.Namespace}, dnsPolicy); err != nil { - return err - } - return nil + return k8sClient.Get(ctx, client.ObjectKey{Name: dnsPolicy.Name, Namespace: dnsPolicy.Namespace}, dnsPolicy) }, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) }) @@ -948,501 +1427,4 @@ var _ = Describe("DNSPolicy", Ordered, func() { }, time.Second*5, time.Second).Should(BeNil()) }) }) - - Context("probes status impact DNS records", func() { - var gateway *gatewayv1beta1.Gateway - var dnsRecordName, lbHash string - var dnsPolicy *v1alpha1.DNSPolicy - var unhealthy bool - - BeforeEach(func() { - gateway = testBuildGateway(TestPlacedGatewayName, gatewayClass.Name, TestAttachedRouteName, testNamespace, "test-dns-policy") - dnsRecordName = fmt.Sprintf("%s-%s", TestPlacedGatewayName, TestAttachedRouteName) - Expect(k8sClient.Create(ctx, gateway)).To(BeNil()) - Eventually(func() error { //gateway exists - if err := k8sClient.Get(ctx, client.ObjectKey{Name: gateway.Name, Namespace: gateway.Namespace}, gateway); err != nil { - return err - } - return nil - }, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) - - threshold := 4 - dnsPolicy = testBuildDNSPolicyWithHealthCheck("test-dns-policy", TestPlacedGatewayName, testNamespace, &threshold) - Expect(k8sClient.Create(ctx, dnsPolicy)).To(BeNil()) - Eventually(func() error { //dns policy exists - if err := k8sClient.Get(ctx, client.ObjectKey{Name: dnsPolicy.Name, Namespace: dnsPolicy.Namespace}, dnsPolicy); err != nil { - return err - } - return nil - }, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) - }) - - AfterEach(func() { - //clean up gateway - gatewayList := &gatewayv1beta1.GatewayList{} - Expect(k8sClient.List(ctx, gatewayList)).To(BeNil()) - for _, gw := range gatewayList.Items { - k8sClient.Delete(ctx, &gw) - } - }) - - It("should create a dns record", func() { - createdDNSRecord := &v1alpha1.DNSRecord{} - Eventually(func() error { // DNS record exists - if err := k8sClient.Get(ctx, client.ObjectKey{Name: dnsRecordName, Namespace: testNamespace}, createdDNSRecord); err != nil { - return err - } - if len(createdDNSRecord.Spec.Endpoints) != 6 { - return fmt.Errorf("expected %v endpoints in DNSRecord, got %v", 6, len(createdDNSRecord.Spec.Endpoints)) - } - return nil - }, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) - }) - It("should have probes that are healthy", func() { - err := k8sClient.Get(ctx, client.ObjectKey{Name: gateway.Name, Namespace: gateway.Namespace}, gateway) - Expect(err).NotTo(HaveOccurred()) - patch := client.MergeFrom(gateway.DeepCopy()) - addressType := mgcgateway.MultiClusterIPAddressType - gateway.Status.Addresses = []gatewayv1beta1.GatewayAddress{ - { - Type: &addressType, - Value: fmt.Sprintf("%s/%s", "kind-mgc-control-plane", TestAttachedRouteAddressOne), - }, - { - Type: &addressType, - Value: fmt.Sprintf("%s/%s", "kind-mgc-control-plane", TestAttachedRouteAddressTwo), - }, - } - Expect(k8sClient.Status().Patch(ctx, gateway, patch)).To(BeNil()) - - probeList := &v1alpha1.DNSHealthCheckProbeList{} - Eventually(func() error { - Expect(k8sClient.List(ctx, probeList, &client.ListOptions{Namespace: testNamespace})).To(BeNil()) - if len(probeList.Items) != 2 { - return fmt.Errorf("expected %v probes, got %v", 2, len(probeList.Items)) - } - return nil - }, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) - Expect(len(probeList.Items)).To(Equal(2)) - }) - - Context("all unhealthy probes", func() { - It("should publish all dns records endpoints", func() { - lbHash = dns.ToBase36hash(fmt.Sprintf("%s-%s", gateway.Name, gateway.Namespace)) - - expectedEndpoints := []*v1alpha1.Endpoint{ - { - DNSName: "2w705o.lb-" + lbHash + ".test.example.com", - Targets: []string{ - TestAttachedRouteAddressTwo, - }, - RecordType: "A", - SetIdentifier: "", - RecordTTL: 60, - }, - { - DNSName: "s07c46.lb-" + lbHash + ".test.example.com", - Targets: []string{ - TestAttachedRouteAddressOne, - }, - RecordType: "A", - SetIdentifier: "", - RecordTTL: 60, - }, - { - DNSName: "default.lb-" + lbHash + ".test.example.com", - Targets: []string{ - "2w705o.lb-" + lbHash + ".test.example.com", - }, - RecordType: "CNAME", - SetIdentifier: "2w705o.lb-" + lbHash + ".test.example.com", - RecordTTL: 60, - ProviderSpecific: v1alpha1.ProviderSpecific{ - { - Name: "weight", - Value: "120", - }, - }, - }, - { - DNSName: "default.lb-" + lbHash + ".test.example.com", - Targets: []string{ - "s07c46.lb-" + lbHash + ".test.example.com", - }, - RecordType: "CNAME", - SetIdentifier: "s07c46.lb-" + lbHash + ".test.example.com", - RecordTTL: 60, - Labels: nil, - ProviderSpecific: v1alpha1.ProviderSpecific{ - { - Name: "weight", - Value: "120", - }, - }, - }, - { - DNSName: "lb-" + lbHash + ".test.example.com", - Targets: []string{ - "default.lb-" + lbHash + ".test.example.com", - }, - RecordType: "CNAME", - SetIdentifier: "default", - RecordTTL: 300, - ProviderSpecific: v1alpha1.ProviderSpecific{ - { - Name: "geo-code", - Value: "*", - }, - }, - }, - { - DNSName: "test.example.com", - Targets: []string{ - "lb-" + lbHash + ".test.example.com", - }, - RecordType: "CNAME", - SetIdentifier: "", - RecordTTL: 300, - }, - } - - err := k8sClient.Get(ctx, client.ObjectKey{Name: gateway.Name, Namespace: gateway.Namespace}, gateway) - Expect(err).NotTo(HaveOccurred()) - patch := client.MergeFrom(gateway.DeepCopy()) - addressType := mgcgateway.MultiClusterIPAddressType - gateway.Status.Addresses = []gatewayv1beta1.GatewayAddress{ - { - Type: &addressType, - Value: fmt.Sprintf("%s/%s", "kind-mgc-control-plane", TestAttachedRouteAddressOne), - }, - { - Type: &addressType, - Value: fmt.Sprintf("%s/%s", "kind-mgc-control-plane", TestAttachedRouteAddressTwo), - }, - } - Expect(k8sClient.Status().Patch(ctx, gateway, patch)).To(BeNil()) - - probeList := &v1alpha1.DNSHealthCheckProbeList{} - Eventually(func() error { - Expect(k8sClient.List(ctx, probeList, &client.ListOptions{Namespace: testNamespace})).To(BeNil()) - if len(probeList.Items) != 2 { - return fmt.Errorf("expected %v probes, got %v", 2, len(probeList.Items)) - } - return nil - }, TestTimeoutLong, TestRetryIntervalMedium).Should(BeNil()) - - for _, probe := range probeList.Items { - Eventually(func() error { - if probe.Name == fmt.Sprintf("%s-%s-%s", TestAttachedRouteAddressTwo, TestPlacedGatewayName, TestAttachedRouteName) || - probe.Name == fmt.Sprintf("%s-%s-%s", TestAttachedRouteAddressOne, TestPlacedGatewayName, TestAttachedRouteName) { - getProbe := &v1alpha1.DNSHealthCheckProbe{} - if err = k8sClient.Get(ctx, client.ObjectKey{Name: probe.Name, Namespace: probe.Namespace}, getProbe); err != nil { - return err - } - patch := client.MergeFrom(getProbe.DeepCopy()) - unhealthy = false - getProbe.Status = v1alpha1.DNSHealthCheckProbeStatus{ - LastCheckedAt: metav1.NewTime(time.Now()), - ConsecutiveFailures: *getProbe.Spec.FailureThreshold + 1, - Healthy: &unhealthy, - } - if err = k8sClient.Status().Patch(ctx, getProbe, patch); err != nil { - return err - } - } - return nil - }, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) - } - createdDNSRecord := &v1alpha1.DNSRecord{} - Eventually(func() error { - err := k8sClient.Get(ctx, client.ObjectKey{Name: dnsRecordName, Namespace: testNamespace}, createdDNSRecord) - if err != nil && k8serrors.IsNotFound(err) { - return err - } - if len(createdDNSRecord.Spec.Endpoints) != len(expectedEndpoints) { - return fmt.Errorf("expected %v endpoints in DNSRecord, got %v", len(expectedEndpoints), len(createdDNSRecord.Spec.Endpoints)) - } - return nil - }, TestTimeoutLong, TestRetryIntervalMedium).Should(BeNil()) - Expect(createdDNSRecord.Spec.Endpoints).To(HaveLen(6)) - Expect(createdDNSRecord.Spec.Endpoints).Should(ContainElements(expectedEndpoints)) - Expect(expectedEndpoints).Should(ContainElements(createdDNSRecord.Spec.Endpoints)) - - }) - }) - Context("some unhealthy probes", func() { - It("should publish expected endpoints", func() { - lbHash = dns.ToBase36hash(fmt.Sprintf("%s-%s", gateway.Name, gateway.Namespace)) - - expectedEndpoints := []*v1alpha1.Endpoint{ - { - DNSName: "2w705o.lb-" + lbHash + ".test.example.com", - Targets: []string{ - TestAttachedRouteAddressTwo, - }, - RecordType: "A", - SetIdentifier: "", - RecordTTL: 60, - }, - { - DNSName: "default.lb-" + lbHash + ".test.example.com", - Targets: []string{ - "2w705o.lb-" + lbHash + ".test.example.com", - }, - RecordType: "CNAME", - SetIdentifier: "2w705o.lb-" + lbHash + ".test.example.com", - RecordTTL: 60, - ProviderSpecific: v1alpha1.ProviderSpecific{ - { - Name: "weight", - Value: "120", - }, - }, - }, - { - DNSName: "lb-" + lbHash + ".test.example.com", - Targets: []string{ - "default.lb-" + lbHash + ".test.example.com", - }, - RecordType: "CNAME", - SetIdentifier: "default", - RecordTTL: 300, - ProviderSpecific: v1alpha1.ProviderSpecific{ - { - Name: "geo-code", - Value: "*", - }, - }, - }, - { - DNSName: "test.example.com", - Targets: []string{ - "lb-" + lbHash + ".test.example.com", - }, - RecordType: "CNAME", - SetIdentifier: "", - RecordTTL: 300, - }, - } - - err := k8sClient.Get(ctx, client.ObjectKey{Name: gateway.Name, Namespace: gateway.Namespace}, gateway) - Expect(err).NotTo(HaveOccurred()) - patch := client.MergeFrom(gateway.DeepCopy()) - addressType := mgcgateway.MultiClusterIPAddressType - gateway.Status.Addresses = []gatewayv1beta1.GatewayAddress{ - { - Type: &addressType, - Value: fmt.Sprintf("%s/%s", "kind-mgc-control-plane", TestAttachedRouteAddressOne), - }, - { - Type: &addressType, - Value: fmt.Sprintf("%s/%s", "kind-mgc-control-plane", TestAttachedRouteAddressTwo), - }, - } - Expect(k8sClient.Status().Patch(ctx, gateway, patch)).To(BeNil()) - - probeList := &v1alpha1.DNSHealthCheckProbeList{} - Eventually(func() error { - Expect(k8sClient.List(ctx, probeList, &client.ListOptions{Namespace: testNamespace})).To(BeNil()) - if len(probeList.Items) != 2 { - return fmt.Errorf("expected %v probes, got %v", 2, len(probeList.Items)) - } - return nil - }, TestTimeoutLong, TestRetryIntervalMedium).Should(BeNil()) - Expect(len(probeList.Items)).To(Equal(2)) - - Eventually(func() error { - getProbe := &v1alpha1.DNSHealthCheckProbe{} - if err = k8sClient.Get(ctx, client.ObjectKey{Name: fmt.Sprintf("%s-%s-%s", TestAttachedRouteAddressOne, TestPlacedGatewayName, TestAttachedRouteName), Namespace: testNamespace}, getProbe); err != nil { - return err - } - patch := client.MergeFrom(getProbe.DeepCopy()) - unhealthy = false - getProbe.Status = v1alpha1.DNSHealthCheckProbeStatus{ - LastCheckedAt: metav1.NewTime(time.Now()), - ConsecutiveFailures: *getProbe.Spec.FailureThreshold + 1, - Healthy: &unhealthy, - } - if err = k8sClient.Status().Patch(ctx, getProbe, patch); err != nil { - return err - } - return nil - }, TestTimeoutLong, TestRetryIntervalMedium).Should(BeNil()) - - // after that verify that in time the endpoints are 5 in the dnsrecord - createdDNSRecord := &v1alpha1.DNSRecord{} - Eventually(func() error { - err := k8sClient.Get(ctx, client.ObjectKey{Name: dnsRecordName, Namespace: testNamespace}, createdDNSRecord) - if err != nil && k8serrors.IsNotFound(err) { - return err - } - if len(createdDNSRecord.Spec.Endpoints) != len(expectedEndpoints) { - return fmt.Errorf("expected %v endpoints in DNSRecord, got %v", len(expectedEndpoints), len(createdDNSRecord.Spec.Endpoints)) - } - return nil - }, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) - Expect(createdDNSRecord.Spec.Endpoints).To(HaveLen(4)) - Expect(createdDNSRecord.Spec.Endpoints).Should(ContainElements(expectedEndpoints)) - Expect(expectedEndpoints).Should(ContainElements(createdDNSRecord.Spec.Endpoints)) - }) - }) - Context("some unhealthy endpoints for other listener", func() { - It("should publish expected endpoints", func() { - lbHash = dns.ToBase36hash(fmt.Sprintf("%s-%s", gateway.Name, gateway.Namespace)) - - expectedEndpoints := []*v1alpha1.Endpoint{ - { - DNSName: "2w705o.lb-" + lbHash + ".test.example.com", - Targets: []string{ - TestAttachedRouteAddressTwo, - }, - RecordType: "A", - SetIdentifier: "", - RecordTTL: 60, - }, - { - DNSName: "s07c46.lb-" + lbHash + ".test.example.com", - Targets: []string{ - TestAttachedRouteAddressOne, - }, - RecordType: "A", - SetIdentifier: "", - RecordTTL: 60, - }, - { - DNSName: "default.lb-" + lbHash + ".test.example.com", - Targets: []string{ - "2w705o.lb-" + lbHash + ".test.example.com", - }, - RecordType: "CNAME", - SetIdentifier: "2w705o.lb-" + lbHash + ".test.example.com", - RecordTTL: 60, - ProviderSpecific: v1alpha1.ProviderSpecific{ - { - Name: "weight", - Value: "120", - }, - }, - }, - { - DNSName: "default.lb-" + lbHash + ".test.example.com", - Targets: []string{ - "s07c46.lb-" + lbHash + ".test.example.com", - }, - RecordType: "CNAME", - SetIdentifier: "s07c46.lb-" + lbHash + ".test.example.com", - RecordTTL: 60, - Labels: nil, - ProviderSpecific: v1alpha1.ProviderSpecific{ - { - Name: "weight", - Value: "120", - }, - }, - }, - { - DNSName: "lb-" + lbHash + ".test.example.com", - Targets: []string{ - "default.lb-" + lbHash + ".test.example.com", - }, - RecordType: "CNAME", - SetIdentifier: "default", - RecordTTL: 300, - ProviderSpecific: v1alpha1.ProviderSpecific{ - { - Name: "geo-code", - Value: "*", - }, - }, - }, - { - DNSName: "test.example.com", - Targets: []string{ - "lb-" + lbHash + ".test.example.com", - }, - RecordType: "CNAME", - SetIdentifier: "", - RecordTTL: 300, - }, - } - - err := k8sClient.Get(ctx, client.ObjectKey{Name: gateway.Name, Namespace: gateway.Namespace}, gateway) - Expect(err).NotTo(HaveOccurred()) - patch := client.MergeFrom(gateway.DeepCopy()) - addressType := mgcgateway.MultiClusterIPAddressType - gateway.Status.Addresses = []gatewayv1beta1.GatewayAddress{ - { - Type: &addressType, - Value: fmt.Sprintf("%s/%s", "kind-mgc-control-plane", TestAttachedRouteAddressOne), - }, - { - Type: &addressType, - Value: fmt.Sprintf("%s/%s", "kind-mgc-control-plane", TestAttachedRouteAddressTwo), - }, - } - Expect(k8sClient.Status().Patch(ctx, gateway, patch)).To(BeNil()) - - err = k8sClient.Get(ctx, client.ObjectKey{Name: gateway.Name, Namespace: gateway.Namespace}, gateway) - Expect(err).NotTo(HaveOccurred()) - Expect(gateway.Spec.Listeners).NotTo(BeNil()) - // add another listener, should result in 4 probes - typedHostname := gatewayv1beta1.Hostname(OtherAttachedRouteName) - otherListener := gatewayv1beta1.Listener{ - Name: gatewayv1beta1.SectionName(OtherAttachedRouteName), - Hostname: &typedHostname, - Port: gatewayv1beta1.PortNumber(80), - Protocol: gatewayv1beta1.HTTPProtocolType, - } - - patch = client.MergeFrom(gateway.DeepCopy()) - gateway.Spec.Listeners = append(gateway.Spec.Listeners, otherListener) - Expect(k8sClient.Patch(ctx, gateway, patch)).To(BeNil()) - - probeList := &v1alpha1.DNSHealthCheckProbeList{} - Eventually(func() error { - Expect(k8sClient.List(ctx, probeList, &client.ListOptions{Namespace: testNamespace})).To(BeNil()) - if len(probeList.Items) != 4 { - return fmt.Errorf("expected %v probes, got %v", 4, len(probeList.Items)) - } - return nil - }, TestTimeoutLong, TestRetryIntervalMedium).Should(BeNil()) - Expect(len(probeList.Items)).To(Equal(4)) - - // - Eventually(func() error { - getProbe := &v1alpha1.DNSHealthCheckProbe{} - if err = k8sClient.Get(ctx, client.ObjectKey{Name: fmt.Sprintf("%s-%s-%s", TestAttachedRouteAddressOne, TestPlacedGatewayName, OtherAttachedRouteName), Namespace: testNamespace}, getProbe); err != nil { - return err - } - patch := client.MergeFrom(getProbe.DeepCopy()) - unhealthy = false - getProbe.Status = v1alpha1.DNSHealthCheckProbeStatus{ - LastCheckedAt: metav1.NewTime(time.Now()), - ConsecutiveFailures: *getProbe.Spec.FailureThreshold + 1, - Healthy: &unhealthy, - } - if err = k8sClient.Status().Patch(ctx, getProbe, patch); err != nil { - return err - } - return nil - }, TestTimeoutLong, TestRetryIntervalMedium).Should(BeNil()) - - // after that verify that in time the endpoints are 5 in the dnsrecord - createdDNSRecord := &v1alpha1.DNSRecord{} - Eventually(func() error { - err := k8sClient.Get(ctx, client.ObjectKey{Name: dnsRecordName, Namespace: testNamespace}, createdDNSRecord) - if err != nil && k8serrors.IsNotFound(err) { - return err - } - if len(createdDNSRecord.Spec.Endpoints) != len(expectedEndpoints) { - return fmt.Errorf("expected %v endpoints in DNSRecord, got %v", len(expectedEndpoints), len(createdDNSRecord.Spec.Endpoints)) - } - return nil - }, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) - Expect(createdDNSRecord.Spec.Endpoints).To(HaveLen(6)) - Expect(createdDNSRecord.Spec.Endpoints).Should(ContainElements(expectedEndpoints)) - Expect(expectedEndpoints).Should(ContainElements(createdDNSRecord.Spec.Endpoints)) - }) - }) - }) }) diff --git a/test/integration/suite_test.go b/test/integration/suite_test.go index c024bba60..69b90ffc8 100644 --- a/test/integration/suite_test.go +++ b/test/integration/suite_test.go @@ -142,7 +142,6 @@ var _ = BeforeSuite(func() { Expect(err).ToNot(HaveOccurred()) plc := placement.NewOCMPlacer(k8sManager.GetClient()) - testPlc := NewTestOCMPlacer() dnsPolicyBaseReconciler := reconcilers.NewBaseReconciler( k8sManager.GetClient(), k8sManager.GetScheme(), k8sManager.GetAPIReader(), @@ -155,7 +154,6 @@ var _ = BeforeSuite(func() { BaseReconciler: dnsPolicyBaseReconciler, }, DNSProvider: providerFactory, - Placer: testPlc, }).SetupWithManager(k8sManager) Expect(err).ToNot(HaveOccurred())