Skip to content

Commit

Permalink
feat: process generated CRDs but before they are written out
Browse files Browse the repository at this point in the history
Fixes #6827
  • Loading branch information
metacosm committed Jan 26, 2025
1 parent eb7fee6 commit 72272b7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

* Fix #5993: Support for Kubernetes v1.31 (elli)
* Fix #6767: Support for Kubernetes v1.32 (penelope)
* Fix #6827: Add CRDPostProcessor to process generated CRDs before they are written out

#### _**Note**_: Breaking changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
public class CRDGenerator {

private static final Logger LOGGER = LoggerFactory.getLogger(CRDGenerator.class);
private static final CRDPostProcessor nullProcessor = new CRDPostProcessor() {};
private final Map<String, AbstractCustomResourceHandler> handlers = new HashMap<>(2);
private CRDOutput<? extends OutputStream> output;
private boolean parallel;
Expand Down Expand Up @@ -156,7 +157,7 @@ public int generate() {
return detailedGenerate().numberOfGeneratedCRDs();
}

public CRDGenerationInfo detailedGenerate() {
public CRDGenerationInfo detailedGenerate(CRDPostProcessor processor) {
if (getCustomResourceInfos().isEmpty()) {
LOGGER.warn("No resources were registered with the 'customResources' method to be generated");
return CRDGenerationInfo.EMPTY;
Expand Down Expand Up @@ -192,22 +193,39 @@ public CRDGenerationInfo detailedGenerate() {

if (parallel) {
handlers.values().stream().map(h -> ForkJoinPool.commonPool().submit(() -> h.handle(info, context.forkContext())))
.collect(Collectors.toList()).stream().forEach(ForkJoinTask::join);
.collect(Collectors.toList())
.forEach(ForkJoinTask::join);
} else {
handlers.values().stream().forEach(h -> h.handle(info, context.forkContext()));
handlers.values().forEach(h -> h.handle(info, context.forkContext()));
}
}
}

final CRDGenerationInfo crdGenerationInfo = new CRDGenerationInfo();
handlers.values().stream().flatMap(AbstractCustomResourceHandler::finish)
.forEach(crd -> emitCrd(crd.getKey(), crd.getValue(), crdGenerationInfo));
.forEach(crd -> emitCrd(crd.getKey(), crd.getValue(), crdGenerationInfo, processor));
return crdGenerationInfo;
}

public CRDGenerationInfo detailedGenerate() {
return detailedGenerate(nullProcessor);
}

/**
* @deprecated use {@link #emitCrd(HasMetadata, Set, CRDGenerationInfo, CRDPostProcessor)} instead
*/
@Deprecated(forRemoval = true)
public void emitCrd(HasMetadata crd, Set<String> dependentClassNames, CRDGenerationInfo crdGenerationInfo) {
emitCrd(crd, dependentClassNames, crdGenerationInfo, nullProcessor);
}

protected void emitCrd(HasMetadata crd, Set<String> dependentClassNames, CRDGenerationInfo crdGenerationInfo, CRDPostProcessor processor) {
final String version = ApiVersionUtil.trimVersion(crd.getApiVersion());
final String crdName = crd.getMetadata().getName();

// post-process the CRD if needed
crd = processor.process(crd, version);

try {
final String outputName = getOutputName(crdName, version);
try (final OutputStreamWriter writer = new OutputStreamWriter(output.outputFor(outputName), StandardCharsets.UTF_8)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.crdv2.generator;

import io.fabric8.kubernetes.api.model.HasMetadata;

public interface CRDPostProcessor {

/**
* Processes the specified CRD (passed as {@link HasMetadata} to be able to handle multiple versions of the CRD spec) after they are generated but before they are written out
*
* @param crd the CRD to process as a {@link HasMetadata}
* @param crdSpecVersion the CRD specification version of the CRD to process (to be able to cast to the appropriate CRD class)
* @return the modified CRD (though, typically, this would just be the CRD specified as input, modified in place)
*/
default HasMetadata process(HasMetadata crd, String crdSpecVersion) {
return crd;
}
}

0 comments on commit 72272b7

Please sign in to comment.