Skip to content

Commit

Permalink
WIP. Refactor the code to support to generate a resource of type: Pip…
Browse files Browse the repository at this point in the history
…eline(Run), Task(Run)

Signed-off-by: cmoulliard <[email protected]>
  • Loading branch information
cmoulliard committed Sep 19, 2024
1 parent d26edda commit a21b404
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 18 deletions.
8 changes: 8 additions & 0 deletions src/main/java/dev/snowdrop/factory/ActionProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.snowdrop.factory;

import dev.snowdrop.model.Configurator;
import io.fabric8.kubernetes.api.model.HasMetadata;

public interface ActionProvider {
HasMetadata buildResource(Configurator cfg);
}
22 changes: 22 additions & 0 deletions src/main/java/dev/snowdrop/factory/JobBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.snowdrop.factory;

import dev.snowdrop.model.Configurator;
import io.fabric8.kubernetes.api.model.HasMetadata;

public class JobBuilder {
private final JobProvider jobProvider;
private String resourceType;

public JobBuilder(JobProvider jobProvider) {
this.jobProvider = jobProvider;
}

public JobBuilder withResourceType(String resourceType) {
this.resourceType = resourceType;
return this;
}

public HasMetadata buildResource(Configurator cfg) {
return jobProvider.buildResource(cfg, resourceType);
}
}
15 changes: 10 additions & 5 deletions src/main/java/dev/snowdrop/factory/JobFactory.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package dev.snowdrop.factory;

public class JobFactory {
public static JobProvider withType(Type type) {
switch (type) {
case KONFLUX:
public static JobBuilder withProvider(Type providerType) {
JobProvider provider = createProvider(providerType.toString()); // convert string to enum if needed
return new JobBuilder(provider);
}

private static JobProvider createProvider(String provider) {
switch (provider.toUpperCase()) {
case "KONFLUX":
return new dev.snowdrop.factory.konflux.pipeline.Pipelines();
case TEKTON:
case "TEKTON":
return new dev.snowdrop.factory.tekton.pipeline.Pipelines();
default:
throw new IllegalArgumentException("Unknown type: " + type);
throw new IllegalArgumentException("Unknown provider type: " + provider);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/dev/snowdrop/factory/JobProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import io.fabric8.kubernetes.api.model.HasMetadata;

public interface JobProvider {
HasMetadata generatePipeline(Configurator cfg);
HasMetadata buildResource(Configurator cfg, String resourceType);
}
15 changes: 8 additions & 7 deletions src/main/java/dev/snowdrop/factory/TektonResource.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.snowdrop.factory;

import dev.snowdrop.factory.tekton.pipeline.TaskRefResolver;
import dev.snowdrop.factory.tekton.TaskRefResolver;
import dev.snowdrop.model.*;
import dev.snowdrop.model.ConfigMap;
import dev.snowdrop.model.Secret;
Expand All @@ -24,20 +24,21 @@ public class TektonResource {
private static final Logger logger = LoggerFactory.getLogger(TektonResource.class);

public static HasMetadata create(Configurator cfg) {
String domain = cfg.getDomain().toUpperCase();
Type TYPE = Type.valueOf(cfg.getType().toUpperCase());
String DOMAIN = cfg.getDomain().toUpperCase();
Type PROVIDER = Type.valueOf(cfg.getType().toUpperCase());

if (TYPE == null) {
if (PROVIDER == null) {
throw new RuntimeException("Missing type/provider");
}

if (domain == null) {
if (DOMAIN == null) {
throw new RuntimeException("Missing domain");
}

return JobFactory
.withType(TYPE)
.generatePipeline(cfg);
.withProvider(PROVIDER)
.withResourceType(cfg.getJob().getResourceType())
.buildResource(cfg);
}

public static PipelineTask createTaskWithEmbeddedScript(Action action, String runAfter, List<String> args, List<When> when, Map<String, Workspace> jobWorkspacesMap, List<TaskResult> results) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import dev.snowdrop.factory.AnnotationsProviderFactory;
import dev.snowdrop.factory.JobProvider;
import dev.snowdrop.factory.LabelsProviderFactory;
import dev.snowdrop.factory.Type;
import dev.snowdrop.model.*;
import dev.snowdrop.service.ConfiguratorSvc;
Expand Down Expand Up @@ -42,7 +40,7 @@ public class Pipelines implements JobProvider {
List<PipelineTask> tasks = new ArrayList<>();

@Override
public HasMetadata generatePipeline(Configurator cfg) {
public HasMetadata buildResource(Configurator cfg, String resourceType) {
TYPE = Type.valueOf(cfg.getType().toUpperCase());

PipelineTask aTask;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.snowdrop.factory.tekton.pipeline;
package dev.snowdrop.factory.tekton;

import dev.snowdrop.model.Bundle;
import io.fabric8.tekton.pipeline.v1.ParamValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class Pipelines implements JobProvider {
private static Type TYPE;

@Override
public HasMetadata generatePipeline(Configurator cfg) {
public HasMetadata buildResource(Configurator cfg, String resourceType) {
TYPE = Type.valueOf(cfg.getType().toUpperCase());
@SuppressWarnings("unchecked")
PipelineTask aTask;
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/dev/snowdrop/factory/tekton/task/Tasks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.snowdrop.factory.tekton.task;

import dev.snowdrop.factory.ActionProvider;
import dev.snowdrop.model.Configurator;
import io.fabric8.kubernetes.api.model.HasMetadata;

public class Tasks implements ActionProvider {
@Override
public HasMetadata buildResource(Configurator cfg) {
return null;
}
}

0 comments on commit a21b404

Please sign in to comment.