From 235d81648572aa5fec5710ea517d775c4e851821 Mon Sep 17 00:00:00 2001 From: Tarun Gupta Akirala Date: Sat, 4 Jan 2025 00:13:47 -0800 Subject: [PATCH] test: update kubecost tests kubecost is moving primary-secondary architecture and we will migrating old kubecost instances to new centralized-kubecost instance thus by deleting the old kubecost instances. Signed-off-by: Tarun Gupta Akirala --- apptests/appscenarios/kubecost.go | 51 +++++++++++++++++++++++++- apptests/appscenarios/kubecost_test.go | 13 +++++-- apptests/constants/apps.go | 7 ++-- 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/apptests/appscenarios/kubecost.go b/apptests/appscenarios/kubecost.go index bb7344939e..21b808e1f3 100644 --- a/apptests/appscenarios/kubecost.go +++ b/apptests/appscenarios/kubecost.go @@ -4,6 +4,9 @@ import ( "context" "path/filepath" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "github.com/mesosphere/kommander-applications/apptests/constants" "github.com/mesosphere/kommander-applications/apptests/environment" ) @@ -11,6 +14,12 @@ import ( type kubeCost struct{} func (r kubeCost) Name() string { + return constants.CentralizedKubecost +} + +// OldName returns the name of the app pre upgrade. +// In 2.15.x we can drop this helper function and just use the Name() function again. +func (r kubeCost) OldName() string { return constants.KubeCost } @@ -31,12 +40,12 @@ func (r kubeCost) Install(ctx context.Context, env *environment.Env) error { } func (r kubeCost) InstallPreviousVersion(ctx context.Context, env *environment.Env) error { - appPath, err := getkAppsUpgradePath(r.Name()) + appPath, err := getkAppsUpgradePath(r.OldName()) if err != nil { return err } - err = r.install(ctx, env, appPath) + err = r.installOldKubecost(ctx, env, appPath) if err != nil { return err } @@ -58,7 +67,45 @@ func (r kubeCost) Upgrade(ctx context.Context, env *environment.Env) error { return err } +// install installs the centralized-kubecost app func (r kubeCost) install(ctx context.Context, env *environment.Env, appPath string) error { + _, err := env.K8sClient.Clientset().CoreV1().Namespaces().Create(ctx, &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{Name: "kubecost"}, + }, metav1.CreateOptions{}) + if err != nil { + return err + } + // apply defaults configmaps first + defaultKustomization := filepath.Join(appPath, "/defaults") + err = env.ApplyKustomizations(ctx, defaultKustomization, map[string]string{ + "releaseNamespace": kommanderNamespace, + }) + if err != nil { + return err + } + + // apply the kustomization for the prereqs + prereqs := filepath.Join(appPath, "/pre-install") + err = env.ApplyKustomizations(ctx, prereqs, map[string]string{ + "releaseNamespace": kommanderNamespace, + }) + if err != nil { + return err + } + + // apply the kustomization for the helmrelease + releasePath := filepath.Join(appPath, "/release") + err = env.ApplyKustomizations(ctx, releasePath, map[string]string{ + "releaseNamespace": kommanderNamespace, + }) + if err != nil { + return err + } + + return err +} + +func (r kubeCost) installOldKubecost(ctx context.Context, env *environment.Env, appPath string) error { // apply defaults configmaps first defaultKustomization := filepath.Join(appPath, "/defaults") err := env.ApplyKustomizations(ctx, defaultKustomization, map[string]string{ diff --git a/apptests/appscenarios/kubecost_test.go b/apptests/appscenarios/kubecost_test.go index cdca182864..af90148968 100644 --- a/apptests/appscenarios/kubecost_test.go +++ b/apptests/appscenarios/kubecost_test.go @@ -91,11 +91,16 @@ var _ = Describe("Kubecost Tests", Label("kubecost"), func() { deploymentList = &appsv1.DeploymentList{} err = k8sClient.List(ctx, deploymentList, listOptions) Expect(err).To(BeNil()) - Expect(deploymentList.Items).To(HaveLen(4)) + Expect(deploymentList.Items).To(HaveLen(4), "Expected 4 deployments to be created - cost-analyzer, grafana, prometheus server and prometheus alertmanager") Expect(err).To(BeNil()) for _, deployment := range deploymentList.Items { - Expect(deployment.Spec.Template.Spec.PriorityClassName).To(Equal(dkpHighPriority)) + Expect(deployment.Spec.Template.Spec.PriorityClassName).To( + Equal(dkpHighPriority), + "Deployment %q had an unexpected PriorityClass %q", + deployment.Name, + deployment.Spec.Template.Spec.PriorityClassName, + ) } }) @@ -145,7 +150,7 @@ var _ = Describe("Kubecost Tests", Label("kubecost"), func() { APIVersion: fluxhelmv2beta2.GroupVersion.Version, }, ObjectMeta: metav1.ObjectMeta{ - Name: kc.Name(), + Name: kc.OldName(), Namespace: kommanderNamespace, }, } @@ -204,7 +209,7 @@ var _ = Describe("Kubecost Tests", Label("kubecost"), func() { APIVersion: fluxhelmv2beta2.GroupVersion.Version, }, ObjectMeta: metav1.ObjectMeta{ - Name: kc.Name(), + Name: kc.OldName(), Namespace: kommanderNamespace, }, } diff --git a/apptests/constants/apps.go b/apptests/constants/apps.go index d965a4eef0..e327cc95bf 100644 --- a/apptests/constants/apps.go +++ b/apptests/constants/apps.go @@ -3,9 +3,10 @@ package constants const ( CertManager = "cert-manager" Karma = "karma" - // KubeCost runs only on the managed clusters (in a lightweight agent mode that depends on the centralized kubecost and a valid object storage configuration). - // Centralized kubecost runs only on the management cluster. - KubeCost = "centralized-kubecost" + // CentralizedKubecost runs only on the management cluster. + CentralizedKubecost = "centralized-kubecost" + // KubeCost runs only on the managed clusters since 2.14.x (in a lightweight agent mode that depends on the centralized kubecost and a valid object storage configuration). + KubeCost = "kubecost" Reloader = "reloader" Traefik = "traefik" KarmaTraefik = "karma-traefik"