Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename resource classes from *Resource to *Type and better check for readiness in case of type == null #79

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ class Test {
}
//...
```
### Register `ResourceType` or `NamespacedResoruceType` classes into `KubeResoruceManager`
### Register `ResourceType` or `NamespacedResourceType` classes into `KubeResourceManager`
```java
KubeResourceManager.getInstance().setResourceTypes(
new NamespaceResource(),
new JobResource(),
new NetworkPolicyResource()
new NamespaceType(),
new JobType(),
new NetworkPolicyType()
);
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;

import io.fabric8.kubernetes.api.model.Endpoints;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Node;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.ReplicationController;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.ReplicaSet;
import io.fabric8.kubernetes.api.model.apps.StatefulSet;
import io.skodjob.testframe.LoggerUtils;
import io.skodjob.testframe.TestFrameConstants;
import io.skodjob.testframe.TestFrameEnv;
Expand Down Expand Up @@ -244,11 +251,10 @@ private <T extends HasMetadata> void createOrUpdateResource(boolean waitReady,
}
if (waitReady) {
assertTrue(waitResourceCondition(resource, new ResourceCondition<>(p -> {
try {
if (isResourceWithReadiness(resource)) {
return client.getClient().resource(resource).isReady();
} catch (Exception ex) {
return client.getClient().resource(resource) != null;
}
return client.getClient().resource(resource) != null;
}, "ready")),
String.format("Timed out waiting for %s/%s in %s to be ready", resource.getKind(),
resource.getMetadata().getName(), resource.getMetadata().getNamespace()));
Expand Down Expand Up @@ -425,4 +431,15 @@ private <T extends HasMetadata> ResourceType<T> findResourceType(T resource) {
}
return null;
}

private <T extends HasMetadata> boolean isResourceWithReadiness(T resource) {
return resource instanceof Deployment
|| resource instanceof io.fabric8.kubernetes.api.model.extensions.Deployment
|| resource instanceof ReplicaSet
|| resource instanceof Pod
|| resource instanceof ReplicationController
|| resource instanceof Endpoints
|| resource instanceof Node
|| resource instanceof StatefulSet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
/**
* Implementation of ResourceType for specific kubernetes resource
*/
public class ClusterRoleBindingResource implements ResourceType<ClusterRoleBinding> {
public class ClusterRoleBindingType implements ResourceType<ClusterRoleBinding> {

private final NonNamespaceOperation<ClusterRoleBinding, ClusterRoleBindingList,
Resource<ClusterRoleBinding>> client;

/**
* Constructor
*/
public ClusterRoleBindingResource() {
public ClusterRoleBindingType() {
this.client = KubeResourceManager.getKubeClient().getClient().rbac().clusterRoleBindings();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
/**
* Implementation of ResourceType for specific kubernetes resource
*/
public class ClusterRoleResource implements ResourceType<ClusterRole> {
public class ClusterRoleType implements ResourceType<ClusterRole> {

private final NonNamespaceOperation<ClusterRole, ClusterRoleList, Resource<ClusterRole>> client;

/**
* Constructor
*/
public ClusterRoleResource() {
public ClusterRoleType() {
this.client = KubeResourceManager.getKubeClient().getClient().rbac().clusterRoles();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
/**
* Implementation of ResourceType for specific kubernetes resource
*/
public class ConfigMapResource implements NamespacedResourceType<ConfigMap> {
public class ConfigMapType implements NamespacedResourceType<ConfigMap> {
private final MixedOperation<ConfigMap, ConfigMapList, Resource<ConfigMap>> client;

/**
* Constructor
*/
public ConfigMapResource() {
public ConfigMapType() {
this.client = KubeResourceManager.getKubeClient().getClient().configMaps();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
/**
* Implementation of ResourceType for specific kubernetes resource
*/
public class CustomResourceDefinitionResource implements ResourceType<CustomResourceDefinition> {
public class CustomResourceDefinitionType implements ResourceType<CustomResourceDefinition> {

private final NonNamespaceOperation<CustomResourceDefinition, CustomResourceDefinitionList,
Resource<CustomResourceDefinition>> client;

/**
* Constructor
*/
public CustomResourceDefinitionResource() {
public CustomResourceDefinitionType() {
this.client = KubeResourceManager.getKubeClient().getClient().apiextensions().v1().customResourceDefinitions();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
/**
* Implementation of ResourceType for specific kubernetes resource
*/
public class DeploymentResource implements NamespacedResourceType<Deployment> {
public class DeploymentType implements NamespacedResourceType<Deployment> {

private final MixedOperation<Deployment, DeploymentList, RollableScalableResource<Deployment>> client;

/**
* Constructor
*/
public DeploymentResource() {
public DeploymentType() {
this.client = KubeResourceManager.getKubeClient().getClient().apps().deployments();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
/**
* Implementation of ResourceType for specific kubernetes resource
*/
public class JobResource implements NamespacedResourceType<Job> {
public class JobType implements NamespacedResourceType<Job> {

private final MixedOperation<Job, JobList, ScalableResource<Job>> client;

/**
* Constructor
*/
public JobResource() {
public JobType() {
this.client = KubeResourceManager.getKubeClient().getClient().batch().v1().jobs();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
/**
* Implementation of ResourceType for specific kubernetes resource
*/
public class LeaseResource implements NamespacedResourceType<Lease> {
public class LeaseType implements NamespacedResourceType<Lease> {

private MixedOperation<Lease, LeaseList, Resource<Lease>> client;

/**
* Constructor
*/
public LeaseResource() {
public LeaseType() {
this.client = KubeResourceManager.getKubeClient().getClient().leases();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
/**
* Implementation of ResourceType for specific kubernetes resource
*/
public class NamespaceResource implements ResourceType<Namespace> {
public class NamespaceType implements ResourceType<Namespace> {

private final NonNamespaceOperation<Namespace, NamespaceList, Resource<Namespace>> client;

/**
* Constructor
*/
public NamespaceResource() {
public NamespaceType() {
this.client = KubeResourceManager.getKubeClient().getClient().namespaces();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
/**
* Implementation of ResourceType for specific kubernetes resource
*/
public class NetworkPolicyResource implements NamespacedResourceType<NetworkPolicy> {
public class NetworkPolicyType implements NamespacedResourceType<NetworkPolicy> {

private final MixedOperation<NetworkPolicy, NetworkPolicyList, Resource<NetworkPolicy>> client;

/**
* Constructor
*/
public NetworkPolicyResource() {
public NetworkPolicyType() {
this.client = KubeResourceManager.getKubeClient().getClient().network().networkPolicies();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
/**
* Implementation of ResourceType for specific kubernetes resource
*/
public class RoleBindingResource implements NamespacedResourceType<RoleBinding> {
public class RoleBindingType implements NamespacedResourceType<RoleBinding> {

private final MixedOperation<RoleBinding, RoleBindingList, Resource<RoleBinding>> client;

/**
* Constructor
*/
public RoleBindingResource() {
public RoleBindingType() {
this.client = KubeResourceManager.getKubeClient().getClient().rbac().roleBindings();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
/**
* Implementation of ResourceType for specific kubernetes resource
*/
public class RoleResource implements NamespacedResourceType<Role> {
public class RoleType implements NamespacedResourceType<Role> {

private final MixedOperation<Role, RoleList, Resource<Role>> client;

/**
* Constructor
*/
public RoleResource() {
public RoleType() {
this.client = KubeResourceManager.getKubeClient().getClient().rbac().roles();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
/**
* Implementation of ResourceType for specific kubernetes resource
*/
public class SecretResource implements NamespacedResourceType<Secret> {
public class SecretType implements NamespacedResourceType<Secret> {

private final MixedOperation<Secret, SecretList, Resource<Secret>> client;

/**
* Constructor
*/
public SecretResource() {
public SecretType() {
this.client = KubeResourceManager.getKubeClient().getClient().secrets();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
import io.fabric8.kubernetes.api.model.ServiceAccount;
import io.fabric8.kubernetes.api.model.ServiceAccountList;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.ServiceAccountResource;
import io.skodjob.testframe.interfaces.NamespacedResourceType;

/**
* Implementation of ResourceType for specific kubernetes resource
*/
public class ServiceAccountResource implements NamespacedResourceType<ServiceAccount> {
public class ServiceAccountType implements NamespacedResourceType<ServiceAccount> {

private final MixedOperation<ServiceAccount, ServiceAccountList,
io.fabric8.kubernetes.client.dsl.ServiceAccountResource> client;
private final MixedOperation<ServiceAccount, ServiceAccountList, ServiceAccountResource> client;

/**
* Constructor
*/
public ServiceAccountResource() {
public ServiceAccountType() {
this.client = KubeResourceManager.getKubeClient().getClient().serviceAccounts();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceList;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.ServiceResource;
import io.skodjob.testframe.interfaces.NamespacedResourceType;

/**
* Implementation of ResourceType for specific kubernetes resource
*/
public class ServiceResource implements NamespacedResourceType<Service> {
public class ServiceType implements NamespacedResourceType<Service> {

private final MixedOperation<Service, ServiceList,
io.fabric8.kubernetes.client.dsl.ServiceResource<Service>> client;
private final MixedOperation<Service, ServiceList, ServiceResource<Service>> client;

/**
* Constructor
*/
public ServiceResource() {
public ServiceType() {
this.client = KubeResourceManager.getKubeClient().getClient().services();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Implementation of ResourceType for specific kubernetes resource
*/
public class ValidatingWebhookConfigurationResource implements ResourceType<ValidatingWebhookConfiguration> {
public class ValidatingWebhookConfigurationType implements ResourceType<ValidatingWebhookConfiguration> {


private final NonNamespaceOperation<ValidatingWebhookConfiguration, ValidatingWebhookConfigurationList,
Expand All @@ -24,7 +24,7 @@ public class ValidatingWebhookConfigurationResource implements ResourceType<Vali
/**
* Constructor
*/
public ValidatingWebhookConfigurationResource() {
public ValidatingWebhookConfigurationType() {
this.client = KubeResourceManager.getKubeClient().getClient()
.admissionRegistration()
.v1()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.skodjob.testframe;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.fabric8.kubernetes.api.model.LabelSelector;
import io.fabric8.kubernetes.api.model.LabelSelectorBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
/**
* Implementation of ResourceType for specific kubernetes resource
*/
public class InstallPlanResource implements NamespacedResourceType<InstallPlan> {
public class InstallPlanType implements NamespacedResourceType<InstallPlan> {

private final MixedOperation<InstallPlan, InstallPlanList, Resource<InstallPlan>> client;

/**
* Constructor
*/
public InstallPlanResource() {
public InstallPlanType() {
this.client = KubeResourceManager.getKubeClient().getOpenShiftClient().operatorHub().installPlans();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
/**
* Implementation of ResourceType for specific kubernetes resource
*/
public class OperatorGroupResource implements NamespacedResourceType<OperatorGroup> {
public class OperatorGroupType implements NamespacedResourceType<OperatorGroup> {

private final MixedOperation<OperatorGroup, OperatorGroupList, Resource<OperatorGroup>> client;

/**
* Constructor
*/
public OperatorGroupResource() {
public OperatorGroupType() {
this.client = KubeResourceManager.getKubeClient().getOpenShiftClient().operatorHub().operatorGroups();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
/**
* Implementation of ResourceType for specific kubernetes resource
*/
public class SubscriptionResource implements NamespacedResourceType<Subscription> {
public class SubscriptionType implements NamespacedResourceType<Subscription> {

private final MixedOperation<Subscription, SubscriptionList, Resource<Subscription>> client;

/**
* Constructor
*/
public SubscriptionResource() {
public SubscriptionType() {
this.client = KubeResourceManager.getKubeClient().getOpenShiftClient().operatorHub().subscriptions();
}

Expand Down
Loading
Loading