|
| 1 | +package com.redhat.hacbs.cli.driver; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import io.fabric8.kubernetes.api.model.Quantity; |
| 8 | +import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder; |
| 9 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 10 | +import io.fabric8.tekton.client.TektonClient; |
| 11 | +import io.fabric8.tekton.pipeline.v1.Param; |
| 12 | +import io.fabric8.tekton.pipeline.v1.ParamBuilder; |
| 13 | +import io.fabric8.tekton.pipeline.v1.PipelineRun; |
| 14 | +import io.fabric8.tekton.pipeline.v1.PipelineRunBuilder; |
| 15 | +import io.fabric8.tekton.pipeline.v1.PipelineTaskRunSpec; |
| 16 | +import io.fabric8.tekton.pipeline.v1.PipelineTaskRunSpecBuilder; |
| 17 | +import io.fabric8.tekton.pipeline.v1.TaskRunStepSpecBuilder; |
| 18 | +import io.fabric8.tekton.pipeline.v1.WorkspaceBinding; |
| 19 | +import io.fabric8.tekton.pipeline.v1.WorkspaceBindingBuilder; |
| 20 | +import io.quarkus.arc.Arc; |
| 21 | +import io.quarkus.arc.InstanceHandle; |
| 22 | +import picocli.CommandLine; |
| 23 | + |
| 24 | +/** |
| 25 | + * Experiment only - see if Fabric8 can be used to create the entire pipelinerun object rather |
| 26 | + * than reading a definition from yaml. |
| 27 | + */ |
| 28 | +@Deprecated |
| 29 | +@CommandLine.Command(name = "fabric8", mixinStandardHelpOptions = true, description = "Creates a pipeline") |
| 30 | +public class Fabric8 extends Base implements Runnable { |
| 31 | + |
| 32 | + @Override |
| 33 | + public void run() { |
| 34 | + |
| 35 | + PipelineRun run; |
| 36 | + |
| 37 | + try (InstanceHandle<TektonClient> instanceHandle = Arc.container().instance(TektonClient.class)) { |
| 38 | + |
| 39 | + // Experiment with creating gitlab project-ncl/konflux-integration/-/blob/main/deploy/mw-pipeline-run-v0.1.yaml |
| 40 | + PipelineRunBuilder pipelineRunBuilder = new PipelineRunBuilder() |
| 41 | + .withNewMetadata().withGenerateName("hacbs-pipeline-").endMetadata() |
| 42 | + .withNewSpec() |
| 43 | + .withNewPipelineRef().withResolver("git").withParams(getGitParams()).endPipelineRef() |
| 44 | + .withWorkspaces(getWorkspace()) |
| 45 | + .withParams(embedParams()) |
| 46 | + .withTaskRunSpecs(configureTaskRunSpecs()) |
| 47 | + .endSpec(); |
| 48 | + run = pipelineRunBuilder.build(); |
| 49 | + } |
| 50 | + try (InstanceHandle<KubernetesClient> instanceHandle = Arc.container().instance(KubernetesClient.class)) { |
| 51 | + PipelineRun created = instanceHandle.get().resource(run).create(); |
| 52 | + System.err.println("### run created : " + created); |
| 53 | + // final CountDownLatch closeLatch = new CountDownLatch(1); |
| 54 | + // instanceHandle.get().resource(run).watch(new Watcher<>() { |
| 55 | + // @Override |
| 56 | + // public void eventReceived(Action action, PipelineRun resource) { |
| 57 | + // System.out.println("### event action " + action.name()); |
| 58 | + // switch (action.name()) { |
| 59 | + // case "ADDED": |
| 60 | + // System.out.println("### added " + resource.getMetadata().getName()); |
| 61 | + // break; |
| 62 | + // case "DELETED": |
| 63 | + // break; |
| 64 | + // case "MODIFIED": |
| 65 | + // System.out.println( |
| 66 | + // "### added " + resource.getMetadata().getName() + " and status " + resource.getStatus() |
| 67 | + // .getResults()); |
| 68 | + // break; |
| 69 | + // // default: |
| 70 | + // } |
| 71 | + // } |
| 72 | + // |
| 73 | + // @Override |
| 74 | + // public void onClose(WatcherException cause) { |
| 75 | + // System.out.println("### close " + cause); |
| 76 | + // closeLatch.countDown(); |
| 77 | + // } |
| 78 | + // }); |
| 79 | + // closeLatch.await(); |
| 80 | + // } catch (InterruptedException e) { |
| 81 | + // throw new RuntimeException(e); |
| 82 | + |
| 83 | + // created.getStatus() |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private List<Param> embedParams() { |
| 88 | + List<Param> result = new ArrayList<>(); |
| 89 | + // The actual parameters to be customized... |
| 90 | + result.add(new ParamBuilder().withName("URL").withNewValue(url).build()); |
| 91 | + result.add(new ParamBuilder().withName("REVISION").withNewValue(revision).build()); |
| 92 | + result.add(new ParamBuilder().withName("BUILD_TOOL").withNewValue(buildTool).build()); |
| 93 | + result.add(new ParamBuilder().withName("BUILD_TOOL_VERSION").withNewValue(buildToolVersion).build()); |
| 94 | + result.add(new ParamBuilder().withName("JAVA_VERSION").withNewValue(javaVersion).build()); |
| 95 | + result.add(new ParamBuilder().withName("BUILD_SCRIPT").withNewValue(buildScript).build()); |
| 96 | + if (accessToken.isPresent()) { |
| 97 | + result.add(new ParamBuilder().withName("ACCESS_TOKEN").withNewValue(accessToken.get()).build()); |
| 98 | + } else { |
| 99 | + System.err.println("Access token not set"); |
| 100 | + } |
| 101 | + // TODO: Hard code these per now, same as in pipelinerun yaml |
| 102 | + result.add(new ParamBuilder().withName("MVN_REPO_DEPLOY_URL").withNewValue(deploy) |
| 103 | + .build()); |
| 104 | + result.add(new ParamBuilder().withName("MVN_REPO_DEPENDENCIES_URL").withNewValue(deploy) |
| 105 | + .build()); |
| 106 | + result.add(new ParamBuilder().withName("BUILD_ID").withNewValue("test-maven-konflux-int-0001").build()); |
| 107 | + |
| 108 | + return result; |
| 109 | + } |
| 110 | + |
| 111 | + // TODO: The memory settings in this function should be customizable for different build sizes |
| 112 | + private List<PipelineTaskRunSpec> configureTaskRunSpecs() { |
| 113 | + var stepSpec = new PipelineTaskRunSpecBuilder().withPipelineTaskName("buildah-oci-ta") |
| 114 | + .withStepSpecs(new TaskRunStepSpecBuilder() |
| 115 | + .withName("build") |
| 116 | + .withComputeResources(new ResourceRequirementsBuilder() |
| 117 | + .withLimits(Collections.singletonMap("memory", new Quantity("5Gi"))) |
| 118 | + .withRequests(Collections.singletonMap("memory", new Quantity("5Gi"))).build()) |
| 119 | + .build()) |
| 120 | + .build(); |
| 121 | + return Collections.singletonList(stepSpec); |
| 122 | + } |
| 123 | + |
| 124 | + private List<Param> getGitParams() { |
| 125 | + List<Param> result = new ArrayList<>(); |
| 126 | + result.add(new ParamBuilder().withName("url") |
| 127 | + .withNewValue("https://gitlab.cee.redhat.com/project-ncl/konflux-integration.git").build()); |
| 128 | + result.add(new ParamBuilder().withName("revision").withNewValue("main").build()); |
| 129 | + result.add(new ParamBuilder().withName("pathInRepo").withNewValue(".tekton/mw-pipeline-v0.1.yaml").build()); |
| 130 | + return result; |
| 131 | + } |
| 132 | + |
| 133 | + private WorkspaceBinding getWorkspace() { |
| 134 | + return new WorkspaceBindingBuilder().withName("source").withNewVolumeClaimTemplate() |
| 135 | + .withNewSpec() |
| 136 | + .addToAccessModes("ReadWriteOnce") |
| 137 | + .withNewResources().withRequests(Collections.singletonMap("storage", new Quantity("1Gi"))).endResources() |
| 138 | + .endSpec() |
| 139 | + .endVolumeClaimTemplate() |
| 140 | + .build(); |
| 141 | + } |
| 142 | +} |
0 commit comments