Skip to content

Commit

Permalink
Merge pull request #196 from modelix/dependabot/gradle/io.kubernetes-…
Browse files Browse the repository at this point in the history
…client-java-22.0.0

build(deps): bump io.kubernetes:client-java from 21.0.1-legacy to 22.0.0
  • Loading branch information
slisson authored Dec 12, 2024
2 parents 46a72ad + 93d3597 commit fafacea
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 129 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ ktor-server-cors = { group = "io.ktor", name = "ktor-server-cors", version.ref =
ktor-server-html-builder = { group = "io.ktor", name = "ktor-server-html-builder", version.ref = "ktor" }
ktor-server-netty = { group = "io.ktor", name = "ktor-server-netty", version.ref = "ktor" }
ktor-server-status-pages = { group = "io.ktor", name = "ktor-server-status-pages", version.ref = "ktor" }
kubernetes-client = { group = "io.kubernetes", name = "client-java", version = "21.0.1-legacy" }
kubernetes-client = { group = "io.kubernetes", name = "client-java", version = "22.0.0" }
logback-classic = { group = "ch.qos.logback", name = "logback-classic", version.ref = "logback" }
maven-invoker = { group = "org.apache.maven.shared", name = "maven-invoker", version = "3.3.0" }
modelix-authorization = { group = "org.modelix", name = "authorization", version.ref = "modelix-core" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,20 +327,10 @@ class DeploymentManager {
}
val appsApi = AppsV1Api()
val coreApi = CoreV1Api()
val deployments = appsApi.listNamespacedDeployment(
KUBERNETES_NAMESPACE,
null,
null,
null,
null,
null,
null,
null,
null,
null,
TIMEOUT_SECONDS,
false
)
val deployments = appsApi
.listNamespacedDeployment(KUBERNETES_NAMESPACE)
.timeoutSeconds(TIMEOUT_SECONDS)
.execute()
for (deployment in deployments.items) {
val name = deployment.metadata!!.name!!
if (name.startsWith(INSTANCE_PREFIX)) {
Expand All @@ -351,30 +341,12 @@ class DeploymentManager {
val toRemove = existingDeployments - expectedDeployments.keys
for (d in toRemove) {
try {
appsApi.deleteNamespacedDeployment(
d.name,
KUBERNETES_NAMESPACE,
null,
null,
null,
null,
null,
null
)
appsApi.deleteNamespacedDeployment(d.name, KUBERNETES_NAMESPACE).execute()
} catch (e: Exception) {
LOG.error("Failed to delete deployment $d", e)
}
try {
coreApi.deleteNamespacedService(
d.name,
KUBERNETES_NAMESPACE,
null,
null,
null,
null,
null,
null
)
coreApi.deleteNamespacedService(d.name, KUBERNETES_NAMESPACE).execute()
} catch (e: Exception) {
LOG.error("Failed to delete service $d", e)
}
Expand Down Expand Up @@ -406,7 +378,7 @@ class DeploymentManager {
var deployment: V1Deployment? = null
for (i in 0 until attempts) {
try {
deployment = appsApi.readNamespacedDeployment(name.name, KUBERNETES_NAMESPACE, null)
deployment = appsApi.readNamespacedDeployment(name.name, KUBERNETES_NAMESPACE).execute()
} catch (ex: ApiException) {
LOG.error("Failed to read deployment: $name", ex)
}
Expand All @@ -422,27 +394,14 @@ class DeploymentManager {

fun getDockerRegistryPort(): Int {
val api = CoreV1Api()
var service: V1Service? = api.readNamespacedService(System.getenv("DOCKER_REGISTRY_SERVICE_NAME"), KUBERNETES_NAMESPACE, null)
var service: V1Service? = api.readNamespacedService(System.getenv("DOCKER_REGISTRY_SERVICE_NAME"), KUBERNETES_NAMESPACE).execute()
return service?.spec?.ports?.firstNotNullOfOrNull { it.nodePort } ?: throw IllegalStateException("Registry port unknown")
}

fun getPod(deploymentName: InstanceName): V1Pod? {
try {
val coreApi = CoreV1Api()
val pods = coreApi.listNamespacedPod(
KUBERNETES_NAMESPACE,
null,
null,
null,
null,
null,
null,
null,
null,
null,
TIMEOUT_SECONDS,
false
)
val pods = coreApi.listNamespacedPod(KUBERNETES_NAMESPACE).timeoutSeconds(TIMEOUT_SECONDS).execute()
for (pod in pods.items) {
if (!pod.metadata!!.name!!.startsWith(deploymentName.name)) continue
return pod
Expand All @@ -457,28 +416,15 @@ class DeploymentManager {
fun getPodLogs(deploymentName: InstanceName): String? {
try {
val coreApi = CoreV1Api()
val pods = coreApi.listNamespacedPod(
KUBERNETES_NAMESPACE,
null,
null,
null,
null,
null,
null,
null,
null,
null,
TIMEOUT_SECONDS,
false
)
val pods = coreApi.listNamespacedPod(KUBERNETES_NAMESPACE).timeoutSeconds(TIMEOUT_SECONDS).execute()
for (pod in pods.items) {
if (!pod.metadata!!.name!!.startsWith(deploymentName.name)) continue
return coreApi.readNamespacedPodLog(
pod.metadata!!.name,
KUBERNETES_NAMESPACE,
pod.spec!!.containers[0].name,
null, null, null, "true", null, null, 10000, null
)
return coreApi
.readNamespacedPodLog(pod.metadata!!.name,KUBERNETES_NAMESPACE)
.container(pod.spec!!.containers[0].name)
.pretty("true")
.tailLines(10_000)
.execute()
}
} catch (e: Exception) {
LOG.error("", e)
Expand All @@ -502,20 +448,10 @@ class DeploymentManager {

fun getEvents(deploymentName: String?): List<CoreV1Event> {
if (deploymentName == null) return emptyList()
val events: CoreV1EventList = CoreV1Api().listNamespacedEvent(
KUBERNETES_NAMESPACE,
null,
null,
null,
null,
null,
null,
null,
null,
null,
TIMEOUT_SECONDS,
false
)
val events: CoreV1EventList = CoreV1Api()
.listNamespacedEvent(KUBERNETES_NAMESPACE)
.timeoutSeconds(TIMEOUT_SECONDS)
.execute()
return events.items
.filter { (it.involvedObject.name ?: "").contains(deploymentName) }
}
Expand Down Expand Up @@ -547,26 +483,13 @@ class DeploymentManager {
): Boolean {
val originalDeploymentName = WORKSPACE_CLIENT_DEPLOYMENT_NAME
val appsApi = AppsV1Api()
val deployments = appsApi.listNamespacedDeployment(
KUBERNETES_NAMESPACE,
null,
null,
null,
null,
null,
null,
null,
null,
null,
5,
false
)
val deployments = appsApi.listNamespacedDeployment(KUBERNETES_NAMESPACE).timeoutSeconds(5).execute()
val deploymentExists =
deployments.items.stream().anyMatch { d: V1Deployment -> instanceName.name == d.metadata!!.name }
if (!deploymentExists) {
// long numExisting = deployments.getItems().stream().filter(d -> d.getMetadata().getName().startsWith(personalDeploymentPrefix)).count();
// if (numExisting > 10) throw new RuntimeException("Too many existing deployments");
val deployment = appsApi.readNamespacedDeployment(originalDeploymentName, KUBERNETES_NAMESPACE, null)
val deployment = appsApi.readNamespacedDeployment(originalDeploymentName, KUBERNETES_NAMESPACE).execute()
deployment.metadata!!.creationTimestamp(null)
deployment.metadata!!.managedFields = null
deployment.metadata!!.uid = null
Expand Down Expand Up @@ -617,26 +540,13 @@ class DeploymentManager {
loadWorkspaceSpecificValues(workspace, deployment)
println("Creating deployment: ")
println(Yaml.dump(deployment))
appsApi.createNamespacedDeployment(KUBERNETES_NAMESPACE, deployment, null, null, null, null)
appsApi.createNamespacedDeployment(KUBERNETES_NAMESPACE, deployment).execute()
}
val coreApi = CoreV1Api()
val services = coreApi.listNamespacedService(
KUBERNETES_NAMESPACE,
null,
null,
null,
null,
null,
null,
null,
null,
null,
TIMEOUT_SECONDS,
false
)
val services = coreApi.listNamespacedService(KUBERNETES_NAMESPACE).timeoutSeconds(TIMEOUT_SECONDS).execute()
val serviceExists = services.items.stream().anyMatch { s: V1Service -> instanceName.name == s.metadata!!.name }
if (!serviceExists) {
val service = coreApi.readNamespacedService(originalDeploymentName, KUBERNETES_NAMESPACE, null)
val service = coreApi.readNamespacedService(originalDeploymentName, KUBERNETES_NAMESPACE).execute()
service.metadata!!.putAnnotationsItem("kubectl.kubernetes.io/last-applied-configuration", null)
service.metadata!!.managedFields = null
service.metadata!!.uid = null
Expand All @@ -655,7 +565,7 @@ class DeploymentManager {
service.spec!!.putSelectorItem("component", instanceName.name)
println("Creating service: ")
println(Yaml.dump(service))
coreApi.createNamespacedService(KUBERNETES_NAMESPACE, service, null, null, null, null)
coreApi.createNamespacedService(KUBERNETES_NAMESPACE, service).execute()
}
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,15 @@ class WorkspaceJobQueue(val tokenGenerator: (Workspace) -> String) {
workspaceHash2job.values.associateBy { it.kubernetesJobName }
}
val existingJobs: Map<String?, V1Job> = BatchV1Api()
.listNamespacedJob(KUBERNETES_NAMESPACE, null, null, null, null, null, null, null, null, null, null, false)
.listNamespacedJob(KUBERNETES_NAMESPACE)
.execute()
.items.filter { it.metadata?.name?.startsWith(JOB_PREFIX) == true }
.associateBy { it.metadata?.name }

val unexpected: Map<String?, V1Job> = existingJobs - expectedJobs.keys
for (toRemove in unexpected) {
expectedJobs[toRemove.key]?.updateLog()
BatchV1Api().deleteNamespacedJob(toRemove.key, KUBERNETES_NAMESPACE, null, null, null, null, null, null)
BatchV1Api().deleteNamespacedJob(toRemove.key, KUBERNETES_NAMESPACE).execute()
}

val missingJobs: Map<String?, Job> = expectedJobs - existingJobs.keys
Expand All @@ -122,7 +123,7 @@ class WorkspaceJobQueue(val tokenGenerator: (Workspace) -> String) {
val yamlString = missingJob.generateJobYaml()
try {
val job: V1Job = Yaml.loadAs(yamlString, V1Job::class.java)
BatchV1Api().createNamespacedJob(KUBERNETES_NAMESPACE, job, null, null, null, null)
BatchV1Api().createNamespacedJob(KUBERNETES_NAMESPACE, job).execute()
missingJob.kubernetesJob = job
missingJob.status = WorkspaceBuildStatus.Queued
} catch (ex: Exception) {
Expand All @@ -134,21 +135,20 @@ class WorkspaceJobQueue(val tokenGenerator: (Workspace) -> String) {
private fun getPodLogs(podNamePrefix: String): String? {
try {
val coreApi = CoreV1Api()
val pods = coreApi
.listNamespacedPod(KUBERNETES_NAMESPACE, null, null, null, null, null, null, null, null, null, null, false)
val pods = coreApi.listNamespacedPod(KUBERNETES_NAMESPACE).execute()
val matchingPods = pods.items.filter { it.metadata!!.name!!.startsWith(podNamePrefix) }
if (matchingPods.isEmpty()) return null
return matchingPods.map { pod ->
return matchingPods.joinToString("\n----------------------------------------------------------------------------\n") { pod ->
try {
coreApi.readNamespacedPodLog(
pod.metadata!!.name,
KUBERNETES_NAMESPACE,
pod.spec!!.containers[0].name,
null, null, null, "true", null, null, 10000, false)
coreApi.readNamespacedPodLog(pod.metadata!!.name, KUBERNETES_NAMESPACE)
.container(pod.spec!!.containers[0].name)
.pretty("true")
.tailLines(10_000)
.execute()
} catch (ex: Exception) {
ex.stackTraceToString()
}
}.joinToString("\n----------------------------------------------------------------------------\n")
}
} catch (ex: Exception) {
return ex.stackTraceToString()
}
Expand Down

0 comments on commit fafacea

Please sign in to comment.