3
3
import static org .apache .commons .lang3 .StringUtils .isEmpty ;
4
4
5
5
import java .io .IOException ;
6
- import java .nio .charset .StandardCharsets ;
7
6
import java .nio .file .Path ;
8
7
import java .util .HashMap ;
9
8
import java .util .Map ;
12
11
import jakarta .enterprise .context .RequestScoped ;
13
12
import jakarta .inject .Inject ;
14
13
15
- import org .apache .commons .io .FileUtils ;
16
14
import org .apache .commons .io .IOUtils ;
17
- import org .apache .commons .text .StringSubstitutor ;
18
15
import org .eclipse .microprofile .config .ConfigProvider ;
19
16
import org .eclipse .microprofile .config .inject .ConfigProperty ;
20
17
import org .eclipse .microprofile .rest .client .inject .RestClient ;
21
18
import org .slf4j .Logger ;
22
19
import org .slf4j .LoggerFactory ;
23
20
24
- import com .fasterxml .jackson .core .JsonProcessingException ;
25
- import com .fasterxml .jackson .databind .ObjectMapper ;
26
- import com .fasterxml .jackson .dataformat .yaml .YAMLFactory ;
27
21
import com .redhat .hacbs .driver .clients .IndyService ;
28
22
import com .redhat .hacbs .driver .clients .IndyTokenRequestDTO ;
29
23
import com .redhat .hacbs .driver .clients .IndyTokenResponseDTO ;
30
24
import com .redhat .hacbs .driver .dto .BuildRequest ;
31
25
26
+ import io .fabric8 .kubernetes .api .model .Quantity ;
32
27
import io .fabric8 .kubernetes .client .KubernetesClient ;
28
+ import io .fabric8 .tekton .client .TektonClient ;
29
+ import io .fabric8 .tekton .pipeline .v1 .ParamBuilder ;
33
30
import io .fabric8 .tekton .pipeline .v1 .PipelineRun ;
34
31
import io .quarkus .oidc .client .OidcClient ;
35
32
import lombok .Setter ;
@@ -39,15 +36,12 @@ public class Driver {
39
36
40
37
private static final Logger logger = LoggerFactory .getLogger (Driver .class );
41
38
42
- private static final ObjectMapper yamlMapper = new ObjectMapper (new YAMLFactory ());
43
-
44
39
@ Inject
45
40
OidcClient oidcClient ;
46
41
47
42
@ RestClient
48
43
IndyService indyService ;
49
44
50
- // TODO: Could use KubernetesClient or OpenShiftClient
51
45
@ Inject
52
46
KubernetesClient client ;
53
47
@@ -72,40 +66,55 @@ public void create(BuildRequest buildRequest) {
72
66
tokenResponseDTO = indyService .getAuthToken (
73
67
new IndyTokenRequestDTO (buildRequest .getRepositoryBuildContentId ()),
74
68
"Bearer " + getFreshAccessToken ());
75
- logger .debug ("### new access token: {}" , tokenResponseDTO .getToken ()); // TODO: REMOVE
76
-
77
69
}
78
70
79
71
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 ());
85
75
templateProperties .put ("BUILD_TOOL" , buildRequest .getBuildTool ());
86
76
templateProperties .put ("BUILD_TOOL_VERSION" , buildRequest .getBuildToolVersion ());
87
77
templateProperties .put ("JAVA_VERSION" , buildRequest .getJavaVersion ());
88
- templateProperties .put ("BUILD_SCRIPT" , buildRequest .getBuildScript ());
89
- templateProperties .put ("MVN_REPO_DEPLOY_URL" , buildRequest .getRepositoryDeployUrl ());
90
78
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 ());
93
85
94
- String pipeline = "" ;
86
+ PipelineRun pipelineRun = null ;
95
87
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.
96
91
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 ( );
99
94
} 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 ( );
101
96
}
102
97
} catch (IOException e ) {
98
+ e .printStackTrace ();
103
99
// TODO: process
104
100
}
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 ();
109
118
}
110
119
111
120
/**
@@ -115,22 +124,6 @@ public void create(BuildRequest buildRequest) {
115
124
* @return fresh access token
116
125
*/
117
126
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 ();
135
128
}
136
129
}
0 commit comments