Search in sources :

Example 6 with Job

use of io.fabric8.kubernetes.api.model.Job in project fabric8 by fabric8io.

the class JenkinsAsserts method assertWaitForJobPathNotExist.

public static void assertWaitForJobPathNotExist(final JenkinsServer jenkins, long timeMillis, String... jobPath) throws Exception {
    final String fullPath = fullJobPath(jobPath);
    LOG.info("Waiting for Jenkins job to no longer exist " + fullPath);
    Asserts.assertWaitFor(timeMillis, new Block() {

        @Override
        public void invoke() throws Exception {
            assertTrue("Jenkins job " + fullPath + " should not exist", findJobPath(jenkins, jobPath) == null);
        }
    });
}
Also used : Block(io.fabric8.utils.Block) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 7 with Job

use of io.fabric8.kubernetes.api.model.Job in project fabric8 by fabric8io.

the class DevOpsConnector method execute.

/**
 * For a given project this operation will try to update the associated DevOps resources
 *
 * @throws Exception
 */
public void execute() throws Exception {
    loadConfigFile();
    KubernetesClient kubernetes = getKubernetes();
    String name = projectName;
    if (Strings.isNullOrBlank(name)) {
        if (projectConfig != null) {
            name = projectConfig.getBuildName();
        }
        if (Strings.isNullOrBlank(name)) {
            name = jenkinsJob;
        }
        if (Strings.isNullOrBlank(name)) {
            name = ProjectRepositories.createBuildName(username, repoName);
            if (projectConfig != null) {
                projectConfig.setBuildName(name);
            }
        }
    }
    if (Strings.isNullOrBlank(projectName)) {
        projectName = name;
    }
    Map<String, String> labels = new HashMap<>();
    labels.put("user", username);
    labels.put("repo", repoName);
    getLog().info("build name " + name);
    taiga = null;
    taigaProject = null;
    try {
        taiga = createTaiga();
        taigaProject = createTaigaProject(taiga);
    } catch (Exception e) {
        getLog().error("Failed to load or lazily create the Taiga project: " + e, e);
    }
    getLog().info("taiga " + taiga);
    LetsChatClient letschat = null;
    try {
        letschat = createLetsChat();
    } catch (Exception e) {
        getLog().error("Failed to load or lazily create the LetsChat client: " + e, e);
    }
    getLog().info("letschat " + letschat);
    /*
         * Create Gerrit Git to if isGerritReview is enabled
         */
    if (projectConfig != null && projectConfig.hasCodeReview()) {
        try {
            createGerritRepo(repoName, gerritUser, gerritPwd, gerritGitInitialCommit, gerritGitRepoDesription);
        } catch (Exception e) {
            getLog().error("Failed to create GerritGit repo : " + e, e);
        }
    }
    Map<String, String> annotations = new HashMap<>();
    jenkinsJobUrl = null;
    String jenkinsUrl = null;
    try {
        jenkinsUrl = getJenkinsServiceUrl(true);
        if (Strings.isNotBlank(jenkinsUrl)) {
            if (Strings.isNotBlank(jenkinsMonitorView)) {
                String url = URLUtils.pathJoin(jenkinsUrl, "/view", jenkinsMonitorView);
                annotationLink(annotations, "fabric8.link.jenkins.monitor/", url, "Monitor");
            }
            if (Strings.isNotBlank(jenkinsPipelineView)) {
                String url = URLUtils.pathJoin(jenkinsUrl, "/view", jenkinsPipelineView);
                annotationLink(annotations, "fabric8.link.jenkins.pipeline/", url, "Pipeline");
            }
            if (Strings.isNotBlank(name)) {
                jenkinsJobUrl = URLUtils.pathJoin(jenkinsUrl, "/job", name);
                annotationLink(annotations, "fabric8.link.jenkins.job/", jenkinsJobUrl, "Job");
            }
        }
    } catch (Exception e) {
        getLog().warn("Could not find the Jenkins URL!: " + e, e);
    }
    getLog().info("jenkins " + jenkinsUrl);
    if (!annotationLink(annotations, "fabric8.link.issues/", issueTrackerUrl, issueTrackerLabel)) {
        String taigaLink = getProjectPageLink(taiga, taigaProject, this.taigaProjectLinkPage);
        annotationLink(annotations, "fabric8.link.taiga/", taigaLink, taigaProjectLinkLabel);
    }
    if (!annotationLink(annotations, "fabric8.link.team/", teamUrl, teamLabel)) {
        String taigaTeamLink = getProjectPageLink(taiga, taigaProject, this.taigaTeamLinkPage);
        annotationLink(annotations, "fabric8.link.taiga.team/", taigaTeamLink, taigaTeamLinkLabel);
    }
    annotationLink(annotations, "fabric8.link.releases/", releasesUrl, releasesLabel);
    String chatRoomLink = getChatRoomLink(letschat);
    annotationLink(annotations, "fabric8.link.letschat.room/", chatRoomLink, letschatRoomLinkLabel);
    annotationLink(annotations, "fabric8.link.repository.browse/", repositoryBrowseLink, repositoryBrowseLabel);
    ProjectConfigs.defaultEnvironments(projectConfig, namespace);
    String consoleUrl = getServiceUrl(ServiceNames.FABRIC8_CONSOLE, namespace, fabric8ConsoleNamespace);
    if (projectConfig != null) {
        Map<String, String> environments = projectConfig.getEnvironments();
        updateEnvironmentConfigMap(environments, kubernetes, annotations, consoleUrl);
    }
    addLink("Git", getGitUrl());
    Controller controller = createController();
    OpenShiftClient openShiftClient = controller.getOpenShiftClientOrJenkinshift();
    BuildConfig buildConfig = null;
    if (openShiftClient != null) {
        try {
            buildConfig = openShiftClient.buildConfigs().withName(projectName).get();
        } catch (Exception e) {
            log.error("Failed to load build config for " + namespace + "/" + projectName + ". " + e, e);
        }
        log.info("Loaded build config for " + namespace + "/" + projectName + " " + buildConfig);
    }
    // if we have loaded a build config then lets assume its correct!
    boolean foundExistingGitUrl = false;
    if (buildConfig != null) {
        BuildConfigSpec spec = buildConfig.getSpec();
        if (spec != null) {
            BuildSource source = spec.getSource();
            if (source != null) {
                GitBuildSource git = source.getGit();
                if (git != null) {
                    gitUrl = git.getUri();
                    log.info("Loaded existing BuildConfig git url: " + gitUrl);
                    foundExistingGitUrl = true;
                }
                LocalObjectReference sourceSecret = source.getSourceSecret();
                if (sourceSecret != null) {
                    gitSourceSecretName = sourceSecret.getName();
                }
            }
        }
        if (!foundExistingGitUrl) {
            log.warn("Could not find a git url in the loaded BuildConfig: " + buildConfig);
        }
        log.info("Loaded gitSourceSecretName: " + gitSourceSecretName);
    }
    log.info("gitUrl is: " + gitUrl);
    if (buildConfig == null) {
        buildConfig = new BuildConfig();
    }
    ObjectMeta metadata = getOrCreateMetadata(buildConfig);
    metadata.setName(projectName);
    metadata.setLabels(labels);
    putAnnotations(metadata, annotations);
    Map<String, String> currentAnnotations = metadata.getAnnotations();
    if (!currentAnnotations.containsKey(Annotations.Builds.GIT_CLONE_URL)) {
        currentAnnotations.put(Annotations.Builds.GIT_CLONE_URL, gitUrl);
    }
    String localGitUrl = getLocalGitUrl();
    if (!currentAnnotations.containsKey(Annotations.Builds.LOCAL_GIT_CLONE_URL) && Strings.isNotBlank(localGitUrl)) {
        currentAnnotations.put(Annotations.Builds.LOCAL_GIT_CLONE_URL, localGitUrl);
    }
    // lets switch to the local git URL to avoid DNS issues in forge or jenkins
    if (Strings.isNotBlank(localGitUrl)) {
        gitUrl = localGitUrl;
    }
    Builds.configureDefaultBuildConfig(buildConfig, name, gitUrl, foundExistingGitUrl, buildImageStream, buildImageTag, s2iCustomBuilderImage, secret, jenkinsUrl);
    try {
        getLog().info("About to apply build config: " + new JSONObject(KubernetesHelper.toJson(buildConfig)).toString(4));
        controller.applyBuildConfig(buildConfig, "maven");
        getLog().info("Created build configuration for " + name + " in namespace: " + controller.getNamespace() + " at " + kubernetes.getMasterUrl());
    } catch (Exception e) {
        getLog().error("Failed to create BuildConfig for " + KubernetesHelper.toJson(buildConfig) + ". " + e, e);
    }
    this.jenkinsJobName = name;
    if (isRegisterWebHooks()) {
        registerWebHooks();
        getLog().info("webhooks done");
    }
    if (modifiedConfig) {
        if (basedir == null) {
            getLog().error("Could not save updated " + ProjectConfigs.FILE_NAME + " due to missing basedir");
        } else {
            try {
                ProjectConfigs.saveToFolder(basedir, projectConfig, true);
                getLog().info("Updated " + ProjectConfigs.FILE_NAME);
            } catch (IOException e) {
                getLog().error("Could not save updated " + ProjectConfigs.FILE_NAME + ": " + e, e);
            }
        }
    }
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) HashMap(java.util.HashMap) IOException(java.io.IOException) Controller(io.fabric8.kubernetes.api.Controller) GitBuildSource(io.fabric8.openshift.api.model.GitBuildSource) SAXException(org.xml.sax.SAXException) WebApplicationException(javax.ws.rs.WebApplicationException) AuthenticationException(org.apache.http.auth.AuthenticationException) ConnectException(java.net.ConnectException) MalformedChallengeException(org.apache.http.auth.MalformedChallengeException) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) BuildSource(io.fabric8.openshift.api.model.BuildSource) GitBuildSource(io.fabric8.openshift.api.model.GitBuildSource) JSONObject(org.json.JSONObject) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) LocalObjectReference(io.fabric8.kubernetes.api.model.LocalObjectReference) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) LetsChatClient(io.fabric8.letschat.LetsChatClient) BuildConfigSpec(io.fabric8.openshift.api.model.BuildConfigSpec)

Example 8 with Job

use of io.fabric8.kubernetes.api.model.Job in project fabric8-maven-plugin by fabric8io.

the class KubernetesResourceUtil method getPodLabelSelector.

public static LabelSelector getPodLabelSelector(HasMetadata entity) {
    LabelSelector selector = null;
    if (entity instanceof Deployment) {
        Deployment resource = (Deployment) entity;
        DeploymentSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    } else if (entity instanceof ReplicaSet) {
        ReplicaSet resource = (ReplicaSet) entity;
        ReplicaSetSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    } else if (entity instanceof DeploymentConfig) {
        DeploymentConfig resource = (DeploymentConfig) entity;
        DeploymentConfigSpec spec = resource.getSpec();
        if (spec != null) {
            selector = toLabelSelector(spec.getSelector());
        }
    } else if (entity instanceof ReplicationController) {
        ReplicationController resource = (ReplicationController) entity;
        ReplicationControllerSpec spec = resource.getSpec();
        if (spec != null) {
            selector = toLabelSelector(spec.getSelector());
        }
    } else if (entity instanceof DaemonSet) {
        DaemonSet resource = (DaemonSet) entity;
        DaemonSetSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    } else if (entity instanceof StatefulSet) {
        StatefulSet resource = (StatefulSet) entity;
        StatefulSetSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    } else if (entity instanceof Job) {
        Job resource = (Job) entity;
        JobSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    }
    return selector;
}
Also used : StatefulSetSpec(io.fabric8.kubernetes.api.model.extensions.StatefulSetSpec) ReplicaSetSpec(io.fabric8.kubernetes.api.model.extensions.ReplicaSetSpec) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) DaemonSetSpec(io.fabric8.kubernetes.api.model.extensions.DaemonSetSpec) DeploymentSpec(io.fabric8.kubernetes.api.model.extensions.DeploymentSpec) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) DaemonSet(io.fabric8.kubernetes.api.model.extensions.DaemonSet) JobSpec(io.fabric8.kubernetes.api.model.JobSpec) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) DeploymentConfigSpec(io.fabric8.openshift.api.model.DeploymentConfigSpec) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet) Job(io.fabric8.kubernetes.api.model.Job) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet) ReplicationControllerSpec(io.fabric8.kubernetes.api.model.ReplicationControllerSpec)

Example 9 with Job

use of io.fabric8.kubernetes.api.model.Job in project fabric8-maven-plugin by fabric8io.

the class JobHandlerTest method jobHandlerTest.

@Test
public void jobHandlerTest() {
    ContainerHandler containerHandler = new ContainerHandler(project, envVarHandler, probeHandler);
    PodTemplateHandler podTemplateHandler = new PodTemplateHandler(containerHandler);
    JobHandler jobHandler = new JobHandler(podTemplateHandler);
    ResourceConfig config = new ResourceConfig.Builder().imagePullPolicy("IfNotPresent").controllerName("testing").withServiceAccount("test-account").volumes(volumes1).build();
    Job job = jobHandler.getJob(config, images);
    // Assertion
    assertNotNull(job.getSpec());
    assertNotNull(job.getMetadata());
    assertNotNull(job.getSpec().getTemplate());
    assertEquals("testing", job.getMetadata().getName());
    assertEquals("test-account", job.getSpec().getTemplate().getSpec().getServiceAccountName());
    assertFalse(job.getSpec().getTemplate().getSpec().getVolumes().isEmpty());
    assertEquals("test", job.getSpec().getTemplate().getSpec().getVolumes().get(0).getName());
    assertEquals("/test/path", job.getSpec().getTemplate().getSpec().getVolumes().get(0).getHostPath().getPath());
    assertNotNull(job.getSpec().getTemplate().getSpec().getContainers());
}
Also used : ResourceConfig(io.fabric8.maven.core.config.ResourceConfig) Job(io.fabric8.kubernetes.api.model.Job) Test(org.junit.Test)

Example 10 with Job

use of io.fabric8.kubernetes.api.model.Job in project fabric8-maven-plugin by fabric8io.

the class KubernetesResourceUtilTest method job.

@Test
public void job() throws Exception {
    HasMetadata ret = getResource(DEFAULT_RESOURCE_VERSIONING, new File(fabric8Dir, "job.yml"), "app");
    assertEquals("Job", ret.getKind());
    assertEquals(JOB_VERSION, ret.getApiVersion());
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) File(java.io.File) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)4 Job (io.fabric8.kubernetes.api.model.Job)3 Block (io.fabric8.utils.Block)3 URISyntaxException (java.net.URISyntaxException)3 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)2 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)2 DaemonSet (io.fabric8.kubernetes.api.model.extensions.DaemonSet)2 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)2 DeploymentSpec (io.fabric8.kubernetes.api.model.extensions.DeploymentSpec)2 ReplicaSet (io.fabric8.kubernetes.api.model.extensions.ReplicaSet)2 StatefulSet (io.fabric8.kubernetes.api.model.extensions.StatefulSet)2 StatefulSetSpec (io.fabric8.kubernetes.api.model.extensions.StatefulSetSpec)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Test (org.junit.Test)2 KubernetesDocumentKeyValueStyleGenerator (com.github.isdream.chameleon.docs.KubernetesDocumentKeyValueStyleGenerator)1 JobWithDetails (com.offbytwo.jenkins.model.JobWithDetails)1 Controller (io.fabric8.kubernetes.api.Controller)1 TypedVisitor (io.fabric8.kubernetes.api.builder.TypedVisitor)1 Event (io.fabric8.kubernetes.api.model.Event)1