Skip to content

Commit

Permalink
add tests for metrics proxy - node and pod metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Ishan Khare <[email protected]>
  • Loading branch information
ishankhare07 committed Nov 9, 2023
1 parent eecb935 commit 9fcf6d9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/controllers/resources/pods/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
podtranslate "github.com/loft-sh/vcluster/pkg/controllers/resources/pods/translate"
synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
generictesting "github.com/loft-sh/vcluster/pkg/controllers/syncer/testing"
"github.com/loft-sh/vcluster/pkg/setup/options"
"github.com/loft-sh/vcluster/pkg/specialservices"
"github.com/loft-sh/vcluster/pkg/util/maps"
"github.com/loft-sh/vcluster/pkg/util/translate"
"gotest.tools/assert"
Expand All @@ -21,6 +23,8 @@ import (
func TestSync(t *testing.T) {
translate.Default = translate.NewSingleNamespaceTranslator(generictesting.DefaultTestTargetNamespace)

specialservices.SetDefault(&options.VirtualClusterOptions{})

PodLogsVolumeName := "pod-logs"
LogsVolumeName := "logs"
KubeletPodVolumeName := "kubelet-pods"
Expand Down
16 changes: 15 additions & 1 deletion pkg/server/filters/metrics_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,13 +517,27 @@ func (p *MetricsServerProxy) rewritePodMetricsListData(data []byte) ([]byte, err
func getVirtualPodObjectsInNamespace(ctx context.Context, vClient client.Client, namespace string) ([]corev1.Pod, error) {
podList := &corev1.PodList{}

// This is to counter an issue which occurred after the latest update
// to 0.28 client libraries where the cache api was changed and broke
// our cachedClients resulting in errors of the form
// "unable to list: kube-system because of unknown namespace for the cache"
err := vClient.List(ctx, podList, &client.ListOptions{
Namespace: namespace,
// Namespace: namespace,
})
if err != nil {
return nil, err
}

// manually filter until the above issue with cachedClient is resolved
filteredPodList := &corev1.PodList{}
for _, pod := range podList.Items {
if pod.Namespace == namespace {
filteredPodList.Items = append(filteredPodList.Items, pod)
}
}

podList.Items = filteredPodList.Items

return podList.Items, nil
}

Expand Down
35 changes: 34 additions & 1 deletion test/e2e_metrics_proxy/metricsProxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ import (

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
apiregistrationv1clientset "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/typed/apiregistration/v1"

metricsv1beta1client "k8s.io/metrics/pkg/client/clientset/versioned/typed/metrics/v1beta1"
)

var _ = ginkgo.Describe("Target Namespace", func() {
f := framework.DefaultFramework

ginkgo.It("Make sure the metrics api service is registered and available", func() {
err := wait.PollUntilContextTimeout(f.Context, time.Second, time.Minute*1, false, func(ctx context.Context) (done bool, err error) {
err := wait.PollUntilContextTimeout(f.Context, time.Second, time.Minute*1, false, func(ctx context.Context) (bool, error) {
apiRegistrationClient := apiregistrationv1clientset.NewForConfigOrDie(f.VclusterConfig)
apiService, err := apiRegistrationClient.APIServices().Get(f.Context, metricsapiservice.MetricsAPIServiceName, metav1.GetOptions{})
if err != nil {
Expand All @@ -33,4 +36,34 @@ var _ = ginkgo.Describe("Target Namespace", func() {
})
framework.ExpectNoError(err)
})

ginkgo.It("Make sure get nodeMetrics and podMetrics succeed", func() {
err := wait.PollUntilContextTimeout(f.Context, time.Second, time.Minute*1, false, func(ctx context.Context) (bool, error) {
metricsClient := metricsv1beta1client.NewForConfigOrDie(f.VclusterConfig)

nodeMetricsList, err := metricsClient.NodeMetricses().List(f.Context, metav1.ListOptions{})
if err != nil {
return false, nil
}

if len(nodeMetricsList.Items) == 0 {
klog.Errorf("expecting node metrics list to have at least 1 entry, got %s", len(nodeMetricsList.Items))
return false, nil
}

podMetricsList, err := metricsClient.PodMetricses("kube-system").List(f.Context, metav1.ListOptions{})
if err != nil {
return false, nil
}

if len(podMetricsList.Items) == 0 {
klog.Errorf("expecting node metrics list to have at least 1 entry, got %s", len(podMetricsList.Items))
return false, nil
}

return true, nil
})

framework.ExpectNoError(err)
})
})

0 comments on commit 9fcf6d9

Please sign in to comment.