Skip to content

Commit

Permalink
Implement support for custom Api Versions in informers (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
cambierr committed Jun 9, 2024
1 parent 6f599ec commit b892746
Showing 1 changed file with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
import io.kubernetes.client.util.Strings;
import org.apache.commons.lang3.tuple.MutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.regex.Pattern;

/**
* This class is inspired by the implementation of the <a href="https://github.com/kubernetes-client/java/blob/release-13/util/src/main/java/io/kubernetes/client/util/ModelMapper.java">io.kubernetes.client.util.ModelMapper.java</a>.
Expand All @@ -38,12 +42,17 @@
*/
public class ModelMapper {

private static final Logger LOG = LoggerFactory.getLogger(ModelMapper.class);

// Model's api-group prefix to kubernetes api-group
private final Map<String, String> preBuiltApiGroups = new HashMap<>();

// Model's api-version midfix to kubernetes api-version
private final List<String> preBuiltApiVersions = new ArrayList<>();

// This allows parsing custom (not included in kubernetes core) api versions
private final Pattern customVersionParser = Pattern.compile("(V[a-z1-9]+)[A-Z]+[a-zA-Z0-9]+");

public ModelMapper() {
initApiGroupMap();
initApiVersionList();
Expand Down Expand Up @@ -95,7 +104,20 @@ private Pair<String, String> getApiVersion(String name) {
.filter(name::startsWith)
.map(v -> new MutablePair<String, String>(v.toLowerCase(), name.substring(v.length())))
.findFirst()
.orElse(new MutablePair<String, String>(null, name));
.orElse(new MutablePair<String, String>(tryGuessCustomApiVersion(name), name));
}

private String tryGuessCustomApiVersion(String name){
var patternMatcher = customVersionParser.matcher(name);

if(patternMatcher.matches() && patternMatcher.groupCount() == 1){
return patternMatcher.group(1);
}

// Warn the user to avoid wasted debug time (cfr https://github.com/micronaut-projects/micronaut-kubernetes/issues/639)
LOG.warn("Could not extract ApiVersion from entity {}", name);

return null;
}

/**
Expand Down

0 comments on commit b892746

Please sign in to comment.