33import static org .apache .commons .lang3 .StringUtils .isEmpty ;
44
55import java .io .IOException ;
6- import java .nio .charset .StandardCharsets ;
76import java .nio .file .Path ;
87import java .util .HashMap ;
98import java .util .Map ;
1211import jakarta .enterprise .context .RequestScoped ;
1312import jakarta .inject .Inject ;
1413
15- import org .apache .commons .io .FileUtils ;
1614import org .apache .commons .io .IOUtils ;
17- import org .apache .commons .text .StringSubstitutor ;
1815import org .eclipse .microprofile .config .ConfigProvider ;
1916import org .eclipse .microprofile .config .inject .ConfigProperty ;
2017import org .eclipse .microprofile .rest .client .inject .RestClient ;
2118import org .slf4j .Logger ;
2219import org .slf4j .LoggerFactory ;
2320
24- import com .fasterxml .jackson .core .JsonProcessingException ;
25- import com .fasterxml .jackson .databind .ObjectMapper ;
26- import com .fasterxml .jackson .dataformat .yaml .YAMLFactory ;
2721import com .redhat .hacbs .driver .clients .IndyService ;
2822import com .redhat .hacbs .driver .clients .IndyTokenRequestDTO ;
2923import com .redhat .hacbs .driver .clients .IndyTokenResponseDTO ;
3024import com .redhat .hacbs .driver .dto .BuildRequest ;
3125
26+ import io .fabric8 .kubernetes .api .model .Quantity ;
3227import io .fabric8 .kubernetes .client .KubernetesClient ;
28+ import io .fabric8 .tekton .client .TektonClient ;
29+ import io .fabric8 .tekton .pipeline .v1 .ParamBuilder ;
3330import io .fabric8 .tekton .pipeline .v1 .PipelineRun ;
3431import io .quarkus .oidc .client .OidcClient ;
3532import lombok .Setter ;
@@ -39,8 +36,6 @@ public class Driver {
3936
4037 private static final Logger logger = LoggerFactory .getLogger (Driver .class );
4138
42- private static final ObjectMapper yamlMapper = new ObjectMapper (new YAMLFactory ());
43-
4439 @ Inject
4540 OidcClient oidcClient ;
4641
@@ -77,35 +72,52 @@ public void create(BuildRequest buildRequest) {
7772 }
7873
7974 Map <String , String > templateProperties = new HashMap <>();
80- templateProperties .put ("REQUEST_PROCESSOR" , processor );
81- templateProperties .put ("QUAY_REPO" , quayRepo );
82- templateProperties .put ("URL" , buildRequest .getScmUrl ());
83- templateProperties .put ("REVISION" , buildRequest .getScmRevision ());
84- templateProperties .put ("RECIPE_IMAGE" , buildRequest .getRecipeImage ());
75+ templateProperties .put ("ACCESS_TOKEN" , tokenResponseDTO .getToken ());
76+ templateProperties .put ("BUILD_ID" , buildRequest .getRepositoryBuildContentId ());
77+ templateProperties .put ("BUILD_SCRIPT" , buildRequest .getBuildScript ());
8578 templateProperties .put ("BUILD_TOOL" , buildRequest .getBuildTool ());
8679 templateProperties .put ("BUILD_TOOL_VERSION" , buildRequest .getBuildToolVersion ());
8780 templateProperties .put ("JAVA_VERSION" , buildRequest .getJavaVersion ());
88- templateProperties .put ("BUILD_SCRIPT" , buildRequest .getBuildScript ());
89- templateProperties .put ("MVN_REPO_DEPLOY_URL" , buildRequest .getRepositoryDeployUrl ());
9081 templateProperties .put ("MVN_REPO_DEPENDENCIES_URL" , buildRequest .getRepositoryDependencyUrl ());
91- templateProperties .put ("ACCESS_TOKEN" , tokenResponseDTO .getToken ());
92- templateProperties .put ("BUILD_ID" , buildRequest .getRepositoryBuildContentId ());
82+ templateProperties .put ("MVN_REPO_DEPLOY_URL" , buildRequest .getRepositoryDeployUrl ());
83+ templateProperties .put ("QUAY_REPO" , quayRepo );
84+ templateProperties .put ("RECIPE_IMAGE" , buildRequest .getRecipeImage ());
85+ templateProperties .put ("JVM_BUILD_SERVICE_REQPROCESSOR_IMAGE" , processor );
86+ templateProperties .put ("REVISION" , buildRequest .getScmRevision ());
87+ templateProperties .put ("URL" , buildRequest .getScmUrl ());
9388
94- String pipeline = "" ;
89+ PipelineRun pipelineRun = null ;
9590 try {
91+ var tc = client .adapt (TektonClient .class );
92+ // Various ways to create the initial PipelineRun object. We can use an objectmapper,
93+ // client.getKubernetesSerialization() or the load calls on the Fabric8 objects.
9694 if (customPipeline .isEmpty ()) {
97- pipeline = IOUtils . resourceToString ( "pipeline.yaml" , StandardCharsets . UTF_8 ,
98- Thread .currentThread ().getContextClassLoader ());
95+ pipelineRun = tc . v1 (). pipelineRuns ()
96+ . load ( IOUtils . resourceToURL ( "pipeline.yaml" , Thread .currentThread ().getContextClassLoader ())). item ( );
9997 } else {
100- pipeline = FileUtils . readFileToString ( Path .of (customPipeline .get ()).toFile (), StandardCharsets . UTF_8 );
98+ pipelineRun = tc . v1 (). pipelineRuns (). load ( Path .of (customPipeline .get ()).toFile ()). item ( );
10199 }
102100 } catch (IOException e ) {
101+ e .printStackTrace ();
103102 // TODO: process
104103 }
105-
106- PipelineRun run = createModelNode (pipeline , templateProperties , PipelineRun .class );
107-
108- var created = client .resource (run ).inNamespace (buildRequest .getNamespace ()).create ();
104+ pipelineRun = pipelineRun .edit ().editOrNewSpec ()
105+ .addAllToParams (templateProperties .entrySet ().stream ()
106+ .map (t -> new ParamBuilder ().withName (t .getKey ()).withNewValue (t .getValue ()).build ()).toList ())
107+ .editFirstTaskRunSpec ()
108+ .editFirstStepSpec ()
109+ .editComputeResources ()
110+ .addToLimits ("memory" , new Quantity (buildRequest .getPodMemoryOverride ()))
111+ .addToRequests ("memory" , new Quantity (buildRequest .getPodMemoryOverride ()))
112+ .endComputeResources ()
113+ .endStepSpec ()
114+ .endTaskRunSpec ()
115+ .endSpec ().build ();
116+
117+ System .err .println ("### Got p " + pipelineRun );
118+ // PipelineRun run = createModelNode(pipeline, templateProperties, PipelineRun.class);
119+ //run.getSpec().setParams();
120+ var created = client .resource (pipelineRun ).inNamespace (buildRequest .getNamespace ()).create ();
109121 }
110122
111123 /**
@@ -120,17 +132,4 @@ public String getFreshAccessToken() {
120132 return result ;
121133 }
122134
123- private <T > T createModelNode (String resourceDefinition , Map <String , String > properties , Class <T > clazz ) {
124- String definition = StringSubstitutor .replace (resourceDefinition , properties , "%{" , "}" );
125-
126- if (logger .isTraceEnabled ()) {
127- logger .trace ("Node definition: {}" , definition );
128- }
129-
130- try {
131- return yamlMapper .readValue (definition , clazz );
132- } catch (JsonProcessingException e ) {
133- throw new RuntimeException (e );
134- }
135- }
136135}
0 commit comments