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,15 +36,12 @@ 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
4742 @ RestClient
4843 IndyService indyService ;
4944
50- // TODO: Could use KubernetesClient or OpenShiftClient
5145 @ Inject
5246 KubernetesClient client ;
5347
@@ -72,40 +66,55 @@ public void create(BuildRequest buildRequest) {
7266 tokenResponseDTO = indyService .getAuthToken (
7367 new IndyTokenRequestDTO (buildRequest .getRepositoryBuildContentId ()),
7468 "Bearer " + getFreshAccessToken ());
75- logger .debug ("### new access token: {}" , tokenResponseDTO .getToken ()); // TODO: REMOVE
76-
7769 }
7870
7971 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 ());
72+ templateProperties .put ("ACCESS_TOKEN" , tokenResponseDTO .getToken ());
73+ templateProperties .put ("BUILD_ID" , buildRequest .getRepositoryBuildContentId ());
74+ templateProperties .put ("BUILD_SCRIPT" , buildRequest .getBuildScript ());
8575 templateProperties .put ("BUILD_TOOL" , buildRequest .getBuildTool ());
8676 templateProperties .put ("BUILD_TOOL_VERSION" , buildRequest .getBuildToolVersion ());
8777 templateProperties .put ("JAVA_VERSION" , buildRequest .getJavaVersion ());
88- templateProperties .put ("BUILD_SCRIPT" , buildRequest .getBuildScript ());
89- templateProperties .put ("MVN_REPO_DEPLOY_URL" , buildRequest .getRepositoryDeployUrl ());
9078 templateProperties .put ("MVN_REPO_DEPENDENCIES_URL" , buildRequest .getRepositoryDependencyUrl ());
91- templateProperties .put ("ACCESS_TOKEN" , tokenResponseDTO .getToken ());
92- templateProperties .put ("BUILD_ID" , buildRequest .getRepositoryBuildContentId ());
79+ templateProperties .put ("MVN_REPO_DEPLOY_URL" , buildRequest .getRepositoryDeployUrl ());
80+ templateProperties .put ("QUAY_REPO" , quayRepo );
81+ templateProperties .put ("RECIPE_IMAGE" , buildRequest .getRecipeImage ());
82+ templateProperties .put ("JVM_BUILD_SERVICE_REQPROCESSOR_IMAGE" , processor );
83+ templateProperties .put ("REVISION" , buildRequest .getScmRevision ());
84+ templateProperties .put ("URL" , buildRequest .getScmUrl ());
9385
94- String pipeline = "" ;
86+ PipelineRun pipelineRun = null ;
9587 try {
88+ var tc = client .adapt (TektonClient .class );
89+ // Various ways to create the initial PipelineRun object. We can use an objectmapper,
90+ // client.getKubernetesSerialization() or the load calls on the Fabric8 objects.
9691 if (customPipeline .isEmpty ()) {
97- pipeline = IOUtils . resourceToString ( "pipeline.yaml" , StandardCharsets . UTF_8 ,
98- Thread .currentThread ().getContextClassLoader ());
92+ pipelineRun = tc . v1 (). pipelineRuns ()
93+ . load ( IOUtils . resourceToURL ( "pipeline.yaml" , Thread .currentThread ().getContextClassLoader ())). item ( );
9994 } else {
100- pipeline = FileUtils . readFileToString ( Path .of (customPipeline .get ()).toFile (), StandardCharsets . UTF_8 );
95+ pipelineRun = tc . v1 (). pipelineRuns (). load ( Path .of (customPipeline .get ()).toFile ()). item ( );
10196 }
10297 } catch (IOException e ) {
98+ e .printStackTrace ();
10399 // TODO: process
104100 }
105-
106- PipelineRun run = createModelNode (pipeline , templateProperties , PipelineRun .class );
107-
108- var created = client .resource (run ).inNamespace (buildRequest .getNamespace ()).create ();
101+ pipelineRun = pipelineRun .edit ().editOrNewSpec ()
102+ .addAllToParams (templateProperties .entrySet ().stream ()
103+ .map (t -> new ParamBuilder ().withName (t .getKey ()).withNewValue (t .getValue ()).build ()).toList ())
104+ .editFirstTaskRunSpec ()
105+ .editFirstStepSpec ()
106+ .editComputeResources ()
107+ .addToLimits ("memory" , new Quantity (buildRequest .getPodMemoryOverride ()))
108+ .addToRequests ("memory" , new Quantity (buildRequest .getPodMemoryOverride ()))
109+ .endComputeResources ()
110+ .endStepSpec ()
111+ .endTaskRunSpec ()
112+ .endSpec ().build ();
113+
114+ System .err .println ("### Got p " + pipelineRun );
115+ // PipelineRun run = createModelNode(pipeline, templateProperties, PipelineRun.class);
116+ //run.getSpec().setParams();
117+ var created = client .resource (pipelineRun ).inNamespace (buildRequest .getNamespace ()).create ();
109118 }
110119
111120 /**
@@ -115,22 +124,6 @@ public void create(BuildRequest buildRequest) {
115124 * @return fresh access token
116125 */
117126 public String getFreshAccessToken () {
118- var result = oidcClient .getTokens ().await ().indefinitely ().getAccessToken ();
119- logger .debug ("### access token: {}" , result ); // TODO: REMOVE
120- return result ;
121- }
122-
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- }
127+ return oidcClient .getTokens ().await ().indefinitely ().getAccessToken ();
135128 }
136129}
0 commit comments