Search in sources :

Example 1 with CustomResourceDefinitionContextProvider

use of cz.xtf.core.openshift.crd.CustomResourceDefinitionContextProvider in project syndesis-qe by syndesisio.

the class CommonSteps method isProjectClean.

/**
 * Workaround for isProjectClean function from XTF. See issue: https://github.com/xtf-cz/xtf/issues/406
 * XTF cannot be update because it uses openshift-client 4.13.0 which contains kubernetes-model 4.11.2 where CRD was moved to the `v1beta1` .
 * Since Syndesis endpoints uses old kubernetes-model, the CRD is missing (`NoClassDefFoundError`)
 * When we let Syndesis endpoints to bring the old kubernetes-model with itself (remove <exclude> from parent pom.xml),
 * it causes undesired behaviour, e.g. portForwarding doesn't work correctly etc.
 * So we need to wait with bump xtf version until the Syndesis will contains newer kubernetes-model
 * <p>
 * This is implementation from XTF with workaround
 * To access to the protected methods, the JavaReflection API is used.
 */
private static Waiter isProjectClean() {
    return new SimpleWaiter(() -> {
        int crdInstances = 0;
        int removableResources = 0;
        try {
            Method privateMethodGetCRDContextProviders = OpenShift.class.getDeclaredMethod("getCRDContextProviders", null);
            privateMethodGetCRDContextProviders.setAccessible(true);
            Method privateMethodListRemovableResources = OpenShift.class.getDeclaredMethod("listRemovableResources", null);
            privateMethodListRemovableResources.setAccessible(true);
            ServiceLoader<CustomResourceDefinitionContextProvider> cRDContextProviders = (ServiceLoader<CustomResourceDefinitionContextProvider>) privateMethodGetCRDContextProviders.invoke(OpenShift.class, null);
            for (CustomResourceDefinitionContextProvider crdContextProvider : cRDContextProviders) {
                try {
                    crdInstances += ((List) (OpenShiftUtils.getInstance().customResource(crdContextProvider.getContext()).list(OpenShiftUtils.getInstance().getNamespace()).get("items"))).size();
                } catch (KubernetesClientException kce) {
                // CRD might not be installed on the cluster
                }
            }
            List<HasMetadata> removableResourcesList = (List<HasMetadata>) privateMethodListRemovableResources.invoke(OpenShiftUtils.getInstance(), null);
            removableResources = removableResourcesList.size();
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            e.printStackTrace();
        }
        return crdInstances == 0 & // <=1 because configMap can be there on OCP 4.7, see https://github.com/xtf-cz/xtf/issues/406
        removableResources <= 1;
    }, TimeUnit.MILLISECONDS, WaitingConfig.timeoutCleanup(), "Cleaning project.");
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Method(java.lang.reflect.Method) CustomResourceDefinitionContextProvider(cz.xtf.core.openshift.crd.CustomResourceDefinitionContextProvider) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServiceLoader(java.util.ServiceLoader) OpenShift(cz.xtf.core.openshift.OpenShift) SimpleWaiter(cz.xtf.core.waiting.SimpleWaiter) List(java.util.List) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Aggregations

OpenShift (cz.xtf.core.openshift.OpenShift)1 CustomResourceDefinitionContextProvider (cz.xtf.core.openshift.crd.CustomResourceDefinitionContextProvider)1 SimpleWaiter (cz.xtf.core.waiting.SimpleWaiter)1 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)1 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 ServiceLoader (java.util.ServiceLoader)1