Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Attila Mészáros <csviri@gmail.com>
  • Loading branch information
csviri committed May 3, 2024
1 parent fee18ed commit 0d27a05
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

public class GlueMetadata {

String name;
String namespace;
private String name;
private String namespace;

public String getName() {
return name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package io.csviri.operator.glue.dependent;

import io.csviri.operator.glue.customresource.glue.Glue;
import io.csviri.operator.glue.templating.GenericTemplateHandler;
import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
import io.javaoperatorsdk.operator.api.reconciler.dependent.GarbageCollected;

public class GCGenericDependentResource extends GenericDependentResource
implements GarbageCollected<Glue> {

public GCGenericDependentResource(GenericKubernetesResource desired, String name,
public GCGenericDependentResource(GenericTemplateHandler genericTemplateHandler,
GenericKubernetesResource desired, String name,
boolean clusterScoped) {
super(desired, name, clusterScoped);
super(genericTemplateHandler, desired, name, clusterScoped);
}

public GCGenericDependentResource(String desiredTemplate, String name, boolean clusterScoped) {
super(desiredTemplate, name, clusterScoped);
public GCGenericDependentResource(GenericTemplateHandler genericTemplateHandler,
String desiredTemplate, String name, boolean clusterScoped) {
super(genericTemplateHandler, desiredTemplate, name, clusterScoped);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,24 @@ public class GenericDependentResource
private final boolean clusterScoped;

// optimize share between instances
private final GenericTemplateHandler genericTemplateHandler = new GenericTemplateHandler();
private final GenericTemplateHandler genericTemplateHandler;

public GenericDependentResource(GenericKubernetesResource desired, String name,
public GenericDependentResource(GenericTemplateHandler genericTemplateHandler,
GenericKubernetesResource desired, String name,
boolean clusterScoped) {
super(new GroupVersionKind(desired.getApiVersion(), desired.getKind()));
this.desired = desired;
this.desiredTemplate = null;
this.name = name;
this.clusterScoped = clusterScoped;
this.genericTemplateHandler = genericTemplateHandler;
}

public GenericDependentResource(String desiredTemplate, String name, boolean clusterScoped) {
public GenericDependentResource(GenericTemplateHandler genericTemplateHandler,
String desiredTemplate, String name, boolean clusterScoped) {
super(new GroupVersionKind(Utils.getApiVersionFromTemplate(desiredTemplate),
Utils.getKindFromTemplate(desiredTemplate)));
this.genericTemplateHandler = genericTemplateHandler;
this.name = name;
this.desiredTemplate = desiredTemplate;
this.desired = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ public class GlueReconciler implements Reconciler<Glue>, Cleaner<Glue>, ErrorSta
private final KubernetesResourceDeletedCondition deletePostCondition =
new KubernetesResourceDeletedCondition();

private final GenericTemplateHandler genericTemplateHandler = new GenericTemplateHandler();

private final GenericTemplateHandler genericTemplateHandler;

public GlueReconciler(ValidationAndErrorHandler validationAndErrorHandler,
InformerRegister informerRegister) {
InformerRegister informerRegister,
GenericTemplateHandler genericTemplateHandler) {
this.validationAndErrorHandler = validationAndErrorHandler;
this.informerRegister = informerRegister;
this.genericTemplateHandler = genericTemplateHandler;
}

/**
Expand Down Expand Up @@ -199,21 +200,23 @@ private void createAndAddDependentToWorkflow(Glue primary, Context<Glue> context
.ifPresent(c -> builder.withReconcilePrecondition(toCondition(c)));
}

private static GenericDependentResource createDependentResource(DependentResourceSpec spec,
private GenericDependentResource createDependentResource(DependentResourceSpec spec,
boolean leafDependent, Boolean resourceInSameNamespaceAsPrimary) {

if (leafDependent && resourceInSameNamespaceAsPrimary && !spec.isClusterScoped()) {
return spec.getResourceTemplate() != null
? new GCGenericDependentResource(spec.getResourceTemplate(), spec.getName(),
? new GCGenericDependentResource(genericTemplateHandler, spec.getResourceTemplate(),
spec.getName(),
spec.isClusterScoped())
: new GCGenericDependentResource(spec.getResource(), spec.getName(),
: new GCGenericDependentResource(genericTemplateHandler, spec.getResource(),
spec.getName(),
spec.isClusterScoped());
} else {
return spec.getResourceTemplate() != null
? new GenericDependentResource(spec.getResourceTemplate(), spec.getName(),
spec.isClusterScoped())
: new GenericDependentResource(spec.getResource(), spec.getName(),
spec.isClusterScoped());
? new GenericDependentResource(genericTemplateHandler,
spec.getResourceTemplate(), spec.getName(), spec.isClusterScoped())
: new GenericDependentResource(genericTemplateHandler,
spec.getResource(), spec.getName(), spec.isClusterScoped());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.csviri.operator.glue.customresource.operator.GlueOperatorSpec;
import io.csviri.operator.glue.customresource.operator.ResourceFlowOperatorStatus;
import io.csviri.operator.glue.reconciler.ValidationAndErrorHandler;
import io.csviri.operator.glue.templating.GenericTemplateHandler;
import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
Expand All @@ -27,7 +28,6 @@
import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource;

import jakarta.annotation.PostConstruct;
import jakarta.inject.Inject;

import static io.csviri.operator.glue.reconciler.glue.GlueReconciler.GLUE_RECONCILER_NAME;

Expand All @@ -43,19 +43,25 @@ public class GlueOperatorReconciler
public static final String PARENT_RELATED_RESOURCE_NAME = "parent";
public static final String GLUE_OPERATOR_RECONCILER_NAME = "glue-operator";

@Inject
ValidationAndErrorHandler validationAndErrorHandler;

@ConfigProperty(name = "quarkus.operator-sdk.controllers." + GLUE_RECONCILER_NAME + ".selector")
Optional<String> glueLabelSelector;

@Inject
ControllerConfig controllerConfig;
private final ControllerConfig controllerConfig;
private final ValidationAndErrorHandler validationAndErrorHandler;
private final GenericTemplateHandler genericTemplateHandler;

private Map<String, String> defaultGlueLabels;

private InformerEventSource<Glue, GlueOperator> glueEventSource;

public GlueOperatorReconciler(ControllerConfig controllerConfig,
ValidationAndErrorHandler validationAndErrorHandler,
GenericTemplateHandler genericTemplateHandler) {
this.controllerConfig = controllerConfig;
this.validationAndErrorHandler = validationAndErrorHandler;
this.genericTemplateHandler = genericTemplateHandler;
}

@PostConstruct
void init() {
defaultGlueLabels = initDefaultLabelsToAddToGlue();
Expand Down Expand Up @@ -120,13 +126,24 @@ private Glue createGlue(GenericKubernetesResource targetParentResource,
}

private ObjectMeta glueMetadata(GlueOperator glueOperator,
GenericKubernetesResource targetParentResource) {
GenericKubernetesResource parent) {

ObjectMetaBuilder objectMetaBuilder = new ObjectMetaBuilder();

objectMetaBuilder.withName(
glueName(targetParentResource.getMetadata().getName(), targetParentResource.getKind()))
.withNamespace(targetParentResource.getMetadata().getNamespace());
var glueMeta = glueOperator.getSpec().getGlueMetadata();
if (glueMeta != null) {
// todo optimize
var data = Map.of(PARENT_RELATED_RESOURCE_NAME, parent);
var glueName = genericTemplateHandler.processInputAndTemplate(data, glueMeta.getName());
var glueNamespace =
genericTemplateHandler.processInputAndTemplate(data, glueMeta.getNamespace());
objectMetaBuilder.withName(glueName);
objectMetaBuilder.withName(glueNamespace);
} else {
objectMetaBuilder.withName(
glueName(parent.getMetadata().getName(), parent.getKind()))
.withNamespace(parent.getMetadata().getNamespace());
}

objectMetaBuilder
.withLabels(Map.of(FOR_GLUE_OPERATOR_LABEL_KEY, FOR_GLUE_OPERATOR_LABEL_VALUE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,46 @@

import io.csviri.operator.glue.Utils;
import io.csviri.operator.glue.customresource.glue.Glue;
import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.quarkus.qute.Engine;
import io.quarkus.qute.Template;

import com.fasterxml.jackson.databind.ObjectMapper;

import jakarta.inject.Singleton;

@Singleton
public class GenericTemplateHandler {

public static final String WORKFLOW_METADATA_KEY = "glueMetadata";

private static final ObjectMapper objectMapper = new ObjectMapper();
private static final Engine engine = Engine.builder().addDefaults().build();

public String processTemplate(Map<String, Map<?, ?>> data, String template) {
Template parsedTemplate = engine.parse(template);
return parsedTemplate.data(data).render();
}

public String processInputAndTemplate(Map<String, GenericKubernetesResource> data,
String template) {
Map<String, Map<?, ?>> res = new HashMap<>();
data.forEach((key, value) -> res.put(key,
value == null ? null : objectMapper.convertValue(value, Map.class)));
return processTemplate(res, template);
}

public String processTemplate(String template, Glue primary,
Context<Glue> context) {

Template hello = engine.parse(template);
var data = createDataWithResources(primary, context);
return hello.data(data).render();
return processTemplate(data, template);
}

@SuppressWarnings("rawtypes")
private static Map<String, Map> createDataWithResources(Glue primary,
private static Map<String, Map<?, ?>> createDataWithResources(Glue primary,
Context<Glue> context) {
Map<String, Map> res = new HashMap<>();
Map<String, Map<?, ?>> res = new HashMap<>();
var actualResourcesByName = Utils.getActualResourcesByNameInWorkflow(context, primary);

actualResourcesByName.forEach((key, value) -> res.put(key,
Expand Down
5 changes: 4 additions & 1 deletion src/test/java/io/csviri/operator/glue/GlueOperatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.stream.IntStream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import io.csviri.operator.glue.customresource.TestCustomResource;
Expand Down Expand Up @@ -189,9 +190,11 @@ void parentWithLabelSelector() {
}


@Disabled
@Test
void secretCopySample() {

create(TestUtils
.loadGlueOperator("/glueoperator/ParentLabelSelector.yaml"));
}


Expand Down
2 changes: 1 addition & 1 deletion src/test/java/io/csviri/operator/glue/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

public class TestUtils {

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

public static final int CRD_READY_WAIT = 1000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ spec:
parent:
apiVersion: v1
kind: Namespace
glueMetadata:
name: copied-secret-glue
namespace: {parent.metadata.name}
childResources:
- name: secret
resource:
Expand Down

0 comments on commit 0d27a05

Please sign in to comment.