Skip to content

Commit

Permalink
Bump k8s client to version 21.0.1 (#553)
Browse files Browse the repository at this point in the history
Signed-off-by: munishchouhan <[email protected]>
Signed-off-by: Paolo Di Tommaso <[email protected]>
Co-authored-by: Paolo Di Tommaso <[email protected]>
  • Loading branch information
munishchouhan and pditommaso authored Nov 25, 2024
1 parent 203e5dd commit 5178857
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ dependencies {
implementation 'io.micronaut.reactor:micronaut-reactor-http-client'
implementation 'org.apache.commons:commons-compress:1.27.1'
implementation 'org.apache.commons:commons-lang3:3.17.0'
implementation 'io.kubernetes:client-java:19.0.0'
implementation 'io.kubernetes:client-java-api-fluent:18.0.1'
implementation 'io.kubernetes:client-java:21.0.1'
implementation 'io.kubernetes:client-java-api-fluent:21.0.1'
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml'
Expand Down
32 changes: 20 additions & 12 deletions src/main/groovy/io/seqera/wave/service/k8s/K8sServiceImpl.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import io.kubernetes.client.custom.Quantity
import io.kubernetes.client.openapi.models.V1ContainerBuilder
import io.kubernetes.client.openapi.models.V1DeleteOptions
import io.kubernetes.client.openapi.models.V1EnvVar
import io.kubernetes.client.openapi.models.V1HostPathVolumeSource
import io.kubernetes.client.openapi.models.V1Job
Expand Down Expand Up @@ -144,7 +143,8 @@ class K8sServiceImpl implements K8sService {
JobStatus getJobStatus(String name) {
final job = k8sClient
.batchV1Api()
.readNamespacedJob(name, namespace, null)
.readNamespacedJob(name, namespace)
.execute()
if( !job ) {
log.warn "K8s job=$name - unknown"
return null
Expand Down Expand Up @@ -178,7 +178,8 @@ class K8sServiceImpl implements K8sService {
V1Pod getPod(String name) {
return k8sClient
.coreV1Api()
.readNamespacedPod(name, namespace, null)
.readNamespacedPod(name, namespace)
.execute()
}

/**
Expand Down Expand Up @@ -293,7 +294,6 @@ class K8sServiceImpl implements K8sService {
.withRestartPolicy("Never")
.addAllToVolumes(volumes)


final requests = new V1ResourceRequirements()
if( requestsCpu )
requests.putRequestsItem('cpu', new Quantity(requestsCpu))
Expand Down Expand Up @@ -356,7 +356,8 @@ class K8sServiceImpl implements K8sService {
void deletePod(String name) {
k8sClient
.coreV1Api()
.deleteNamespacedPod(name, namespace, (String)null, (String)null, (Integer)null, (Boolean)null, (String)null, (V1DeleteOptions)null)
.deleteNamespacedPod(name, namespace)
.execute()
}

@Deprecated
Expand Down Expand Up @@ -391,7 +392,6 @@ class K8sServiceImpl implements K8sService {
.withRestartPolicy("Never")
.addAllToVolumes(volumes)


final requests = new V1ResourceRequirements()
if( scanConfig.requestsCpu )
requests.putRequestsItem('cpu', new Quantity(scanConfig.requestsCpu))
Expand Down Expand Up @@ -431,7 +431,8 @@ class K8sServiceImpl implements K8sService {

return k8sClient
.batchV1Api()
.createNamespacedJob(namespace, spec, null, null, null,null)
.createNamespacedJob(namespace, spec)
.execute()
}

V1Job createTransferJobSpec(String name, String containerImage, List<String> args, BlobCacheConfig blobConfig) {
Expand Down Expand Up @@ -494,7 +495,8 @@ class K8sServiceImpl implements K8sService {
final spec = buildJobSpec(name, containerImage, args, workDir, creds, timeout, nodeSelector)
return k8sClient
.batchV1Api()
.createNamespacedJob(namespace, spec, null, null, null,null)
.createNamespacedJob(namespace, spec)
.execute()
}

V1Job buildJobSpec(String name, String containerImage, List<String> args, Path workDir, Path credsFile, Duration timeout, Map<String,String> nodeSelector) {
Expand Down Expand Up @@ -583,7 +585,8 @@ class K8sServiceImpl implements K8sService {
final spec = scanJobSpec(name, containerImage, args, workDir, creds, scanConfig)
return k8sClient
.batchV1Api()
.createNamespacedJob(namespace, spec, null, null, null,null)
.createNamespacedJob(namespace, spec)
.execute()
}

V1Job scanJobSpec(String name, String containerImage, List<String> args, Path workDir, Path credsFile, ScanConfig scanConfig) {
Expand Down Expand Up @@ -650,7 +653,8 @@ class K8sServiceImpl implements K8sService {
final spec = mirrorJobSpec(name, containerImage, args, workDir, creds, config)
return k8sClient
.batchV1Api()
.createNamespacedJob(namespace, spec, null, null, null,null)
.createNamespacedJob(namespace, spec)
.execute()
}

V1Job mirrorJobSpec(String name, String containerImage, List<String> args, Path workDir, Path credsFile, MirrorConfig config) {
Expand Down Expand Up @@ -722,15 +726,19 @@ class K8sServiceImpl implements K8sService {
void deleteJob(String name) {
k8sClient
.batchV1Api()
.deleteNamespacedJob(name, namespace, null, null, null, null,"Foreground", null)
.deleteNamespacedJob(name, namespace)
.propagationPolicy("Foreground")
.execute()
}

@Override
V1Pod getLatestPodForJob(String jobName) {
// list all pods for the given job
final allPods = k8sClient
.coreV1Api()
.listNamespacedPod(namespace, null, null, null, null, "job-name=${jobName}", null, null, null, null, null, null)
.listNamespacedPod(namespace)
.labelSelector("job-name=${jobName}")
.execute()

if( !allPods || !allPods.items )
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,11 +513,16 @@ class K8sServiceImplTest extends Specification {
def "getLatestPodForJob should return the latest pod when multiple pods are present"() {
given:
def jobName = "test-job"
def namespace = "test-ns"
def pod1 = new V1Pod().metadata(new V1ObjectMeta().creationTimestamp(OffsetDateTime.now().minusDays(1)))
def pod2 = new V1Pod().metadata(new V1ObjectMeta().creationTimestamp(OffsetDateTime.now()))
def allPods = new V1PodList().items(Arrays.asList(pod1, pod2))
def api = Mock(CoreV1Api)
api.listNamespacedPod(_, _, _, _, _, "job-name=${jobName}", _, _, _, _, _, _) >> allPods
def podRequest2 = Mock(CoreV1Api. APIlistNamespacedPodRequest)
podRequest2.execute() >> allPods
def podRequest1 = Mock(CoreV1Api. APIlistNamespacedPodRequest)
podRequest1.labelSelector("job-name=${jobName}") >> podRequest2
api.listNamespacedPod(namespace) >> podRequest1
def k8sClient = new K8sClient() {
@Override
ApiClient apiClient() {
Expand All @@ -528,7 +533,7 @@ class K8sServiceImplTest extends Specification {
}
}
and:
def k8sService = new K8sServiceImpl(k8sClient: k8sClient)
def k8sService = new K8sServiceImpl(k8sClient: k8sClient, namespace: namespace)

when:
def latestPod = k8sService.getLatestPodForJob(jobName)
Expand All @@ -540,8 +545,13 @@ class K8sServiceImplTest extends Specification {
def "getLatestPodForJob should return null when no pod is present"() {
given:
def jobName = "test-job"
def namespace = "test-ns"
def api = Mock(CoreV1Api)
api.listNamespacedPod(_, _, _, _, _, "job-name=${jobName}", _, _, _, _, _, _) >> null
def podRequest2 = Mock(CoreV1Api. APIlistNamespacedPodRequest)
podRequest2.execute() >> null
def podRequest1 = Mock(CoreV1Api. APIlistNamespacedPodRequest)
podRequest1.labelSelector("job-name=${jobName}") >> podRequest2
api.listNamespacedPod(namespace) >> podRequest1
def k8sClient = new K8sClient() {
@Override
ApiClient apiClient() {
Expand All @@ -552,7 +562,7 @@ class K8sServiceImplTest extends Specification {
}
}
and:
def k8sService = new K8sServiceImpl(k8sClient: k8sClient)
def k8sService = new K8sServiceImpl(k8sClient: k8sClient, namespace: namespace)

when:
def latestPod = k8sService.getLatestPodForJob(jobName)
Expand Down Expand Up @@ -826,7 +836,7 @@ class K8sServiceImplTest extends Specification {
job.spec.backoffLimit == 3
job.spec.template.spec.containers[0].image == containerImage
job.spec.template.spec.containers[0].args == args
job.spec.template.spec.containers[0].resources.requests == null
job.spec.template.spec.containers[0].resources.requests == [:]
job.spec.template.spec.containers[0].env == [new V1EnvVar().name('REGISTRY_AUTH_FILE').value('/tmp/config.json')]
and:
job.spec.template.spec.containers[0].volumeMounts.size() == 2
Expand Down Expand Up @@ -886,7 +896,7 @@ class K8sServiceImplTest extends Specification {
job.spec.backoffLimit == 3
job.spec.template.spec.containers[0].image == containerImage
job.spec.template.spec.containers[0].args == args
job.spec.template.spec.containers[0].resources.requests == null
job.spec.template.spec.containers[0].resources.requests == [:]
job.spec.template.spec.volumes.size() == 1
job.spec.template.spec.volumes[0].persistentVolumeClaim.claimName == 'bar'
job.spec.template.spec.restartPolicy == 'Never'
Expand Down Expand Up @@ -973,11 +983,14 @@ class K8sServiceImplTest extends Specification {
def api = Mock(BatchV1Api)
def client = Mock(K8sClient) { batchV1Api()>>api }
def service = Spy(new K8sServiceImpl(namespace:NS, k8sClient: client))
def jobRequest = Mock(BatchV1Api. APIreadNamespacedJobRequest)

when:
def status = service.getJobStatus(NAME)

then:
1 * api.readNamespacedJob(NAME, NS, null) >> JOB
jobRequest.execute() >> JOB
1 * api.readNamespacedJob(NAME, NS) >> jobRequest
and:
status == EXPECTED

Expand Down

0 comments on commit 5178857

Please sign in to comment.