Skip to content

Commit

Permalink
fix: status detection in non-CR resources
Browse files Browse the repository at this point in the history
Fixes #673
  • Loading branch information
metacosm committed Aug 10, 2023
1 parent 94d4c18 commit 163bb31
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,21 @@ public static boolean isImplementationOf(IndexView index, ClassInfo info, DotNam
}
}
}

public static boolean hasField(IndexView index, ClassInfo info, String fieldName) {
ClassInfo tmpClassInfo = info;
while (tmpClassInfo != null) {
final var status = tmpClassInfo.field(fieldName);
if (status != null) {
return true;
}
// look up the hierarchy
if (tmpClassInfo.superName() != null) {
tmpClassInfo = index.getClassByName(tmpClassInfo.superName());
} else {
tmpClassInfo = null;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public class CustomResourceAugmentedClassInfo extends ReconciledResourceAugmente

public static final String EXISTING_CRDS_KEY = "existing-crds-key";

private boolean hasNonVoidStatus;

protected CustomResourceAugmentedClassInfo(ClassInfo classInfo, String associatedReconcilerName) {
super(classInfo, Constants.CUSTOM_RESOURCE, 2, associatedReconcilerName);
}
Expand All @@ -38,26 +36,22 @@ protected boolean doKeep(IndexView index, Logger log, Map<String, Object> contex
protected void doAugment(IndexView index, Logger log, Map<String, Object> context) {
super.doAugment(index, log, context);

// check if the primary is also a CR, in which case we also need to register its
// spec and status classes if we can determine them
// register spec and status for reflection if we're targeting a CustomResource
// note that this shouldn't be necessary anymore once https://github.com/quarkusio/quarkus/pull/26188
// is merged and available as the kubernetes-client extension will properly take care of the
// registration of the custom resource and associated status / spec classes for reflection
// registering these classes is not necessary anymore since the kubernetes client extension takes care of it
// however, we keep doing it here so that the name of these classes appear in the logs as has been the case since the first version of this extension
final var specClassName = typeAt(0).name().toString();
final var statusClassName = typeAt(1).name().toString();
hasNonVoidStatus = ClassUtils.isStatusNotVoid(statusClassName);
registerForReflection(specClassName);
registerForReflection(statusClassName);
}

@Override
public boolean isCR() {
return true;
protected boolean hasStatus(IndexView index) {
final var statusClassName = typeAt(1).name().toString();
return ClassUtils.isStatusNotVoid(statusClassName);
}

@Override
public boolean hasNonVoidStatus() {
return hasNonVoidStatus;
public boolean isCR() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package io.quarkiverse.operatorsdk.common;

import java.util.Map;

import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.IndexView;
import org.jboss.logging.Logger;

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

public class ReconciledResourceAugmentedClassInfo<T extends HasMetadata> extends ReconciledAugmentedClassInfo<T> {

public static final String STATUS = "status";
protected boolean hasStatus;

protected ReconciledResourceAugmentedClassInfo(ClassInfo classInfo,
DotName extendedOrImplementedClass, int expectedParameterTypesCardinality,
String associatedReconcilerName) {
Expand Down Expand Up @@ -35,7 +42,17 @@ public boolean isResource() {
return true;
}

@Override
protected void doAugment(IndexView index, Logger log, Map<String, Object> context) {
super.doAugment(index, log, context);
hasStatus = hasStatus(index);
}

protected boolean hasStatus(IndexView index) {
return ClassUtils.hasField(index, classInfo(), STATUS);
}

public boolean hasNonVoidStatus() {
return false;
return hasStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.quarkiverse.operatorsdk.common;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.certificates.v1.CertificateSigningRequest;
import io.quarkus.test.common.TestClassIndexer;

public class ClassUtilsTest {

@Test
void canFindStatusDirectly() {
final var index = TestClassIndexer.readIndex(CertificateSigningRequest.class);
final var info = index.getClassByName(CertificateSigningRequest.class);
assertTrue(ClassUtils.hasField(index, info, "status"));
}

@Test
void canFindStatusInHierachy() {
final var index = TestClassIndexer.readIndex(Child.class);
final var info = index.getClassByName(Child.class);
assertTrue(ClassUtils.hasField(index, info, "status"));
}

@Test
void shouldNotFindNonExistingField() {
final var index = TestClassIndexer.readIndex(Child.class);
final var info = index.getClassByName(Child.class);
assertFalse(ClassUtils.hasField(index, info, "foo"));
}

static abstract class Parent implements HasMetadata {
private Object status;
}

static class Child extends Parent {

@Override
public ObjectMeta getMetadata() {
return null;
}

@Override
public void setMetadata(ObjectMeta objectMeta) {

}

@Override
public void setApiVersion(String s) {

}
}
}

0 comments on commit 163bb31

Please sign in to comment.