Skip to content

Commit

Permalink
Add initial REST API. Add configuration for build-driver
Browse files Browse the repository at this point in the history
  • Loading branch information
rnc committed Dec 4, 2024
1 parent fe6c4cb commit 911dc6c
Show file tree
Hide file tree
Showing 13 changed files with 370 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.redhat.hacbs.driver.Driver;
import com.redhat.hacbs.driver.dto.BuildRequest;
import com.redhat.hacbs.driver.dto.BuildResponse;

import picocli.CommandLine;

Expand All @@ -19,7 +20,7 @@ public class Pipeline extends Base implements Runnable {
@Inject
Driver driver;

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

@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")
Expand Down Expand Up @@ -47,6 +48,8 @@ public void run() {
// Just use default from buildah-oci-ta for now.
.podMemoryOverride("4Gi")
.build();
driver.create(request);
BuildResponse b = driver.create(request);

logger.info("Got response {}", b);
}
}
50 changes: 43 additions & 7 deletions java-components/driver/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.redhat-appstudio.jvmbuild</groupId>
Expand Down Expand Up @@ -45,6 +44,11 @@
<artifactId>commons-text</artifactId>
<version>${version.commons-text}</version>
</dependency>
<dependency>
<groupId>org.jboss.pnc</groupId>
<artifactId>pnc-api</artifactId>
<version>3.0.0</version>
</dependency>
<!-- Can't just import PNC environment-driver code as it uses Quarkus 2.16 versus 3.x (javax -> jakarta) -->
<!-- <dependency>-->
<!-- <groupId>org.jboss.pnc</groupId>-->
Expand All @@ -58,6 +62,30 @@
<version>${version.lombok}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-info</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-mockito</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-test-kubernetes-client</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down Expand Up @@ -86,6 +114,17 @@
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries combine.children="append">
<Build-Time>${maven.build.timestamp}</Build-Time>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
Expand All @@ -109,11 +148,8 @@
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>
${project.build.directory}/${project.build.finalName}-runner
</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager
</java.util.logging.manager>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand All @@ -22,7 +24,10 @@
import com.redhat.hacbs.driver.clients.IndyTokenRequestDTO;
import com.redhat.hacbs.driver.clients.IndyTokenResponseDTO;
import com.redhat.hacbs.driver.dto.BuildRequest;
import com.redhat.hacbs.driver.dto.BuildResponse;
import com.redhat.hacbs.driver.dto.CancelRequest;

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

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

@Setter
private String processor = "quay.io/redhat-user-workloads-stage/pnc-devel-tenant/pnc";
@ConfigProperty(name = "konflux-build-driver.quay-repo")
String quayRepo;

@ConfigProperty(name = "konflux-build-driver.pipeline-resolver")
String resolverTarget;

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

public void create(BuildRequest buildRequest) {
public BuildResponse create(BuildRequest buildRequest) {
IndyTokenResponseDTO tokenResponseDTO = new IndyTokenResponseDTO(accessToken);

if (isEmpty(accessToken)) {
Expand Down Expand Up @@ -95,10 +105,13 @@ public void create(BuildRequest buildRequest) {
pipelineRun = tc.v1().pipelineRuns().load(Path.of(customPipeline.get()).toFile()).item();
}
} catch (IOException e) {
e.printStackTrace();
// TODO: process
throw new RuntimeException(e);
}
pipelineRun = pipelineRun.edit().editOrNewSpec()
.editPipelineRef()
.editFirstParam().editOrNewValue().withStringVal(resolverTarget).endValue()
.endParam()
.endPipelineRef()
.addAllToParams(templateProperties.entrySet().stream()
.map(t -> new ParamBuilder().withName(t.getKey()).withNewValue(t.getValue()).build()).toList())
.editFirstTaskRunSpec()
Expand All @@ -111,14 +124,37 @@ public void create(BuildRequest buildRequest) {
.endTaskRunSpec()
.endSpec().build();

System.err.println("### Got p " + pipelineRun);
var created = client.resource(pipelineRun).inNamespace(buildRequest.namespace()).create();
System.err.println("### Got c " + created);

return BuildResponse.builder().namespace(buildRequest.namespace()).pipelineId(created.getMetadata().getName()).build();
}

public void cancel(CancelRequest request) {
var tc = client.adapt(TektonClient.class);
var pipeline = tc.v1beta1().pipelineRuns().inNamespace(request.namespace()).withName(request.pipelineId()).get();

logger.info("Retrieved pipeline {}", pipeline);

List<Condition> conditions = new ArrayList<>();
// https://tekton.dev/docs/pipelines/pipelineruns/#monitoring-execution-status
Condition cancelCondition = new Condition();
cancelCondition.setType("Succeeded");
cancelCondition.setStatus("False");
cancelCondition.setReason("CancelledRunFinally");
cancelCondition.setMessage("The PipelineRun was cancelled");
conditions.add(cancelCondition);

pipeline.getStatus().setConditions(conditions);

tc.v1beta1().pipelineRuns().inNamespace(request.namespace()).resource(pipeline).updateStatus();
// https://tekton.dev/docs/pipelines/pipelineruns/#gracefully-cancelling-a-pipelinerun
// tc.v1beta1().pipelineRuns().updateStatus().inNamespace(request.namespace()).withName(request.pipelineId()).patch()
// .edit(p -> p.edit().editOrNewSpec().withStatus("CancelledRunFinally").endSpec().build());
}

/**
* Get a fresh access token for the service account. This is done because we want to get a super-new token to be
* used since we're not entirely sure when the http request will be done inside the completablefuture.
* Get a fresh access token for the service account. This is done because we want to get a
* super-new token to be used since we're not entirely sure when the http request will be done.
*
* @return fresh access token
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.redhat.hacbs.driver.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import lombok.Builder;

@Builder(builderClassName = "Builder")
@JsonIgnoreProperties(ignoreUnknown = true)
public record BuildResponse(String pipelineId, String namespace) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.redhat.hacbs.driver.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import lombok.Builder;

@Builder(builderClassName = "Builder")
@JsonIgnoreProperties(ignoreUnknown = true)
public record CancelRequest(String pipelineId, String namespace) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* JBoss, Home of Professional Open Source.
* Copyright 2021 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.redhat.hacbs.driver.endpoints;

import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import org.jboss.pnc.api.dto.ComponentVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.redhat.hacbs.driver.Driver;
import com.redhat.hacbs.driver.dto.BuildRequest;
import com.redhat.hacbs.driver.dto.BuildResponse;
import com.redhat.hacbs.driver.util.Info;

import io.smallrye.common.annotation.RunOnVirtualThread;

/**
* Endpoint to start/cancel the build.
*
* @author <a href="mailto:[email protected]">Matej Lazar</a>
*/
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class Public {

private static final Logger logger = LoggerFactory.getLogger(Public.class);

@Inject
Driver driver;

@Inject
Info info;

@POST
@Path("/build")
@RunOnVirtualThread
// public CompletionStage<BuildResponse> build(BuildRequest buildRequest) {
public BuildResponse build(BuildRequest buildRequest) {
logger.info("Requested project build: {}", buildRequest.projectName());
var result = driver.create(buildRequest);
logger.info("### Got {}", result);
return result;
}

// TODO: Is delete possible in konflux?
//
// /**
// * Cancel the build execution.
// */
// @PUT
// @Path("/cancel")
// public CompletionStage<Response> cancel(BuildCancelRequest buildCancelRequest) {
// logger.info("Requested cancel: {}", buildCancelRequest.getBuildExecutionId());
// return driver.cancel(buildCancelRequest).thenApply((r) -> Response.status(r.getCode()).build());
// }

@Path("/version")
@GET
@RunOnVirtualThread
public ComponentVersion getVersion() {
var r = info.getVersion();
logger.info("Requested version {}", r);
return r;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.redhat.hacbs.driver.util;

import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.pnc.api.dto.ComponentVersion;

import io.quarkus.info.BuildInfo;
import io.quarkus.info.GitInfo;

@RequestScoped
public class Info {

@ConfigProperty(name = "quarkus.application.name")
String name;

@Inject
GitInfo gitInfo;

@Inject
BuildInfo buildInfo;

public ComponentVersion getVersion() {
return ComponentVersion.builder()
.name(name)
.builtOn(buildInfo.time().toZonedDateTime())
.commit(gitInfo.latestCommitId())
.version(buildInfo.version())
.build();
}

}
5 changes: 5 additions & 0 deletions java-components/driver/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
konflux-build-driver:
quay-repo: "quay.io/redhat-user-workloads-stage/pnc-devel-tenant/pnc-konflux"
konflux-processor: "quay.io/redhat-user-workloads/konflux-jbs-pnc-tenant/jvm-build-service/build-request-processor:latest"
# TODO: This will eventually be build-definitions repository
pipeline-resolver: "https://raw.githubusercontent.com/rnc/jvm-build-service/refs/heads/NCL8774/deploy/pipeline/mw-pipeline-v0.1.yaml"
quarkus:
application:
name: konflux-build-driver
Expand Down
4 changes: 2 additions & 2 deletions java-components/driver/src/main/resources/pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ metadata:
generateName: run-mw-pipeline-
spec:
pipelineRef:
# TODO: Might want in future to change this to OCI bundle reference?
resolver: http
params:
# TODO: This will eventually be build-definitions repository
- name: url
value: https://raw.githubusercontent.com/rnc/jvm-build-service/refs/heads/NCL8774/deploy/pipeline/mw-pipeline-v0.1.yaml
value: ""
workspaces:
- name: source
# TODO: If we have a custom git step we can share this with prebuild thereby eliminating the need for a volumeClaimTemplate
Expand Down
Loading

0 comments on commit 911dc6c

Please sign in to comment.