github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/update/dnsac/dnsac_test.go (about)

     1  // Copyright (c) 2022, 2023, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package dnsac
     5  
     6  import (
     7  	"fmt"
     8  	"reflect"
     9  	"strings"
    10  	"time"
    11  
    12  	"github.com/verrazzano/verrazzano/tests/e2e/pkg/update"
    13  
    14  	. "github.com/onsi/ginkgo/v2"
    15  	"github.com/onsi/gomega"
    16  	vzapi "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1alpha1"
    17  	"github.com/verrazzano/verrazzano/tests/e2e/multicluster"
    18  	"github.com/verrazzano/verrazzano/tests/e2e/pkg"
    19  	"github.com/verrazzano/verrazzano/tests/e2e/pkg/test/framework"
    20  	corev1 "k8s.io/api/core/v1"
    21  )
    22  
    23  var (
    24  	t               = framework.NewTestFramework("update dns")
    25  	adminCluster    *multicluster.Cluster
    26  	managedClusters []*multicluster.Cluster
    27  	shortWait       = 5 * time.Minute
    28  	longWait        = 10 * time.Minute
    29  	pollingInterval = 5 * time.Second
    30  	fluentdName     = "fluentd"
    31  	adminFluentd    *vzapi.FluentdComponent
    32  )
    33  
    34  type DNSModifier struct {
    35  	DNS *vzapi.DNSComponent
    36  }
    37  
    38  func (m *DNSModifier) ModifyCR(cr *vzapi.Verrazzano) {
    39  	cr.Spec.Components.DNS = m.DNS
    40  }
    41  
    42  var beforeSuite = t.BeforeSuiteFunc(func() {
    43  	cr := update.GetCR()
    44  	adminFluentd = cr.Spec.Components.Fluentd
    45  	adminCluster = multicluster.AdminCluster()
    46  	managedClusters = multicluster.ManagedClusters()
    47  	verifyRegistration()
    48  })
    49  
    50  var _ = BeforeSuite(beforeSuite)
    51  
    52  var _ = t.Describe("Update admin-cluster dns", Serial, Ordered, Label("f:platform-lcm.update"), func() {
    53  	t.Describe("multicluster dns verify", Label("f:platform-lcm.multicluster-verify"), func() {
    54  		t.It("managed-cluster dns wildcard sslip.io config", func() {
    55  			if systemOpenSearch() {
    56  				oldEsIng := updateAdminClusterDNS()
    57  				newEsIng := verifyOpenSearchIngress(oldEsIng)
    58  				verifyManagedClusterFluentd(newEsIng)
    59  			} else {
    60  				pkg.Log(pkg.Info, fmt.Sprintf(
    61  					"Skip %v dns update test as custom log collecter cannot be used to assert ednpoint URL update", adminCluster.Name))
    62  			}
    63  		})
    64  	})
    65  	t.Describe("multicluster dns verify", Label("f:platform-lcm.multicluster-verify"), func() {
    66  		t.It("managed-cluster dns default nip.io config", func() {
    67  			if systemOpenSearch() {
    68  				oldEsIng := updateAdminClusterDNS()
    69  				newEsIng := verifyOpenSearchIngress(oldEsIng)
    70  				verifyManagedClusterFluentd(newEsIng)
    71  			} else {
    72  				pkg.Log(pkg.Info, fmt.Sprintf(
    73  					"Skip %v dns update test as custom log collecter cannot be used to assert ednpoint URL update", adminCluster.Name))
    74  			}
    75  		})
    76  	})
    77  })
    78  
    79  // updateAdminClusterDNS switch dns config to sslip.io or nip.io
    80  func updateAdminClusterDNS() string {
    81  	adminVZ := adminCluster.GetCR(true)
    82  	var oldDNS = adminVZ.Spec.Components.DNS
    83  	var newDNS *vzapi.DNSComponent
    84  	var domainOld, domainNew = pkg.NipDomain, pkg.SslipDomain
    85  	oldEsIng := pkg.GetSystemOpenSearchIngressURL(adminCluster.KubeConfigPath)
    86  	if pkg.IsDefaultDNS(oldDNS) {
    87  		newDNS = &vzapi.DNSComponent{Wildcard: &vzapi.Wildcard{Domain: pkg.SslipDomain}}
    88  	} else {
    89  		domainOld, domainNew = pkg.SslipDomain, pkg.NipDomain
    90  	}
    91  	m := &DNSModifier{DNS: newDNS}
    92  	gomega.Expect(strings.Contains(oldEsIng, domainOld)).Should(gomega.BeTrue())
    93  	gomega.Expect(strings.Contains(oldEsIng, domainNew)).Should(gomega.BeFalse())
    94  	update.RetryUpdate(m, adminCluster.KubeConfigPath, false, pollingInterval, shortWait)
    95  	return oldEsIng
    96  }
    97  
    98  func systemOpenSearch() bool {
    99  	return !pkg.UseExternalOpensearch() &&
   100  		(adminFluentd == nil || reflect.DeepEqual(*adminFluentd, vzapi.FluentdComponent{}))
   101  }
   102  
   103  func verifyOpenSearchIngress(oldEsIng string) string {
   104  	start := time.Now()
   105  	gomega.Eventually(func() bool {
   106  		newEsIng := pkg.GetSystemOpenSearchIngressURL(adminCluster.KubeConfigPath)
   107  		return newEsIng != oldEsIng
   108  	}, longWait, pollingInterval).Should(gomega.BeTrue(), fmt.Sprintf("admin-cluster %s is not updated for %v", oldEsIng, time.Since(start)))
   109  	return pkg.GetSystemOpenSearchIngressURL(adminCluster.KubeConfigPath)
   110  }
   111  
   112  func verifyManagedClusterFluentd(newEsIng string) {
   113  	start := time.Now()
   114  	for _, managedCluster := range managedClusters {
   115  		gomega.Eventually(func() bool {
   116  			fp := managedCluster.FindFluentdPod()
   117  			if fp == nil || len(fp.Spec.Containers) == 0 {
   118  				return false
   119  			}
   120  			esURL := findEsURL(fp)
   121  			pkg.Log(pkg.Info, fmt.Sprintf("Cluster %v Fluentd OpenSearch URL updated: %v from %v to %v for %v \n", managedCluster.Name, strings.Contains(esURL, newEsIng), esURL, newEsIng, time.Since(start)))
   122  			return strings.Contains(esURL, newEsIng)
   123  		}, longWait, pollingInterval).Should(gomega.BeTrue(), fmt.Sprintf("%s Fluentd is not updated for %v", managedCluster.Name, time.Since(start)))
   124  	}
   125  }
   126  
   127  func findEsURL(fp *corev1.Pod) string {
   128  	for _, c := range fp.Spec.Containers {
   129  		if c.Name == fluentdName {
   130  			for _, env := range c.Env {
   131  				if env.Name == "ELASTICSEARCH_URL" {
   132  					return env.Value
   133  				}
   134  			}
   135  		}
   136  	}
   137  	return ""
   138  }
   139  
   140  func verifyRegistration() {
   141  	for _, managedCluster := range managedClusters {
   142  		reg, _ := adminCluster.GetRegistration(managedCluster.Name)
   143  		if reg == nil {
   144  			adminCluster.Register(managedCluster)
   145  			gomega.Eventually(func() bool {
   146  				reg, err := adminCluster.GetRegistration(managedCluster.Name)
   147  				return reg != nil && err == nil
   148  			}, longWait, pollingInterval).Should(gomega.BeTrue(), fmt.Sprintf("%s is not registered", managedCluster.Name))
   149  		}
   150  	}
   151  }