Skip to content

Commit

Permalink
Remove deprecated K8s methods (#734)
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Di Tommaso <[email protected]>
  • Loading branch information
pditommaso authored Nov 2, 2024
1 parent 1502696 commit 481298b
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 261 deletions.
24 changes: 1 addition & 23 deletions src/main/groovy/io/seqera/wave/service/k8s/K8sService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ import java.time.Duration

import io.kubernetes.client.openapi.models.V1Job
import io.kubernetes.client.openapi.models.V1Pod
import io.kubernetes.client.openapi.models.V1PodList
import io.seqera.wave.configuration.BlobCacheConfig
import io.seqera.wave.configuration.ScanConfig
import io.seqera.wave.configuration.MirrorConfig

import io.seqera.wave.configuration.ScanConfig
/**
* Defines Kubernetes operations
*
Expand All @@ -43,23 +41,6 @@ interface K8sService {

void deletePod(String name)

@Deprecated
V1Pod buildContainer(String name, String containerImage, List<String> args, Path workDir, Path creds, Duration timeout, Map <String,String> nodeSelector)

@Deprecated
V1Pod scanContainer(String name, String containerImage, List<String> args, Path workDir, Path creds, ScanConfig scanConfig, Map<String,String> nodeSelector)

@Deprecated
Integer waitPodCompletion(V1Pod pod, long timeout)

@Deprecated
void deletePodWhenReachStatus(String podName, String statusName, long timeout)

@Deprecated
V1Job createJob(String name, String containerImage, List<String> args)

V1Job getJob(String name)

JobStatus getJobStatus(String name)

void deleteJob(String name)
Expand All @@ -72,9 +53,6 @@ interface K8sService {

V1Job launchMirrorJob(String name, String containerImage, List<String> args, Path workDir, Path creds, MirrorConfig config)

@Deprecated
V1PodList waitJob(V1Job job, Long timeout)

V1Pod getLatestPodForJob(String jobName)

}
181 changes: 1 addition & 180 deletions src/main/groovy/io/seqera/wave/service/k8s/K8sServiceImpl.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import java.nio.file.Path
import java.time.Duration
import javax.annotation.PostConstruct

import groovy.transform.CompileDynamic
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import io.kubernetes.client.custom.Quantity
Expand All @@ -36,7 +35,6 @@ import io.kubernetes.client.openapi.models.V1JobStatus
import io.kubernetes.client.openapi.models.V1PersistentVolumeClaimVolumeSource
import io.kubernetes.client.openapi.models.V1Pod
import io.kubernetes.client.openapi.models.V1PodBuilder
import io.kubernetes.client.openapi.models.V1PodList
import io.kubernetes.client.openapi.models.V1ResourceRequirements
import io.kubernetes.client.openapi.models.V1Volume
import io.kubernetes.client.openapi.models.V1VolumeMount
Expand All @@ -46,9 +44,9 @@ import io.micronaut.context.annotation.Value
import io.micronaut.core.annotation.Nullable
import io.seqera.wave.configuration.BlobCacheConfig
import io.seqera.wave.configuration.BuildConfig
import io.seqera.wave.configuration.MirrorConfig
import io.seqera.wave.configuration.ScanConfig
import io.seqera.wave.core.ContainerPlatform
import io.seqera.wave.configuration.MirrorConfig
import io.seqera.wave.service.scan.Trivy
import jakarta.inject.Inject
import jakarta.inject.Singleton
Expand Down Expand Up @@ -136,61 +134,6 @@ class K8sServiceImpl implements K8sService {
}
}

/**
* Create a K8s job with the specified name
*
* @param name
* The K8s job name. It must be unique
* @param containerImage
* The container image to be used to run the job
* @param args
* The command to be executed by the job
* @return
* An instance of {@link V1Job}
*/
@Override
@CompileDynamic
@Deprecated
V1Job createJob(String name, String containerImage, List<String> args) {

V1Job body = new V1JobBuilder()
.withNewMetadata()
.withNamespace(namespace)
.withName(name)
.endMetadata()
.withNewSpec()
.withBackoffLimit(0)
.withNewTemplate()
.editOrNewSpec()
.addNewContainer()
.withName(name)
.withImage(containerImage)
.withArgs(args)
.endContainer()
.withRestartPolicy("Never")
.endSpec()
.endTemplate()
.endSpec()
.build()

return k8sClient
.batchV1Api()
.createNamespacedJob(namespace, body, null, null, null,null)
}

/**
* Get a Jobs Job.
*
* @param name The job name
* @return An instance of {@link V1Job}
*/
@Override
V1Job getJob(String name) {
k8sClient
.batchV1Api()
.readNamespacedJob(name, namespace, null)
}

/**
* Get a Job status
*
Expand Down Expand Up @@ -307,31 +250,7 @@ class K8sServiceImpl implements K8sService {
.subPath(rel)
}

/**
* Create a container for container image building via buildkit
*
* @param name
* The name of pod
* @param containerImage
* The container image to be used
* @param args
* The build command to be performed
* @param workDir
* The build context directory
* @param creds
* The target container repository credentials
* @return
* The {@link V1Pod} description the submitted pod
*/
@Override
@Deprecated
V1Pod buildContainer(String name, String containerImage, List<String> args, Path workDir, Path creds, Duration timeout, Map<String,String> nodeSelector) {
final spec = buildSpec(name, containerImage, args, workDir, creds, timeout, nodeSelector)
return k8sClient
.coreV1Api()
.createNamespacedPod(namespace, spec, null, null, null,null)
}

V1Pod buildSpec(String name, String containerImage, List<String> args, Path workDir, Path credsFile, Duration timeout, Map<String,String> nodeSelector) {

// dirty dependency to avoid introducing another parameter
Expand Down Expand Up @@ -408,47 +327,6 @@ class K8sServiceImpl implements K8sService {
builder.build()
}

/**
* Wait for a pod a completion.
*
* NOTE: this method assumes the pod is running exactly *one* container.
*
* @param pod
* The pod name
* @param timeout
* Max wait time in milliseconds
* @return
* An Integer value representing the container exit code or {@code null} if the state cannot be determined
* or timeout was reached.
*/
@Override
@Deprecated
Integer waitPodCompletion(V1Pod pod, long timeout) {
final start = System.currentTimeMillis()
// wait for termination
while( true ) {
final phase = pod.status?.phase
if( phase && phase != 'Pending' ) {
final status = pod.status.containerStatuses.first()
if( !status )
return null
if( !status.state )
return null
if( status.state.terminated ) {
return status.state.terminated.exitCode
}
}

if( phase == 'Failed' )
return null
final delta = System.currentTimeMillis()-start
if( delta > timeout )
return null
sleep 5_000
pod = getPod(pod.metadata.name)
}
}

/**
* Fetch the logs of a pod.
*
Expand Down Expand Up @@ -481,36 +359,6 @@ class K8sServiceImpl implements K8sService {
.deleteNamespacedPod(name, namespace, (String)null, (String)null, (Integer)null, (Boolean)null, (String)null, (V1DeleteOptions)null)
}

/**
* Delete a pod where the status is reached
*
* @param name The name of the pod to be deleted
* @param statusName The status to be reached
* @param timeout The max wait time in milliseconds
*/
@Override
@Deprecated
void deletePodWhenReachStatus(String podName, String statusName, long timeout){
final pod = getPod(podName)
final start = System.currentTimeMillis()
while( (System.currentTimeMillis() - start) < timeout ) {
if( pod?.status?.phase == statusName ) {
deletePod(podName)
return
}
sleep 5_000
}
}

@Override
@Deprecated
V1Pod scanContainer(String name, String containerImage, List<String> args, Path workDir, Path creds, ScanConfig scanConfig, Map<String,String> nodeSelector) {
final spec = scanSpec(name, containerImage, args, workDir, creds, scanConfig, nodeSelector)
return k8sClient
.coreV1Api()
.createNamespacedPod(namespace, spec, null, null, null,null)
}

@Deprecated
V1Pod scanSpec(String name, String containerImage, List<String> args, Path workDir, Path credsFile, ScanConfig scanConfig, Map<String,String> nodeSelector) {

Expand Down Expand Up @@ -865,33 +713,6 @@ class K8sServiceImpl implements K8sService {
return result
}

/**
* Wait for a job to complete
*
* @param k8s job
* @param timeout
* Max wait time in milliseconds
* @return list of pods created by the job
*/
@Deprecated
@Override
V1PodList waitJob(V1Job job, Long timeout) {
sleep 5_000
final startTime = System.currentTimeMillis()
// wait for termination
while (System.currentTimeMillis() - startTime < timeout) {
final name = job.metadata.name
final status = getJobStatus(name)
if (status != JobStatus.Pending) {
return k8sClient
.coreV1Api()
.listNamespacedPod(namespace, null, null, null, null, "job-name=$name", null, null, null, null, null, null)
}
job = getJob(name)
}
return null
}

/**
* Delete a job
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import java.time.OffsetDateTime

import io.kubernetes.client.openapi.models.V1Job
import io.kubernetes.client.openapi.models.V1Pod
import io.kubernetes.client.openapi.models.V1PodList
import io.kubernetes.client.openapi.models.V1PodStatus
import io.seqera.wave.configuration.BlobCacheConfig
import io.seqera.wave.service.blob.BlobEntry
Expand All @@ -48,11 +47,8 @@ class KubeTransferStrategyTest extends Specification {
def podName = "$jobName-abc".toString()
def pod = new V1Pod(metadata: [name: podName, creationTimestamp: OffsetDateTime.now()])
pod.status = new V1PodStatus(phase: "Succeeded")
def podList = new V1PodList(items: [pod])
k8sService.launchTransferJob(_, _, _, _) >> new V1Job(metadata: [name: jobName])
k8sService.waitJob(_, _) >> podList
k8sService.getPod(_) >> pod
k8sService.waitPodCompletion(_, _) >> 0
k8sService.logsPod(_) >> "transfer successful"

when:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@ import io.kubernetes.client.openapi.models.V1JobStatus
import io.kubernetes.client.openapi.models.V1ObjectMeta
import io.kubernetes.client.openapi.models.V1Pod
import io.kubernetes.client.openapi.models.V1PodList
import io.kubernetes.client.openapi.models.V1PodStatus
import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.Replaces
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import io.seqera.wave.configuration.BlobCacheConfig
import io.seqera.wave.configuration.ScanConfig
import io.seqera.wave.configuration.MirrorConfig
import io.seqera.wave.configuration.ScanConfig
/**
*
* @author Paolo Di Tommaso <[email protected]>
Expand Down Expand Up @@ -522,58 +521,6 @@ class K8sServiceImplTest extends Specification {
ctx.close()
}

def "deletePodWhenReachStatus should delete pod when status is reached within timeout"() {
given:
def podName = "test-pod"
def statusName = "Succeeded"
def timeout = 5000
def api = Mock(CoreV1Api)
api.readNamespacedPod(_,_,_) >> new V1Pod(status: new V1PodStatus(phase: statusName))
def k8sClient = new K8sClient() {
@Override
ApiClient apiClient() {
return null
}
CoreV1Api coreV1Api() {
return api
}
}

def k8sService = new K8sServiceImpl(k8sClient: k8sClient)

when:
k8sService.deletePodWhenReachStatus(podName, statusName, timeout)

then:
1 * api.deleteNamespacedPod('test-pod', null, null, null, null, null, null, null)
}

def "deletePodWhenReachStatus should not delete pod if status is not reached within timeout"() {
given:
def podName = "test-pod"
def statusName = "Succeeded"
def timeout = 5000
def api = Mock(CoreV1Api)
api.readNamespacedPod(_,_,_) >> new V1Pod(status: new V1PodStatus(phase: "Running"))
def k8sClient = new K8sClient() {
@Override
ApiClient apiClient() {
return null
}
CoreV1Api coreV1Api() {
return api
}
}

def k8sService = new K8sServiceImpl(k8sClient: k8sClient)

when:
k8sService.deletePodWhenReachStatus(podName, statusName, timeout)

then:
0 * api.deleteNamespacedPod('test-pod', null, null, null, null, null, null, null)
}

def "getLatestPodForJob should return the latest pod when multiple pods are present"() {
given:
def jobName = "test-job"
Expand Down

0 comments on commit 481298b

Please sign in to comment.