Skip to content

Commit 911dc6c

Browse files
committed
Add initial REST API. Add configuration for build-driver
1 parent fe6c4cb commit 911dc6c

File tree

13 files changed

+370
-20
lines changed

13 files changed

+370
-20
lines changed

java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/Pipeline.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import com.redhat.hacbs.driver.Driver;
1010
import com.redhat.hacbs.driver.dto.BuildRequest;
11+
import com.redhat.hacbs.driver.dto.BuildResponse;
1112

1213
import picocli.CommandLine;
1314

@@ -19,7 +20,7 @@ public class Pipeline extends Base implements Runnable {
1920
@Inject
2021
Driver driver;
2122

22-
@CommandLine.Option(names = "--quay", description = "Quay repo", defaultValue = "quay.io/redhat-user-workloads-stage/pnc-devel-tenant/pnc")
23+
@CommandLine.Option(names = "--quay", description = "Quay repo", defaultValue = "quay.io/redhat-user-workloads-stage/pnc-devel-tenant/pnc-konflux")
2324
String quayRepo;
2425

2526
@CommandLine.Option(names = "--processor", description = "Request Process Image", defaultValue = "quay.io/redhat-user-workloads/konflux-jbs-pnc-tenant/jvm-build-service/build-request-processor:latest")
@@ -47,6 +48,8 @@ public void run() {
4748
// Just use default from buildah-oci-ta for now.
4849
.podMemoryOverride("4Gi")
4950
.build();
50-
driver.create(request);
51+
BuildResponse b = driver.create(request);
52+
53+
logger.info("Got response {}", b);
5154
}
5255
}

java-components/driver/pom.xml

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
3-
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
43
<modelVersion>4.0.0</modelVersion>
54
<parent>
65
<groupId>io.github.redhat-appstudio.jvmbuild</groupId>
@@ -45,6 +44,11 @@
4544
<artifactId>commons-text</artifactId>
4645
<version>${version.commons-text}</version>
4746
</dependency>
47+
<dependency>
48+
<groupId>org.jboss.pnc</groupId>
49+
<artifactId>pnc-api</artifactId>
50+
<version>3.0.0</version>
51+
</dependency>
4852
<!-- Can't just import PNC environment-driver code as it uses Quarkus 2.16 versus 3.x (javax -> jakarta) -->
4953
<!-- <dependency>-->
5054
<!-- <groupId>org.jboss.pnc</groupId>-->
@@ -58,6 +62,30 @@
5862
<version>${version.lombok}</version>
5963
<scope>provided</scope>
6064
</dependency>
65+
<dependency>
66+
<groupId>io.quarkus</groupId>
67+
<artifactId>quarkus-info</artifactId>
68+
</dependency>
69+
<dependency>
70+
<groupId>io.quarkus</groupId>
71+
<artifactId>quarkus-junit5</artifactId>
72+
<scope>test</scope>
73+
</dependency>
74+
<dependency>
75+
<groupId>io.quarkus</groupId>
76+
<artifactId>quarkus-junit5-mockito</artifactId>
77+
<scope>test</scope>
78+
</dependency>
79+
<dependency>
80+
<groupId>io.rest-assured</groupId>
81+
<artifactId>rest-assured</artifactId>
82+
<scope>test</scope>
83+
</dependency>
84+
<dependency>
85+
<groupId>io.quarkus</groupId>
86+
<artifactId>quarkus-test-kubernetes-client</artifactId>
87+
<scope>test</scope>
88+
</dependency>
6189
</dependencies>
6290
<build>
6391
<plugins>
@@ -86,6 +114,17 @@
86114
</systemPropertyVariables>
87115
</configuration>
88116
</plugin>
117+
<plugin>
118+
<groupId>org.apache.maven.plugins</groupId>
119+
<artifactId>maven-jar-plugin</artifactId>
120+
<configuration>
121+
<archive>
122+
<manifestEntries combine.children="append">
123+
<Build-Time>${maven.build.timestamp}</Build-Time>
124+
</manifestEntries>
125+
</archive>
126+
</configuration>
127+
</plugin>
89128
</plugins>
90129
</build>
91130
<profiles>
@@ -109,11 +148,8 @@
109148
</goals>
110149
<configuration>
111150
<systemPropertyVariables>
112-
<native.image.path>
113-
${project.build.directory}/${project.build.finalName}-runner
114-
</native.image.path>
115-
<java.util.logging.manager>org.jboss.logmanager.LogManager
116-
</java.util.logging.manager>
151+
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
152+
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
117153
<maven.home>${maven.home}</maven.home>
118154
</systemPropertyVariables>
119155
</configuration>

java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
import java.io.IOException;
66
import java.nio.file.Path;
7+
import java.util.ArrayList;
78
import java.util.HashMap;
9+
import java.util.List;
810
import java.util.Map;
911
import java.util.Optional;
1012

@@ -22,7 +24,10 @@
2224
import com.redhat.hacbs.driver.clients.IndyTokenRequestDTO;
2325
import com.redhat.hacbs.driver.clients.IndyTokenResponseDTO;
2426
import com.redhat.hacbs.driver.dto.BuildRequest;
27+
import com.redhat.hacbs.driver.dto.BuildResponse;
28+
import com.redhat.hacbs.driver.dto.CancelRequest;
2529

30+
import io.fabric8.knative.internal.pkg.apis.Condition;
2631
import io.fabric8.kubernetes.api.model.Quantity;
2732
import io.fabric8.kubernetes.client.KubernetesClient;
2833
import io.fabric8.tekton.client.TektonClient;
@@ -49,15 +54,20 @@ public class Driver {
4954
private String accessToken;
5055

5156
@Setter
52-
private String quayRepo = "quay.io/redhat-user-workloads/konflux-jbs-pnc-tenant/jvm-build-service/build-request-processor:latest";
57+
@ConfigProperty(name = "konflux-build-driver.konflux-processor")
58+
String processor;
5359

5460
@Setter
55-
private String processor = "quay.io/redhat-user-workloads-stage/pnc-devel-tenant/pnc";
61+
@ConfigProperty(name = "konflux-build-driver.quay-repo")
62+
String quayRepo;
63+
64+
@ConfigProperty(name = "konflux-build-driver.pipeline-resolver")
65+
String resolverTarget;
5666

5767
@ConfigProperty(name = "build-driver.pipeline")
5868
Optional<String> customPipeline;
5969

60-
public void create(BuildRequest buildRequest) {
70+
public BuildResponse create(BuildRequest buildRequest) {
6171
IndyTokenResponseDTO tokenResponseDTO = new IndyTokenResponseDTO(accessToken);
6272

6373
if (isEmpty(accessToken)) {
@@ -95,10 +105,13 @@ public void create(BuildRequest buildRequest) {
95105
pipelineRun = tc.v1().pipelineRuns().load(Path.of(customPipeline.get()).toFile()).item();
96106
}
97107
} catch (IOException e) {
98-
e.printStackTrace();
99-
// TODO: process
108+
throw new RuntimeException(e);
100109
}
101110
pipelineRun = pipelineRun.edit().editOrNewSpec()
111+
.editPipelineRef()
112+
.editFirstParam().editOrNewValue().withStringVal(resolverTarget).endValue()
113+
.endParam()
114+
.endPipelineRef()
102115
.addAllToParams(templateProperties.entrySet().stream()
103116
.map(t -> new ParamBuilder().withName(t.getKey()).withNewValue(t.getValue()).build()).toList())
104117
.editFirstTaskRunSpec()
@@ -111,14 +124,37 @@ public void create(BuildRequest buildRequest) {
111124
.endTaskRunSpec()
112125
.endSpec().build();
113126

114-
System.err.println("### Got p " + pipelineRun);
115127
var created = client.resource(pipelineRun).inNamespace(buildRequest.namespace()).create();
116-
System.err.println("### Got c " + created);
128+
129+
return BuildResponse.builder().namespace(buildRequest.namespace()).pipelineId(created.getMetadata().getName()).build();
130+
}
131+
132+
public void cancel(CancelRequest request) {
133+
var tc = client.adapt(TektonClient.class);
134+
var pipeline = tc.v1beta1().pipelineRuns().inNamespace(request.namespace()).withName(request.pipelineId()).get();
135+
136+
logger.info("Retrieved pipeline {}", pipeline);
137+
138+
List<Condition> conditions = new ArrayList<>();
139+
// https://tekton.dev/docs/pipelines/pipelineruns/#monitoring-execution-status
140+
Condition cancelCondition = new Condition();
141+
cancelCondition.setType("Succeeded");
142+
cancelCondition.setStatus("False");
143+
cancelCondition.setReason("CancelledRunFinally");
144+
cancelCondition.setMessage("The PipelineRun was cancelled");
145+
conditions.add(cancelCondition);
146+
147+
pipeline.getStatus().setConditions(conditions);
148+
149+
tc.v1beta1().pipelineRuns().inNamespace(request.namespace()).resource(pipeline).updateStatus();
150+
// https://tekton.dev/docs/pipelines/pipelineruns/#gracefully-cancelling-a-pipelinerun
151+
// tc.v1beta1().pipelineRuns().updateStatus().inNamespace(request.namespace()).withName(request.pipelineId()).patch()
152+
// .edit(p -> p.edit().editOrNewSpec().withStatus("CancelledRunFinally").endSpec().build());
117153
}
118154

119155
/**
120-
* Get a fresh access token for the service account. This is done because we want to get a super-new token to be
121-
* used since we're not entirely sure when the http request will be done inside the completablefuture.
156+
* Get a fresh access token for the service account. This is done because we want to get a
157+
* super-new token to be used since we're not entirely sure when the http request will be done.
122158
*
123159
* @return fresh access token
124160
*/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.redhat.hacbs.driver.dto;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
5+
import lombok.Builder;
6+
7+
@Builder(builderClassName = "Builder")
8+
@JsonIgnoreProperties(ignoreUnknown = true)
9+
public record BuildResponse(String pipelineId, String namespace) {
10+
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.redhat.hacbs.driver.dto;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
5+
import lombok.Builder;
6+
7+
@Builder(builderClassName = "Builder")
8+
@JsonIgnoreProperties(ignoreUnknown = true)
9+
public record CancelRequest(String pipelineId, String namespace) {
10+
11+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* JBoss, Home of Professional Open Source.
3+
* Copyright 2021 Red Hat, Inc., and individual contributors
4+
* as indicated by the @author tags.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.redhat.hacbs.driver.endpoints;
20+
21+
import jakarta.inject.Inject;
22+
import jakarta.ws.rs.Consumes;
23+
import jakarta.ws.rs.GET;
24+
import jakarta.ws.rs.POST;
25+
import jakarta.ws.rs.Path;
26+
import jakarta.ws.rs.Produces;
27+
import jakarta.ws.rs.core.MediaType;
28+
29+
import org.jboss.pnc.api.dto.ComponentVersion;
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
32+
33+
import com.redhat.hacbs.driver.Driver;
34+
import com.redhat.hacbs.driver.dto.BuildRequest;
35+
import com.redhat.hacbs.driver.dto.BuildResponse;
36+
import com.redhat.hacbs.driver.util.Info;
37+
38+
import io.smallrye.common.annotation.RunOnVirtualThread;
39+
40+
/**
41+
* Endpoint to start/cancel the build.
42+
*
43+
* @author <a href="mailto:[email protected]">Matej Lazar</a>
44+
*/
45+
@Path("/")
46+
@Consumes(MediaType.APPLICATION_JSON)
47+
@Produces(MediaType.APPLICATION_JSON)
48+
public class Public {
49+
50+
private static final Logger logger = LoggerFactory.getLogger(Public.class);
51+
52+
@Inject
53+
Driver driver;
54+
55+
@Inject
56+
Info info;
57+
58+
@POST
59+
@Path("/build")
60+
@RunOnVirtualThread
61+
// public CompletionStage<BuildResponse> build(BuildRequest buildRequest) {
62+
public BuildResponse build(BuildRequest buildRequest) {
63+
logger.info("Requested project build: {}", buildRequest.projectName());
64+
var result = driver.create(buildRequest);
65+
logger.info("### Got {}", result);
66+
return result;
67+
}
68+
69+
// TODO: Is delete possible in konflux?
70+
//
71+
// /**
72+
// * Cancel the build execution.
73+
// */
74+
// @PUT
75+
// @Path("/cancel")
76+
// public CompletionStage<Response> cancel(BuildCancelRequest buildCancelRequest) {
77+
// logger.info("Requested cancel: {}", buildCancelRequest.getBuildExecutionId());
78+
// return driver.cancel(buildCancelRequest).thenApply((r) -> Response.status(r.getCode()).build());
79+
// }
80+
81+
@Path("/version")
82+
@GET
83+
@RunOnVirtualThread
84+
public ComponentVersion getVersion() {
85+
var r = info.getVersion();
86+
logger.info("Requested version {}", r);
87+
return r;
88+
}
89+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.redhat.hacbs.driver.util;
2+
3+
import jakarta.enterprise.context.RequestScoped;
4+
import jakarta.inject.Inject;
5+
6+
import org.eclipse.microprofile.config.inject.ConfigProperty;
7+
import org.jboss.pnc.api.dto.ComponentVersion;
8+
9+
import io.quarkus.info.BuildInfo;
10+
import io.quarkus.info.GitInfo;
11+
12+
@RequestScoped
13+
public class Info {
14+
15+
@ConfigProperty(name = "quarkus.application.name")
16+
String name;
17+
18+
@Inject
19+
GitInfo gitInfo;
20+
21+
@Inject
22+
BuildInfo buildInfo;
23+
24+
public ComponentVersion getVersion() {
25+
return ComponentVersion.builder()
26+
.name(name)
27+
.builtOn(buildInfo.time().toZonedDateTime())
28+
.commit(gitInfo.latestCommitId())
29+
.version(buildInfo.version())
30+
.build();
31+
}
32+
33+
}

java-components/driver/src/main/resources/application.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
konflux-build-driver:
2+
quay-repo: "quay.io/redhat-user-workloads-stage/pnc-devel-tenant/pnc-konflux"
3+
konflux-processor: "quay.io/redhat-user-workloads/konflux-jbs-pnc-tenant/jvm-build-service/build-request-processor:latest"
4+
# TODO: This will eventually be build-definitions repository
5+
pipeline-resolver: "https://raw.githubusercontent.com/rnc/jvm-build-service/refs/heads/NCL8774/deploy/pipeline/mw-pipeline-v0.1.yaml"
16
quarkus:
27
application:
38
name: konflux-build-driver

java-components/driver/src/main/resources/pipeline.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ metadata:
44
generateName: run-mw-pipeline-
55
spec:
66
pipelineRef:
7+
# TODO: Might want in future to change this to OCI bundle reference?
78
resolver: http
89
params:
9-
# TODO: This will eventually be build-definitions repository
1010
- name: url
11-
value: https://raw.githubusercontent.com/rnc/jvm-build-service/refs/heads/NCL8774/deploy/pipeline/mw-pipeline-v0.1.yaml
11+
value: ""
1212
workspaces:
1313
- name: source
1414
# TODO: If we have a custom git step we can share this with prebuild thereby eliminating the need for a volumeClaimTemplate

0 commit comments

Comments
 (0)