use of io.fabric8.kubernetes.client.CustomResource 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.");
}
use of io.fabric8.kubernetes.client.CustomResource in project strimzi by strimzi.
the class CrdGenerator method buildSpec.
@SuppressWarnings("NPathComplexity")
private ObjectNode buildSpec(ApiVersion crdApiVersion, Crd.Spec crd, Class<? extends CustomResource> crdClass) {
checkKubeVersionsSupportCrdVersion(crdApiVersion);
ObjectNode result = nf.objectNode();
result.put("group", crd.group());
ArrayNode versions = nf.arrayNode();
// Kube apiserver with CRD v1beta1 is picky about only using per-version subresources, schemas and printercolumns
// if they actually differ across the versions. If they're the same, it insists these things are
// declared top level
// With CRD v1 they have to be per-version :face-with-rolling-eyes:
Map<ApiVersion, ObjectNode> subresources = buildSubresources(crd);
boolean perVersionSubResources = needsPerVersion("subresources", subresources);
Map<ApiVersion, ObjectNode> schemas = buildSchemas(crd, crdClass);
boolean perVersionSchemas = needsPerVersion("schemas", schemas);
Map<ApiVersion, ArrayNode> printerColumns = buildPrinterColumns(crd);
boolean perVersionPrinterColumns = needsPerVersion("additionalPrinterColumns", printerColumns);
result.set("names", buildNames(crd.names()));
result.put("scope", crd.scope());
if (!perVersionPrinterColumns) {
ArrayNode cols = printerColumns.values().iterator().next();
if (!cols.isEmpty()) {
result.set("additionalPrinterColumns", cols);
}
}
if (!perVersionSubResources) {
ObjectNode subresource = subresources.values().iterator().next();
if (!subresource.isEmpty()) {
result.set("subresources", subresource);
}
}
if (conversionStrategy instanceof WebhookConversionStrategy) {
// "Webhook": must be None if spec.preserveUnknownFields is true
result.put("preserveUnknownFields", false);
}
result.set("conversion", buildConversion(crdApiVersion));
for (Crd.Spec.Version version : crd.versions()) {
ApiVersion crApiVersion = ApiVersion.parse(version.name());
if (!shouldIncludeVersion(crApiVersion)) {
continue;
}
ObjectNode versionNode = versions.addObject();
versionNode.put("name", crApiVersion.toString());
versionNode.put("served", servedVersion != null ? servedVersion.contains(crApiVersion) : version.served());
versionNode.put("storage", storageVersion != null ? crApiVersion.equals(storageVersion) : version.storage());
if (perVersionSubResources) {
ObjectNode subresourcesForVersion = subresources.get(crApiVersion);
if (!subresourcesForVersion.isEmpty()) {
versionNode.set("subresources", subresourcesForVersion);
}
}
if (perVersionPrinterColumns) {
ArrayNode cols = printerColumns.get(crApiVersion);
if (!cols.isEmpty()) {
versionNode.set("additionalPrinterColumns", cols);
}
}
if (perVersionSchemas) {
versionNode.set("schema", schemas.get(crApiVersion));
}
}
result.set("versions", versions);
if (crdApiVersion.compareTo(V1) < 0 && targetKubeVersions.intersects(KubeVersion.parseRange("1.11-1.15"))) {
result.put("version", Arrays.stream(crd.versions()).map(v -> ApiVersion.parse(v.name())).filter(this::shouldIncludeVersion).findFirst().map(ApiVersion::toString).orElseThrow());
}
if (!perVersionSchemas) {
result.set("validation", schemas.values().iterator().next());
}
return result;
}
use of io.fabric8.kubernetes.client.CustomResource in project strimzi by strimzi.
the class CrdGenerator method main.
public static void main(String[] args) throws IOException, ClassNotFoundException {
CommandOptions opts = new CommandOptions(args);
CrdGenerator generator = new CrdGenerator(opts.targetKubeVersions, opts.crdApiVersion, opts.yaml ? YAML_MAPPER.configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true) : JSON_MATTER, opts.labels, new DefaultReporter(), opts.apiVersions, opts.storageVersion, null, opts.conversionStrategy, opts.describeVersions);
for (Map.Entry<String, Class<? extends CustomResource>> entry : opts.classes.entrySet()) {
File file = new File(entry.getKey());
if (file.getParentFile().exists()) {
if (!file.getParentFile().isDirectory()) {
generator.err(file.getParentFile() + " is not a directory");
}
} else if (!file.getParentFile().mkdirs()) {
generator.err(file.getParentFile() + " does not exist and could not be created");
}
try (Writer w = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
generator.generate(entry.getValue(), w);
}
}
if (generator.numErrors > 0) {
System.err.println("There were " + generator.numErrors + " errors");
System.exit(1);
} else {
System.exit(0);
}
}
use of io.fabric8.kubernetes.client.CustomResource in project strimzi by strimzi.
the class MockKube method mockCrs.
@SuppressWarnings({ "unchecked", "deprecation" })
public void mockCrs(KubernetesClient mockClient) {
when(mockClient.customResources(any(CustomResourceDefinitionContext.class), any(Class.class), any(Class.class))).thenAnswer(invocation -> {
Class<CustomResource> crClass = invocation.getArgument(1);
String key = crdKey(crClass);
CreateOrReplaceable createOrReplaceable = crdMixedOps.get(key);
if (createOrReplaceable == null) {
throw new RuntimeException("Unknown CRD " + key);
}
return createOrReplaceable;
});
when(mockClient.customResources(any(Class.class), any(Class.class))).thenAnswer(invocation -> {
Class<CustomResource> crClass = invocation.getArgument(0);
String key = crdKey(crClass);
CreateOrReplaceable createOrReplaceable = crdMixedOps.get(key);
if (createOrReplaceable == null) {
throw new RuntimeException("Unknown CRD " + key);
}
return createOrReplaceable;
});
when(mockClient.resources(any(Class.class), any(Class.class))).thenAnswer(invocation -> {
Class<CustomResource> crClass = invocation.getArgument(0);
String key = crdKey(crClass);
CreateOrReplaceable createOrReplaceable = crdMixedOps.get(key);
if (createOrReplaceable == null) {
throw new RuntimeException("Unknown CRD " + key);
}
return createOrReplaceable;
});
}
use of io.fabric8.kubernetes.client.CustomResource in project strimzi by strimzi.
the class ResourceManager method logCurrentResourceStatus.
/**
* Log actual status of custom resource with pods.
* @param customResource - Kafka, KafkaConnect etc. - every resource that HasMetadata and HasStatus (Strimzi status)
*/
public static <T extends CustomResource<? extends Spec, ? extends Status>> void logCurrentResourceStatus(T customResource) {
if (customResource != null) {
List<String> printWholeCR = Arrays.asList(KafkaConnector.RESOURCE_KIND, KafkaTopic.RESOURCE_KIND, KafkaUser.RESOURCE_KIND);
String kind = customResource.getKind();
String name = customResource.getMetadata().getName();
if (printWholeCR.contains(kind)) {
LOGGER.info(customResource);
} else {
List<String> log = new ArrayList<>(asList(kind, " status:\n", "\nConditions:\n"));
if (customResource.getStatus() != null) {
List<Condition> conditions = customResource.getStatus().getConditions();
if (conditions != null) {
for (Condition condition : customResource.getStatus().getConditions()) {
if (condition.getMessage() != null) {
log.add("\tType: " + condition.getType() + "\n");
log.add("\tMessage: " + condition.getMessage() + "\n");
}
}
}
log.add("\nPods with conditions and messages:\n\n");
for (Pod pod : kubeClient().namespace(customResource.getMetadata().getNamespace()).listPodsByPrefixInName(name)) {
log.add(pod.getMetadata().getName() + ":");
for (PodCondition podCondition : pod.getStatus().getConditions()) {
if (podCondition.getMessage() != null) {
log.add("\n\tType: " + podCondition.getType() + "\n");
log.add("\tMessage: " + podCondition.getMessage() + "\n");
} else {
log.add("\n\tType: <EMPTY>\n");
log.add("\tMessage: <EMPTY>\n");
}
}
log.add("\n\n");
}
LOGGER.info("{}", String.join("", log).strip());
}
}
}
}
Aggregations