Skip to content

Commit

Permalink
feat(jobs): allow jobs to be canceled (#2462)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwander committed Mar 29, 2018
1 parent 5c5b819 commit 0b6dd89
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ public interface JobProvider<T extends JobStatus> {

Map<String, Object> getFileContents(String account, String location, String id, String fileName)

void cancelJob(String account, String location, String id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,7 @@ class DcosJobProvider implements JobProvider<DcosJobStatus> {
}
}
}

@Override
void cancelJob(String account, String location, String id) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.springframework.stereotype.Component
@Component
class KubernetesJobProvider implements JobProvider<KubernetesJobStatus> {
String platform = "kubernetes"

@Autowired
AccountCredentialsProvider accountCredentialsProvider

Expand Down Expand Up @@ -69,4 +70,23 @@ class KubernetesJobProvider implements JobProvider<KubernetesJobStatus> {
return [:]
}

@Override
void cancelJob(String account, String location, String id) {
def credentials = accountCredentialsProvider.getCredentials(account)
if (!(credentials?.credentials instanceof KubernetesV1Credentials)) {
return
}

def trueCredentials = (KubernetesV1Credentials) (credentials as KubernetesNamedAccountCredentials).credentials

try {
if (!trueCredentials.apiAdaptor.getPod(location, id)) {
return
}

trueCredentials.apiAdaptor.deletePod(location, id)
} catch (Exception e) {
log.warn("Unable to delete $id in $location: $e.message", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,6 @@ class TitusJobProvider implements JobProvider<TitusJobStatus> {
null
}

@Override
void cancelJob(String account, String location, String id) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@

package com.netflix.spinnaker.clouddriver.controllers

import com.netflix.spinnaker.clouddriver.model.JobStatus
import com.netflix.spinnaker.clouddriver.model.JobProvider
import com.netflix.spinnaker.clouddriver.model.JobStatus
import com.netflix.spinnaker.kork.web.exceptions.NotFoundException
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.MessageSource
import org.springframework.context.i18n.LocaleContextHolder
import org.springframework.http.HttpStatus
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.*
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/applications/{application}/jobs")
Expand All @@ -53,6 +54,16 @@ class JobController {
jobMatches.first()
}

@PreAuthorize("hasPermission(#application, 'APPLICATION', 'WRITE') and hasPermission(#account, 'ACCOUNT', 'WRITE')") @ApiOperation(value = "Collect a JobStatus", notes = "Collects the output of the job, may modify the job.")
@RequestMapping(value = "/{account}/{location}/{id:.+}", method = RequestMethod.DELETE)
void cancelJob(@ApiParam(value = "Application name", required = true) @PathVariable String application,
@ApiParam(value = "Account job was created by", required = true) @PathVariable String account,
@ApiParam(value = "Namespace, region, or zone job is running in", required = true) @PathVariable String location,
@ApiParam(value = "Unique identifier of job being looked up", required = true) @PathVariable String id) {
jobProviders.forEach {
it.cancelJob(account, location, id)
}
}

@PreAuthorize("hasPermission(#application, 'APPLICATION', 'WRITE') and hasPermission(#account, 'ACCOUNT', 'WRITE')") @ApiOperation(value = "Collect a file from a job", notes = "Collects the file result of a job.")
@RequestMapping(value = "/{account}/{location}/{id}/{fileName:.+}", method = RequestMethod.GET)
Expand Down

0 comments on commit 0b6dd89

Please sign in to comment.