Skip to content

Commit

Permalink
Change KubeResourceManager API
Browse files Browse the repository at this point in the history
Signed-off-by: David Kornel <[email protected]>
  • Loading branch information
kornys committed Jan 9, 2025
1 parent 1963e9d commit 5874e1e
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 56 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Test {
class Test {
@Test
void testMethod() {
KubeResourceManager.getInstance().createResourceWithWait(
KubeResourceManager.get().createResourceWithWait(
new NamespaceBuilder().withNewMetadata().withName("test").endMetadata().build());
assertNotNull(KubeResourceManager.getKubeCmdClient().get("namespace", "test"));
}
Expand All @@ -79,7 +79,7 @@ class Test {
```
### Register `ResourceType` or `NamespacedResourceType` classes into `KubeResourceManager`
```java
KubeResourceManager.getInstance().setResourceTypes(
KubeResourceManager.get().setResourceTypes(
new NamespaceType(),
new JobType(),
new NetworkPolicyType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public void afterAll(ExtensionContext extensionContext) {
findAnnotation(extensionContext.getRequiredTestClass(), ResourceManager.class);

if (annotation.isPresent() && annotation.get().cleanResources()) {
KubeResourceManager.setTestContext(extensionContext);
KubeResourceManager.getInstance().deleteResources();
KubeResourceManager.get().setTestContext(extensionContext);
KubeResourceManager.get().deleteResources();
}
}

Expand All @@ -50,8 +50,8 @@ public void afterEach(ExtensionContext extensionContext) {
findAnnotation(extensionContext.getRequiredTestClass(), ResourceManager.class);

if (annotation.isPresent() && annotation.get().cleanResources()) {
KubeResourceManager.setTestContext(extensionContext);
KubeResourceManager.getInstance().deleteResources(annotation.get().asyncDeletion());
KubeResourceManager.get().setTestContext(extensionContext);
KubeResourceManager.get().deleteResources(annotation.get().asyncDeletion());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ public class ResourceManagerExtension
implements BeforeAllCallback, BeforeEachCallback, AfterAllCallback, AfterEachCallback {

private ResourceManagerExtension() {
KubeResourceManager.getInstance();
KubeResourceManager.get();
}

@Override
public void beforeAll(ExtensionContext extensionContext) {
KubeResourceManager.setTestContext(extensionContext);
KubeResourceManager.get().setTestContext(extensionContext);
}

@Override
public void beforeEach(ExtensionContext extensionContext) {
KubeResourceManager.setTestContext(extensionContext);
KubeResourceManager.get().setTestContext(extensionContext);
}

@Override
public void afterAll(ExtensionContext extensionContext) {
KubeResourceManager.setTestContext(extensionContext);
KubeResourceManager.get().setTestContext(extensionContext);
}

@Override
public void afterEach(ExtensionContext extensionContext) {
KubeResourceManager.setTestContext(extensionContext);
KubeResourceManager.get().setTestContext(extensionContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class KubeResourceManager {
private static final ThreadLocal<ExtensionContext> TEST_CONTEXT = new ThreadLocal<>();
private static final Map<String, Stack<ResourceItem<?>>> STORED_RESOURCES = new LinkedHashMap<>();

private static String storeYamlPath = null;
private String storeYamlPath = null;

private KubeResourceManager() {
// Private constructor to prevent instantiation
Expand All @@ -72,8 +72,20 @@ private KubeResourceManager() {
* Retrieves the singleton instance of KubeResourceManager.
*
* @return The singleton instance of KubeResourceManager.
* @deprecated Will be removed in future release.
*/
@Deprecated
public static synchronized KubeResourceManager getInstance() {
return get();
}


/**
* Retrieves the singleton instance of KubeResourceManager.
*
* @return The singleton instance of KubeResourceManager.
*/
public static synchronized KubeResourceManager get() {
if (instance == null) {
instance = new KubeResourceManager();
instance.resourceTypes = new ResourceType[]{};
Expand Down Expand Up @@ -110,7 +122,7 @@ public static KubeCmdClient<?> getKubeCmdClient() {
*
* @param context The extension context.
*/
public static void setTestContext(ExtensionContext context) {
public void setTestContext(ExtensionContext context) {
TEST_CONTEXT.set(context);
}

Expand All @@ -119,7 +131,7 @@ public static void setTestContext(ExtensionContext context) {
*
* @return The extension context.
*/
public static ExtensionContext getTestContext() {
public ExtensionContext getTestContext() {
return TEST_CONTEXT.get();
}

Expand Down Expand Up @@ -155,7 +167,7 @@ public final void addDeleteCallback(Consumer<HasMetadata> callback) {
*
* @param path root path for storing
*/
public static void setStoreYamlPath(String path) {
public void setStoreYamlPath(String path) {
storeYamlPath = path;
}

Expand All @@ -164,7 +176,7 @@ public static void setStoreYamlPath(String path) {
*
* @return path
*/
public static String getStoreYamlPath() {
public String getStoreYamlPath() {
return storeYamlPath;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,27 @@ void setupClient() {

@Test
void testCreateDeleteNamespace() {
KubeResourceManager.getInstance().createResourceWithWait(
KubeResourceManager.get().createResourceWithWait(
new NamespaceBuilder().withNewMetadata().withName("test").endMetadata().build());
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test").get());
}

@Test
void testDeleteAllResources() {
KubeResourceManager.getInstance().createResourceWithWait(
KubeResourceManager.get().createResourceWithWait(
new NamespaceBuilder().withNewMetadata().withName("test2").endMetadata().build());
assertNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test").get());
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test2").get());
KubeResourceManager.getInstance().deleteResources();
KubeResourceManager.get().deleteResources();
assertNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test2").get());
}

@Test
void testUpdateResource() {
Namespace ns = new NamespaceBuilder().withNewMetadata().withName("test3").endMetadata().build();
KubeResourceManager.getInstance().createResourceWithWait(ns);
KubeResourceManager.get().createResourceWithWait(ns);
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test3").get());
KubeResourceManager.getInstance().updateResource(ns.edit()
KubeResourceManager.get().updateResource(ns.edit()
.editMetadata().addToLabels(Collections.singletonMap("test-label", "true")).endMetadata().build());
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test3").get()
.getMetadata().getLabels().get("test-label"));
Expand All @@ -75,20 +75,20 @@ void testUpdateResource() {
@Test
void testCreateOrUpdateResource() {
Namespace ns = new NamespaceBuilder().withNewMetadata().withName("test4").endMetadata().build();
KubeResourceManager.getInstance().createResourceWithWait(ns);
KubeResourceManager.get().createResourceWithWait(ns);
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test4").get());
KubeResourceManager.getInstance().createOrUpdateResourceWithWait(ns);
KubeResourceManager.get().createOrUpdateResourceWithWait(ns);
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test4").get());
KubeResourceManager.getInstance().createOrUpdateResourceWithoutWait(ns);
KubeResourceManager.get().createOrUpdateResourceWithoutWait(ns);
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test4").get());
}

@Test
void testLoggingManagedResources() {
// create resources
KubeResourceManager.getInstance().createResourceWithWait(
KubeResourceManager.get().createResourceWithWait(
new NamespaceBuilder().withNewMetadata().withName("test-ns").endMetadata().build());
KubeResourceManager.getInstance().createResourceWithWait(
KubeResourceManager.get().createResourceWithWait(
new ServiceAccountBuilder().withNewMetadata().withName("test-sa").endMetadata().build());

// setup mock logger appender
Expand All @@ -102,7 +102,7 @@ void testLoggingManagedResources() {
testAppender.start();

// print resources
KubeResourceManager.getInstance().printCurrentResources(org.slf4j.event.Level.INFO);
KubeResourceManager.get().printCurrentResources(org.slf4j.event.Level.INFO);
System.out.println(testAppender.getLogEvents());
List<LogEvent> events = testAppender.getLogEvents();

Expand All @@ -114,7 +114,7 @@ void testLoggingManagedResources() {
testAppender.clean();

// print all resources on debug output
KubeResourceManager.getInstance().printAllResources(org.slf4j.event.Level.DEBUG);
KubeResourceManager.get().printAllResources(org.slf4j.event.Level.DEBUG);
events = testAppender.getLogEvents();

assertEquals(4, events.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ void setupClient() {

@Test
void testPodUtils() {
KubeResourceManager.getInstance().createResourceWithWait(
KubeResourceManager.get().createResourceWithWait(
new NamespaceBuilder().withNewMetadata().withName("test").endMetadata().build());
KubeResourceManager.getInstance().createResourceWithoutWait(
KubeResourceManager.get().createResourceWithoutWait(
new PodBuilder()
.withNewMetadata()
.withName("test-pod")
Expand Down Expand Up @@ -71,7 +71,7 @@ void testPodUtils() {

@Test
void testKubeUtils() {
KubeResourceManager.getInstance().createResourceWithWait(
KubeResourceManager.get().createResourceWithWait(
new NamespaceBuilder().withNewMetadata().withName("test").endMetadata().build());

KubeUtils.labelNamespace("test", "test-label", "true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ protected void setExec(Exec exec) {

private synchronized KubernetesClient getKubeClient() {
if (kubeClient == null) {
KubeResourceManager resourceManager = KubeResourceManager.getInstance();
kubeClient = resourceManager.getKubeClient().getClient();
KubeResourceManager resourceManager = KubeResourceManager.get();
kubeClient = KubeResourceManager.getKubeClient().getClient();
if (kubeClient == null) {
throw new IllegalStateException("KubeClient is not available");
}
Expand All @@ -219,8 +219,8 @@ private synchronized KubernetesClient getKubeClient() {

private synchronized KubeCmdClient<?> getKubeCmdClient() {
if (kubeCmdClient == null) {
final KubeResourceManager resourceManager = KubeResourceManager.getInstance();
kubeCmdClient = resourceManager.getKubeCmdClient();
final KubeResourceManager resourceManager = KubeResourceManager.get();
kubeCmdClient = KubeResourceManager.getKubeCmdClient();
if (kubeCmdClient == null) {
throw new IllegalStateException("KubeCmdClient is not available");
}
Expand Down Expand Up @@ -411,14 +411,14 @@ private void deployScraperPod() {
.endContainer()
.endSpec()
.build();
KubeResourceManager.getInstance().createResourceWithWait(scraperPod);
KubeResourceManager.get().createResourceWithWait(scraperPod);
}

/**
* Delete own scraper pod
*/
private void deleteScraperPod() {
KubeResourceManager.getInstance().deleteResource(
KubeResourceManager.get().deleteResource(
new PodBuilder()
.withNewMetadata()
.withName(this.scraperPodName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@ public abstract class AbstractIT {

static {
// Register resources which KRM uses for handling instead of native status check
KubeResourceManager.getInstance().setResourceTypes(
KubeResourceManager.get().setResourceTypes(
new NamespaceType(),
new ServiceAccountType(),
new DeploymentType()
);

// Allow storing yaml files
KubeResourceManager.setStoreYamlPath(LOG_DIR.toString());
KubeResourceManager.get().setStoreYamlPath(LOG_DIR.toString());

// Register callback which are called with every create resource method for every resource
KubeResourceManager.getInstance().addCreateCallback(r -> {
KubeResourceManager.get().addCreateCallback(r -> {
isCreateHandlerCalled.set(true);
if (r.getKind().equals("Namespace")) {
KubeUtils.labelNamespace(r.getMetadata().getName(), "test-label", "true");
}
});

// Register callback which are called with every delete resource method for every resource
KubeResourceManager.getInstance().addDeleteCallback(r -> {
KubeResourceManager.get().addDeleteCallback(r -> {
isDeleteHandlerCalled.set(true);
if (r.getKind().equals("Namespace")) {
LoggerUtils.logResource("Deleted", r);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void testCreateResourcesFromYaml() throws IOException {
List<HasMetadata> resources = KubeResourceManager.getKubeClient()
.readResourcesFromFile(getClass().getClassLoader().getResourceAsStream("resources.yaml"));

KubeResourceManager.getInstance().createResourceWithWait(resources.toArray(new HasMetadata[0]));
KubeResourceManager.get().createResourceWithWait(resources.toArray(new HasMetadata[0]));

assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName(nsName4).get());
assertNotNull(KubeResourceManager.getKubeClient().getClient().serviceAccounts()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public final class KubeResourceManagerCleanerIT extends AbstractIT {

@BeforeAll
void setupAll() {
KubeResourceManager.getInstance().createResourceWithWait(
KubeResourceManager.get().createResourceWithWait(
new NamespaceBuilder().withNewMetadata().withName(nsName1).endMetadata().build());
}

@BeforeEach
void setupEach() {
KubeResourceManager.getInstance().createResourceWithWait(
KubeResourceManager.get().createResourceWithWait(
new NamespaceBuilder().withNewMetadata().withName(nsName2).endMetadata().build());
}

Expand All @@ -51,12 +51,12 @@ void afterAll() {
@Test
void createResource() {
Namespace ns = new NamespaceBuilder().withNewMetadata().withName(nsName3).endMetadata().build();
KubeResourceManager.getInstance().createResourceWithWait(ns);
KubeResourceManager.get().createResourceWithWait(ns);
assertTrue(isCreateHandlerCalled.get());
KubeResourceManager.getInstance().createOrUpdateResourceWithWait(ns);
KubeResourceManager.get().createOrUpdateResourceWithWait(ns);
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName(nsName3).get()
.getMetadata().getLabels().get("test-label"));
KubeResourceManager.getInstance().printAllResources(Level.INFO);
KubeResourceManager.get().printAllResources(Level.INFO);
}

@Test
Expand All @@ -78,6 +78,6 @@ void testKubeCmdClientNamespacesExists() {
void testCreateMultipleResourcesAsync() throws IOException {
List<HasMetadata> resources = KubeResourceManager.getKubeClient()
.readResourcesFromFile(getClass().getClassLoader().getResourceAsStream("metrics-example.yaml"));
KubeResourceManager.getInstance().createOrUpdateResourceAsyncWait(resources.toArray(new HasMetadata[0]));
KubeResourceManager.get().createOrUpdateResourceAsyncWait(resources.toArray(new HasMetadata[0]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ public final class KubeResourceManagerIT extends AbstractIT {

@BeforeEach
void setupEach() {
KubeResourceManager.getInstance().createResourceWithWait(ns1);
KubeResourceManager.get().createResourceWithWait(ns1);
}

@AfterEach
void afterEach() {
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName(nsName2).get());
KubeResourceManager.getInstance().deleteResources();
KubeResourceManager.get().deleteResources();
}

@Test
void createResource() {
KubeResourceManager.getInstance().createResourceWithWait(
KubeResourceManager.get().createResourceWithWait(
new NamespaceBuilder().withNewMetadata().withName("test2").endMetadata().build());
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName(nsName1).get());
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName(nsName2).get());
KubeResourceManager.getInstance().deleteResource(false, ns1);
KubeResourceManager.get().deleteResource(false, ns1);
assertTrue(isDeleteHandlerCalled.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void testCollectFromMultipleNamespacesWithDifferentResources() {

int deploymentReplicas = 2;

KubeResourceManager.getInstance().createResourceWithWait(
KubeResourceManager.get().createResourceWithWait(
new NamespaceBuilder()
.editOrNewMetadata()
.withName(namespaceName1)
Expand All @@ -79,7 +79,7 @@ void testCollectFromMultipleNamespacesWithDifferentResources() {
.build()
);

KubeResourceManager.getInstance().createResourceWithWait(
KubeResourceManager.get().createResourceWithWait(
new DeploymentBuilder()
.editOrNewMetadata()
.withNamespace(namespaceName1)
Expand Down
Loading

0 comments on commit 5874e1e

Please sign in to comment.