Skip to content

Commit

Permalink
Added a test for fetching Node Metrics with labels
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanKanojia committed Apr 2, 2020
1 parent 32b28ac commit dfb27ba
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.fabric8.kubernetes.client.dsl.base.OperationContext;
import io.fabric8.kubernetes.client.dsl.base.OperationSupport;
import io.fabric8.kubernetes.client.utils.URLUtils;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;

public class NodeMetricOperationsImpl extends OperationSupport {
Expand Down Expand Up @@ -51,18 +52,16 @@ public NodeMetrics metrics(String nodeName) {
}
}

public NodeMetricsList metrics(Map<String ,Object> labelsMap) {
public NodeMetricsList metrics(Map<String, Object> labelsMap) {
try {
StringBuilder sb = new StringBuilder();
sb.append("?labelSelector=");
HttpUrl.Builder httpUrlBuilder = HttpUrl.get(URLUtils.join(config.getMasterUrl(), METRIC_ENDPOINT_URL)).newBuilder();

StringBuilder sb = new StringBuilder();
for(Map.Entry<String, Object> entry : labelsMap.entrySet()) {
sb.append(entry.getKey()).append("=").append(entry.getValue().toString()).append(",");
}
String paras = sb.toString().substring(0, sb.toString().length() - 1);

String resourceUrl = URLUtils.join(config.getMasterUrl(), METRIC_ENDPOINT_URL, paras);
return handleMetric(resourceUrl, NodeMetricsList.class);
httpUrlBuilder.addQueryParameter("labelSelector", sb.toString().substring(0, sb.toString().length() - 1));
return handleMetric(httpUrlBuilder.build().toString(), NodeMetricsList.class);
} catch(Exception e) {
throw KubernetesClientException.launderThrowable(e);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
import io.fabric8.kubernetes.api.model.metrics.v1beta1.PodMetricsListBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import io.fabric8.kubernetes.client.utils.Utils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

Expand Down Expand Up @@ -96,6 +99,24 @@ public void testNodeMetric() {
assertEquals("foo", nodeMetrics.getMetadata().getName());
}

@Test
public void testNodeMetricWithLabels() {
// Given
server.expect().get().withPath("/apis/metrics.k8s.io/v1beta1/nodes?labelSelector=" + Utils.toUrlEncoded("ss=true,cs=true"))
.andReturn(200, new NodeMetricsListBuilder().withItems(getNodeMetric()).build()).once();

KubernetesClient client = server.getClient();
Map<String,Object> lablesMap = new HashMap();
lablesMap.put("ss", "true");
lablesMap.put("cs", "true");

// When
NodeMetricsList nodeMetricList = client.top().nodes().metrics(lablesMap);

// Then
assertEquals(1, nodeMetricList.getItems().size());
}

private PodMetrics getPodMetric() throws Exception {
return new PodMetricsBuilder()
.withNewMetadata().withName("foo").endMetadata()
Expand Down

0 comments on commit dfb27ba

Please sign in to comment.