Skip to content

Commit

Permalink
Change ResoruceCondition class to record type (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
kornys authored May 16, 2024
1 parent 3508591 commit bebed60
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@
*/
public class KubeResourceManager {
private static final Logger LOGGER = LogManager.getLogger(KubeResourceManager.class);

private static KubeResourceManager instance;
private static KubeClient client;
private static KubeCmdClient<?> kubeCmdClient;
private static ThreadLocal<ExtensionContext> testContext = new ThreadLocal<>();
private static final Map<String, Stack<ResourceItem<?>>> STORED_RESOURCES = new LinkedHashMap<>();
private ResourceType<?>[] resourceTypes;
private List<Consumer<HasMetadata>> createCallbacks = new LinkedList<>();
private List<Consumer<HasMetadata>> deleteCallbacks = new LinkedList<>();
private final List<Consumer<HasMetadata>> createCallbacks = new LinkedList<>();
private final List<Consumer<HasMetadata>> deleteCallbacks = new LinkedList<>();

private static final ThreadLocal<ExtensionContext> TEST_CONTEXT = new ThreadLocal<>();
private static final Map<String, Stack<ResourceItem<?>>> STORED_RESOURCES = new LinkedHashMap<>();

private KubeResourceManager() {
// Private constructor to prevent instantiation
Expand Down Expand Up @@ -90,15 +92,15 @@ public static KubeCmdClient<?> getKubeCmdClient() {
* @param context The extension context.
*/
public static void setTestContext(ExtensionContext context) {
testContext.set(context);
TEST_CONTEXT.set(context);
}

/**
* Retrieves the test context.
* @return The extension context.
*/
public static ExtensionContext getTestContext() {
return testContext.get();
return TEST_CONTEXT.get();
}

/**
Expand Down Expand Up @@ -348,11 +350,11 @@ public final <T extends HasMetadata> boolean waitResourceCondition(T resource, R
boolean[] resourceReady = new boolean[1];

Wait.until(String.format("Resource condition: %s to be fulfilled for resource %s/%s",
condition.getConditionName(), resource.getKind(), resource.getMetadata().getName()),
condition.conditionName(), resource.getKind(), resource.getMetadata().getName()),
TestFrameConstants.GLOBAL_POLL_INTERVAL_MEDIUM, TestFrameConstants.GLOBAL_TIMEOUT,
() -> {
T res = getKubeClient().getClient().resource(resource).get();
resourceReady[0] = condition.getPredicate().test(res);
resourceReady[0] = condition.predicate().test(res);
return resourceReady[0];
},
() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,11 @@
/**
* Represents a condition that can be applied to Kubernetes resources.
*
* @param predicate predicate function
* @param conditionName conditionName
* @param <T> Type of Kubernetes resource.
*/
public class ResourceCondition<T extends HasMetadata> {
private final Predicate<T> predicate;
private final String conditionName;

/**
* Constructs a ResourceCondition with the given predicate and condition name.
*
* @param predicate The predicate representing the condition.
* @param conditionName The name of the condition.
*/
public ResourceCondition(Predicate<T> predicate, String conditionName) {
this.predicate = predicate;
this.conditionName = conditionName;
}

/**
* Gets the name of the condition.
*
* @return The name of the condition.
*/
public String getConditionName() {
return conditionName;
}

/**
* Gets the predicate representing the condition.
*
* @return The predicate representing the condition.
*/
public Predicate<T> getPredicate() {
return predicate;
}
public record ResourceCondition<T extends HasMetadata>(Predicate<T> predicate, String conditionName) {

/**
* Creates a ResourceCondition representing readiness of a resource of the given type.
Expand Down

0 comments on commit bebed60

Please sign in to comment.