Search in sources :

Example 1 with WebHookTriggerBuilder

use of io.fabric8.openshift.api.model.WebHookTriggerBuilder in project kubernetes-client by fabric8io.

the class BuildConfigExamples method main.

@SuppressWarnings("java:S106")
public static void main(String[] args) throws InterruptedException {
    try (KubernetesClient kubernetesClient = new KubernetesClientBuilder().build()) {
        final OpenShiftClient client = kubernetesClient.adapt(OpenShiftClient.class);
        final String namespace;
        if (client.getNamespace() != null) {
            namespace = client.getNamespace();
            logger.info("Using configured namespace: {}", namespace);
        } else {
            final Namespace ns = client.namespaces().create(new NamespaceBuilder().withNewMetadata().withName(NAMESPACE).addToLabels("this", "rocks").endMetadata().build());
            namespace = ns.getMetadata().getName();
            logger.info("Created namespace: {}", namespace);
        }
        client.serviceAccounts().inNamespace(namespace).create(new ServiceAccountBuilder().withNewMetadata().withName("fabric8").endMetadata().build());
        final ImageStream is = client.imageStreams().inNamespace(namespace).create(new ImageStreamBuilder().withNewMetadata().withName("example-camel-cdi").endMetadata().withNewSpec().addNewTag().withName("latest").endTag().withDockerImageRepository("fabric8/example-camel-cdi").endSpec().withNewStatus().withDockerImageRepository("").endStatus().build());
        logger.info("Created image stream: {}", is.getMetadata().getName());
        final String buildConfigName = "custom-build-config";
        final BuildConfig buildConfig = client.buildConfigs().inNamespace(namespace).create(new BuildConfigBuilder().withNewMetadata().withName(buildConfigName).endMetadata().withNewSpec().withServiceAccount("fabric8").withNewSource().withType("Git").withNewGit().withUri("https://github.com/fabric8io/example-camel-cdi.git").endGit().endSource().withNewResources().addToLimits("mykey", new Quantity("10")).addToRequests("mykey", new Quantity("10")).endResources().withNewStrategy().withType("Source").withNewSourceStrategy().withNewFrom().withName("java-sti:latest").withKind("DockerImage").endFrom().endSourceStrategy().endStrategy().withNewOutput().withNewTo().withKind("DockerImage").withName("example-camel-cdi:latest").endTo().endOutput().addNewTrigger().withType("GitHub").withNewGithub().withSecret("secret101").endGithub().endTrigger().endSpec().build());
        logger.info("Created Build Config: {}", buildConfig.getMetadata().getName());
        final Build build = client.buildConfigs().inNamespace(namespace).withName("custom-build-config").instantiate(new BuildRequestBuilder().withNewMetadata().withName(buildConfigName).endMetadata().build());
        logger.info("Instantiated Build: {}", build.getMetadata().getName());
        client.buildConfigs().inNamespace(namespace).withName(buildConfigName).withSecret("secret101").withType("github").trigger(new WebHookTriggerBuilder().withSecret("secret101").build());
        logger.info("Triggered Build Config: {}", buildConfigName);
        Thread.sleep(6000);
        logger.info("Builds:");
        for (Build b : client.builds().inNamespace(namespace).list().getItems()) {
            logger.info("\t\t\t{}", b.getMetadata().getName());
            logger.info("\t\t\t\t\t Log:");
            client.builds().inNamespace(namespace).withName(b.getMetadata().getName()).watchLog(System.out);
        }
        logger.info("Done");
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ServiceAccountBuilder(io.fabric8.kubernetes.api.model.ServiceAccountBuilder) ImageStream(io.fabric8.openshift.api.model.ImageStream) Quantity(io.fabric8.kubernetes.api.model.Quantity) BuildConfigBuilder(io.fabric8.openshift.api.model.BuildConfigBuilder) Namespace(io.fabric8.kubernetes.api.model.Namespace) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder) ImageStreamBuilder(io.fabric8.openshift.api.model.ImageStreamBuilder) KubernetesClientBuilder(io.fabric8.kubernetes.client.KubernetesClientBuilder) Build(io.fabric8.openshift.api.model.Build) BuildRequestBuilder(io.fabric8.openshift.api.model.BuildRequestBuilder) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) WebHookTriggerBuilder(io.fabric8.openshift.api.model.WebHookTriggerBuilder)

Example 2 with WebHookTriggerBuilder

use of io.fabric8.openshift.api.model.WebHookTriggerBuilder in project kubernetes-client by fabric8io.

the class BuildConfigTest method buildConfigBuilderTest.

@Test
public void buildConfigBuilderTest() {
    BuildConfig buildConfig = new BuildConfigBuilder().withNewMetadata().withName("ruby-sample-build").endMetadata().withNewSpec().addToTriggers(0, new BuildTriggerPolicyBuilder().withType("Github").withGithub(new WebHookTriggerBuilder().withSecret("secret101").build()).build()).addToTriggers(1, new BuildTriggerPolicyBuilder().withType("Generic").withGithub(new WebHookTriggerBuilder().withSecret("secret101").withAllowEnv(true).build()).build()).addToTriggers(2, new BuildTriggerPolicyBuilder().withType("ImageChange").withImageChange(new ImageChangeTriggerBuilder().withLastTriggeredImageID("triggeredID").withFrom(new io.fabric8.kubernetes.api.model.ObjectReferenceBuilder().withKind("ImageStreamTag").withName("custom-image:latest").withApiVersion("v1").withNamespace("default").withUid("16161").withFieldPath("/path/field").build()).build()).build()).withRunPolicy("Parallel").withServiceAccount("build-ServiceAccount").withNewSource().withType("Git").withBinary(new BinaryBuildSourceBuilder().withAsFile("webapp.war").build()).withGit(new GitBuildSourceBuilder().withUri("https://github.com/openshift/ruby-hello-world").withRef("master").withHttpProxy("http://proxy.example.com").withHttpsProxy("http://proxy.example.com").withNoProxy("http://proxy.example2.com").build()).withDockerfile("FROM openshift/ruby-22-centos7\nUSER example").withContextDir("app/dir").withSourceSecret(new io.fabric8.kubernetes.api.model.LocalObjectReferenceBuilder().withName("basicsecret").build()).withImages(new ImageSourceBuilder().withFrom(new io.fabric8.kubernetes.api.model.ObjectReferenceBuilder().withNamespace("mynamespace").withName("myinputimage:latest").withKind("ImageStreamTag").build()).withPaths(new ImageSourcePathBuilder().withDestinationDir("app/dir/injected/dir").withSourcePath("/usr/lib/somefile.jar").build()).withPullSecret(new io.fabric8.kubernetes.api.model.LocalObjectReferenceBuilder().withName("pullsecret").build()).build()).addToSecrets(0, new SecretBuildSourceBuilder().withSecret(new io.fabric8.kubernetes.api.model.LocalObjectReferenceBuilder().withName("secret-npmrc").build()).withDestinationDir("app/dir/secret/dir").build()).endSource().withNewRevision().withType("Source").withGit(new GitSourceRevisionBuilder().withCommit("d75775").withAuthor(new SourceControlUserBuilder().withName("Author").withEmail("author@gmail.com").build()).withCommitter(new SourceControlUserBuilder().withName("Committer").withEmail("commiter@gmail.com").build()).withMessage("Commit Git").build()).endRevision().withNewStrategy().withType("Source").withSourceStrategy(new SourceBuildStrategyBuilder().withFrom(new io.fabric8.kubernetes.api.model.ObjectReferenceBuilder().withKind("ImageStreamTag").withName("ruby-20-centos7:latest").build()).withPullSecret(new io.fabric8.kubernetes.api.model.LocalObjectReferenceBuilder().withName("pullsecret").build()).addToEnv(0, new io.fabric8.kubernetes.api.model.EnvVarBuilder().withName("BUILD_LOGLEVEL").withValue("2").build()).withScripts("/scripts").withIncremental(true).withForcePull(true).build()).endStrategy().withNewOutput().withTo(new io.fabric8.kubernetes.api.model.ObjectReferenceBuilder().withKind("Docker Image").withName("private.registry.com/org/private-image:latest").build()).withPushSecret(new io.fabric8.kubernetes.api.model.LocalObjectReferenceBuilder().withName("dockerhub").build()).addToImageLabels(0, new ImageLabelBuilder().withName("imageLabelName").withValue("imageLabelValue").build()).endOutput().withNewResources().withLimits(new HashMap<String, Quantity>() {

        {
            put("cpu", new io.fabric8.kubernetes.api.model.QuantityBuilder().withAmount("100").withFormat("m").build());
            put("memory", new io.fabric8.kubernetes.api.model.QuantityBuilder().withAmount("256").withFormat("Mi").build());
        }
    }).withRequests(new HashMap<String, Quantity>() {

        {
            put("cpu", new io.fabric8.kubernetes.api.model.QuantityBuilder().withAmount("50").withFormat("m").build());
            put("memory", new io.fabric8.kubernetes.api.model.QuantityBuilder().withAmount("128").withFormat("Mi").build());
        }
    }).endResources().withPostCommit(new BuildPostCommitSpecBuilder().withScript("bundle exec rake test").addToArgs(0, "bundle").addToArgs(1, "exec").addToArgs(2, "rake").addToArgs(3, "test").addToArgs(4, "--verbose").build()).withCompletionDeadlineSeconds(1000L).withNodeSelector(new HashMap<String, String>() {

        {
            put("key1", "value1");
            put("key2", "value2");
        }
    }).withSuccessfulBuildsHistoryLimit(5).withFailedBuildsHistoryLimit(5).endSpec().build();
    assertNotNull(buildConfig);
    assertEquals("ruby-sample-build", buildConfig.getMetadata().getName());
    assertNotNull(buildConfig.getSpec().getTriggers());
    assertEquals(3, buildConfig.getSpec().getTriggers().size());
    assertEquals("Github", buildConfig.getSpec().getTriggers().get(0).getType());
    assertEquals("secret101", buildConfig.getSpec().getTriggers().get(0).getGithub().getSecret());
    assertEquals("Generic", buildConfig.getSpec().getTriggers().get(1).getType());
    assertEquals("secret101", buildConfig.getSpec().getTriggers().get(1).getGithub().getSecret());
    assertTrue(buildConfig.getSpec().getTriggers().get(1).getGithub().getAllowEnv());
    assertEquals("ImageChange", buildConfig.getSpec().getTriggers().get(2).getType());
    assertEquals("triggeredID", buildConfig.getSpec().getTriggers().get(2).getImageChange().getLastTriggeredImageID());
    assertEquals("v1", buildConfig.getSpec().getTriggers().get(2).getImageChange().getFrom().getApiVersion());
    assertEquals("custom-image:latest", buildConfig.getSpec().getTriggers().get(2).getImageChange().getFrom().getName());
    assertEquals("default", buildConfig.getSpec().getTriggers().get(2).getImageChange().getFrom().getNamespace());
    assertEquals("ImageStreamTag", buildConfig.getSpec().getTriggers().get(2).getImageChange().getFrom().getKind());
    assertEquals("16161", buildConfig.getSpec().getTriggers().get(2).getImageChange().getFrom().getUid());
    assertEquals("/path/field", buildConfig.getSpec().getTriggers().get(2).getImageChange().getFrom().getFieldPath());
    assertEquals("Parallel", buildConfig.getSpec().getRunPolicy());
    assertEquals("build-ServiceAccount", buildConfig.getSpec().getServiceAccount());
    assertEquals("Git", buildConfig.getSpec().getSource().getType());
    assertNotNull(buildConfig.getSpec().getSource().getGit());
    assertEquals("https://github.com/openshift/ruby-hello-world", buildConfig.getSpec().getSource().getGit().getUri());
    assertEquals("master", buildConfig.getSpec().getSource().getGit().getRef());
    assertEquals("http://proxy.example.com", buildConfig.getSpec().getSource().getGit().getHttpProxy());
    assertEquals("http://proxy.example.com", buildConfig.getSpec().getSource().getGit().getHttpsProxy());
    assertEquals("http://proxy.example2.com", buildConfig.getSpec().getSource().getGit().getNoProxy());
    assertEquals("FROM openshift/ruby-22-centos7\nUSER example", buildConfig.getSpec().getSource().getDockerfile());
    assertEquals("app/dir", buildConfig.getSpec().getSource().getContextDir());
    assertNotNull(buildConfig.getSpec().getSource().getSourceSecret());
    assertEquals("basicsecret", buildConfig.getSpec().getSource().getSourceSecret().getName());
    assertNotNull(buildConfig.getSpec().getSource().getImages());
    assertEquals(1, buildConfig.getSpec().getSource().getImages().size());
    assertNotNull(buildConfig.getSpec().getSource().getImages().get(0).getFrom());
    assertEquals("ImageStreamTag", buildConfig.getSpec().getSource().getImages().get(0).getFrom().getKind());
    assertEquals("mynamespace", buildConfig.getSpec().getSource().getImages().get(0).getFrom().getNamespace());
    assertEquals("myinputimage:latest", buildConfig.getSpec().getSource().getImages().get(0).getFrom().getName());
    assertNotNull(buildConfig.getSpec().getSource().getImages().get(0).getPaths());
    assertEquals(1, buildConfig.getSpec().getSource().getImages().get(0).getPaths().size());
    assertEquals("app/dir/injected/dir", buildConfig.getSpec().getSource().getImages().get(0).getPaths().get(0).getDestinationDir());
    assertEquals("/usr/lib/somefile.jar", buildConfig.getSpec().getSource().getImages().get(0).getPaths().get(0).getSourcePath());
    assertNotNull(buildConfig.getSpec().getSource().getImages().get(0).getPullSecret());
    assertEquals("pullsecret", buildConfig.getSpec().getSource().getImages().get(0).getPullSecret().getName());
    assertNotNull(buildConfig.getSpec().getSource().getSecrets());
    assertEquals(1, buildConfig.getSpec().getSource().getSecrets().size());
    assertNotNull(buildConfig.getSpec().getSource().getSecrets().get(0).getSecret());
    assertEquals("secret-npmrc", buildConfig.getSpec().getSource().getSecrets().get(0).getSecret().getName());
    assertEquals("app/dir/secret/dir", buildConfig.getSpec().getSource().getSecrets().get(0).getDestinationDir());
    assertNotNull(buildConfig.getSpec().getRevision());
    assertEquals("Source", buildConfig.getSpec().getRevision().getType());
    assertNotNull(buildConfig.getSpec().getRevision().getGit());
    assertEquals("d75775", buildConfig.getSpec().getRevision().getGit().getCommit());
    assertNotNull(buildConfig.getSpec().getRevision().getGit().getAuthor());
    assertEquals("Author", buildConfig.getSpec().getRevision().getGit().getAuthor().getName());
    assertEquals("author@gmail.com", buildConfig.getSpec().getRevision().getGit().getAuthor().getEmail());
    assertNotNull(buildConfig.getSpec().getRevision().getGit().getCommit());
    assertEquals("Committer", buildConfig.getSpec().getRevision().getGit().getCommitter().getName());
    assertEquals("commiter@gmail.com", buildConfig.getSpec().getRevision().getGit().getCommitter().getEmail());
    assertEquals("Commit Git", buildConfig.getSpec().getRevision().getGit().getMessage());
    assertNotNull(buildConfig.getSpec().getStrategy());
    assertEquals("Source", buildConfig.getSpec().getStrategy().getType());
    assertNotNull(buildConfig.getSpec().getStrategy().getSourceStrategy());
    assertNotNull(buildConfig.getSpec().getStrategy().getSourceStrategy().getFrom());
    assertEquals("ImageStreamTag", buildConfig.getSpec().getStrategy().getSourceStrategy().getFrom().getKind());
    assertEquals("ruby-20-centos7:latest", buildConfig.getSpec().getStrategy().getSourceStrategy().getFrom().getName());
    assertNotNull(buildConfig.getSpec().getStrategy().getSourceStrategy().getPullSecret());
    assertEquals("pullsecret", buildConfig.getSpec().getStrategy().getSourceStrategy().getPullSecret().getName());
    assertNotNull(buildConfig.getSpec().getStrategy().getSourceStrategy().getEnv());
    assertEquals(1, buildConfig.getSpec().getStrategy().getSourceStrategy().getEnv().size());
    assertEquals("BUILD_LOGLEVEL", buildConfig.getSpec().getStrategy().getSourceStrategy().getEnv().get(0).getName());
    assertEquals("2", buildConfig.getSpec().getStrategy().getSourceStrategy().getEnv().get(0).getValue());
    assertEquals("/scripts", buildConfig.getSpec().getStrategy().getSourceStrategy().getScripts());
    assertEquals(true, buildConfig.getSpec().getStrategy().getSourceStrategy().getIncremental());
    assertEquals(true, buildConfig.getSpec().getStrategy().getSourceStrategy().getForcePull());
    assertNotNull(buildConfig.getSpec().getOutput());
    assertNotNull(buildConfig.getSpec().getOutput().getTo());
    assertEquals("Docker Image", buildConfig.getSpec().getOutput().getTo().getKind());
    assertEquals("private.registry.com/org/private-image:latest", buildConfig.getSpec().getOutput().getTo().getName());
    assertNotNull(buildConfig.getSpec().getOutput().getPushSecret());
    assertEquals("dockerhub", buildConfig.getSpec().getOutput().getPushSecret().getName());
    assertNotNull(buildConfig.getSpec().getOutput().getImageLabels());
    assertEquals(1, buildConfig.getSpec().getOutput().getImageLabels().size());
    assertNotNull(buildConfig.getSpec().getOutput().getImageLabels().get(0));
    assertEquals("imageLabelName", buildConfig.getSpec().getOutput().getImageLabels().get(0).getName());
    assertEquals("imageLabelValue", buildConfig.getSpec().getOutput().getImageLabels().get(0).getValue());
    assertNotNull(buildConfig.getSpec().getResources());
    assertNotNull(buildConfig.getSpec().getResources().getLimits());
    assertEquals("100", buildConfig.getSpec().getResources().getLimits().get("cpu").getAmount());
    assertEquals("m", buildConfig.getSpec().getResources().getLimits().get("cpu").getFormat());
    assertEquals("256", buildConfig.getSpec().getResources().getLimits().get("memory").getAmount());
    assertEquals("Mi", buildConfig.getSpec().getResources().getLimits().get("memory").getFormat());
    assertNotNull(buildConfig.getSpec().getResources().getRequests());
    assertEquals("50", buildConfig.getSpec().getResources().getRequests().get("cpu").getAmount());
    assertEquals("m", buildConfig.getSpec().getResources().getRequests().get("cpu").getFormat());
    assertEquals("128", buildConfig.getSpec().getResources().getRequests().get("memory").getAmount());
    assertEquals("Mi", buildConfig.getSpec().getResources().getRequests().get("memory").getFormat());
    assertNotNull(buildConfig.getSpec().getPostCommit());
    assertEquals("bundle exec rake test", buildConfig.getSpec().getPostCommit().getScript());
    assertNotNull(buildConfig.getSpec().getPostCommit().getArgs());
    assertEquals(5, buildConfig.getSpec().getPostCommit().getArgs().size());
    assertEquals("bundle", buildConfig.getSpec().getPostCommit().getArgs().get(0));
    assertEquals("exec", buildConfig.getSpec().getPostCommit().getArgs().get(1));
    assertEquals("rake", buildConfig.getSpec().getPostCommit().getArgs().get(2));
    assertEquals("test", buildConfig.getSpec().getPostCommit().getArgs().get(3));
    assertEquals("--verbose", buildConfig.getSpec().getPostCommit().getArgs().get(4));
    assertEquals(1000L, buildConfig.getSpec().getCompletionDeadlineSeconds().intValue());
    assertEquals(2, buildConfig.getSpec().getNodeSelector().size());
    assertEquals("value1", buildConfig.getSpec().getNodeSelector().get("key1"));
    assertEquals("value2", buildConfig.getSpec().getNodeSelector().get("key2"));
    assertEquals(5, buildConfig.getSpec().getSuccessfulBuildsHistoryLimit().intValue());
    assertEquals(5, buildConfig.getSpec().getFailedBuildsHistoryLimit().intValue());
}
Also used : ImageSourceBuilder(io.fabric8.openshift.api.model.ImageSourceBuilder) HashMap(java.util.HashMap) GitBuildSourceBuilder(io.fabric8.openshift.api.model.GitBuildSourceBuilder) BuildConfigBuilder(io.fabric8.openshift.api.model.BuildConfigBuilder) BuildTriggerPolicyBuilder(io.fabric8.openshift.api.model.BuildTriggerPolicyBuilder) BuildPostCommitSpecBuilder(io.fabric8.openshift.api.model.BuildPostCommitSpecBuilder) GitSourceRevisionBuilder(io.fabric8.openshift.api.model.GitSourceRevisionBuilder) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) WebHookTriggerBuilder(io.fabric8.openshift.api.model.WebHookTriggerBuilder) SourceBuildStrategyBuilder(io.fabric8.openshift.api.model.SourceBuildStrategyBuilder) SecretBuildSourceBuilder(io.fabric8.openshift.api.model.SecretBuildSourceBuilder) ImageChangeTriggerBuilder(io.fabric8.openshift.api.model.ImageChangeTriggerBuilder) ImageSourcePathBuilder(io.fabric8.openshift.api.model.ImageSourcePathBuilder) ImageLabelBuilder(io.fabric8.openshift.api.model.ImageLabelBuilder) BinaryBuildSourceBuilder(io.fabric8.openshift.api.model.BinaryBuildSourceBuilder) SourceControlUserBuilder(io.fabric8.openshift.api.model.SourceControlUserBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

BuildConfig (io.fabric8.openshift.api.model.BuildConfig)2 BuildConfigBuilder (io.fabric8.openshift.api.model.BuildConfigBuilder)2 WebHookTriggerBuilder (io.fabric8.openshift.api.model.WebHookTriggerBuilder)2 Namespace (io.fabric8.kubernetes.api.model.Namespace)1 NamespaceBuilder (io.fabric8.kubernetes.api.model.NamespaceBuilder)1 Quantity (io.fabric8.kubernetes.api.model.Quantity)1 ServiceAccountBuilder (io.fabric8.kubernetes.api.model.ServiceAccountBuilder)1 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 KubernetesClientBuilder (io.fabric8.kubernetes.client.KubernetesClientBuilder)1 BinaryBuildSourceBuilder (io.fabric8.openshift.api.model.BinaryBuildSourceBuilder)1 Build (io.fabric8.openshift.api.model.Build)1 BuildPostCommitSpecBuilder (io.fabric8.openshift.api.model.BuildPostCommitSpecBuilder)1 BuildRequestBuilder (io.fabric8.openshift.api.model.BuildRequestBuilder)1 BuildTriggerPolicyBuilder (io.fabric8.openshift.api.model.BuildTriggerPolicyBuilder)1 GitBuildSourceBuilder (io.fabric8.openshift.api.model.GitBuildSourceBuilder)1 GitSourceRevisionBuilder (io.fabric8.openshift.api.model.GitSourceRevisionBuilder)1 ImageChangeTriggerBuilder (io.fabric8.openshift.api.model.ImageChangeTriggerBuilder)1 ImageLabelBuilder (io.fabric8.openshift.api.model.ImageLabelBuilder)1 ImageSourceBuilder (io.fabric8.openshift.api.model.ImageSourceBuilder)1 ImageSourcePathBuilder (io.fabric8.openshift.api.model.ImageSourcePathBuilder)1