use of org.onosproject.k8snetworking.api.K8sNetworkService in project onos by opennetworkinglab.
the class K8sNetworkListCommand method doExecute.
@Override
protected void doExecute() {
K8sNetworkService service = get(K8sNetworkService.class);
List<K8sNetwork> networks = Lists.newArrayList(service.networks());
networks.sort(Comparator.comparing(K8sNetwork::name));
String format = genFormatString(ImmutableList.of(CLI_ID_LENGTH, CLI_NAME_LENGTH, CLI_TYPE_LENGTH, CLI_SEG_ID_LENGTH, CLI_IP_ADDRESS_LENGTH));
if (outputJson()) {
print("%s", json(networks));
} else {
print(format, "ID", "Name", "Type", "SegId", "Gateway");
for (K8sNetwork net : networks) {
print(format, StringUtils.substring(net.networkId(), 0, CLI_ID_LENGTH - CLI_MARGIN_LENGTH), StringUtils.substring(net.name(), 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH), net.type().toString(), net.segmentId(), net.gatewayIp() == null ? "" : net.gatewayIp().toString());
}
}
}
use of org.onosproject.k8snetworking.api.K8sNetworkService in project onos by opennetworkinglab.
the class K8sIpAddressListCommand method doExecute.
@Override
protected void doExecute() {
K8sIpamService ipamService = get(K8sIpamService.class);
K8sNetworkService networkService = get(K8sNetworkService.class);
if (networkIds == null || networkIds.length == 0) {
networkIds = networkService.networks().stream().map(K8sNetwork::networkId).toArray(String[]::new);
}
Map<String, Map<IpAddress, MacAddress>> ipMacs = Maps.newConcurrentMap();
if (available && reserved) {
error("Only one of list options (available | reserved) can be specified.");
return;
}
if (!(available || reserved)) {
error("At least one of list options (available | reserved) should be specified.");
return;
}
for (String networkId : networkIds) {
Map<IpAddress, MacAddress> tmpIpMacs = Maps.newConcurrentMap();
if (available) {
ipamService.availableIps(networkId).forEach(n -> tmpIpMacs.put(n, ZERO));
}
if (reserved) {
Set<K8sPort> ports = networkService.ports(networkId);
ipamService.allocatedIps(networkId).forEach(ip -> {
MacAddress mac = ports.stream().filter(p -> p.ipAddress().equals(ip)).map(K8sPort::macAddress).findAny().orElse(ZERO);
tmpIpMacs.put(ip, mac);
});
}
ipMacs.put(networkId, tmpIpMacs);
}
if (ipMacs.size() > 0) {
print(FORMAT, "Network ID", "IP Address", "MAC Address");
ipMacs.forEach((k, v) -> v.forEach((ip, mac) -> print(FORMAT, k, ip, mac)));
} else {
print("No IP addresses are available or reserved.");
}
}
use of org.onosproject.k8snetworking.api.K8sNetworkService 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.k8snetworking.api.K8sNetworkService in project onos by opennetworkinglab.
the class K8sNetworkIdCompleter method complete.
@Override
public int complete(Session session, CommandLine commandLine, List<String> candidates) {
StringsCompleter delegate = new StringsCompleter();
K8sNetworkService networkService = get(K8sNetworkService.class);
Set<String> netNames = networkService.networks().stream().map(K8sNetwork::name).collect(Collectors.toSet());
SortedSet<String> strings = delegate.getStrings();
strings.addAll(netNames);
return delegate.complete(session, commandLine, candidates);
}
use of org.onosproject.k8snetworking.api.K8sNetworkService in project onos by opennetworkinglab.
the class K8sPortListCommand method doExecute.
@Override
protected void doExecute() {
K8sNetworkService service = get(K8sNetworkService.class);
List<K8sPort> ports = Lists.newArrayList(service.ports());
ports.sort(Comparator.comparing(K8sPort::networkId));
String format = genFormatString(ImmutableList.of(CLI_ID_LENGTH, CLI_NAME_LENGTH, CLI_MAC_ADDRESS_LENGTH, CLI_IP_ADDRESSES_LENGTH));
if (!Strings.isNullOrEmpty(networkId)) {
ports.removeIf(port -> !port.networkId().equals(networkId));
}
if (outputJson()) {
print("%s", json(ports));
} else {
print(format, "ID", "Network", "MAC Address", "Fixed IPs");
for (K8sPort port : ports) {
K8sNetwork k8sNet = service.network(port.networkId());
print(format, StringUtils.substring(port.portId(), 0, CLI_ID_LENGTH - CLI_MARGIN_LENGTH), k8sNet == null ? "" : StringUtils.substring(k8sNet.name(), 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH), StringUtils.substring(port.macAddress().toString(), 0, CLI_MAC_ADDRESS_LENGTH - CLI_MARGIN_LENGTH), port.ipAddress() == null ? "" : port.ipAddress());
}
}
}
Aggregations