Skip to content

Commit

Permalink
Handle Pipeline parameters
Browse files Browse the repository at this point in the history
Signed-off-by: cmoulliard <[email protected]>
  • Loading branch information
cmoulliard committed Aug 8, 2024
1 parent 786cd5e commit 2b5d0d7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 22 deletions.
10 changes: 10 additions & 0 deletions configurations/tekton/pack-builder-cfg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ job:
resourceType: PipelineRun
name: pack-builder-push
description: "This Pipeline builds a builder image using the pack CLI."
params:
- debug: true
- git-url: "https://github.com/redhat-buildpacks/ubi-image-builder.git"
- source-dir: "."
- output-image: "quay.io/snowdrop/ubi-builder"
- imageUrl: "buildpacksio/pack"
- imageTag: "latest"
- packCmdBuilderFlags:
- -v
- --publish
actions:
- name: git-clone
ref: bundle://quay.io/konflux-ci/tekton-catalog/task-git-clone:0.1@sha256:de0ca8872c791944c479231e21d68379b54877aaf42e5f766ef4a8728970f8b3
Expand Down
33 changes: 31 additions & 2 deletions src/main/java/dev/snowdrop/factory/tekton/pipeline/Pipelines.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static dev.snowdrop.factory.Bundles.getBundleURL;

Expand Down Expand Up @@ -98,13 +99,19 @@ public static <T> T createJob(Configurator cfg, List<Action> actions) {

Class<T> type;
List<PipelineTask> tasks = new ArrayList<>();
List<Param> pipelineParams = new ArrayList<>();
PipelineTask aTask;

String tektonResourceType = cfg.getJob().getResourceType().toLowerCase();
if (tektonResourceType == null) {
throw new RuntimeException("Missing tekton resource type");
}

List<Map<String, Object>> params = cfg.getJob().getParams();
if (params != null && !params.isEmpty()) {
pipelineParams = populatePipelineParams(cfg.getJob().getParams());
}

for (Action action : actions) {
if (action.getRef() != null) {
aTask = createTaskUsingRef(action.getName(), action.getRef());
Expand All @@ -120,7 +127,7 @@ public static <T> T createJob(Configurator cfg, List<Action> actions) {
switch (tektonResourceType) {
case "pipelinerun":
type = (Class<T>) PipelineRun.class;
return type.cast(generatePipelineRun(cfg, tasks));
return type.cast(generatePipelineRun(cfg, tasks, pipelineParams));

case "pipeline":
type = (Class<T>) Pipeline.class;
Expand All @@ -131,7 +138,27 @@ public static <T> T createJob(Configurator cfg, List<Action> actions) {
}
}

public static PipelineRun generatePipelineRun(Configurator cfg, List<PipelineTask> tasks) {
private static List<Param> populatePipelineParams(List<Map<String, Object>> params) {
List<Param> paramList = new ArrayList<>();
for (Map<String, Object> hash : params) {
hash.forEach((key, val) -> {
String newVal;
if (val instanceof String) {
newVal = String.valueOf(val);
} else if (val instanceof Boolean) {
newVal = Boolean.toString((Boolean) val);
} else {
newVal = String.valueOf(val); // Default to String representation
}

paramList.add(new ParamBuilder().withName(key).withValue(new ParamValue(newVal)).build());
}
);
}
return paramList;
}

public static PipelineRun generatePipelineRun(Configurator cfg, List<PipelineTask> tasks, List<Param> params) {
// @formatter:off
PipelineRun pipelineRun = new PipelineRunBuilder()
.withNewMetadata()
Expand All @@ -141,6 +168,7 @@ public static PipelineRun generatePipelineRun(Configurator cfg, List<PipelineTas
.withNamespace(cfg.getNamespace())
.endMetadata()
.withNewSpec()
.withParams(params)
.withNewPipelineSpec()
.withTasks(tasks)
.endPipelineSpec()
Expand All @@ -151,6 +179,7 @@ public static PipelineRun generatePipelineRun(Configurator cfg, List<PipelineTas
}

public static Pipeline generatePipeline(Configurator cfg, List<PipelineTask> tasks) {
// TODO: To be reviewed
// @formatter:off
Pipeline pipeline = new PipelineBuilder()
.withNewMetadata()
Expand Down
32 changes: 12 additions & 20 deletions src/main/java/dev/snowdrop/model/Job.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
package dev.snowdrop.model;

import java.util.List;
import java.util.Map;

/**
* The type Job.
*/
public class Job {
private String resourceType;
private String name;
private String description;
private String resourceType;
private List<Map<String, Object>> params;
private List<Action> actions;
private Builder builder;

public List<Map<String, Object>> getParams() {
return params;
}

public void setParams(List<Map<String, Object>> params) {
this.params = params;
}

public List<Action> getActions() {
return actions;
}
Expand Down Expand Up @@ -55,24 +65,6 @@ public void setDescription(String description) {
this.description = description;
}

/**
* Gets builder.
*
* @return the builder
*/
public Builder getBuilder() {
return builder;
}

/**
* Sets builder.
*
* @param builder the builder
*/
public void setBuilder(Builder builder) {
this.builder = builder;
}

/**
* Gets name.
*
Expand Down

0 comments on commit 2b5d0d7

Please sign in to comment.