diff --git a/tests/e2e/ambient/ambient_test.go b/tests/e2e/ambient/ambient_test.go index 092dd5f41d..0b98251db1 100644 --- a/tests/e2e/ambient/ambient_test.go +++ b/tests/e2e/ambient/ambient_test.go @@ -129,23 +129,11 @@ spec: When("the Istio CR is created with ambient profile", func() { BeforeAll(func() { - istioYAML := ` -apiVersion: sailoperator.io/v1 -kind: Istio -metadata: - name: default -spec: - values: - pilot: - trustedZtunnelNamespace: ztunnel - profile: ambient - version: %s - namespace: %s` - istioYAML = fmt.Sprintf(istioYAML, version.Name, controlPlaneNamespace) - Log("Istio YAML:", istioYAML) - Expect(k.CreateFromString(istioYAML)). - To(Succeed(), "Istio CR failed to be created") - Success("Istio CR created") + common.CreateIstio(k, version.Name, ` +values: + pilot: + trustedZtunnelNamespace: ztunnel +profile: ambient`) }) It("updates the Istio CR status to Reconciled", func(ctx SpecContext) { diff --git a/tests/e2e/controlplane/control_plane_test.go b/tests/e2e/controlplane/control_plane_test.go index e540a90bb9..284695a26c 100644 --- a/tests/e2e/controlplane/control_plane_test.go +++ b/tests/e2e/controlplane/control_plane_test.go @@ -108,18 +108,7 @@ metadata: When("the IstioCNI CR is created", func() { BeforeAll(func() { - yaml := ` -apiVersion: sailoperator.io/v1 -kind: IstioCNI -metadata: - name: default -spec: - version: %s - namespace: %s` - yaml = fmt.Sprintf(yaml, version.Name, istioCniNamespace) - Log("IstioCNI YAML:", indent(yaml)) - Expect(k.CreateFromString(yaml)).To(Succeed(), "IstioCNI creation failed") - Success("IstioCNI created") + common.CreateIstioCNI(k, version.Name) }) It("deploys the CNI DaemonSet", func(ctx SpecContext) { @@ -158,19 +147,7 @@ spec: When("the Istio CR is created", func() { BeforeAll(func() { - istioYAML := ` -apiVersion: sailoperator.io/v1 -kind: Istio -metadata: - name: default -spec: - version: %s - namespace: %s` - istioYAML = fmt.Sprintf(istioYAML, version.Name, controlPlaneNamespace) - Log("Istio YAML:", indent(istioYAML)) - Expect(k.CreateFromString(istioYAML)). - To(Succeed(), "Istio CR failed to be created") - Success("Istio CR created") + common.CreateIstio(k, version.Name) }) It("updates the Istio CR status to Reconciled", func(ctx SpecContext) { @@ -297,11 +274,6 @@ func ImageFromRegistry(regexp string) types.GomegaMatcher { return HaveField("Image", MatchRegexp(regexp)) } -func indent(str string) string { - indent := strings.Repeat(" ", 2) - return indent + strings.ReplaceAll(str, "\n", "\n"+indent) -} - func getProxyVersion(podName, namespace string) (*semver.Version, error) { output, err := k.WithNamespace(namespace).Exec( podName, diff --git a/tests/e2e/controlplane/control_plane_update_test.go b/tests/e2e/controlplane/control_plane_update_test.go index 67dc4d3348..ae63cb5385 100644 --- a/tests/e2e/controlplane/control_plane_update_test.go +++ b/tests/e2e/controlplane/control_plane_update_test.go @@ -58,19 +58,7 @@ var _ = Describe("Control Plane updates", Label("control-plane", "slow"), Ordere Expect(k.CreateNamespace(controlPlaneNamespace)).To(Succeed(), "Istio namespace failed to be created") Expect(k.CreateNamespace(istioCniNamespace)).To(Succeed(), "IstioCNI namespace failed to be created") - yaml := ` -apiVersion: sailoperator.io/v1 -kind: IstioCNI -metadata: - name: default -spec: - version: %s - namespace: %s` - yaml = fmt.Sprintf(yaml, istioversion.Base, istioCniNamespace) - Log("IstioCNI YAML:", indent(yaml)) - Expect(k.CreateFromString(yaml)).To(Succeed(), "IstioCNI creation failed") - Success("IstioCNI created") - + common.CreateIstioCNI(k, istioversion.Base) Eventually(common.GetObject).WithArguments(ctx, cl, kube.Key(istioCniName), &v1.IstioCNI{}). Should(HaveConditionStatus(v1.IstioCNIConditionReady, metav1.ConditionTrue), "IstioCNI is not Ready; unexpected Condition") Success("IstioCNI is Ready") @@ -78,22 +66,10 @@ spec: When(fmt.Sprintf("the Istio CR is created with RevisionBased updateStrategy for base version %s", istioversion.Base), func() { BeforeAll(func() { - istioYAML := ` -apiVersion: sailoperator.io/v1 -kind: Istio -metadata: - name: default -spec: - version: %s - namespace: %s - updateStrategy: - type: RevisionBased - inactiveRevisionDeletionGracePeriodSeconds: 30` - istioYAML = fmt.Sprintf(istioYAML, istioversion.Base, controlPlaneNamespace) - Log("Istio YAML:", indent(istioYAML)) - Expect(k.CreateFromString(istioYAML)). - To(Succeed(), "Istio CR failed to be created") - Success("Istio CR created") + common.CreateIstio(k, istioversion.Base, ` +updateStrategy: + type: RevisionBased + inactiveRevisionDeletionGracePeriodSeconds: 30`) }) It("deploys istiod and pod is Ready", func(ctx SpecContext) { @@ -114,7 +90,7 @@ spec: targetRef: kind: Istio name: default` - Log("IstioRevisionTag YAML:", indent(IstioRevisionTagYAML)) + Log("IstioRevisionTag YAML:", common.Indent(IstioRevisionTagYAML)) Expect(k.CreateFromString(IstioRevisionTagYAML)). To(Succeed(), "IstioRevisionTag CR failed to be created") Success("IstioRevisionTag CR created") diff --git a/tests/e2e/dualstack/dualstack_test.go b/tests/e2e/dualstack/dualstack_test.go index db9b413b43..02a5fac516 100644 --- a/tests/e2e/dualstack/dualstack_test.go +++ b/tests/e2e/dualstack/dualstack_test.go @@ -84,18 +84,7 @@ var _ = Describe("DualStack configuration ", Label("dualstack"), Ordered, func() When("the IstioCNI CR is created", func() { BeforeAll(func() { - cniYAML := ` -apiVersion: sailoperator.io/v1 -kind: IstioCNI -metadata: - name: default -spec: - version: %s - namespace: %s` - cniYAML = fmt.Sprintf(cniYAML, version.Name, istioCniNamespace) - Log("IstioCNI YAML:", cniYAML) - Expect(k.CreateFromString(cniYAML)).To(Succeed(), "IstioCNI creation failed") - Success("IstioCNI created") + common.CreateIstioCNI(k, version.Name) }) It("deploys the CNI DaemonSet", func(ctx SpecContext) { @@ -111,30 +100,19 @@ spec: When("the Istio CR is created with DualStack configuration", func() { BeforeAll(func() { - istioYAML := ` -apiVersion: sailoperator.io/v1 -kind: Istio -metadata: - name: default -spec: - values: - meshConfig: - defaultConfig: - proxyMetadata: - ISTIO_DUAL_STACK: "true" - pilot: - ipFamilyPolicy: %s - env: + spec := ` +values: + meshConfig: + defaultConfig: + proxyMetadata: ISTIO_DUAL_STACK: "true" - cni: - enabled: true - version: %s - namespace: %s` - istioYAML = fmt.Sprintf(istioYAML, corev1.IPFamilyPolicyRequireDualStack, version.Name, controlPlaneNamespace) - Log("Istio YAML:", istioYAML) - Expect(k.CreateFromString(istioYAML)). - To(Succeed(), "Istio CR failed to be created") - Success("Istio CR created") + pilot: + ipFamilyPolicy: %s + env: + ISTIO_DUAL_STACK: "true" + cni: + enabled: true` + common.CreateIstio(k, version.Name, fmt.Sprintf(spec, corev1.IPFamilyPolicyRequireDualStack)) }) It("updates the Istio CR status to Reconciled", func(ctx SpecContext) { diff --git a/tests/e2e/multicluster/multicluster_externalcontrolplane_test.go b/tests/e2e/multicluster/multicluster_externalcontrolplane_test.go index 7f1e463d76..df25a95e61 100644 --- a/tests/e2e/multicluster/multicluster_externalcontrolplane_test.go +++ b/tests/e2e/multicluster/multicluster_externalcontrolplane_test.go @@ -70,41 +70,11 @@ var _ = Describe("Multicluster deployment models", Label("multicluster", "multic Expect(k1.CreateNamespace(controlPlaneNamespace)).To(Succeed(), "Namespace failed to be created") Expect(k1.CreateNamespace(istioCniNamespace)).To(Succeed(), "Istio CNI namespace failed to be created") - multiclusterCNIYAML := ` -apiVersion: sailoperator.io/v1 -kind: IstioCNI -metadata: - name: {{ .Name }} -spec: - version: {{ .Version }} - namespace: {{ .Namespace }}` - multiclusterCNIYAML = genTemplate(multiclusterCNIYAML, map[string]any{ - "Name": istioCniName, - "Namespace": istioCniNamespace, - "Version": v.Name, - }) - Log("Istio CNI CR Cluster #1: ", multiclusterCNIYAML) - Expect(k1.CreateFromString(multiclusterCNIYAML)).To(Succeed(), "Istio CNI Resource creation failed on Cluster #1") - - multiclusterYAML := ` -apiVersion: sailoperator.io/v1 -kind: Istio -metadata: - name: {{ .Name }} -spec: - version: {{ .Version }} - namespace: {{ .Namespace }} - values: - global: - network: {{ .Network }}` - multiclusterYAML = genTemplate(multiclusterYAML, map[string]any{ - "Name": istioName, - "Namespace": controlPlaneNamespace, - "Network": "network1", - "Version": v.Name, - }) - Log("Istio CR Cluster #1: ", multiclusterYAML) - Expect(k1.CreateFromString(multiclusterYAML)).To(Succeed(), "Istio Resource creation failed on Cluster #1") + common.CreateIstioCNI(k1, v.Name) + common.CreateIstio(k1, v.Name, ` +values: + global: + network: network1`) }) It("updates the default Istio CR status to Ready", func(ctx SpecContext) { @@ -143,21 +113,8 @@ spec: Expect(clRemote.Get(ctx, client.ObjectKey{Name: externalControlPlaneNamespace}, &corev1.Namespace{})).To(Succeed()) Expect(k2.CreateNamespace(istioCniNamespace)).To(Succeed(), "Istio CNI namespace failed to be created") - remoteIstioCNIYAML := ` -apiVersion: sailoperator.io/v1 -kind: IstioCNI -metadata: - name: {{ .Name }} -spec: - version: {{ .Version }} - namespace: {{ .Namespace }}` - remoteIstioCNIYAML = genTemplate(remoteIstioCNIYAML, map[string]any{ - "Name": istioCniName, - "Namespace": istioCniNamespace, - "Version": v.Name, - }) - Log("Istio CNI CR Cluster #2: ", remoteIstioCNIYAML) - Expect(k2.CreateFromString(remoteIstioCNIYAML)).To(Succeed(), "Istio CNI Resource creation failed on Cluster #2") + common.CreateIstioCNI(k2, v.Name) + Log("Istio CNI created on Cluster #2") remotePilotAddress := common.GetSVCLoadBalancerAddress(ctx, clPrimary, controlPlaneNamespace, "istio-ingressgateway") remotePilotIP, err := common.ResolveHostDomainToIP(remotePilotAddress) diff --git a/tests/e2e/multicluster/multicluster_multiprimary_test.go b/tests/e2e/multicluster/multicluster_multiprimary_test.go index ff00ac3ff8..f98b42efe5 100644 --- a/tests/e2e/multicluster/multicluster_multiprimary_test.go +++ b/tests/e2e/multicluster/multicluster_multiprimary_test.go @@ -76,43 +76,18 @@ var _ = Describe("Multicluster deployment models", Label("multicluster", "multic return err }).ShouldNot(HaveOccurred(), "Secret is not created on Cluster #1") - multiclusterIstioCNIYAML := ` -apiVersion: sailoperator.io/v1 -kind: IstioCNI -metadata: - name: default -spec: - version: %s - namespace: %s` - multiclusterIstioCNICluster1YAML := fmt.Sprintf(multiclusterIstioCNIYAML, version.Name, istioCniNamespace) - Log("Istio CNI CR Cluster #1: ", multiclusterIstioCNICluster1YAML) - Expect(k1.CreateFromString(multiclusterIstioCNICluster1YAML)).To(Succeed(), "Istio CNI Resource creation failed on Cluster #1") - - multiclusterIstioCNICluster2YAML := fmt.Sprintf(multiclusterIstioCNIYAML, version.Name, istioCniNamespace) - Log("Istio CNI CR Cluster #2: ", multiclusterIstioCNICluster2YAML) - Expect(k2.CreateFromString(multiclusterIstioCNICluster2YAML)).To(Succeed(), "Istio CNI Resource creation failed on Cluster #2") - - multiclusterIstioYAML := ` -apiVersion: sailoperator.io/v1 -kind: Istio -metadata: - name: default -spec: - version: %s - namespace: %s - values: - global: - meshID: %s - multiCluster: - clusterName: %s - network: %s` - multiclusterIstioCluster1YAML := fmt.Sprintf(multiclusterIstioYAML, version.Name, controlPlaneNamespace, "mesh1", "cluster1", "network1") - Log("Istio CR Cluster #1: ", multiclusterIstioCluster1YAML) - Expect(k1.CreateFromString(multiclusterIstioCluster1YAML)).To(Succeed(), "Istio Resource creation failed on Cluster #1") - - multiclusterIstioCluster2YAML := fmt.Sprintf(multiclusterIstioYAML, version.Name, controlPlaneNamespace, "mesh1", "cluster2", "network2") - Log("Istio CR Cluster #2: ", multiclusterIstioCluster2YAML) - Expect(k2.CreateFromString(multiclusterIstioCluster2YAML)).To(Succeed(), "Istio Resource creation failed on Cluster #2") + common.CreateIstioCNI(k1, version.Name) + common.CreateIstioCNI(k2, version.Name) + + spec := ` +values: + global: + meshID: mesh1 + multiCluster: + clusterName: %s + network: %s` + common.CreateIstio(k1, version.Name, fmt.Sprintf(spec, "cluster1", "network1")) + common.CreateIstio(k2, version.Name, fmt.Sprintf(spec, "cluster2", "network2")) }) It("updates both Istio CR status to Ready", func(ctx SpecContext) { diff --git a/tests/e2e/multicluster/multicluster_primaryremote_test.go b/tests/e2e/multicluster/multicluster_primaryremote_test.go index 773e332ea2..21c59e8b5a 100644 --- a/tests/e2e/multicluster/multicluster_primaryremote_test.go +++ b/tests/e2e/multicluster/multicluster_primaryremote_test.go @@ -85,39 +85,19 @@ var _ = Describe("Multicluster deployment models", Label("multicluster", "multic return err }).ShouldNot(HaveOccurred(), "Secret is not created on Primary Cluster") - PrimaryIstioCNIYAML := ` -apiVersion: sailoperator.io/v1 -kind: IstioCNI -metadata: - name: default -spec: - version: %s - namespace: %s` - - multiclusterPrimaryIstioCNIYAML := fmt.Sprintf(PrimaryIstioCNIYAML, v.Name, istioCniNamespace) - Log("IstioCNI CR Primary: ", multiclusterPrimaryIstioCNIYAML) - Expect(k1.CreateFromString(multiclusterPrimaryIstioCNIYAML)).To(Succeed(), "Istio CNI Resource creation failed on Primary Cluster") - - PrimaryIstioYAML := ` -apiVersion: sailoperator.io/v1 -kind: Istio -metadata: - name: default -spec: - version: %s - namespace: %s - values: - pilot: - env: - EXTERNAL_ISTIOD: "true" - global: - meshID: %s - multiCluster: - clusterName: %s - network: %s` - multiclusterPrimaryIstioYAML := fmt.Sprintf(PrimaryIstioYAML, v.Name, controlPlaneNamespace, "mesh1", "cluster1", "network1") - Log("Istio CR Primary: ", multiclusterPrimaryIstioYAML) - Expect(k1.CreateFromString(multiclusterPrimaryIstioYAML)).To(Succeed(), "Istio Resource creation failed on Primary Cluster") + common.CreateIstioCNI(k1, v.Name) + + spec := ` +values: + pilot: + env: + EXTERNAL_ISTIOD: "true" + global: + meshID: mesh1 + multiCluster: + clusterName: cluster1 + network: network1` + common.CreateIstio(k1, v.Name, spec) }) It("updates Istio CR on Primary cluster status to Ready", func(ctx SpecContext) { @@ -174,34 +154,15 @@ spec: When("Istio and IstioCNI are created in Remote cluster", func() { BeforeAll(func(ctx SpecContext) { - istioCNIYAMLTemplate := ` -apiVersion: sailoperator.io/v1 -kind: IstioCNI -metadata: - name: default -spec: - version: %s - namespace: %s` - - istioCNIYAML := fmt.Sprintf(istioCNIYAMLTemplate, v.Name, istioCniNamespace) - Log("IstioCNI CR: ", istioCNIYAML) - By("Creating IstioCNI CR on Remote Cluster") - Expect(k2.CreateFromString(istioCNIYAML)).To(Succeed(), "IstioCNI Resource creation failed on Remote Cluster") - - istioYAMLTemplate := ` -apiVersion: sailoperator.io/v1 -kind: Istio -metadata: - name: default -spec: - version: %s - namespace: %s - values: - profile: remote - istiodRemote: - injectionPath: /inject/cluster/remote/net/network2 - global: - remotePilotAddress: %s` + common.CreateIstioCNI(k2, v.Name) + + spec := ` +values: + profile: remote + istiodRemote: + injectionPath: /inject/cluster/remote/net/network2 + global: + remotePilotAddress: %s` remotePilotAddress := common.GetSVCLoadBalancerAddress(ctx, clPrimary, controlPlaneNamespace, "istio-eastwestgateway") Expect(remotePilotAddress).NotTo(BeEmpty(), "Remote Pilot Address is empty") @@ -209,10 +170,7 @@ spec: remotePilotIP, err := common.ResolveHostDomainToIP(remotePilotAddress) Expect(remotePilotIP).NotTo(BeEmpty(), "Remote Pilot IP is empty") Expect(err).NotTo(HaveOccurred(), "Error getting Remote Pilot IP") - istioYAML := fmt.Sprintf(istioYAMLTemplate, v.Name, controlPlaneNamespace, remotePilotIP) - Log("Istio CR: ", istioYAML) - By("Creating Istio CR on Remote Cluster") - Expect(k2.CreateFromString(istioYAML)).To(Succeed(), "Istio Resource creation failed on Remote Cluster") + common.CreateIstio(k2, v.Name, fmt.Sprintf(spec, remotePilotIP)) // Set the controlplane cluster and network for Remote namespace By("Patching the istio-system namespace on Remote Cluster") diff --git a/tests/e2e/multicluster/multicluster_suite_test.go b/tests/e2e/multicluster/multicluster_suite_test.go index 5cf3c5540a..d9476a2560 100644 --- a/tests/e2e/multicluster/multicluster_suite_test.go +++ b/tests/e2e/multicluster/multicluster_suite_test.go @@ -113,8 +113,8 @@ func setup(t *testing.T) { exposeIstiodYAML = fmt.Sprintf("%s/docs/deployment-models/resources/expose-istiod.yaml", baseRepoDir) // Initialize kubectl utilities, one for each cluster - k1 = kubectl.New().WithKubeconfig(kubeconfig) - k2 = kubectl.New().WithKubeconfig(kubeconfig2) + k1 = kubectl.New().WithKubeconfig(kubeconfig).WithClusterName("primary") + k2 = kubectl.New().WithKubeconfig(kubeconfig2).WithClusterName("remote") clr1 = cleaner.New(clPrimary, "cluster=primary") clr2 = cleaner.New(clRemote, "cluster=remote") } diff --git a/tests/e2e/multicontrolplane/multi_control_plane_test.go b/tests/e2e/multicontrolplane/multi_control_plane_test.go index ad788e7758..162c68d3ae 100644 --- a/tests/e2e/multicontrolplane/multi_control_plane_test.go +++ b/tests/e2e/multicontrolplane/multi_control_plane_test.go @@ -66,17 +66,7 @@ var _ = Describe("Multi control plane deployment model", Label("smoke", "multico }) It("Installs IstioCNI", func(ctx SpecContext) { - yaml := ` -apiVersion: sailoperator.io/v1 -kind: IstioCNI -metadata: - name: default -spec: - version: %s - namespace: %s` - yaml = fmt.Sprintf(yaml, version, istioCniNamespace) - Expect(k.CreateFromString(yaml)).To(Succeed(), "failed to create IstioCNI") - Success("IstioCNI created") + common.CreateIstioCNI(k, version) Eventually(common.GetObject).WithArguments(ctx, cl, kube.Key(istioCniName), &v1.IstioCNI{}). Should(HaveConditionStatus(v1.IstioCNIConditionReady, metav1.ConditionTrue), "IstioCNI is not Ready; unexpected Condition") diff --git a/tests/e2e/util/common/e2e_utils.go b/tests/e2e/util/common/e2e_utils.go index 9649ab8ffa..0e6faf85f0 100644 --- a/tests/e2e/util/common/e2e_utils.go +++ b/tests/e2e/util/common/e2e_utils.go @@ -30,6 +30,7 @@ import ( "github.com/istio-ecosystem/sail-operator/pkg/env" "github.com/istio-ecosystem/sail-operator/pkg/istioversion" "github.com/istio-ecosystem/sail-operator/pkg/test/project" + . "github.com/istio-ecosystem/sail-operator/pkg/test/util/ginkgo" "github.com/istio-ecosystem/sail-operator/tests/e2e/util/helm" "github.com/istio-ecosystem/sail-operator/tests/e2e/util/istioctl" "github.com/istio-ecosystem/sail-operator/tests/e2e/util/kubectl" @@ -266,11 +267,10 @@ func logCertsDebugInfo(k kubectl.Kubectl) { func logDebugElement(caption string, info string, err error) { GinkgoWriter.Println("\n" + caption + ":") - indent := " " if err != nil { - GinkgoWriter.Println(indent + err.Error()) + GinkgoWriter.Println(Indent(err.Error())) } else { - GinkgoWriter.Println(indent + strings.ReplaceAll(strings.TrimSpace(info), "\n", "\n"+indent)) + GinkgoWriter.Println(Indent(strings.TrimSpace(info))) } } @@ -415,3 +415,54 @@ func ResolveHostDomainToIP(hostDomain string) (string, error) { return "", fmt.Errorf("failed to resolve hostname %s after %d retries: %w", hostDomain, maxRetries, lastErr) } + +// CreateIstio custom resource using a given `kubectl` client and with the specified version. +// An optional spec list can be given to inject into the CR's spec. +func CreateIstio(k kubectl.Kubectl, version string, specs ...string) { + yaml := ` +apiVersion: sailoperator.io/v1 +kind: Istio +metadata: + name: %s +spec: + version: %s + namespace: %s` + yaml = fmt.Sprintf(yaml, istioName, version, controlPlaneNamespace) + for _, spec := range specs { + yaml += Indent(spec) + } + + Log("Istio YAML:", Indent(yaml)) + Expect(k.CreateFromString(yaml)). + To(Succeed(), withClusterName("Istio CR failed to be created", k)) + Success(withClusterName("Istio CR created", k)) +} + +// CreateIstioCNI custom resource using a given `kubectl` client and with the specified version. +func CreateIstioCNI(k kubectl.Kubectl, version string) { + yaml := ` +apiVersion: sailoperator.io/v1 +kind: IstioCNI +metadata: + name: %s +spec: + version: %s + namespace: %s` + yaml = fmt.Sprintf(yaml, istioCniName, version, istioCniNamespace) + Log("IstioCNI YAML:", Indent(yaml)) + Expect(k.CreateFromString(yaml)).To(Succeed(), withClusterName("IstioCNI creation failed", k)) + Success(withClusterName("IstioCNI created", k)) +} + +func Indent(str string) string { + indent := " " + return indent + strings.ReplaceAll(str, "\n", "\n"+indent) +} + +func withClusterName(m string, k kubectl.Kubectl) string { + if k.ClusterName == "" { + return m + } + + return m + " on " + k.ClusterName +}