Skip to content

improve: glue for glue operator handling #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.csviri.operator.glue.dependent.GenericDependentResource;
import io.csviri.operator.glue.dependent.GenericResourceDiscriminator;
import io.csviri.operator.glue.reconciler.ValidationAndErrorHandler;
import io.csviri.operator.glue.reconciler.operator.GlueOperatorReconciler;
import io.csviri.operator.glue.templating.GenericTemplateHandler;
import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
import io.fabric8.kubernetes.api.model.HasMetadata;
Expand All @@ -30,6 +31,7 @@
import io.javaoperatorsdk.operator.processing.dependent.workflow.WorkflowBuilder;

import static io.csviri.operator.glue.Utils.getResourceForSSAFrom;
import static io.csviri.operator.glue.reconciler.operator.GlueOperatorReconciler.FOR_GLUE_OPERATOR_LABEL_VALUE;
import static io.csviri.operator.glue.reconciler.operator.GlueOperatorReconciler.PARENT_RELATED_RESOURCE_NAME;

@ControllerConfiguration(name = GlueReconciler.GLUE_RECONCILER_NAME)
Expand Down Expand Up @@ -222,6 +224,9 @@ private Condition toCondition(ConditionSpec condition) {
}

private void addFinalizersToParentResource(Glue primary, Context<Glue> context) {
if (!isGlueOfAGlueOperator(primary)) {
return;
}
var parent = getParentRelatedResource(primary, context);

parent.ifPresent(p -> {
Expand All @@ -237,6 +242,9 @@ private void addFinalizersToParentResource(Glue primary, Context<Glue> context)
}

private void removeFinalizerForParent(Glue primary, Context<Glue> context) {
if (!isGlueOfAGlueOperator(primary)) {
return;
}
var parent = getParentRelatedResource(primary, context);
parent.ifPresentOrElse(p -> {
log.debug("Removing finalizer from parent. Glue name: {} namespace: {}",
Expand Down Expand Up @@ -295,4 +303,12 @@ public ErrorStatusUpdateControl<Glue> updateErrorStatus(Glue resource, Context<G
}
return validationAndErrorHandler.updateStatusErrorMessage(e, resource);
}

public static boolean isGlueOfAGlueOperator(Glue glue) {
var labelValue =
glue.getMetadata().getLabels().get(GlueOperatorReconciler.FOR_GLUE_OPERATOR_LABEL_KEY);
return FOR_GLUE_OPERATOR_LABEL_VALUE.equals(labelValue);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public class GlueOperatorReconciler

private static final Logger log = LoggerFactory.getLogger(GlueOperatorReconciler.class);

public static final String GLUE_LABEL_KEY = "foroperator";
public static final String GLUE_LABEL_VALUE = "true";
public static final String FOR_GLUE_OPERATOR_LABEL_KEY = "for-glue-operator";
public static final String FOR_GLUE_OPERATOR_LABEL_VALUE = "true";
public static final String PARENT_RELATED_RESOURCE_NAME = "parent";
public static final String GLUE_OPERATOR_RECONCILER_NAME = "glue-operator";

Expand Down Expand Up @@ -98,7 +98,7 @@ private Glue createGlue(GenericKubernetesResource targetParentResource,
.withName(
glueName(targetParentResource.getMetadata().getName(), targetParentResource.getKind()))
.withNamespace(targetParentResource.getMetadata().getNamespace())
.withLabels(Map.of(GLUE_LABEL_KEY, GLUE_LABEL_VALUE))
.withLabels(Map.of(FOR_GLUE_OPERATOR_LABEL_KEY, FOR_GLUE_OPERATOR_LABEL_VALUE))
.build());
glue.setSpec(toWorkflowSpec(glueOperator.getSpec()));

Expand Down Expand Up @@ -160,7 +160,7 @@ public Map<String, EventSource> prepareEventSources(
EventSourceContext<GlueOperator> eventSourceContext) {
glueEventSource = new InformerEventSource<>(
InformerConfiguration.from(Glue.class, eventSourceContext)
.withLabelSelector(GLUE_LABEL_KEY + "=" + GLUE_LABEL_VALUE)
.withLabelSelector(FOR_GLUE_OPERATOR_LABEL_KEY + "=" + FOR_GLUE_OPERATOR_LABEL_VALUE)
.build(),
eventSourceContext);
return EventSourceInitializer.nameEventSources(glueEventSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class GlueLabelSelectorTest extends TestBase {
@Test
void testLabelSelectorHandling() {
Glue glue =
TestUtils.loadResoureFlow("/glue/SimpleGlue.yaml");
TestUtils.loadGlue("/glue/SimpleGlue.yaml");
glue = create(glue);

await().pollDelay(INITIAL_RECONCILE_WAIT_TIMEOUT).untilAsserted(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ void applyCRD() {

@Test
void testGlueOperatorLabelSelector() {
var go = create(TestUtils
.loadResourceFlowOperator("/glueoperator/SimpleGlueOperator.yaml"));
create(TestUtils
.loadGlueOperator("/glueoperator/SimpleGlueOperator.yaml"));

var testCR = create(TestData.testCustomResource());

Expand All @@ -49,7 +49,7 @@ void testGlueOperatorLabelSelector() {
});

delete(testCR);
await().untilAsserted(() -> {
await().timeout(TestUtils.GC_WAIT_TIMEOUT).untilAsserted(() -> {
var glue = get(Glue.class, GlueOperatorReconciler.glueName(testCR.getMetadata().getName(),
testCR.getKind()));
assertThat(glue).isNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void applyCRD() {
@Test
void testGlueOperatorLabelSelector() {
var go = create(TestUtils
.loadResourceFlowOperator("/glueoperator/SimpleGlueOperator.yaml"));
.loadGlueOperator("/glueoperator/SimpleGlueOperator.yaml"));

var testCR = create(TestData.testCustomResource());

Expand Down
18 changes: 9 additions & 9 deletions src/test/java/io/csviri/operator/glue/GlueOperatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import io.quarkus.test.junit.QuarkusTest;

import static io.csviri.operator.glue.TestData.*;
import static io.csviri.operator.glue.TestUtils.GC_WAIT_TIMEOUT_SECOND;
import static io.csviri.operator.glue.TestUtils.GC_WAIT_TIMEOUT;
import static io.csviri.operator.glue.customresource.TestCustomResource.CR_GROUP;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
Expand Down Expand Up @@ -55,7 +55,7 @@ void smokeTest() {
@Test
void templating() {
create(TestUtils
.loadResourceFlowOperator("/glueoperator/SimpleGlueOperator.yaml"));
.loadGlueOperator("/glueoperator/SimpleGlueOperator.yaml"));

var cr = create(testCustomResource());
String initialValue = cr.getSpec().getValue();
Expand All @@ -78,7 +78,7 @@ void templating() {

delete(cr);

await().timeout(Duration.ofMinutes(5)).untilAsserted(() -> {
await().timeout(GC_WAIT_TIMEOUT).untilAsserted(() -> {
var cm1 = get(ConfigMap.class, name);
var actualCR = get(TestCustomResource.class, name);
assertThat(cm1).isNull();
Expand All @@ -91,7 +91,7 @@ void templating() {
void simpleConcurrencyTest() {
int num = 10;
create(TestUtils
.loadResourceFlowOperator("/glueoperator/Concurrency.yaml"));
.loadGlueOperator("/glueoperator/Concurrency.yaml"));

var resources =
IntStream.range(0, num).mapToObj(n -> create(testCustomResource(n))).toList();
Expand All @@ -115,9 +115,9 @@ void simpleConcurrencyTest() {
void simpleConcurrencyForMultipleOperatorTest() {
int num = 10;
create(TestUtils
.loadResourceFlowOperator("/glueoperator/Concurrency.yaml"));
.loadGlueOperator("/glueoperator/Concurrency.yaml"));
create(TestUtils
.loadResourceFlowOperator("/glueoperator/Concurrency2.yaml"));
.loadGlueOperator("/glueoperator/Concurrency2.yaml"));

var crs =
IntStream.range(0, num).mapToObj(n -> create(testCustomResource(n))).toList();
Expand All @@ -137,7 +137,7 @@ void simpleConcurrencyForMultipleOperatorTest() {
crs.forEach(this::delete);
cr2s.forEach(this::delete);

await().timeout(GC_WAIT_TIMEOUT_SECOND)
await().timeout(GC_WAIT_TIMEOUT)
.untilAsserted(() -> IntStream.range(0, num).forEach(n -> {
var cm = get(ConfigMap.class, TEST_RESOURCE_PREFIX + n);
assertThat(cm).isNull();
Expand All @@ -149,7 +149,7 @@ void simpleConcurrencyForMultipleOperatorTest() {
@Test
void nonUniqueNameTest() {
var go = create(TestUtils
.loadResourceFlowOperator("/glueoperator/NonUniqueName.yaml"));
.loadGlueOperator("/glueoperator/NonUniqueName.yaml"));

await().untilAsserted(() -> {
var actual = get(GlueOperator.class, go.getMetadata().getName());
Expand All @@ -163,7 +163,7 @@ void nonUniqueNameTest() {
@Test
void parentWithLabelSelector() {
create(TestUtils
.loadResourceFlowOperator("/glueoperator/ParentLabelSelector.yaml"));
.loadGlueOperator("/glueoperator/ParentLabelSelector.yaml"));

var cr = create(testCustomResource());
String name = cr.getMetadata().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void applyCRD() {
void simpleRelatedResourceUsage() {
create(secret());
Glue glue =
TestUtils.loadResoureFlow("/glue/RelatedResourceSimpleWithCondition.yaml");
TestUtils.loadGlue("/glue/RelatedResourceSimpleWithCondition.yaml");

create(glue);

Expand Down Expand Up @@ -64,7 +64,7 @@ void multipleResourceNamesInRelated() {
create(secret("test-secret2", BASE64_VALUE_2));

Glue glue =
create(TestUtils.loadResoureFlow("/glue/MultiNameRelatedResource.yaml"));
create(TestUtils.loadGlue("/glue/MultiNameRelatedResource.yaml"));

await().untilAsserted(() -> {
var cm1 = get(ConfigMap.class, "cm1");
Expand All @@ -85,7 +85,7 @@ void multipleResourceNamesInRelated() {
void managedAndRelatedResourceOfSameTypeAndTriggering() {
var relatedConfigMap = create(configMap());
Glue glue =
create(TestUtils.loadResoureFlow("/glue/RelatesResourceSameType.yaml"));
create(TestUtils.loadGlue("/glue/RelatesResourceSameType.yaml"));

await().untilAsserted(() -> {
var cm1 = get(ConfigMap.class, "cm1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class GlueResourceLabelSelectorTest extends TestBase {

@Test
void showCreatingResourceWithLabelSelectorAndLabel() {
var glue = create(TestUtils.loadResoureFlow("/glue/SimpleGlueWithLabeledResource.yaml"));
var glue = create(TestUtils.loadGlue("/glue/SimpleGlueWithLabeledResource.yaml"));

// this serves more like a sample, hard to test here if the label selector is on informer
await().untilAsserted(() -> {
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/io/csviri/operator/glue/GlueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class GlueTest extends TestBase {
@Test
void simpleTemplating() {
Glue glue =
TestUtils.loadResoureFlow("/glue/Templating.yaml");
TestUtils.loadGlue("/glue/Templating.yaml");
glue = create(glue);

await().untilAsserted(() -> {
Expand Down Expand Up @@ -64,7 +64,7 @@ void simpleTemplating() {
@Test
void crossReferenceResource() {
Glue glue =
TestUtils.loadResoureFlow("/glue/CrossReferenceResource.yaml");
TestUtils.loadGlue("/glue/CrossReferenceResource.yaml");
glue = create(glue);

await().untilAsserted(() -> {
Expand Down Expand Up @@ -103,7 +103,7 @@ void crossReferenceResource() {
@Test
void javaScriptCondition() {
Glue glue =
TestUtils.loadResoureFlow("/glue/TwoResourcesAndCondition.yaml");
TestUtils.loadGlue("/glue/TwoResourcesAndCondition.yaml");
create(glue);

await().pollDelay(Duration.ofMillis(150)).untilAsserted(() -> {
Expand Down Expand Up @@ -138,7 +138,7 @@ void javaScriptCondition() {

@Test
void stringTemplate() {
Glue glue = create(TestUtils.loadResoureFlow("/glue/ResourceTemplate.yaml"));
Glue glue = create(TestUtils.loadGlue("/glue/ResourceTemplate.yaml"));

await().timeout(Duration.ofSeconds(120)).untilAsserted(() -> {
var cm1 = get(ConfigMap.class, "templconfigmap1");
Expand Down Expand Up @@ -188,7 +188,7 @@ void simpleConcurrencyTest() {

@Test
void changingWorkflow() {
Glue glue = create(TestUtils.loadResoureFlow("/glue/ChanginResources.yaml"));
Glue glue = create(TestUtils.loadGlue("/glue/ChanginResources.yaml"));

await().untilAsserted(() -> {
var cm1 = get(ConfigMap.class, "configmap1");
Expand Down Expand Up @@ -226,7 +226,7 @@ void changingWorkflow() {

@Test
void nonUniqueNameResultsInErrorMessageOnStatus() {
Glue glue = create(TestUtils.loadResoureFlow("/glue/NonUniqueName.yaml"));
Glue glue = create(TestUtils.loadGlue("/glue/NonUniqueName.yaml"));

await().untilAsserted(() -> {
var actualGlue = get(Glue.class, glue.getMetadata().getName());
Expand Down Expand Up @@ -271,7 +271,7 @@ private List<Glue> testWorkflowList(int num) {
List<Glue> res = new ArrayList<>();
IntStream.range(0, num).forEach(index -> {
Glue w =
TestUtils.loadResoureFlow("/glue/TemplateForConcurrency.yaml");
TestUtils.loadGlue("/glue/TemplateForConcurrency.yaml");
w.getMetadata().setName(w.getMetadata().getName() + index);
res.add(w);
});
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/io/csviri/operator/glue/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@

public class TestUtils {

public static final Duration GC_WAIT_TIMEOUT_SECOND = Duration.ofSeconds(45);
public static final Duration GC_WAIT_TIMEOUT = Duration.ofSeconds(90);
public static final Duration INITIAL_RECONCILE_WAIT_TIMEOUT = Duration.ofMillis(150);

public static final int CRD_READY_WAIT = 1000;


private static final Logger log = LoggerFactory.getLogger(TestUtils.class);

public static Glue loadResoureFlow(String path) {
public static Glue loadGlue(String path) {
try (InputStream is = TestUtils.class.getResourceAsStream(path)) {
return Serialization.unmarshal(is, Glue.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static GlueOperator loadResourceFlowOperator(String path) {
public static GlueOperator loadGlueOperator(String path) {
try (InputStream is = TestUtils.class.getResourceAsStream(path)) {
return Serialization.unmarshal(is, GlueOperator.class);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.dsl.NonDeletingOperation;

import static io.csviri.operator.glue.TestUtils.GC_WAIT_TIMEOUT_SECOND;
import static io.csviri.operator.glue.TestUtils.GC_WAIT_TIMEOUT;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

Expand Down Expand Up @@ -55,7 +55,7 @@ void testMutationHookDeployment() {

client.resource(glue).delete();

await().timeout(GC_WAIT_TIMEOUT_SECOND).untilAsserted(() -> {
await().timeout(GC_WAIT_TIMEOUT).untilAsserted(() -> {
var deployment = client.apps().deployments().withName("pod-mutating-hook").get();
assertThat(deployment).isNull();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.dsl.NonDeletingOperation;

import static io.csviri.operator.glue.TestUtils.GC_WAIT_TIMEOUT_SECOND;
import static io.csviri.operator.glue.TestUtils.GC_WAIT_TIMEOUT;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

Expand Down Expand Up @@ -62,7 +62,7 @@ void testWebPageCRUDOperations() {

client.resource(createdWebPage).delete();

await().timeout(GC_WAIT_TIMEOUT_SECOND).untilAsserted(() -> {
await().timeout(GC_WAIT_TIMEOUT).untilAsserted(() -> {
var deployment =
client.resources(Deployment.class).withName(webPage.getMetadata().getName()).get();
var configMap =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void webPageCRUD() {

delete(webPage);

await().timeout(TestUtils.GC_WAIT_TIMEOUT_SECOND).untilAsserted(() -> {
await().timeout(TestUtils.GC_WAIT_TIMEOUT).untilAsserted(() -> {
var deployment = get(Deployment.class, webPage.getMetadata().getName());
assertThat(deployment).isNull();
});
Expand Down
Loading