Skip to content

Commit

Permalink
Merge pull request #2091 from damozhiying/master
Browse files Browse the repository at this point in the history
  • Loading branch information
fusesource-ci authored Apr 7, 2020
2 parents 1d97313 + dfb27ba commit be6b472
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Fix #1987: Added an example for Task and TaskRun with updated model
* Fix #2019: Added CustomResourceCrudTest
* Fix #2054: JobExample doesn't work
* Fix #2082: Added filter node metrics via labels

#### Dependency Upgrade
* Updated Knative model to v0.13.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,56 @@
*/
package io.fabric8.kubernetes.client.dsl.internal;

import java.util.Map;

import io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetrics;
import io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetricsList;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClientException;
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 {
private static String METRIC_ENDPOINT_URL = "apis/metrics.k8s.io/v1beta1/nodes";

public NodeMetricOperationsImpl(OkHttpClient client, Config config) {
super(new OperationContext().withOkhttpClient(client).withConfig(config));
}

public NodeMetricsList metrics() {
try {
String resourceUrl = URLUtils.join(config.getMasterUrl(), METRIC_ENDPOINT_URL);
return handleMetric(resourceUrl, NodeMetricsList.class);
} catch(Exception e) {
throw KubernetesClientException.launderThrowable(e);
}
}

public NodeMetrics metrics(String nodeName) {
try {
String resourceUrl = URLUtils.join(config.getMasterUrl(), METRIC_ENDPOINT_URL, nodeName);
return handleMetric(resourceUrl, NodeMetrics.class);
} catch(Exception e) {
throw KubernetesClientException.launderThrowable(e);
}
}
private static String METRIC_ENDPOINT_URL = "apis/metrics.k8s.io/v1beta1/nodes";

public NodeMetricOperationsImpl(OkHttpClient client, Config config) {
super(new OperationContext().withOkhttpClient(client).withConfig(config));
}

public NodeMetricsList metrics() {
try {
String resourceUrl = URLUtils.join(config.getMasterUrl(), METRIC_ENDPOINT_URL);
return handleMetric(resourceUrl, NodeMetricsList.class);
} catch(Exception e) {
throw KubernetesClientException.launderThrowable(e);
}
}

public NodeMetrics metrics(String nodeName) {
try {
String resourceUrl = URLUtils.join(config.getMasterUrl(), METRIC_ENDPOINT_URL, nodeName);
return handleMetric(resourceUrl, NodeMetrics.class);
} catch(Exception e) {
throw KubernetesClientException.launderThrowable(e);
}
}

public NodeMetricsList metrics(Map<String, Object> labelsMap) {
try {
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(",");
}
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);
}
}

}
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 be6b472

Please sign in to comment.