Skip to content

Commit bf30b66

Browse files
Revert "- MaintenanceLinkTest issues are resolved, completely. - New: CloudUuidAction - a minimal action class just for containing the duplicate cloud's UUID. - CloudUuidStore - helper for dealing with duplicate clouds. - New targetKey format for duplicate clouds - CLOUD:cloud-name:cloud-uuid. While non duplicate clouds' format remains as is. - Saving maintenance-config.xml UUID directory if the cloud is found to have duplicates. - Fixed deletion bugs in MaintenanceLink."
This reverts commit 1b8ba9d
1 parent c29ef42 commit bf30b66

File tree

10 files changed

+50
-300
lines changed

10 files changed

+50
-300
lines changed

src/main/java/com/sap/prd/jenkins/plugins/agent_maintenance/CloudMaintenanceActionFactory.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import hudson.slaves.Cloud;
77
import java.util.Collection;
88
import java.util.Collections;
9-
import java.util.logging.Level;
10-
import java.util.logging.Logger;
119
import jenkins.model.TransientActionFactory;
1210

1311
/**
@@ -16,21 +14,13 @@
1614
@Extension
1715
public class CloudMaintenanceActionFactory extends TransientActionFactory<Cloud> {
1816

19-
private static final Logger LOGGER = Logger.getLogger(CloudMaintenanceActionFactory.class.getName());
20-
2117
@NonNull
2218
@Override
2319
public Collection<? extends Action> createFor(@NonNull Cloud target) {
24-
try {
25-
String uuid = CloudUuidStore.getInstance().getOrCreateUuid(target);
26-
MaintenanceTarget mt = new MaintenanceTarget(MaintenanceTarget.TargetType.CLOUD, target.name, uuid);
27-
MaintenanceAction action = new MaintenanceAction(mt);
20+
MaintenanceTarget mt = new MaintenanceTarget(MaintenanceTarget.TargetType.CLOUD, target.name);
21+
MaintenanceAction action = new MaintenanceAction(mt);
2822

29-
return Collections.singletonList(action);
30-
} catch (Exception e) {
31-
LOGGER.log(Level.WARNING, "Failed to resolve UUID for cloud: " + target.name, e);
32-
return Collections.emptyList();
33-
}
23+
return Collections.singletonList(action);
3424
}
3525

3626
@Override

src/main/java/com/sap/prd/jenkins/plugins/agent_maintenance/CloudMaintenanceProvisioningListener.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ public class CloudMaintenanceProvisioningListener extends CloudProvisioningListe
1717
@Override
1818
public CauseOfBlockage canProvision(Cloud cloud, Cloud.CloudState state, int numExecutors) {
1919
try {
20-
String uuid = CloudUuidStore.getInstance().getUuidIfPresent(cloud);
21-
MaintenanceTarget target = new MaintenanceTarget(MaintenanceTarget.TargetType.CLOUD, cloud.name, uuid);
22-
20+
MaintenanceTarget target = new MaintenanceTarget(MaintenanceTarget.TargetType.CLOUD, cloud.name);
2321
LOGGER.log(Level.FINER, "Checking for Maintenance Window for cloud {0}", cloud.name);
2422

2523
// Triggers automatic cleanup of expired windows.

src/main/java/com/sap/prd/jenkins/plugins/agent_maintenance/CloudUuidAction.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/main/java/com/sap/prd/jenkins/plugins/agent_maintenance/CloudUuidStore.java

Lines changed: 0 additions & 139 deletions
This file was deleted.

src/main/java/com/sap/prd/jenkins/plugins/agent_maintenance/MaintenanceHelper.java

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ public class MaintenanceHelper {
3434

3535
private static final MaintenanceHelper INSTANCE = new MaintenanceHelper();
3636

37-
private static final CloudUuidStore CLOUD_UUID_STORE = CloudUuidStore.getInstance();
38-
3937
private final Map<String, MaintenanceDefinitions> cache = new ConcurrentHashMap<>();
4038

4139
private MaintenanceHelper() {
@@ -82,17 +80,16 @@ private boolean isValidUuid(String id) {
8280
public boolean isValidTarget(String targetKey) throws IOException {
8381
MaintenanceTarget target = MaintenanceTarget.fromKey(targetKey);
8482
String name = target.getName();
85-
String uuid = target.getUuid();
86-
Jenkins j = Jenkins.get();
83+
8784
return switch (target.getType()) {
88-
case AGENT -> j.getComputer(name) != null;
85+
case AGENT -> Jenkins.get().getComputer(name) != null;
8986
case CLOUD -> {
90-
if (uuid == null) {
91-
yield j.getCloud(name) != null;
87+
try {
88+
yield Jenkins.get().getCloud(name) != null;
89+
} catch (Exception e) {
90+
yield Jenkins.get().clouds != null
91+
&& Jenkins.get().clouds.stream().anyMatch(c -> c.name.equals(name));
9292
}
93-
yield j.clouds.stream()
94-
.anyMatch(c -> c.name.equals(name)
95-
&& uuid.equals(CLOUD_UUID_STORE.getUuidIfPresent(c)));
9693
}
9794
};
9895
}
@@ -401,33 +398,24 @@ public void saveMaintenanceWindows(String targetKey, MaintenanceDefinitions md)
401398
private XmlFile getMaintenanceWindowsFile(String targetKey) throws IOException {
402399
MaintenanceTarget target = MaintenanceTarget.fromKey(targetKey);
403400
String name = target.getName();
404-
String uuid = target.getUuid();
405401

406402
File baseDir = getTargetDirectory(target.getType());
407403

408-
if (MaintenanceTarget.TargetType.CLOUD.equals(target.getType())
409-
&& uuid != null) {
410-
File cloudDir = new File(baseDir, name);
411-
File uuidDir = new File(cloudDir, uuid);
412-
if (!uuidDir.mkdirs() && !uuidDir.exists()) {
413-
throw new IOException("Failed to create " + uuid + " directory: " + uuidDir);
414-
}
415-
return new XmlFile(new File(uuidDir, "maintenance-windows.xml"));
416-
}
417-
418404
return new XmlFile(new File(new File(baseDir, name), "maintenance-windows.xml"));
419405
}
420406

421-
public File getTargetDirectory(MaintenanceTarget.TargetType target) throws IOException {
407+
private File getTargetDirectory(MaintenanceTarget.TargetType target) throws IOException {
422408
// jenkins.model.Nodes#getNodesDirectory() is private, so we have to duplicate
423409
// it here.
424410
String type = switch (target) {
425411
case AGENT -> "nodes";
426412
case CLOUD -> "clouds";
427413
};
428414
File targetDir = new File(Jenkins.get().getRootDir(), type);
429-
if (!targetDir.mkdirs() && !targetDir.exists()) {
430-
throw new IOException("Failed to create " + type + " directory: " + targetDir);
415+
if (!targetDir.exists()) {
416+
if (!targetDir.mkdirs() && !targetDir.exists()) {
417+
throw new IOException("Failed to create " + type + " directory: " + targetDir);
418+
}
431419
} else if (!targetDir.isDirectory()) {
432420
throw new IOException(targetDir + " is not a directory");
433421
}
@@ -445,24 +433,21 @@ public void deleteAgent(String targetKey) {
445433
* @param newName new name of the agent
446434
*/
447435
public void renameAgent(String oldName, String newName) {
448-
MaintenanceTarget oldTarget = new MaintenanceTarget(MaintenanceTarget.TargetType.AGENT, oldName);
449-
MaintenanceTarget newTarget = new MaintenanceTarget(MaintenanceTarget.TargetType.AGENT, newName);
450-
MaintenanceDefinitions md = cache.get(oldTarget.toKey());
436+
MaintenanceDefinitions md = cache.get(oldName);
451437
if (md != null) {
452438
LOGGER.log(Level.FINEST, "Persisting existing maintenance windows after agent rename");
453-
cache.remove(oldTarget.toKey());
454-
cache.put(newTarget.toKey(), md);
439+
cache.remove(oldName);
440+
cache.put(newName, md);
455441
try {
456-
saveMaintenanceWindows(newTarget.toKey(), md);
442+
saveMaintenanceWindows(newName, md);
457443
} catch (IOException e) {
458444
LOGGER.log(Level.WARNING, "Failed to persists agent maintenance windows after agent rename {0}", newName);
459445
}
460446
}
461447
}
462448

463449
public void createAgent(String nodeName) {
464-
MaintenanceTarget target = new MaintenanceTarget(MaintenanceTarget.TargetType.AGENT, nodeName);
465-
cache.put(target.toKey(), new MaintenanceDefinitions(new TreeSet<>(), new HashSet<>()));
450+
cache.put(nodeName, new MaintenanceDefinitions(new TreeSet<>(), new HashSet<>()));
466451
}
467452

468453
@Restricted(NoExternalUse.class)
@@ -506,7 +491,6 @@ public boolean injectRetentionStrategy(Computer c) {
506491
public boolean removeRetentionStrategy(Computer c) {
507492
if (c instanceof SlaveComputer computer) {
508493
String computerName = computer.getName();
509-
MaintenanceTarget target = new MaintenanceTarget(MaintenanceTarget.TargetType.AGENT, computerName);
510494
@SuppressWarnings("unchecked")
511495
RetentionStrategy<SlaveComputer> strategy = computer.getRetentionStrategy();
512496
if (strategy instanceof AgentMaintenanceRetentionStrategy maintenanceStrategy) {
@@ -515,8 +499,8 @@ public boolean removeRetentionStrategy(Computer c) {
515499
node.setRetentionStrategy(maintenanceStrategy.getRegularRetentionStrategy());
516500
try {
517501
node.save();
518-
deleteAgent(target.toKey());
519-
XmlFile maintenanceFile = getMaintenanceWindowsFile(target.toKey());
502+
deleteAgent(computerName);
503+
XmlFile maintenanceFile = getMaintenanceWindowsFile(computerName);
520504
if (maintenanceFile.exists()) {
521505
maintenanceFile.delete();
522506
}

0 commit comments

Comments
 (0)