use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Node in project openhab-addons by openhab.
the class HomieImplementationTest method retrieveAttributes.
@SuppressWarnings("null")
@Test
public void retrieveAttributes() throws InterruptedException, ExecutionException {
assertThat(connection.hasSubscribers(), is(false));
Node node = new Node(DEVICE_TOPIC, "testnode", ThingChannelConstants.testHomieThing, callback, new NodeAttributes());
Property property = spy(new Property(DEVICE_TOPIC + "/testnode", node, "temperature", callback, new PropertyAttributes()));
// Create a scheduler
ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(4);
property.subscribe(connection, scheduler, 500).get();
assertThat(property.attributes.settable, is(true));
assertThat(property.attributes.retained, is(true));
assertThat(property.attributes.name, is("Testprop"));
assertThat(property.attributes.unit, is("°C"));
assertThat(property.attributes.datatype, is(DataTypeEnum.float_));
waitForAssert(() -> assertThat(property.attributes.format, is("-100:100")));
verify(property, timeout(500).atLeastOnce()).attributesReceived();
// Receive property value
ChannelState channelState = spy(property.getChannelState());
PropertyHelper.setChannelState(property, channelState);
property.startChannel(connection, scheduler, 500).get();
verify(channelState).start(any(), any(), anyInt());
verify(channelState, timeout(500)).processMessage(any(), any());
verify(callback).updateChannelState(any(), any());
assertThat(property.getChannelState().getCache().getChannelState(), is(new DecimalType(10)));
property.stop().get();
assertThat(connection.hasSubscribers(), is(false));
}
use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Node in project openhab-addons by openhab.
the class HomieImplementationTest method createSpyProperty.
// Inject a spy'ed property
public Property createSpyProperty(InvocationOnMock invocation) {
final Node node = (Node) invocation.getMock();
final String id = (String) invocation.getArguments()[0];
return spy(node.createProperty(id, spy(new PropertyAttributes())));
}
use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Node in project openhab-addons by openhab.
the class HomieImplementationTest method createSpyNode.
// Inject a spy'ed node
public Node createSpyNode(InvocationOnMock invocation) {
final Device device = (Device) invocation.getMock();
final String id = (String) invocation.getArguments()[0];
// Create the node
Node node = spy(device.createNode(id, spy(new NodeAttributes())));
// Intercept creating a property in the next call and inject a spy'ed property.
doAnswer(this::createSpyProperty).when(node).createProperty(any());
return node;
}
use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Node in project yakc by manusa.
the class TopNodes method main.
public static void main(String[] args) {
try (KubernetesClient kc = new KubernetesClient()) {
final Node node = kc.create(CoreV1Api.class).listNode().stream().findFirst().orElseThrow(() -> new IllegalStateException("No nodes found in cluster"));
final Map<String, String> nodeAllocatable = node.getStatus().getAllocatable();
final double allocatableCpu = Resource.CPU.get(nodeAllocatable);
final double allocatableMemory = Resource.MEMORY.get(nodeAllocatable);
final double allocatablePods = Resource.PODS.get(nodeAllocatable);
System.out.printf("Node %s allocatable resources, cpu: %s, memory: %s, pods %s%n", node.getMetadata().getName(), allocatableCpu, bytesToHumanReadable(allocatableMemory), (int) allocatablePods);
System.out.printf(LOG_FORMAT, "POD (CONTAINER)", "CPU / LIMIT", "MEM / LIMIT");
logSeparator();
double totalCpu = 0D;
double totalCpuLimit = 0D;
double totalMemory = 0D;
double totalMemoryLimit = 0D;
final List<PodContainer> containers = kc.create(CoreV1Api.class).listPodForAllNamespaces(new ListPodForAllNamespaces().fieldSelector("spec.nodeName=" + node.getMetadata().getName())).stream().flatMap(p -> p.getSpec().getContainers().stream().map(c -> new PodContainer(p, c))).collect(Collectors.toList());
for (PodContainer c : containers) {
final Map<String, String> containerRequests = c.container.getResources().getRequests();
final double cpu = Resource.CPU.get(containerRequests);
final double memory = Resource.MEMORY.get(containerRequests);
final Map<String, String> containerLimits = c.container.getResources().getLimits();
final double cpuLimit = Resource.CPU.get(containerLimits);
final double memoryLimit = Resource.MEMORY.get(containerLimits);
System.out.printf(LOG_FORMAT, String.format("%s (%s)", c.pod.getMetadata().getName(), c.container.getName()), cpu + " / " + cpuLimit, bytesToHumanReadable(memory) + " / " + bytesToHumanReadable(memoryLimit));
totalCpu += cpu;
totalMemory += memory;
totalCpuLimit += cpuLimit;
totalMemoryLimit += memoryLimit;
}
logSeparator();
System.out.printf(LOG_FORMAT, "TOTAL", totalCpu + " / " + totalCpuLimit, bytesToHumanReadable(totalMemory) + " / " + bytesToHumanReadable(totalMemoryLimit));
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Node in project yakc by manusa.
the class MetricsV1beta1ApiIT method readNodeMetrics.
@Test
@DisplayName("readNodeMetrics, cluster contains at least a Node with some metrics")
void readNodeMetrics() throws IOException, InterruptedException {
// Given
final String node = KC.create(CoreV1Api.class).listNode().stream().findFirst().map(Node::getMetadata).map(ObjectMeta::getName).orElseThrow(() -> new AssertionError("No nodes found"));
// When
final NodeMetrics result = getWithRetry(() -> KC.create(MetricsV1beta1Api.class).readNodeMetrics(node).get());
// Then
assertThat(result).hasFieldOrPropertyWithValue("metadata.name", node).hasFieldOrProperty("usage.cpu").hasFieldOrProperty("usage.memory").hasFieldOrProperty("window").extracting(NodeMetrics::getTimestamp).isNotNull();
}
Aggregations