|
| 1 | +package org.csanchez.jenkins.plugins.kubernetes; |
| 2 | + |
| 3 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | +import java.util.logging.Level; |
| 7 | +import java.util.logging.Logger; |
| 8 | + |
| 9 | +/** |
| 10 | + * Registers result for slots registration and provides an easy way to unregister. |
| 11 | + */ |
| 12 | +class LimitRegistrationResults { |
| 13 | + private static final Logger LOGGER = Logger.getLogger(LimitRegistrationResults.class.getName()); |
| 14 | + |
| 15 | + private final KubernetesCloud cloud; |
| 16 | + |
| 17 | + private final List<Result> results = new ArrayList<>(); |
| 18 | + |
| 19 | + public LimitRegistrationResults(@NonNull KubernetesCloud cloud) { |
| 20 | + this.cloud = cloud; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Register a slot for the given pod template and number of executors. |
| 25 | + * @return true is the registration was successful |
| 26 | + */ |
| 27 | + public boolean register(@NonNull PodTemplate podTemplate, int numExecutors) { |
| 28 | + var result = new Result( |
| 29 | + KubernetesProvisioningLimits.get().register(cloud, podTemplate, numExecutors), |
| 30 | + podTemplate, |
| 31 | + numExecutors); |
| 32 | + results.add(result); |
| 33 | + return result.success; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Unregister all slots that were registered through this object previously. |
| 38 | + */ |
| 39 | + public void unregister() { |
| 40 | + results.forEach(result -> result.unregister(cloud)); |
| 41 | + } |
| 42 | + |
| 43 | + private static class Result { |
| 44 | + final boolean success; |
| 45 | + final int numExecutors; |
| 46 | + |
| 47 | + @NonNull |
| 48 | + PodTemplate podTemplate; |
| 49 | + |
| 50 | + Result(boolean success, @NonNull PodTemplate podTemplate, int numExecutors) { |
| 51 | + this.success = success; |
| 52 | + this.podTemplate = podTemplate; |
| 53 | + this.numExecutors = numExecutors; |
| 54 | + } |
| 55 | + |
| 56 | + void unregister(KubernetesCloud cloud) { |
| 57 | + if (success) { |
| 58 | + LOGGER.log( |
| 59 | + Level.FINEST, |
| 60 | + () -> "Registration was successful, unregistering slot for podTemplate " + podTemplate.getName() |
| 61 | + + " from cloud " + cloud.name + " with " + numExecutors + " executors"); |
| 62 | + KubernetesProvisioningLimits.get().unregister(cloud, podTemplate, numExecutors); |
| 63 | + } else { |
| 64 | + LOGGER.log( |
| 65 | + Level.FINEST, |
| 66 | + () -> "Registration previously failed, no need to unregister slot for podTemplate " |
| 67 | + + podTemplate.getName() + " from cloud " + cloud.name + " with " + numExecutors |
| 68 | + + " executors"); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments