Skip to content

Commit

Permalink
fix: make sure new generator instances start with a clean context
Browse files Browse the repository at this point in the history
This is needed when classes might have been reloaded since the last
generation round. If we were to hold onto already generated TypeDefs,
we would get an outdated CRD. While this isn't a typical scenario, this
situation occurs during Quarkus live reload.

Fixes #3442
  • Loading branch information
metacosm committed Sep 3, 2021
1 parent 0896a56 commit c94a5d3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 38 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
### 5.8-SNAPSHOT

#### Bugs
* Fix #3442: make sure new CRDGenerator instances start with a clean generation context

#### Improvements
#### Dependency Upgrade
* Upgrade Sundrio to 0.50.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;
import io.fabric8.crd.generator.utils.Types;
import io.fabric8.crd.generator.v1.CustomResourceHandler;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.CustomResource;
Expand Down Expand Up @@ -55,6 +56,7 @@ public class CRDGenerator {

public CRDGenerator() {
resources = new Resources();
Types.resetGenerationContext(); // make sure the new generator starts up with a clean slate
}

public CRDGenerator inOutputDir(File outputDir) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,35 @@
import io.sundr.adapter.api.AdapterContext;
import io.sundr.adapter.api.Adapters;
import io.sundr.builder.TypedVisitor;
import io.sundr.model.ClassRef;
import io.sundr.model.ClassRefBuilder;
import io.sundr.model.PrimitiveRef;
import io.sundr.model.Property;
import io.sundr.model.PropertyBuilder;
import io.sundr.model.TypeDef;
import io.sundr.model.TypeDefBuilder;
import io.sundr.model.TypeParamDef;
import io.sundr.model.TypeParamRef;
import io.sundr.model.TypeRef;
import io.sundr.model.VoidRef;
import io.sundr.model.WildcardRef;
import io.sundr.model.*;
import io.sundr.model.functions.GetDefinition;
import io.sundr.model.repo.DefinitionRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Types {
private Types() {
throw new IllegalStateException("Utility class");
}

private static final Logger LOGGER = LoggerFactory.getLogger(Types.class);
private static final String NAMESPACED = Namespaced.class.getName();
public static final String JAVA_LANG_VOID = "java.lang.Void";
public static final AdapterContext REFLECTION_CONTEXT = AdapterContext.create(DefinitionRepository.getRepository());

/**
* Make sure the generation context is reset so that types can be properly introspected if classes have changed since the last generation round.
*/
public static void resetGenerationContext() {
DefinitionRepository.getRepository().clear();
}

public static TypeDef typeDefFrom(Class<?> clazz) {
return unshallow(Adapters.adaptType(clazz, REFLECTION_CONTEXT));
}
Expand Down Expand Up @@ -166,24 +157,24 @@ private static Set<ClassRef> projectSuperClasses(TypeDef definition) {
private static List<Property> projectProperties(TypeDef typeDef) {
final String fqn = typeDef.getFullyQualifiedName();
return Stream.concat(
typeDef.getProperties().stream().filter(p -> {
// enums have self-referential static properties for each enum case so we cannot ignore them
if(typeDef.isEnum()) {
final TypeRef typeRef = p.getTypeRef();
if (typeRef instanceof ClassRef && fqn.equals(((ClassRef) typeRef).getFullyQualifiedName())) {
// we're dealing with an enum case so keep it
return true;
typeDef.getProperties().stream().filter(p -> {
// enums have self-referential static properties for each enum case so we cannot ignore them
if (typeDef.isEnum()) {
final TypeRef typeRef = p.getTypeRef();
if (typeRef instanceof ClassRef && fqn.equals(((ClassRef) typeRef).getFullyQualifiedName())) {
// we're dealing with an enum case so keep it
return true;
}
}
}
// otherwise exclude all static properties
return !p.isStatic();
}),
typeDef.getExtendsList().stream()
.filter(e -> !e.getFullyQualifiedName().startsWith("java."))
.flatMap(e -> projectProperties(projectDefinition(e))
.stream()
.filter(p -> filterCustomResourceProperties(e).test(p)))
)
// otherwise exclude all static properties
return !p.isStatic();
}),
typeDef.getExtendsList().stream()
.filter(e -> !e.getFullyQualifiedName().startsWith("java."))
.flatMap(e -> projectProperties(projectDefinition(e))
.stream()
.filter(p -> filterCustomResourceProperties(e).test(p)))
)

.collect(Collectors.toList());
}
Expand Down

0 comments on commit c94a5d3

Please sign in to comment.