Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,24 @@ static void writeVmMetadata(List<String[]> vmData, String tempDirName, File open
* First we generate a JSON object using {@link #getNetworkDataJsonObjectForNic(NicProfile, List)}, then we write it to a file called "network_data.json".
*/
static void writeNetworkData(List<NicProfile> nics, Map<Long, List<Network.Service>> supportedServices, File openStackFolder) {
if (!needForGeneratingNetworkData(supportedServices)) {
// No NIC has the ConfigDrive provider assigned for Dhcp/Dns, meaning some other provider
// (e.g. a VirtualRouter or a network extension) is responsible for handing out network
// config over the wire. Skip writing network_data.json altogether: writing an empty-but-
// present file here would make cloud-init's DataSourceConfigDrive believe explicit (empty)
// network config was supplied, suppressing its default per-NIC DHCP auto-configuration and
// leaving the guest NIC administratively down.
return;
}

JsonObject finalNetworkData = new JsonObject();
if (needForGeneratingNetworkData(supportedServices)) {
for (NicProfile nic : nics) {
List<Network.Service> supportedService = supportedServices.get(nic.getId());
JsonObject networkData = getNetworkDataJsonObjectForNic(nic, supportedService);

mergeJsonArraysAndUpdateObject(finalNetworkData, networkData, "links", "id", "type");
mergeJsonArraysAndUpdateObject(finalNetworkData, networkData, "networks", "id", "type");
mergeJsonArraysAndUpdateObject(finalNetworkData, networkData, "services", "address", "type");
}
for (NicProfile nic : nics) {
List<Network.Service> supportedService = supportedServices.get(nic.getId());
JsonObject networkData = getNetworkDataJsonObjectForNic(nic, supportedService);

mergeJsonArraysAndUpdateObject(finalNetworkData, networkData, "links", "id", "type");
mergeJsonArraysAndUpdateObject(finalNetworkData, networkData, "networks", "id", "type");
mergeJsonArraysAndUpdateObject(finalNetworkData, networkData, "services", "address", "type");
}

writeFile(openStackFolder, "network_data.json", finalNetworkData.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,19 +644,15 @@ public void testWriteNetworkDataEmptyJson() throws Exception {
folder.create();
File openStackFolder = folder.newFolder("openStack");

// Expected JSON structure
String expectedJson = "{}";

// Action
ConfigDriveBuilder.writeNetworkData(Arrays.asList(nicp), supportedServices, openStackFolder);

// Verify
// Verify: no NIC has the ConfigDrive provider assigned for Dhcp/Dns, so network_data.json
// must not be written at all -- an empty-but-present file would make cloud-init believe
// explicit (empty) network config was supplied, suppressing its default DHCP auto-config
// and leaving the guest NIC administratively down.
File networkDataFile = new File(openStackFolder, "network_data.json");
String content = FileUtils.readFileToString(networkDataFile, StandardCharsets.UTF_8);
JsonObject actualJson = new JsonParser().parse(content).getAsJsonObject();
JsonObject expectedJsonObject = new JsonParser().parse(expectedJson).getAsJsonObject();

Assert.assertEquals(expectedJsonObject, actualJson);
Assert.assertFalse(networkDataFile.exists());
folder.delete();
}
}
Loading