use of org.onosproject.k8snode.api.K8sNodeService in project onos by opennetworkinglab.
the class K8sServiceHandler method setStatefulGroupFlowRules.
private void setStatefulGroupFlowRules(DeviceId deviceId, long ctState, long ctMask, Service service, boolean install) {
List<GroupBucket> buckets = Lists.newArrayList();
String serviceName = service.getMetadata().getName();
String serviceIp = service.getSpec().getClusterIP();
// TODO: multi-ports case should be addressed
Integer servicePort = service.getSpec().getPorts().get(0).getPort();
String serviceProtocol = service.getSpec().getPorts().get(0).getProtocol();
String svcStr = servicePortStr(serviceIp, servicePort, serviceProtocol);
int groupId = svcStr.hashCode();
List<Endpoints> endpointses = k8sEndpointsService.endpointses().stream().filter(ep -> serviceName.equals(ep.getMetadata().getName())).collect(Collectors.toList());
Map<String, String> nodeIpGatewayIpMap = nodeIpGatewayIpMap(k8sNodeService, k8sNetworkService);
for (Endpoints endpoints : endpointses) {
for (EndpointSubset endpointSubset : endpoints.getSubsets()) {
List<EndpointPort> ports = endpointSubset.getPorts().stream().filter(p -> p.getProtocol().equals(TCP)).collect(Collectors.toList());
for (EndpointAddress address : endpointSubset.getAddresses()) {
String podIp = nodeIpGatewayIpMap.containsKey(address.getIp()) ? nodeIpGatewayIpMap.get(address.getIp()) : address.getIp();
NiciraConnTrackTreatmentBuilder connTreatmentBuilder = niciraConnTrackTreatmentBuilder(driverService, deviceId).commit(true).natAction(true).natIp(IpAddress.valueOf(podIp)).natFlag(CT_NAT_DST_FLAG);
ports.forEach(p -> {
ExtensionTreatment ctNatTreatment = connTreatmentBuilder.natPortMin(TpPort.tpPort(p.getPort())).natPortMax(TpPort.tpPort(p.getPort())).build();
ExtensionTreatment resubmitTreatment = buildResubmitExtension(deviceService.getDevice(deviceId), ACL_TABLE);
TrafficTreatment treatment = DefaultTrafficTreatment.builder().extension(ctNatTreatment, deviceId).extension(resubmitTreatment, deviceId).build();
buckets.add(buildGroupBucket(treatment, SELECT, (short) -1));
});
}
}
}
if (!buckets.isEmpty()) {
k8sGroupRuleService.setRule(appId, deviceId, groupId, SELECT, buckets, install);
setTrackNew(deviceId, ctState, ctMask, IpAddress.valueOf(serviceIp), TpPort.tpPort(servicePort), NAT_TABLE, groupId, PRIORITY_CT_RULE, install);
}
}
use of org.onosproject.k8snode.api.K8sNodeService in project onos by opennetworkinglab.
the class K8sNodeInitCommand method doExecute.
@Override
protected void doExecute() {
K8sNodeService nodeService = get(K8sNodeService.class);
K8sNodeAdminService nodeAdminService = get(K8sNodeAdminService.class);
if ((!isAll && !isIncomplete && hostnames == null) || (isAll && isIncomplete) || (isIncomplete && hostnames != null) || (hostnames != null && isAll)) {
print("Please specify one of hostname, --all, and --incomplete options.");
return;
}
if (isAll) {
hostnames = nodeService.nodes().stream().map(K8sNode::hostname).toArray(String[]::new);
} else if (isIncomplete) {
hostnames = nodeService.nodes().stream().filter(node -> node.state() != COMPLETE).map(K8sNode::hostname).toArray(String[]::new);
}
for (String hostname : hostnames) {
K8sNode node = nodeService.node(hostname);
if (node == null) {
print("Unable to find %s", hostname);
continue;
}
print("Initializing %s", hostname);
K8sNode updated = node.updateState(INIT);
nodeAdminService.updateNode(updated);
}
print("Done.");
}
use of org.onosproject.k8snode.api.K8sNodeService in project onos by opennetworkinglab.
the class K8sNodeListCommand method doExecute.
@Override
protected void doExecute() {
K8sNodeService nodeService = get(K8sNodeService.class);
List<K8sNode> nodes = Lists.newArrayList(nodeService.nodes());
nodes.sort(Comparator.comparing(K8sNode::hostname));
String format = genFormatString(ImmutableList.of(HOSTNAME_LENGTH, TYPE_LENGTH, MANAGEMENT_IP_LENGTH, DATA_IP_LENGTH, STATUS));
if (outputJson()) {
print("%s", json(nodes));
} else {
print(format, "Hostname", "Type", "Management IP", "Data IP", "State");
for (K8sNode node : nodes) {
print(format, StringUtils.substring(node.hostname(), 0, HOSTNAME_LENGTH - MARGIN_LENGTH), node.type(), StringUtils.substring(node.managementIp().toString(), 0, MANAGEMENT_IP_LENGTH - MARGIN_LENGTH), node.dataIp() != null ? StringUtils.substring(node.dataIp().toString(), 0, DATA_IP_LENGTH - MARGIN_LENGTH) : "", node.state());
}
print("Total %s nodes", nodeService.nodes().size());
}
}
use of org.onosproject.k8snode.api.K8sNodeService in project onos by opennetworkinglab.
the class K8sPurgeRulesCommand method doExecute.
@Override
protected void doExecute() {
FlowRuleService flowRuleService = get(FlowRuleService.class);
GroupService groupService = get(GroupService.class);
CoreService coreService = get(CoreService.class);
K8sNodeService k8sNodeService = get(K8sNodeService.class);
ApplicationId appId = coreService.getAppId(K8S_NETWORKING_APP_ID);
if (appId == null) {
error("Failed to purge kubernetes networking flow rules.");
return;
}
flowRuleService.removeFlowRulesById(appId);
print("Successfully purged flow rules installed by kubernetes networking app.");
boolean result = true;
long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS;
// we make sure all flow rules are removed from the store
while (stream(flowRuleService.getFlowEntriesById(appId).spliterator(), false).count() > 0) {
long waitMs = timeoutExpiredMs - System.currentTimeMillis();
try {
sleep(SLEEP_MS);
} catch (InterruptedException e) {
log.error("Exception caused during rule purging...");
}
if (stream(flowRuleService.getFlowEntriesById(appId).spliterator(), false).count() == 0) {
break;
} else {
flowRuleService.removeFlowRulesById(appId);
print("Failed to purging flow rules, retrying rule purging...");
}
if (waitMs <= 0) {
result = false;
break;
}
}
for (K8sNode node : k8sNodeService.completeNodes()) {
for (Group group : groupService.getGroups(node.intgBridge(), appId)) {
groupService.removeGroup(node.intgBridge(), group.appCookie(), appId);
}
}
if (result) {
print("Successfully purged flow rules!");
} else {
error("Failed to purge flow rules.");
}
}
use of org.onosproject.k8snode.api.K8sNodeService in project onos by opennetworkinglab.
the class K8sServiceHandler method setGroupBuckets.
private void setGroupBuckets(Service service, boolean install) {
Map<ServicePort, Set<String>> spEpasMap = getSportEpAddressMap(service);
Map<ServicePort, List<GroupBucket>> spGrpBkts = Maps.newConcurrentMap();
Map<String, String> nodeIpGatewayIpMap = nodeIpGatewayIpMap(k8sNodeService, k8sNetworkService);
for (K8sNode node : k8sNodeService.completeNodes()) {
spEpasMap.forEach((sp, epas) -> {
List<GroupBucket> bkts = Lists.newArrayList();
for (String ip : epas) {
GroupBucket bkt = buildBuckets(node.intgBridge(), nodeIpGatewayIpMap.getOrDefault(ip, ip), sp);
if (bkt == null) {
continue;
}
if (install) {
bkts.add(bkt);
} else {
bkts.remove(bkt);
}
}
spGrpBkts.put(sp, bkts);
});
String serviceIp = service.getSpec().getClusterIP();
spGrpBkts.forEach((sp, bkts) -> {
String svcStr = servicePortStr(serviceIp, sp.getPort(), sp.getProtocol());
int groupId = svcStr.hashCode();
if (bkts.size() > 0) {
k8sGroupRuleService.setBuckets(appId, node.intgBridge(), groupId, bkts);
}
});
spEpasMap.forEach((sp, epas) -> epas.forEach(epa -> {
String podIp = nodeIpGatewayIpMap.getOrDefault(epa, epa);
int targetPort;
if (sp.getTargetPort().getIntVal() == null) {
Pod pod = podByIp(k8sPodService, podIp);
targetPort = portNumberByName(pod, sp.getTargetPort().getStrVal());
} else {
targetPort = sp.getTargetPort().getIntVal();
}
if (targetPort != 0) {
setUnshiftDomainRules(node.intgBridge(), POD_TABLE, PRIORITY_NAT_RULE, serviceIp, sp.getPort(), sp.getProtocol(), podIp, targetPort, install);
}
}));
}
}
Aggregations