/** * {@inheritDoc} */ @Override public void from(io.cattle.platform.core.model.Nic from) { setId(from.getId()); setName(from.getName()); setAccountId(from.getAccountId()); setKind(from.getKind()); setUuid(from.getUuid()); setDescription(from.getDescription()); setState(from.getState()); setCreated(from.getCreated()); setRemoved(from.getRemoved()); setRemoveTime(from.getRemoveTime()); setData(from.getData()); setInstanceId(from.getInstanceId()); setNetworkId(from.getNetworkId()); setDeviceNumber(from.getDeviceNumber()); setMacAddress(from.getMacAddress()); }
protected Instance getInstanceForPrimaryIp(IpAddress ipAddress) { if (IpAddressConstants.ROLE_PRIMARY.equals(ipAddress.getRole())) { for (Nic nic : getObjectManager().mappedChildren(ipAddress, Nic.class)) { if (nic.getDeviceNumber() != null && nic.getDeviceNumber() == 0) { Instance instance = getObjectManager().loadResource(Instance.class, nic.getInstanceId()); if (instance != null) { return instance; } } } } return null; }
@Override public IpAddress mapNewIpAddress(Nic nic, Object key, Object... values) { if ( nic.getNetworkId() == null ) { throw new IllegalStateException("Can not map new IP to nic with no network assigned to nic"); } Map<Object,Object> inputProperties = key == null ? Collections.emptyMap() : CollectionUtils.asMap(key, values); Map<Object,Object> properties = CollectionUtils.asMap((Object)IP_ADDRESS.ACCOUNT_ID, nic.getAccountId()); properties.putAll(inputProperties); IpAddress ipAddress = objectManager.create(IpAddress.class, objectManager.convertToPropertiesFor(IpAddress.class, properties)); objectManager.create(IpAddressNicMap.class, IP_ADDRESS_NIC_MAP.IP_ADDRESS_ID, ipAddress.getId(), IP_ADDRESS_NIC_MAP.NIC_ID, nic.getId()); return ipAddress; }
protected void populateNic(IdFormatter idFormatter, Map<String, Object> instanceData, Nic nic, Network network) { Integer deviceNumber = nic.getDeviceNumber(); String mac = nic.getMacAddress(); if (mac == null) { return; } @SuppressWarnings("unchecked") Map<String, Object> data = (Map<String, Object>) CollectionUtils.getNestedValue(instanceData, "network", "interfaces", "macs", mac); if (data == null) { data = new HashMap<String, Object>(); setNestedValue(instanceData, data, "network", "interfaces", "macs", mac); } else { return; } data.put("device-number", deviceNumber); data.put("mac", nic.getMacAddress()); data.put("owner-id", formatId(idFormatter, Instance.class, nic.getInstanceId())); // TODO don't have a security group concept yet data.put("security-group-ids", ""); data.put("security-groups", ""); data.put("vpc-id", formatId(idFormatter, Network.class, network.getId())); data.put("vpc-ipv4-cidr-block", DataAccessor.fieldString(network, NetworkConstants.FIELD_CIDR)); if (deviceNumber != null && deviceNumber.intValue() == 0) { instanceData.put("mac", nic.getMacAddress()); setNestedValue(instanceData, HostnameGenerator.getServicesDomain(network), "services", "domain"); } }
protected Set<Long> createNicsFromIds(Instance instance, List<Nic> nics, Map<String, Object> data, List<Long> networkIds) { Set<Long> nicIds = new TreeSet<Long>(); int deviceId = 0; if (networkIds != null) { for (int i = 0; i < networkIds.size(); i++) { Number createId = networkIds.get(i); if (createId == null) { deviceId++; continue; } Nic newNic = null; for (Nic nic : nics) { if (nic.getNetworkId() == createId.longValue()) { newNic = nic; break; } } if (newNic == null) { newNic = objectManager.create(Nic.class, NIC.ACCOUNT_ID, instance.getAccountId(), NIC.NETWORK_ID, createId, NIC.INSTANCE_ID, instance .getId(), NIC.DEVICE_NUMBER, deviceId); } deviceId++; processManager.executeStandardProcess(StandardProcess.CREATE, newNic, data); nicIds.add(newNic.getId()); } } return nicIds; }
protected void populateIps(IdFormatter idFormatter, Map<Long, String> primaryIps, Map<String, Object> instanceMetadata, Instance instance, Nic nic, Network network, Subnet subnet, IpAddress localIp) { boolean firstNic = (org.apache.commons.lang3.ObjectUtils.equals(nic.getDeviceNumber(), 0)); boolean primaryIp = localIp == null ? firstNic : IpAddressConstants.ROLE_PRIMARY.equals(localIp.getRole()); String addressKey = localIpAddress == null ? nic.getMacAddress() : localIpAddress; .getMacAddress());
protected Network getInstancePrimaryNetwork(Instance instance) { Nic primaryNic = getPrimaryNic(instance.getId()); return objectManager.loadResource(Network.class, primaryNic.getNetworkId()); }
@Override public HandlerResult handle(ProcessState state, ProcessInstance process) { Nic nic = (Nic) state.getResource(); Instance instance = objectManager.loadResource(Instance.class, nic.getInstanceId()); if (instance == null) { return null; } Map<String, Object> labels = new HashMap<>(DataAccessor.fieldMap(instance, InstanceConstants.FIELD_LABELS)); if (!"true".equals(labels.get(SystemLabels.LABEL_VM))) { return null; } Map<String, Object> metadata = metadataService.getMetadataForInstance(instance, idFormatter); Map<String, Object> osMetadata = metadataService.getOsMetadata(instance, metadata); try { labels.put(SystemLabels.LABEL_VM_METADATA, jsonMapper.writeValueAsString(metadata)); } catch (IOException e) { log.error("Failed to marshall metadata", e); } try { labels.put(SystemLabels.LABEL_VM_OS_METADATA, jsonMapper.writeValueAsString(osMetadata)); } catch (IOException e) { log.error("Failed to marshall metadata", e); } objectManager.setFields(instance, InstanceConstants.FIELD_LABELS, labels); return null; }
protected void network(Instance instance) { List<Nic> nics = getObjectManager().children(instance, Nic.class); for (Nic nic : nics) { if (nic.getRemoved() == null && !nic.getState().equals(CommonStatesConstants.REMOVED)) { deactivate(nic, null); } } for (Port port : getObjectManager().children(instance, Port.class)) { if (port.getRemoved() == null && !port.getState().equals(CommonStatesConstants.REMOVED)) { deactivate(port, null); } } for (InstanceLink link : getObjectManager().children(instance, InstanceLink.class, InstanceLinkConstants.FIELD_INSTANCE_ID)) { if (link.getRemoved() == null && !link.getState().equals(CommonStatesConstants.REMOVED)) { deactivate(link, null); } } }
protected void assignPrimaryIpAddress(Instance instance, Map<String, Object> resultData) { int min = Integer.MAX_VALUE; IpAddress ip = null; IpAddress fallBackIp = null; for (Nic nic : getObjectManager().children(instance, Nic.class)) { if (nic.getDeviceNumber().intValue() < min) { min = nic.getDeviceNumber(); ip = ipAddressDao.getPrimaryIpAddress(nic); if (ip == null) { List<IpAddress> ips = getObjectManager().mappedChildren(nic, IpAddress.class); if (ips.size() > 0) { fallBackIp = ips.get(0); } } } } String address = null; if (ip == null) { address = fallBackIp == null ? null : fallBackIp.getAddress(); } else { address = ip.getAddress(); } if (address != null) { resultData.put(InstanceConstants.FIELD_PRIMARY_IP_ADDRESS, address); } }
@Override protected List<Object> map(List<Object> input) { result.put(((Nic) input.get(0)).getId(), (IpAddress) input.get(1)); return input; } };
protected String assignMacAddress(Network network, Nic nic) { String mac = nic.getMacAddress(); if (mac != null) { return mac; } PooledResource resource = poolManager.allocateOneResource(network, nic, new PooledResourceOptions().withQualifier(ResourcePoolConstants.MAC)); if (resource == null) { throw new ExecutionException("MAC allocation error", "Failed to allocate MAC from network", nic); } return resource.getName(); }
protected IpAddress getIpAddress(Nic nic, Network network) { IpAddress ipAddress = ipAddressDao.getPrimaryIpAddress(nic); if (ipAddress == null && networkService.shouldAssignIpAddress(network)) { ipAddress = ipAddressDao.mapNewIpAddress(nic, IP_ADDRESS.ROLE, IpAddressConstants.ROLE_PRIMARY, IP_ADDRESS.NETWORK_ID, nic.getNetworkId()); } for (IpAddressNicMap map : mapDao.findNonRemoved(IpAddressNicMap.class, Nic.class, nic.getId())) { getObjectProcessManager().executeStandardProcess(StandardProcess.CREATE, map, null); } if (ipAddress != null) { getObjectProcessManager().executeStandardProcess(StandardProcess.CREATE, ipAddress, null); } return ipAddress; }
@Override public void handleRequest(Request req) throws IOException { Instance instance = objectManager.findAny(Instance.class, INSTANCE.AGENT_ID, req.getClient().getResourceId()); if (instance == null) { return; } Nic primaryNic = nicDao.getPrimaryNic(instance); if (primaryNic == null) { return; } String key = dataDao.getOrCreate(String.format("psk.%d", primaryNic.getNetworkId()), false, new Callable<String>() { @Override public String call() throws Exception { return randomKey(); } }); req.getOutputStream().write(key.getBytes("UTF-8")); }
Integer device = nic.getDeviceNumber();
@Override public HandlerResult handle(ProcessState state, ProcessInstance process) { /* Release requested IPs on delete, not on purge */ Nic nic = (Nic) state.getResource(); for (IpAddressNicMap map : mapDao.findToRemove(IpAddressNicMap.class, Nic.class, nic.getId())) { IpAddress ipAddress = getObjectManager().loadResource(IpAddress.class, map.getIpAddressId()); if (DockerIpAddressConstants.KIND_DOCKER.equals(ipAddress.getKind())) { /* Deactivate Docker IPs on stop */ deactivate(ipAddress, state.getData()); } } return null; }
@Override public HandlerResult handle(ProcessState state, ProcessInstance process) { Nic nic = (Nic) state.getResource(); Network network = loadResource(Network.class, nic.getNetworkId()); for (IpAddressNicMap map : mapDao.findToRemove(IpAddressNicMap.class, Nic.class, nic.getId())) { IpAddress ipAddress = getObjectManager().loadResource(IpAddress.class, map.getIpAddressId()); deactivateThenRemove(ipAddress, state.getData()); } if (network != null) { poolManager.releaseResource(network, nic, new PooledResourceOptions().withQualifier(ResourcePoolConstants.MAC)); } return new HandlerResult(NIC.MAC_ADDRESS, new Object[] { null }).withShouldContinue(true); }
@Override public HandlerResult handle(ProcessState state, ProcessInstance process) { Nic nic = (Nic) state.getResource(); Network network = getObjectManager().loadResource(Network.class, nic.getNetworkId()); if (network == null) { return null; } IpAddress ipAddress = getIpAddress(nic, network); if (ipAddress != null) { activate(ipAddress, state.getData()); } String mac = assignMacAddress(network, nic); return new HandlerResult(NIC.MAC_ADDRESS, mac); }
@Override public HandlerResult handle(ProcessState state, ProcessInstance process) { /* Release requested IPs on delete, not on purge */ Nic nic = (Nic) state.getResource(); for (IpAddressNicMap map : mapDao.findToRemove(IpAddressNicMap.class, Nic.class, nic.getId())) { IpAddress ipAddress = getObjectManager().loadResource(IpAddress.class, map.getIpAddressId()); /* Deactivate to release the IP address */ deactivate(ipAddress, state.getData()); } return null; }
@Override public IpAddress getPrimaryIpAddress(Nic nic) { List<? extends IpAddress> ipAddresses = create() .select(IP_ADDRESS.fields()) .from(IP_ADDRESS) .join(IP_ADDRESS_NIC_MAP) .on(IP_ADDRESS_NIC_MAP.IP_ADDRESS_ID.eq(IP_ADDRESS.ID) .and(IP_ADDRESS_NIC_MAP.NIC_ID.eq(nic.getId()))) .where(IP_ADDRESS_NIC_MAP.REMOVED.isNull()) .fetchInto(IpAddressRecord.class); if (ipAddresses.size() == 1) { return ipAddresses.get(0); } for (IpAddress ipAddress : ipAddresses) { if (IpAddressConstants.ROLE_PRIMARY.equals(ipAddress.getRole())) { return ipAddress; } } return null; }