use of org.onosproject.k8snode.api.HostNodesInfo in project onos by opennetworkinglab.
the class K8sApiConfigCodec method decode.
@Override
public K8sApiConfig decode(ObjectNode json, CodecContext context) {
if (json == null || !json.isObject()) {
return null;
}
JsonNode clusterNameJson = json.get(CLUSTER_NAME);
String clusterNameStr = "";
if (clusterNameJson == null) {
clusterNameStr = DEFAULT_CLUSTER_NAME;
} else {
clusterNameStr = clusterNameJson.asText();
}
JsonNode segmentIdJson = json.get(SEGMENT_ID);
int segmentId = DEFAULT_SEGMENT_ID;
if (segmentIdJson != null) {
segmentId = segmentIdJson.asInt();
}
JsonNode modeJson = json.get(MODE);
String modeStr = "";
if (modeJson == null) {
modeStr = DEFAULT_CONFIG_MODE;
} else {
modeStr = modeJson.asText();
}
Mode mode = Mode.valueOf(modeStr);
Scheme scheme = Scheme.valueOf(nullIsIllegal(json.get(SCHEME).asText(), SCHEME + MISSING_MESSAGE));
IpAddress ipAddress = IpAddress.valueOf(nullIsIllegal(json.get(IP_ADDRESS).asText(), IP_ADDRESS + MISSING_MESSAGE));
int port = json.get(PORT).asInt();
K8sApiConfig.Builder builder = DefaultK8sApiConfig.builder().clusterName(clusterNameStr).segmentId(segmentId).mode(mode).scheme(scheme).ipAddress(ipAddress).port(port).state(DISCONNECTED);
JsonNode dvrJson = json.get(DVR);
if (dvrJson != null) {
builder.dvr(dvrJson.asBoolean());
}
JsonNode tokenJson = json.get(TOKEN);
JsonNode caCertDataJson = json.get(CA_CERT_DATA);
JsonNode clientCertDataJson = json.get(CLIENT_CERT_DATA);
JsonNode clientKeyDataJson = json.get(CLIENT_KEY_DATA);
String token = "";
String caCertData = "";
String clientCertData = "";
String clientKeyData = "";
if (scheme == HTTPS) {
caCertData = nullIsIllegal(caCertDataJson.asText(), CA_CERT_DATA + MISSING_MESSAGE);
clientCertData = nullIsIllegal(clientCertDataJson.asText(), CLIENT_CERT_DATA + MISSING_MESSAGE);
clientKeyData = nullIsIllegal(clientKeyDataJson.asText(), CLIENT_KEY_DATA + MISSING_MESSAGE);
if (tokenJson != null) {
token = tokenJson.asText();
}
} else {
if (tokenJson != null) {
token = tokenJson.asText();
}
if (caCertDataJson != null) {
caCertData = caCertDataJson.asText();
}
if (clientCertDataJson != null) {
clientCertData = clientCertDataJson.asText();
}
if (clientKeyDataJson != null) {
clientKeyData = clientKeyDataJson.asText();
}
}
if (StringUtils.isNotEmpty(token)) {
builder.token(token);
}
if (StringUtils.isNotEmpty(caCertData)) {
builder.caCertData(caCertData);
}
if (StringUtils.isNotEmpty(clientCertData)) {
builder.clientCertData(clientCertData);
}
if (StringUtils.isNotEmpty(clientKeyData)) {
builder.clientKeyData(clientKeyData);
}
JsonNode extNetworkCidrJson = json.get(EXT_NETWORK_CIDR);
if (extNetworkCidrJson != null) {
builder.extNetworkCidr(IpPrefix.valueOf(extNetworkCidrJson.asText()));
}
Set<HostNodesInfo> infos = new HashSet<>();
ArrayNode infosJson = (ArrayNode) json.get(HOST_NODES_INFO);
if (infosJson != null) {
for (JsonNode infoJson : infosJson) {
HostNodesInfo info = context.codec(HostNodesInfo.class).decode((ObjectNode) infoJson, context);
infos.add(info);
}
builder.infos(infos);
}
return builder.build();
}
use of org.onosproject.k8snode.api.HostNodesInfo in project onos by opennetworkinglab.
the class K8sApiConfigCodecTest method testK8sApiConfigDecode.
/**
* Tests the kubernetes API config decoding.
*
* @throws IOException IO exception
*/
@Test
public void testK8sApiConfigDecode() throws IOException {
K8sApiConfig config = getK8sApiConfig("K8sApiConfig.json");
assertEquals("kubernetes", config.clusterName());
assertEquals(1, config.segmentId());
assertEquals("192.168.200.0/24", config.extNetworkCidr().toString());
assertEquals("NORMAL", config.mode().name());
assertEquals("HTTPS", config.scheme().name());
assertEquals("10.134.34.223", config.ipAddress().toString());
assertEquals(6443, config.port());
assertEquals("token", config.token());
assertEquals("caCertData", config.caCertData());
assertEquals("clientCertData", config.clientCertData());
assertEquals("clientKeyData", config.clientKeyData());
assertTrue(config.dvr());
Set<HostNodesInfo> infos = config.infos();
assertEquals(1, infos.size());
}
use of org.onosproject.k8snode.api.HostNodesInfo in project onos by opennetworkinglab.
the class K8sApiConfigCodecTest method testK8sApiConfigEncode.
/**
* Tests the kubernetes API config encoding.
*/
@Test
public void testK8sApiConfigEncode() {
HostNodesInfo info = new DefaultHostNodesInfo.Builder().hostIp(IpAddress.valueOf("192.168.10.10")).nodes(ImmutableSet.of("master", "worker")).build();
K8sApiConfig config = DefaultK8sApiConfig.builder().clusterName("kubernetes").segmentId(1).extNetworkCidr(IpPrefix.valueOf("192.168.200.0/24")).mode(K8sApiConfig.Mode.NORMAL).scheme(K8sApiConfig.Scheme.HTTPS).ipAddress(IpAddress.valueOf("10.10.10.23")).port(6443).state(CONNECTED).token("token").caCertData("caCertData").clientCertData("clientCertData").clientKeyData("clientKeyData").infos(ImmutableSet.of(info)).dvr(true).build();
ObjectNode configJson = k8sApiConfigCodec.encode(config, context);
assertThat(configJson, matchesK8sApiConfig(config));
}
use of org.onosproject.k8snode.api.HostNodesInfo in project onos by opennetworkinglab.
the class K8sApiConfigJsonMatcher method matchesSafely.
@Override
protected boolean matchesSafely(JsonNode jsonNode, Description description) {
// check cluster name
String jsonClusterName = jsonNode.get(CLUSTER_NAME).asText();
String clusterName = k8sApiConfig.clusterName();
if (!jsonClusterName.equals(clusterName)) {
description.appendText("cluster name was " + jsonClusterName);
return false;
}
// check segment ID
int jsonSegmentId = jsonNode.get(SEGMENT_ID).asInt();
int segmentId = k8sApiConfig.segmentId();
if (jsonSegmentId != segmentId) {
description.appendText("Segment ID was " + jsonSegmentId);
return false;
}
// check mode
String jsonMode = jsonNode.get(MODE).asText();
String mode = k8sApiConfig.mode().name();
if (!jsonMode.equals(mode)) {
description.appendText("mode was " + jsonMode);
return false;
}
// check external network CIDR
JsonNode jsonCidr = jsonNode.get(EXT_NETWORK_CIDR);
String cidr = k8sApiConfig.extNetworkCidr().toString();
if (jsonCidr != null) {
if (!jsonCidr.asText().equals(cidr)) {
description.appendText("External network CIDR was " + jsonCidr);
return false;
}
}
// check scheme
String jsonScheme = jsonNode.get(SCHEME).asText();
String scheme = k8sApiConfig.scheme().name();
if (!jsonScheme.equals(scheme)) {
description.appendText("scheme was " + jsonScheme);
return false;
}
// check IP address
String jsonIpAddress = jsonNode.get(IP_ADDRESS).asText();
String ipAddress = k8sApiConfig.ipAddress().toString();
if (!jsonIpAddress.equals(ipAddress)) {
description.appendText("ipAddress was " + jsonIpAddress);
return false;
}
// check port
int jsonPort = jsonNode.get(PORT).asInt();
int port = k8sApiConfig.port();
if (jsonPort != port) {
description.appendText("port was " + jsonPort);
return false;
}
// check state
JsonNode jsonState = jsonNode.get(STATE);
String state = k8sApiConfig.state().name();
if (jsonState != null) {
if (!jsonState.asText().equals(state)) {
description.appendText("state was " + jsonState);
return false;
}
}
// check DVR
JsonNode jsonDvr = jsonNode.get(DVR);
boolean dvr = k8sApiConfig.dvr();
if (jsonDvr != null) {
if (jsonDvr.asBoolean() != dvr) {
description.appendText("DVR was " + jsonDvr);
return false;
}
}
// check token
JsonNode jsonToken = jsonNode.get(TOKEN);
String token = k8sApiConfig.token();
if (jsonToken != null) {
if (!jsonToken.asText().equals(token)) {
description.appendText("token was " + jsonToken);
return false;
}
}
// check caCertData
JsonNode jsonCaCertData = jsonNode.get(CA_CERT_DATA);
String caCertData = k8sApiConfig.caCertData();
if (jsonCaCertData != null) {
if (!jsonCaCertData.asText().equals(caCertData)) {
description.appendText("caCertData was " + jsonCaCertData);
return false;
}
}
// check clientCertData
JsonNode jsonClientCertData = jsonNode.get(CLIENT_CERT_DATA);
String clientCertData = k8sApiConfig.clientCertData();
if (jsonClientCertData != null) {
if (!jsonClientCertData.asText().equals(clientCertData)) {
description.appendText("clientCertData was " + jsonClientCertData);
return false;
}
}
// check clientKeyData
JsonNode jsonClientKeyData = jsonNode.get(CLIENT_KEY_DATA);
String clientKeyData = k8sApiConfig.clientKeyData();
if (jsonClientKeyData != null) {
if (!jsonClientKeyData.asText().equals(clientKeyData)) {
description.appendText("clientKeyData was " + jsonClientKeyData);
return false;
}
}
// check hostNodesInfo size
JsonNode jsonInfos = jsonNode.get(HOST_NODES_INFO);
if (jsonInfos.size() != k8sApiConfig.infos().size()) {
description.appendText("Info size was " + jsonInfos.size());
return false;
}
// check info
for (HostNodesInfo info : k8sApiConfig.infos()) {
boolean infoFound = false;
for (int infoIndex = 0; infoIndex < jsonInfos.size(); infoIndex++) {
HostNodesInfoJsonMatcher infoMatcher = HostNodesInfoJsonMatcher.matchesHostNodesInfo(info);
if (infoMatcher.matches(jsonInfos.get(infoIndex))) {
infoFound = true;
break;
}
}
if (!infoFound) {
description.appendText("Info not found " + info.toString());
return false;
}
}
return true;
}
use of org.onosproject.k8snode.api.HostNodesInfo in project onos by opennetworkinglab.
the class DefaultK8sApiConfigHandler method buildK8sNode.
private K8sNode buildK8sNode(Node node, K8sApiConfig config) {
String hostname = node.getMetadata().getName();
IpAddress managementIp = null;
IpAddress dataIp = null;
IpAddress nodeIp = null;
// normal mode: we use K8S node's internal IP as the management and data IP
if (config.mode() == PASSTHROUGH) {
HostNodesInfo info = config.infos().stream().filter(h -> h.nodes().contains(hostname)).findAny().orElse(null);
if (info == null) {
log.error("None of the nodes were found in the host nodes info mapping list");
} else {
managementIp = info.hostIp();
dataIp = info.hostIp();
}
for (NodeAddress nodeAddress : node.getStatus().getAddresses()) {
if (nodeAddress.getType().equals(INTERNAL_IP)) {
nodeIp = IpAddress.valueOf(nodeAddress.getAddress());
}
}
} else {
for (NodeAddress nodeAddress : node.getStatus().getAddresses()) {
if (nodeAddress.getType().equals(INTERNAL_IP)) {
managementIp = IpAddress.valueOf(nodeAddress.getAddress());
dataIp = IpAddress.valueOf(nodeAddress.getAddress());
nodeIp = IpAddress.valueOf(nodeAddress.getAddress());
}
}
}
String roleStr = node.getMetadata().getLabels().keySet().stream().filter(l -> l.contains(K8S_ROLE)).findFirst().orElse(null);
K8sNode.Type nodeType = MINION;
if (roleStr != null) {
String role = roleStr.split("/")[1];
if (MASTER.name().equalsIgnoreCase(role)) {
nodeType = MASTER;
} else {
nodeType = MINION;
}
}
Map<String, String> annots = node.getMetadata().getAnnotations();
String extIntf = "";
String extGatewayIpStr = DEFAULT_GATEWAY_IP;
String extBridgeIpStr = DEFAULT_BRIDGE_IP;
if (config.mode() == PASSTHROUGH) {
extNetworkService.registerNetwork(config.extNetworkCidr());
extIntf = EXTERNAL_TO_ROUTER + "-" + config.clusterShortName();
IpAddress gatewayIp = extNetworkService.getGatewayIp(config.extNetworkCidr());
IpAddress bridgeIp = extNetworkService.allocateIp(config.extNetworkCidr());
if (gatewayIp != null) {
extGatewayIpStr = gatewayIp.toString();
}
if (bridgeIp != null) {
extBridgeIpStr = bridgeIp.toString();
}
} else {
extIntf = annots.get(EXT_INTF_NAME);
extGatewayIpStr = annots.get(EXT_GATEWAY_IP);
extBridgeIpStr = annots.get(EXT_BRIDGE_IP);
}
K8sNode.Builder builder = DefaultK8sNode.builder().clusterName(DEFAULT_CLUSTER_NAME).hostname(hostname).managementIp(managementIp).dataIp(dataIp).nodeInfo(new K8sNodeInfo(nodeIp, null)).extIntf(extIntf).type(nodeType).segmentId(config.segmentId()).state(PRE_ON_BOARD).mode(config.mode()).extBridgeIp(IpAddress.valueOf(extBridgeIpStr)).extGatewayIp(IpAddress.valueOf(extGatewayIpStr)).podCidr(node.getSpec().getPodCIDR());
if (config.dvr()) {
builder.extGatewayMac(MacAddress.valueOf(DEFAULT_EXTERNAL_GATEWAY_MAC));
}
return builder.build();
}
Aggregations