Skip to content

Commit

Permalink
Adds test cases for control plane pod requests
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSalisbury committed Jun 14, 2024
1 parent 30351f2 commit 6c826a9
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ func Run(cfg *TestConfig) {
runHelloWorld(cfg.ExternalDnsSupported)
runScale(cfg.AutoScalingSupported)
runStorage()
runRequests()
}
81 changes: 81 additions & 0 deletions internal/common/requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package common

import (
"context"
"time"

"github.com/giantswarm/cluster-test-suites/internal/state"
"github.com/giantswarm/clustertest/pkg/client"
"github.com/giantswarm/clustertest/pkg/wait"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
cr "sigs.k8s.io/controller-runtime/pkg/client"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

const (
MaximumCPURequestUtilization float64 = 0.8
MaximumMemoryRequestUtilization float64 = 0.8
)

func runRequests() {
Context("requests", func() {
var wcClient *client.Client

BeforeEach(func() {
var err error

wcClient, err = state.GetFramework().WC(state.GetCluster().Name)
if err != nil {
Fail(err.Error())
}
})

It("has spare CPU request capacity on the control plane nodes", func() {
Eventually(checkControlPlaneNodesHaveSpareRequestCapacity(wcClient, corev1.ResourceCPU, MaximumCPURequestUtilization)).
WithTimeout(15 * time.Minute).
WithPolling(wait.DefaultInterval).
Should(BeTrue())
})

It("has spare memory request capacity on the control plane nodes", func() {
Eventually(checkControlPlaneNodesHaveSpareRequestCapacity(wcClient, corev1.ResourceMemory, MaximumMemoryRequestUtilization)).
WithTimeout(15 * time.Minute).
WithPolling(wait.DefaultInterval).
Should(BeTrue())
})
})
}

func checkControlPlaneNodesHaveSpareRequestCapacity(wcClient *client.Client, resourceName corev1.ResourceName, utilization float64) wait.WaitCondition {
return func() (bool, error) {
totalRequested := resource.NewQuantity(0, resource.DecimalSI)
totalAllocatable := resource.NewQuantity(0, resource.DecimalSI)

nodes := &corev1.NodeList{}
err := wcClient.List(context.Background(), nodes, &cr.MatchingLabels{"node-role.kubernetes.io/control-plane": ""})
if err != nil {
return false, err
}

for _, node := range nodes.Items {
pods := &corev1.PodList{}
err := wcClient.List(context.Background(), pods, &cr.MatchingFields{"spec.nodeName": node.Name})
if err != nil {
return false, err
}

for _, pod := range pods.Items {
for _, container := range pod.Spec.Containers {
totalRequested.Add(container.Resources.Requests[resourceName])
}
}

totalAllocatable.Add(node.Status.Allocatable[resourceName])
}

return totalRequested.AsApproximateFloat64() < totalAllocatable.AsApproximateFloat64()*utilization, nil
}
}

0 comments on commit 6c826a9

Please sign in to comment.