Skip to content

Commit

Permalink
Add createOrUpdate functionality into KubeResourceManager (#49)
Browse files Browse the repository at this point in the history
* Add createOrUpdate functionality into KubeResourceManager

Signed-off-by: David Kornel <[email protected]>
  • Loading branch information
kornys authored May 6, 2024
1 parent c5f7e63 commit 7146063
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.Collections;

import io.fabric8.kubernetes.api.model.HasMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -47,4 +48,23 @@ public static void logSeparator() {
public static void logSeparator(String delimiterChar, int length) {
LOGGER.info(String.join("", Collections.nCopies(length, delimiterChar)));
}

/**
* Log resource with correct format
*
* @param operation operation with resource
* @param resource resource
* @param <T> The type of the resources.
*/
public static <T extends HasMetadata> void logResource(String operation, T resource) {
if (resource.getMetadata().getNamespace() == null) {
LOGGER.info(LoggerUtils.RESOURCE_LOGGER_PATTERN,
operation, resource.getKind(),
resource.getMetadata().getName());
} else {
LOGGER.info(LoggerUtils.RESOURCE_WITH_NAMESPACE_LOGGER_PATTERN,
operation,
resource.getKind(), resource.getMetadata().getName(), resource.getMetadata().getNamespace());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public final <T extends HasMetadata> void pushToStack(T resource) {
*/
@SafeVarargs
public final <T extends HasMetadata> void createResourceWithoutWait(T... resources) {
createResource(false, resources);
createOrUpdateResource(false, false, resources);
}

/**
Expand All @@ -168,33 +168,53 @@ public final <T extends HasMetadata> void createResourceWithoutWait(T... resourc
*/
@SafeVarargs
public final <T extends HasMetadata> void createResourceWithWait(T... resources) {
createResource(true, resources);
createOrUpdateResource(true, false, resources);
}

/**
* Creates or updates resources and waits for readiness.
* @param resources The resources to create.
* @param <T> The type of the resources.
*/
@SafeVarargs
public final <T extends HasMetadata> void createOrUpdateResourceWithWait(T... resources) {
createOrUpdateResource(true, true, resources);
}

/**
* Creates or updates resources.
* @param resources The resources to create.
* @param <T> The type of the resources.
*/
@SafeVarargs
public final <T extends HasMetadata> void createOrUpdateResourceWithoutWait(T... resources) {
createOrUpdateResource(false, true, resources);
}

/**
* Creates resources with or without waiting for readiness.
* @param waitReady Flag indicating whether to wait for readiness.
* @param allowUpdate Flag indicating if update resource is allowed
* @param resources The resources to create.
* @param <T> The type of the resources.
*/
@SafeVarargs
private <T extends HasMetadata> void createResource(boolean waitReady, T... resources) {
private <T extends HasMetadata> void createOrUpdateResource(boolean waitReady,
boolean allowUpdate,
T... resources) {
for (T resource : resources) {
ResourceType<T> type = findResourceType(resource);
pushToStack(resource);

if (resource.getMetadata().getNamespace() == null) {
LOGGER.info(LoggerUtils.RESOURCE_LOGGER_PATTERN,
"Creating", resource.getKind(), resource.getMetadata().getName());
} else {
LOGGER.info(LoggerUtils.RESOURCE_WITH_NAMESPACE_LOGGER_PATTERN,
"Creating", resource.getKind(), resource.getMetadata().getName(),
resource.getMetadata().getNamespace());
}

if (type == null) {
// Generic create for any resource
client.getClient().resource(resource).create();
if (allowUpdate && client.getClient().resource(resource).get() != null) {
LoggerUtils.logResource("Updating", resource);
client.getClient().resource(resource).update();
} else {
LoggerUtils.logResource("Creating", resource);
client.getClient().resource(resource).create();
}
if (waitReady) {
assertTrue(waitResourceCondition(resource, new ResourceCondition<>(p -> {
try {
Expand All @@ -208,7 +228,13 @@ private <T extends HasMetadata> void createResource(boolean waitReady, T... reso
}
} else {
// Create for typed resource implementing ResourceType
type.create(resource);
if (allowUpdate && client.getClient().resource(resource).get() != null) {
LoggerUtils.logResource("Updating", resource);
type.update(resource);
} else {
LoggerUtils.logResource("Creating", resource);
type.create(resource);
}
if (waitReady) {
assertTrue(waitResourceCondition(resource, ResourceCondition.readiness(type)),
String.format("Timed out waiting for %s/%s in %s to be ready", resource.getKind(),
Expand All @@ -227,14 +253,7 @@ private <T extends HasMetadata> void createResource(boolean waitReady, T... reso
public final <T extends HasMetadata> void deleteResource(T... resources) {
for (T resource : resources) {
ResourceType<T> type = findResourceType(resource);
if (resource.getMetadata().getNamespace() == null) {
LOGGER.info(LoggerUtils.RESOURCE_LOGGER_PATTERN,
"Deleting", resource.getKind(), resource.getMetadata().getName());
} else {
LOGGER.info(LoggerUtils.RESOURCE_WITH_NAMESPACE_LOGGER_PATTERN,
"Deleting", resource.getKind(), resource.getMetadata().getName(),
resource.getMetadata().getNamespace());
}
LoggerUtils.logResource("Deleting", resource);
try {
if (type == null) {
client.getClient().resource(resource).delete();
Expand Down Expand Up @@ -267,14 +286,7 @@ public final <T extends HasMetadata> void deleteResource(T... resources) {
@SafeVarargs
public final <T extends HasMetadata> void updateResource(T... resources) {
for (T resource : resources) {
if (resource.getMetadata().getNamespace() == null) {
LOGGER.info(LoggerUtils.RESOURCE_LOGGER_PATTERN,
"Updating", resource.getKind(), resource.getMetadata().getName());
} else {
LOGGER.info(LoggerUtils.RESOURCE_WITH_NAMESPACE_LOGGER_PATTERN,
"Updating", resource.getKind(), resource.getMetadata().getName(),
resource.getMetadata().getNamespace());
}
LoggerUtils.logResource("Updating", resource);
ResourceType<T> type = findResourceType(resource);
if (type != null) {
type.update(resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package io.skodjob.testframe.test.integration;

import io.fabric8.kubernetes.api.model.Namespace;
import io.fabric8.kubernetes.api.model.NamespaceBuilder;
import io.skodjob.testframe.annotations.ResourceManager;
import io.skodjob.testframe.annotations.TestVisualSeparator;
Expand Down Expand Up @@ -44,8 +45,9 @@ void afterAll() {

@Test
void createResource() {
KubeResourceManager.getInstance().createResourceWithWait(
new NamespaceBuilder().withNewMetadata().withName("test3").endMetadata().build());
Namespace ns = new NamespaceBuilder().withNewMetadata().withName("test3").endMetadata().build();
KubeResourceManager.getInstance().createResourceWithWait(ns);
KubeResourceManager.getInstance().createOrUpdateResourceWithWait(ns);
}

@Test
Expand Down

0 comments on commit 7146063

Please sign in to comment.