Skip to content

Instantly share code, notes, and snippets.

@jsnowfreedman
Created February 14, 2020 20:44
Show Gist options
  • Save jsnowfreedman/a647fd69b55c8bfec6d1bc02227d0fb5 to your computer and use it in GitHub Desktop.
Save jsnowfreedman/a647fd69b55c8bfec6d1bc02227d0fb5 to your computer and use it in GitHub Desktop.
"deployments" to DataFetcher {
data class RespDeploymentData(
val namespace: String,
val name: String,
val uid: String,
val labels: Map<String, String>,
val annotations: Map<String, String>,
val replicas: Int,
val updatedReplicas: Int,
val readyReplicas: Int,
val availableReplicas: Int
)
kubernetesConfiguration.config.currentContext.run {
kubernetesConfiguration.config.contexts.first {
it.name!!.contentEquals(this)
}.run {
val clusterHolder = kubernetesConfiguration.config.clusters.first {
it.name!!.contentEquals(this.context.cluster)
}
val userHolder = kubernetesConfiguration.config.users.first {
it.name!!.contentEquals(this.context.user)
}
Triple(this, clusterHolder, userHolder)
}.run {
val (contextHolder, clusterHolder, userHolder) = this
val tokenByExec = kubernetesConfiguration.getTokenByExec(userHolder)
if (tokenByExec.isEmpty) {
throw RuntimeException("Error, invalid token execution response")
}
Triple(contextHolder, clusterHolder, tokenByExec.get())
}.run {
val (contextHolder, clusterHolder, execToken) = this
val url = "${clusterHolder.cluster.server}/apis/extensions/v1beta1/namespaces/default/deployments"
val request = Request.Builder()
.addHeader("Authorization", "Bearer ${execToken.status.token}")
.url(url)
.get()
.build()
val (sslContext, trustManager) = kubernetesConfiguration.getTrustInfoForCluster(clusterHolder)
val okHttpClient = OkHttpClient.Builder()
.sslSocketFactory(sslContext.socketFactory, trustManager as X509TrustManager)
.build()
val response = mutableListOf<RespDeploymentData>()
try {
val execute = okHttpClient.newCall(request).execute()
val body = execute.body
if (body != null) {
val deploymentList = gson.fromJson(body.string(), Deployment.DeploymentList::class.java)
body.close()
if (deploymentList.items != null) {
deploymentList.items.forEach {
response += RespDeploymentData(
it.metadata.namespace,
it.metadata.name,
it.metadata.uid,
it.metadata.labels,
it.metadata.annotations,
it.status.replicas,
it.status.updatedReplicas,
it.status.readyReplicas,
it.status.availableReplicas
)
}
}
}
} catch (e: IOException) {
e.printStackTrace()
}
response
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment