Skip to content

Commit

Permalink
feat: allow resource resolution for latest apiVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
fhussonnois committed Jun 1, 2024
1 parent 8bacaab commit 2cb9586
Show file tree
Hide file tree
Showing 24 changed files with 494 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ public <T extends HasMetadata> T getResource(@NotNull ResourceType type,
public ApiResourceChangeList getDiff(@NotNull HasItems resources,
@NotNull ResourceChangeFilter filter,
@NotNull ReconciliationContext context) {
// Exclude any resource changes that might be transmitted by inadvertance to the diff command.
// Exclude any resource changes that might be transmitted by inadvertence to the diff command.
List<? extends HasMetadata> list = resources.getItems()
.stream()
.filter(Predicate.not(o -> o instanceof ResourceChange))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface Extension extends HasName {

/**
* Initializes this extension with the specified context.
*
* <p>
* This method is invoked each time the extension is used. Note that the given context is tied
* to this extension and therefore cannot be passed on to another extension through this method.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ else if (!Strings.isBlank(kind)) {
}

return equals == Objects.equals(this.type, resourceType);

}

/**
Expand All @@ -88,8 +89,7 @@ else if (!Strings.isBlank(kind)) {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SupportedResourceQualifier)) return false;
SupportedResourceQualifier<?> that = (SupportedResourceQualifier<?>) o;
if (!(o instanceof SupportedResourceQualifier<?> that)) return false;
return type.equals(that.type);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface HasMetadataAcceptable {
*/
default boolean canAccept(@NotNull ResourceType type) {
List<ResourceType> resources = getSupportedResources(this.getClass());
return resources.isEmpty() || resources.stream().anyMatch(resourceType -> resourceType.canAccept(type));
return resources.isEmpty() || resources.stream().anyMatch(resourceType -> resourceType. canAccept(type));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import io.streamthoughts.jikkou.core.annotation.Kind;
import io.streamthoughts.jikkou.core.annotation.Reflectable;
import io.streamthoughts.jikkou.core.annotation.Transient;
import io.streamthoughts.jikkou.core.io.ResourceDeserializer;
import io.streamthoughts.jikkou.core.resource.ResourceDeserializer;
import java.io.Serializable;

@Evolving
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,43 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.JsonNode;
import io.streamthoughts.jikkou.common.utils.Strings;
import io.streamthoughts.jikkou.core.annotation.Reflectable;
import java.beans.ConstructorProperties;
import java.util.Objects;
import java.util.Optional;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Represent a resource type.
*
* @param kind The resource Kind.
* @param group The resource API Group.
* @param apiVersion The resource API Version.
* @param isTransient Specify whether the resource is transient.
* @param kind The resource Kind.
* @param group The resource API Group.
* @param apiVersion The resource API Version.
* @param isTransient Specify whether the resource is transient.
*/
@JsonPropertyOrder({
"kind",
"group",
"apiVersion",
"isTransient"
"kind",
"group",
"apiVersion",
"isTransient"
})
@Reflectable
public record ResourceType(@JsonProperty("kind") @NotNull String kind,
@JsonProperty("group") @Nullable String group,
@JsonProperty("apiVersion") @Nullable String apiVersion,
@JsonProperty("isTransient") boolean isTransient) implements HasMetadataAcceptable {

private static final String KIND = "kind";
private static final String API_VERSION = "apiVersion";

@ConstructorProperties({
"kind",
"group",
"apiVersion",
"isTransient"
"kind",
"group",
"apiVersion",
"isTransient"
})
public ResourceType {
Objects.requireNonNull(kind, "'kind' cannot be null");
Expand Down Expand Up @@ -73,20 +78,48 @@ public boolean canAccept(@NotNull ResourceType that) {
return true;
}

if (that.group != null && this.group != null) {
if (that.group != null &&
this.group != null &&
that.apiVersion != null &&
this.apiVersion != null
) {
return Objects.equals(group, that.group) &&
Objects.equals(apiVersion, that.apiVersion) &&
Objects.equals(kind, that.kind);
}

if (that.group != null && this.group != null
) {
return Objects.equals(group, that.group) &&
Objects.equals(kind, that.kind) &&
Objects.equals(apiVersion, that.apiVersion);
Objects.equals(kind, that.kind);
}

if (that.apiVersion != null && this.apiVersion != null) {
return Objects.equals(kind, that.kind) &&
Objects.equals(apiVersion, that.apiVersion);
Objects.equals(apiVersion, that.apiVersion);
}

return Objects.equals(kind, that.kind);
}

/**
* Static helper method to create a new {@link ResourceType} from the given {@link JsonNode}.
*
* @param node the {@link JsonNode}.
* @return a new {@link ResourceType}.
*/
public static ResourceType of(@NotNull JsonNode node) {
String apiVersion = Optional.ofNullable(node.get(API_VERSION))
.map(JsonNode::textValue)
.orElse(null);

String kind = Optional.ofNullable(node.get(KIND))
.map(JsonNode::textValue)
.orElse(null);

return (kind == null) ? null : ResourceType.of(kind, apiVersion);
}

/**
* Static helper method to create a new {@link ResourceType} from the given resource.
*
Expand All @@ -98,9 +131,9 @@ public static ResourceType of(@NotNull Resource resource) {
throw new IllegalArgumentException("resource cannot be null");
}
return of(
resource.getKind(),
resource.getApiVersion(),
Resource.isTransient(resource.getClass())
resource.getKind(),
resource.getApiVersion(),
Resource.isTransient(resource.getClass())
);
}

Expand All @@ -115,9 +148,9 @@ public static ResourceType of(@NotNull Class<? extends Resource> resource) {
throw new IllegalArgumentException("resource cannot be null");
}
return of(
Resource.getKind(resource),
Resource.getApiVersion(resource),
Resource.isTransient(resource)
Resource.getKind(resource),
Resource.getApiVersion(resource),
Resource.isTransient(resource)
);
}

Expand Down Expand Up @@ -158,7 +191,7 @@ public static ResourceType of(@NotNull final String kind,
if (Strings.isBlank(apiVersion)) {
return new ResourceType(kind, null, null, isTransient);
} else {
String[] versionParts = new String[]{null, apiVersion};
String[] versionParts = new String[]{apiVersion, null};
if (apiVersion.contains("/")) {
versionParts = apiVersion.split("/", 2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,4 @@ static List<StateChange> computeChanges(Map<String, ?> actualStates,
static <I, T, R> ChangeComputerBuilder<I, T, R> builder() {
return new DefaultChangeComputerBuilder<>();
}


}
Loading

0 comments on commit 2cb9586

Please sign in to comment.