Skip to content

Commit

Permalink
Fix CRDUtils::apply by using get(), update() and create() when SSA is…
Browse files Browse the repository at this point in the history
… not configured
  • Loading branch information
Donnerbart committed Jul 18, 2023
1 parent 3758a57 commit d73e005
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ private CRDUtils() {
}

public static void applyCRD(KubernetesClient client, CRDGenerationInfo crdInfo, String crdName) {
applyCRD(client, crdInfo, crdName, true);
}

public static void applyCRD(KubernetesClient client, CRDGenerationInfo crdInfo, String crdName, boolean useSaa) {
try {
crdInfo.getCRDInfosFor(crdName).forEach((crdVersion, info) -> {
final var filePath = Path.of(info.getFilePath());
try {
final var crd = client.getKubernetesSerialization()
.unmarshal(Files.newInputStream(filePath), getCRDClassFor(crdVersion));
apply(client, crdVersion, crd);
apply(client, crdVersion, crd, useSaa);
LOGGER.infov("Applied {0} CRD named ''{1}'' from {2}", crdVersion, crdName, filePath);
} catch (IOException ex) {
throw new IllegalArgumentException("Couldn't read CRD file at " + filePath
Expand All @@ -35,17 +39,29 @@ public static void applyCRD(KubernetesClient client, CRDGenerationInfo crdInfo,
}
}

private static void apply(KubernetesClient client, String v, Object crd) {
private static void apply(KubernetesClient client, String v, Object crd, boolean useSaa) {
switch (v) {
case "v1":
client.apiextensions().v1().customResourceDefinitions()
.resource((CustomResourceDefinition) crd)
.serverSideApply();
final var resource = client.apiextensions().v1().customResourceDefinitions()
.resource((CustomResourceDefinition) crd);
if (useSaa) {
resource.serverSideApply();
} else if (resource.get() != null) {
resource.update();
} else {
resource.create();
}
break;
case "v1beta1":
client.apiextensions().v1beta1().customResourceDefinitions()
.resource((io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) crd)
.serverSideApply();
final var legacyResource = client.apiextensions().v1beta1().customResourceDefinitions()
.resource((io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) crd);
if (useSaa) {
legacyResource.serverSideApply();
} else if (legacyResource.get() != null) {
legacyResource.update();
} else {
legacyResource.create();
}
break;
default:
throw new IllegalArgumentException("Unknown CRD version: " + v);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ Operator operator(QuarkusConfigurationService configuration, Instance<Reconciler
final var crdInfo = configuration.getCRDGenerationInfo();
if (crdInfo.isApplyCRDs()) {
for (String generatedCrdName : crdInfo.getGenerated()) {
applyCRD(configuration.getKubernetesClient(), crdInfo, generatedCrdName);
applyCRD(configuration.getKubernetesClient(), crdInfo, generatedCrdName,
configuration.ssaBasedCreateUpdateMatchForDependentResources());
}
}

Expand Down

0 comments on commit d73e005

Please sign in to comment.