private void configureDefaultResources(NamespacedOpenShiftClient client, File resourcesDir) { String namespace = client.getNamespace(); KubeResourceApplier.applyIfDifferent(new File(resourcesDir, "brokeredinfraconfigs"), client.customResources(AdminCrd.brokeredInfraConfigs(), BrokeredInfraConfig.class, BrokeredInfraConfigList.class, DoneableBrokeredInfraConfig.class).inNamespace(namespace), BrokeredInfraConfig.class, Comparator.comparing(BrokeredInfraConfig::getVersion)); KubeResourceApplier.applyIfDifferent(new File(resourcesDir, "standardinfraconfigs"), client.customResources(AdminCrd.standardInfraConfigs(), StandardInfraConfig.class, StandardInfraConfigList.class, DoneableStandardInfraConfig.class).inNamespace(namespace), StandardInfraConfig.class, Comparator.comparing(StandardInfraConfig::getVersion)); KubeResourceApplier.createIfNoneExists(new File(resourcesDir, "addressplans"), client.customResources(AdminCrd.addressPlans(), AddressPlan.class, AddressPlanList.class, DoneableAddressPlan.class).inNamespace(namespace), AddressPlan.class); KubeResourceApplier.createIfNoneExists(new File(resourcesDir, "addressspaceplans"), client.customResources(AdminCrd.addressSpacePlans(), AddressSpacePlan.class, AddressSpacePlanList.class, DoneableAddressSpacePlan.class).inNamespace(namespace), AddressSpacePlan.class); }
/** * Creates resources in resourceDir if no resource of this type exists. */ public static <T extends HasMetadata, L extends KubernetesResourceList, D extends Doneable<T>> void createIfNoneExists(File resourceDir, NonNamespaceOperation<T, L, D, Resource<T, D>> operation, Class<T> resourceClass) { if (operation.list().getItems().isEmpty()) { if (resourceDir.isDirectory()) { File[] files = resourceDir.listFiles(); if (files != null) { for (File file : files) { T newResource = readAndParse(file, resourceClass); log.info("Creating {} {}", newResource.getKind(), newResource.getMetadata().getName()); operation.create(newResource); } } } } }
/** * Apply new version of resources if considered changed */ public static <T extends HasMetadata, L extends KubernetesResourceList, D extends Doneable<T>> void applyIfDifferent(File resourceDir, NonNamespaceOperation<T, L, D, Resource<T, D>> operation, Class<T> resourceClass, Comparator<T> comparator) { if (resourceDir.isDirectory()) { File[] files = resourceDir.listFiles(); if (files != null) { for (File file : files) { T newResource = readAndParse(file, resourceClass); if (newResource != null) { T currentResource = operation.withName(newResource.getMetadata().getName()).get(); if (currentResource == null) { log.info("Creating {} {}", newResource.getKind(), newResource.getMetadata().getName()); operation.create(newResource); } else if (comparator.compare(currentResource, newResource) != 0) { log.info("Updating {} {}", newResource.getKind(), newResource.getMetadata().getName()); operation.createOrReplace(newResource); } } } } } }