Skip to content

Commit

Permalink
Update KubeResourceManager api for kubernetes clients (#214)
Browse files Browse the repository at this point in the history
## Description

1. Remove static ability to get clients because clients are not
initialized without singleton is initialized first so it is save to get
it from instance.
2. remove the get.. prefix from kubeClient() and kubeCmdClient()


## Type of Change

Please delete options that are not relevant.

* Breaking change (fix or feature that would cause existing
functionality to not work as expected)

## Checklist

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit/integration tests pass locally with my
changes

Signed-off-by: David Kornel <[email protected]>
  • Loading branch information
kornys authored Jan 22, 2025
1 parent b06f0e8 commit 9a6c828
Show file tree
Hide file tree
Showing 30 changed files with 84 additions and 82 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Test {
void testMethod() {
KubeResourceManager.get().createResourceWithWait(
new NamespaceBuilder().withNewMetadata().withName("test").endMetadata().build());
assertNotNull(KubeResourceManager.getKubeCmdClient().get("namespace", "test"));
assertNotNull(KubeResourceManager.get().kubeCmdClient().get("namespace", "test"));
}
}
//...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static synchronized KubeResourceManager get() {
*
* @return The Kubernetes client.
*/
public static KubeClient getKubeClient() {
public KubeClient kubeClient() {
return client;
}

Expand All @@ -113,7 +113,7 @@ public static KubeClient getKubeClient() {
*
* @return The Kubernetes command-line client.
*/
public static KubeCmdClient<?> getKubeCmdClient() {
public KubeCmdClient<?> kubeCmdClient() {
return kubeCmdClient;
}

Expand Down Expand Up @@ -490,7 +490,7 @@ public final <T extends HasMetadata> boolean waitResourceCondition(T resource, R
condition.conditionName(), resource.getKind(), resource.getMetadata().getName()),
TestFrameConstants.GLOBAL_POLL_INTERVAL_MEDIUM, TestFrameConstants.GLOBAL_TIMEOUT,
() -> {
T res = getKubeClient().getClient().resource(resource).get();
T res = kubeClient().getClient().resource(resource).get();
resourceReady[0] = condition.predicate().test(res);
return resourceReady[0];
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public static void approveInstallPlan(String namespaceName, String installPlanNa
Wait.until("InstallPlan approval", TestFrameConstants.GLOBAL_POLL_INTERVAL_SHORT, 15_000, () -> {
try {
InstallPlan installPlan =
new InstallPlanBuilder(KubeResourceManager.getKubeClient()
new InstallPlanBuilder(KubeResourceManager.get().kubeClient()
.getOpenShiftClient().operatorHub().installPlans()
.inNamespace(namespaceName).withName(installPlanName).get())
.editSpec()
.withApproved()
.endSpec()
.build();

KubeResourceManager.getKubeClient().getOpenShiftClient().operatorHub().installPlans()
KubeResourceManager.get().kubeClient().getOpenShiftClient().operatorHub().installPlans()
.inNamespace(namespaceName).withName(installPlanName).patch(installPlan);
return true;
} catch (Exception ex) {
Expand All @@ -62,7 +62,7 @@ public static void approveInstallPlan(String namespaceName, String installPlanNa
* @return list of not approved install-plans
*/
public static InstallPlan getNonApprovedInstallPlan(String namespaceName, String csvPrefix) {
return KubeResourceManager.getKubeClient().getOpenShiftClient().operatorHub().installPlans()
return KubeResourceManager.get().kubeClient().getOpenShiftClient().operatorHub().installPlans()
.inNamespace(namespaceName).list().getItems().stream()
.filter(installPlan -> !installPlan.getSpec().getApproved()
&& installPlan.getSpec().getClusterServiceVersionNames().toString().contains(csvPrefix))
Expand All @@ -77,11 +77,11 @@ public static InstallPlan getNonApprovedInstallPlan(String namespaceName, String
* @param value label value
*/
public static void labelNamespace(String namespace, String key, String value) {
if (KubeResourceManager.getKubeClient().namespaceExists(namespace)) {
if (KubeResourceManager.get().kubeClient().namespaceExists(namespace)) {
Wait.until(String.format("Namespace %s has label: %s", namespace, key),
TestFrameConstants.GLOBAL_POLL_INTERVAL_1_SEC, TestFrameConstants.GLOBAL_STABILITY_TIME, () -> {
try {
KubeResourceManager.getKubeClient().getClient().namespaces().withName(namespace).edit(n ->
KubeResourceManager.get().kubeClient().getClient().namespaces().withName(namespace).edit(n ->
new NamespaceBuilder(n)
.editMetadata()
.addToLabels(key, value)
Expand All @@ -90,7 +90,7 @@ public static void labelNamespace(String namespace, String key, String value) {
} catch (Exception ex) {
return false;
}
Namespace n = KubeResourceManager.getKubeClient()
Namespace n = KubeResourceManager.get().kubeClient()
.getClient().namespaces().withName(namespace).get();
if (n != null) {
return n.getMetadata().getLabels().get(key) != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static void waitForPodsReady(String namespaceName, boolean containersRead
Wait.until("readiness of all Pods in namespace " + namespaceName,
TestFrameConstants.GLOBAL_POLL_INTERVAL_MEDIUM, READINESS_TIMEOUT,
() -> {
List<Pod> pods = KubeResourceManager.getKubeClient().getClient()
List<Pod> pods = KubeResourceManager.get().kubeClient().getClient()
.pods().inNamespace(namespaceName).list().getItems();
if (pods.isEmpty()) {
LOGGER.debug("There are no existing Pods in Namespace {}", namespaceName);
Expand Down Expand Up @@ -84,7 +84,7 @@ public static void waitForPodsReady(String namespaceName, LabelSelector selector
Wait.until("readiness of all Pods matching " + selector + " in Namespace " + namespaceName,
TestFrameConstants.GLOBAL_POLL_INTERVAL_MEDIUM, READINESS_TIMEOUT,
() -> {
List<Pod> pods = KubeResourceManager.getKubeClient().getClient().pods()
List<Pod> pods = KubeResourceManager.get().kubeClient().getClient().pods()
.inNamespace(namespaceName).withLabelSelector(selector).list().getItems();
if (pods.isEmpty() && expectPodsCount == 0) {
LOGGER.debug("All expected Pods {} in Namespace {} are ready", selector, namespaceName);
Expand Down Expand Up @@ -135,9 +135,9 @@ public static void waitForPodsReadyWithRestart(String namespaceName, LabelSelect
});
} catch (Exception ex) {
LOGGER.warn("Pods {}/{} are not ready. Going to restart them", namespaceName, selector);
KubeResourceManager.getKubeClient().getClient().pods()
KubeResourceManager.get().kubeClient().getClient().pods()
.inNamespace(namespaceName).withLabelSelector(selector).list().getItems().forEach(p ->
KubeResourceManager.getKubeClient().getClient().resource(p).delete());
KubeResourceManager.get().kubeClient().getClient().resource(p).delete());
waitForPodsReady(namespaceName, selector, expectedPodsCount, containersReady, () -> {
});
}
Expand All @@ -152,7 +152,7 @@ public static void waitForPodsReadyWithRestart(String namespaceName, LabelSelect
* @return key value map podName -> uid
*/
public static Map<String, String> podSnapshot(String namespaceName, LabelSelector selector) {
List<Pod> pods = KubeResourceManager.getKubeClient().getClient().pods()
List<Pod> pods = KubeResourceManager.get().kubeClient().getClient().pods()
.inNamespace(namespaceName).withLabelSelector(selector).list().getItems();
return pods.stream()
.collect(
Expand All @@ -174,7 +174,7 @@ public static void verifyThatPodsAreStable(String namespaceName, LabelSelector s
namespaceName, selector, phase),
TestFrameConstants.GLOBAL_POLL_INTERVAL_SHORT, TestFrameConstants.GLOBAL_TIMEOUT,
() -> {
List<Pod> existingPod = KubeResourceManager.getKubeClient().getClient().pods()
List<Pod> existingPod = KubeResourceManager.get().kubeClient().getClient().pods()
.inNamespace(namespaceName).withLabelSelector(selector).list().getItems();
LOGGER.debug("Considering the following Pods {}", existingPod.stream()
.map(p -> p.getMetadata().getName()).toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,46 +41,46 @@ public class KubeResourceManagerTest {

@BeforeEach
void setupClient() {
KubeResourceManager.getKubeClient().testReconnect(kubernetesClient.getConfiguration());
KubeResourceManager.get().kubeClient().testReconnect(kubernetesClient.getConfiguration());
}

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

@Test
void testDeleteAllResources() {
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());
assertNull(KubeResourceManager.get().kubeClient().getClient().namespaces().withName("test").get());
assertNotNull(KubeResourceManager.get().kubeClient().getClient().namespaces().withName("test2").get());
KubeResourceManager.get().deleteResources();
assertNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test2").get());
assertNull(KubeResourceManager.get().kubeClient().getClient().namespaces().withName("test2").get());
}

@Test
void testUpdateResource() {
Namespace ns = new NamespaceBuilder().withNewMetadata().withName("test3").endMetadata().build();
KubeResourceManager.get().createResourceWithWait(ns);
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test3").get());
assertNotNull(KubeResourceManager.get().kubeClient().getClient().namespaces().withName("test3").get());
KubeResourceManager.get().updateResource(ns.edit()
.editMetadata().addToLabels(Collections.singletonMap("test-label", "true")).endMetadata().build());
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test3").get()
assertNotNull(KubeResourceManager.get().kubeClient().getClient().namespaces().withName("test3").get()
.getMetadata().getLabels().get("test-label"));
}

@Test
void testCreateOrUpdateResource() {
Namespace ns = new NamespaceBuilder().withNewMetadata().withName("test4").endMetadata().build();
KubeResourceManager.get().createResourceWithWait(ns);
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test4").get());
assertNotNull(KubeResourceManager.get().kubeClient().getClient().namespaces().withName("test4").get());
KubeResourceManager.get().createOrUpdateResourceWithWait(ns);
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test4").get());
assertNotNull(KubeResourceManager.get().kubeClient().getClient().namespaces().withName("test4").get());
KubeResourceManager.get().createOrUpdateResourceWithoutWait(ns);
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test4").get());
assertNotNull(KubeResourceManager.get().kubeClient().getClient().namespaces().withName("test4").get());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class UtilsTest {

@BeforeEach
void setupClient() {
KubeResourceManager.getKubeClient().testReconnect(kubernetesClient.getConfiguration());
KubeResourceManager.get().kubeClient().testReconnect(kubernetesClient.getConfiguration());
}

@Test
Expand All @@ -61,7 +61,7 @@ void testPodUtils() {
LabelSelector lb = new LabelSelectorBuilder()
.withMatchLabels(Collections.singletonMap("test-label", "true")).build();

assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test").get());
assertNotNull(KubeResourceManager.get().kubeClient().getClient().namespaces().withName("test").get());

PodUtils.waitForPodsReady("test", false, () -> {
});
Expand All @@ -75,7 +75,7 @@ void testKubeUtils() {
new NamespaceBuilder().withNewMetadata().withName("test").endMetadata().build());

KubeUtils.labelNamespace("test", "test-label", "true");
assertEquals("true", KubeResourceManager.getKubeClient().getClient()
assertEquals("true", KubeResourceManager.get().kubeClient().getClient()
.namespaces().withName("test").get().getMetadata().getLabels().get("test-label"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ClusterRoleBindingType implements ResourceType<ClusterRoleBinding>
* Constructor
*/
public ClusterRoleBindingType() {
this.client = KubeResourceManager.getKubeClient().getClient().rbac().clusterRoleBindings();
this.client = KubeResourceManager.get().kubeClient().getClient().rbac().clusterRoleBindings();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ClusterRoleType implements ResourceType<ClusterRole> {
* Constructor
*/
public ClusterRoleType() {
this.client = KubeResourceManager.getKubeClient().getClient().rbac().clusterRoles();
this.client = KubeResourceManager.get().kubeClient().getClient().rbac().clusterRoles();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ConfigMapType implements ResourceType<ConfigMap> {
* Constructor
*/
public ConfigMapType() {
this.client = KubeResourceManager.getKubeClient().getClient().configMaps();
this.client = KubeResourceManager.get().kubeClient().getClient().configMaps();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class CustomResourceDefinitionType implements ResourceType<CustomResource
* Constructor
*/
public CustomResourceDefinitionType() {
this.client = KubeResourceManager.getKubeClient().getClient().apiextensions().v1().customResourceDefinitions();
this.client = KubeResourceManager.get().kubeClient()
.getClient().apiextensions().v1().customResourceDefinitions();
}

/**
Expand Down Expand Up @@ -82,7 +83,7 @@ public void delete(CustomResourceDefinition resource) {
* from which is the current {@link CustomResourceDefinition} resource updated
*
* @param resource {@link CustomResourceDefinition} resource that will be replaced
* @param editor {@link Consumer} containing updates to the resource
* @param editor {@link Consumer} containing updates to the resource
*/
@Override
public void replace(CustomResourceDefinition resource, Consumer<CustomResourceDefinition> editor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class DeploymentType implements ResourceType<Deployment> {
* Constructor
*/
public DeploymentType() {
this.client = KubeResourceManager.getKubeClient().getClient().apps().deployments();
this.client = KubeResourceManager.get().kubeClient().getClient().apps().deployments();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class JobType implements ResourceType<Job> {
* Constructor
*/
public JobType() {
this.client = KubeResourceManager.getKubeClient().getClient().batch().v1().jobs();
this.client = KubeResourceManager.get().kubeClient().getClient().batch().v1().jobs();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class LeaseType implements ResourceType<Lease> {
* Constructor
*/
public LeaseType() {
this.client = KubeResourceManager.getKubeClient().getClient().leases();
this.client = KubeResourceManager.get().kubeClient().getClient().leases();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class NamespaceType implements ResourceType<Namespace> {
* Constructor
*/
public NamespaceType() {
this.client = KubeResourceManager.getKubeClient().getClient().namespaces();
this.client = KubeResourceManager.get().kubeClient().getClient().namespaces();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class NetworkPolicyType implements ResourceType<NetworkPolicy> {
* Constructor
*/
public NetworkPolicyType() {
this.client = KubeResourceManager.getKubeClient().getClient().network().networkPolicies();
this.client = KubeResourceManager.get().kubeClient().getClient().network().networkPolicies();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class RoleBindingType implements ResourceType<RoleBinding> {
* Constructor
*/
public RoleBindingType() {
this.client = KubeResourceManager.getKubeClient().getClient().rbac().roleBindings();
this.client = KubeResourceManager.get().kubeClient().getClient().rbac().roleBindings();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class RoleType implements ResourceType<Role> {
* Constructor
*/
public RoleType() {
this.client = KubeResourceManager.getKubeClient().getClient().rbac().roles();
this.client = KubeResourceManager.get().kubeClient().getClient().rbac().roles();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class SecretType implements ResourceType<Secret> {
* Constructor
*/
public SecretType() {
this.client = KubeResourceManager.getKubeClient().getClient().secrets();
this.client = KubeResourceManager.get().kubeClient().getClient().secrets();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ServiceAccountType implements ResourceType<ServiceAccount> {
* Constructor
*/
public ServiceAccountType() {
this.client = KubeResourceManager.getKubeClient().getClient().serviceAccounts();
this.client = KubeResourceManager.get().kubeClient().getClient().serviceAccounts();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ServiceType implements ResourceType<Service> {
* Constructor
*/
public ServiceType() {
this.client = KubeResourceManager.getKubeClient().getClient().services();
this.client = KubeResourceManager.get().kubeClient().getClient().services();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class ValidatingWebhookConfigurationType implements ResourceType<Validati
* Constructor
*/
public ValidatingWebhookConfigurationType() {
this.client = KubeResourceManager.getKubeClient().getClient()
this.client = KubeResourceManager.get().kubeClient().getClient()
.admissionRegistration()
.v1()
.validatingWebhookConfigurations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,19 @@ protected void setExec(Exec exec) {
private synchronized KubernetesClient getKubeClient() {
if (kubeClient == null) {
KubeResourceManager resourceManager = KubeResourceManager.get();
kubeClient = KubeResourceManager.getKubeClient().getClient();
kubeClient = KubeResourceManager.get().kubeClient().getClient();
if (kubeClient == null) {
throw new IllegalStateException("KubeClient is not available");
}
kubeClient = KubeResourceManager.getKubeClient().getClient();
kubeClient = KubeResourceManager.get().kubeClient().getClient();
}
return kubeClient;
}

private synchronized KubeCmdClient<?> getKubeCmdClient() {
if (kubeCmdClient == null) {
final KubeResourceManager resourceManager = KubeResourceManager.get();
kubeCmdClient = KubeResourceManager.getKubeCmdClient();
kubeCmdClient = KubeResourceManager.get().kubeCmdClient();
if (kubeCmdClient == null) {
throw new IllegalStateException("KubeCmdClient is not available");
}
Expand Down
Loading

0 comments on commit 9a6c828

Please sign in to comment.