Skip to content

Commit

Permalink
test: update kubecost tests
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
takirala committed Jan 4, 2025
1 parent 42c3719 commit 235d816
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
51 changes: 49 additions & 2 deletions apptests/appscenarios/kubecost.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@ 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"
)

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
}

Expand All @@ -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
}
Expand All @@ -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{
Expand Down
13 changes: 9 additions & 4 deletions apptests/appscenarios/kubecost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
})

Expand Down Expand Up @@ -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,
},
}
Expand Down Expand Up @@ -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,
},
}
Expand Down
7 changes: 4 additions & 3 deletions apptests/constants/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 235d816

Please sign in to comment.