use of com.mesosphere.sdk.http.EndpointUtils.VipInfo in project dcos-commons by mesosphere.
the class AuxLabelAccess method getVIPsFromLabels.
/**
* Returns the VIP information, if any, within the provided {@link Protos.Port}.
* This is the inverse of {@link #setVIPLabels(org.apache.mesos.Protos.Port.Builder, NamedVIPSpec)}.
*/
public static Collection<VipInfo> getVIPsFromLabels(String taskName, Protos.Port port) {
List<VipInfo> vips = new ArrayList<>();
for (Label label : port.getLabels().getLabelsList()) {
Optional<EndpointUtils.VipInfo> vipInfo = parseVipLabel(taskName, label);
if (!vipInfo.isPresent()) {
// Label doesn't appear to be for a VIP
continue;
}
vips.add(vipInfo.get());
}
return vips;
}
use of com.mesosphere.sdk.http.EndpointUtils.VipInfo in project dcos-commons by mesosphere.
the class AuxLabelAccessTest method testParseVipLabel.
@Test
public void testParseVipLabel() {
assertEquals(0, AuxLabelAccess.getVIPsFromLabels(TestConstants.TASK_NAME, withLabel("", "")).size());
assertEquals(0, AuxLabelAccess.getVIPsFromLabels(TestConstants.TASK_NAME, withLabel("asdf", "ara")).size());
assertEquals(0, AuxLabelAccess.getVIPsFromLabels(TestConstants.TASK_NAME, withLabel("VIP_0000", "ara")).size());
assertEquals(0, AuxLabelAccess.getVIPsFromLabels(TestConstants.TASK_NAME, withLabel("VIP_0000", "ara:rar")).size());
VipInfo info = AuxLabelAccess.getVIPsFromLabels(TestConstants.TASK_NAME, withLabel("VIP_0000", "myvip:321")).iterator().next();
assertEquals("myvip", info.getVipName());
assertEquals(321, info.getVipPort());
}
use of com.mesosphere.sdk.http.EndpointUtils.VipInfo in project dcos-commons by mesosphere.
the class AuxLabelAccess method parseVipLabel.
/**
* Extracts VIP information from the provided label, if VIP information is present.
*
* @param taskName task name for use in logs if there's a problem
* @param label a label from a {@code org.apache.mesos.Protos.DiscoveryInfo}
* @return the VIP information or an empty Optional if the provided label is invalid or inapplicable
*/
private static Optional<VipInfo> parseVipLabel(String taskName, Label label) {
if (!label.getKey().startsWith(LabelConstants.VIP_LABEL_PREFIX)) {
return Optional.empty();
}
// Expected VIP label format: "<vipname>:<port>"
List<String> namePort = Splitter.on(':').splitToList(label.getValue());
if (namePort.size() != 2) {
LOGGER.error("Task {}'s VIP value for {} is invalid, expected 2 components but got {}: {}", taskName, label.getKey(), namePort.size(), label.getValue());
return Optional.empty();
}
int vipPort;
try {
vipPort = Integer.parseInt(namePort.get(1));
} catch (NumberFormatException e) {
LOGGER.error(String.format("Unable to Task %s's VIP port from %s as an int", taskName, label.getValue()), e);
return Optional.empty();
}
return Optional.of(new VipInfo(namePort.get(0), vipPort));
}