Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IngressController code optimization #2913

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,24 @@ object KubernetesRetriever extends Logger {
// see org.apache.flink.configuration.RestOptions.RETRY_MAX_ATTEMPTS
val FLINK_REST_RETRY_MAX_ATTEMPTS = 2

private var kubernetesClient: KubernetesClient = _

/** get new KubernetesClient */
@throws(classOf[KubernetesClientException])
def newK8sClient(): KubernetesClient = {
new DefaultKubernetesClient()
def getK8sClient(): KubernetesClient = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are better ways to implement the singleton pattern in scala:

private lazy val kubernetesClient: KubernetesClient = new DefaultKubernetesClient()
def getK8sClient(): KubernetesClient = kubernetesClient

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks for reviewing the code

if (kubernetesClient == null) {
synchronized {
if (kubernetesClient == null) {
kubernetesClient = new DefaultKubernetesClient()
}
}
}
kubernetesClient
}

/** check connection of kubernetes cluster */
def checkK8sConnection(): Boolean = {
Try(newK8sClient().getVersion != null).getOrElse(false)
Try(getK8sClient().getVersion != null).getOrElse(false)
}

private val clusterClientServiceLoader = new DefaultClusterClientServiceLoader()
Expand Down Expand Up @@ -107,7 +116,7 @@ object KubernetesRetriever extends Logger {
* deployment namespace
*/
def isDeploymentExists(name: String, namespace: String): Boolean = {
using(KubernetesRetriever.newK8sClient()) {
using(KubernetesRetriever.getK8sClient()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The KubernetesRetriever.getK8sClient() method will return a singleton KubernetesClient. However, the using method closes the KubernetesClient after used, which can lead to bugs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks for reviewing the code

client =>
client
.apps()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import scala.util.{Success, Try}
object KubernetesDeploymentHelper extends Logger {

private[this] def getPods(nameSpace: String, deploymentName: String): List[Pod] = {
using(KubernetesRetriever.newK8sClient()) {
using(KubernetesRetriever.getK8sClient()) {
client =>
Try {
client.pods
Expand Down Expand Up @@ -68,7 +68,7 @@ object KubernetesDeploymentHelper extends Logger {
}

def deleteTaskDeployment(nameSpace: String, deploymentName: String): Boolean = {
using(KubernetesRetriever.newK8sClient()) {
using(KubernetesRetriever.getK8sClient()) {
client =>
Try {
val r = client.apps.deployments
Expand All @@ -90,7 +90,7 @@ object KubernetesDeploymentHelper extends Logger {
}

def watchDeploymentLog(nameSpace: String, jobName: String, jobId: String): String = {
using(KubernetesRetriever.newK8sClient()) {
using(KubernetesRetriever.getK8sClient()) {
client =>
val path = KubernetesDeploymentHelper.getJobLog(jobId)
val file = new File(path)
Expand All @@ -101,7 +101,7 @@ object KubernetesDeploymentHelper extends Logger {
}

def watchPodTerminatedLog(nameSpace: String, jobName: String, jobId: String): String = {
using(KubernetesRetriever.newK8sClient()) {
using(KubernetesRetriever.getK8sClient()) {
client =>
Try {
val podName = getPods(nameSpace, jobName).head.getMetadata.getName
Expand All @@ -120,7 +120,7 @@ object KubernetesDeploymentHelper extends Logger {
}

def deleteTaskConfigMap(nameSpace: String, deploymentName: String): Boolean = {
using(KubernetesRetriever.newK8sClient()) {
using(KubernetesRetriever.getK8sClient()) {
client =>
Try {
val r = client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package org.apache.streampark.flink.kubernetes.ingress

import org.apache.streampark.common.util.Logger
import org.apache.streampark.common.util.Utils.using
import org.apache.streampark.flink.kubernetes.KubernetesRetriever

import io.fabric8.kubernetes.client.DefaultKubernetesClient
import org.apache.flink.client.program.ClusterClient

import scala.language.postfixOps
Expand All @@ -30,7 +30,7 @@ object IngressController extends Logger {
private[this] val VERSION_REGEXP = "(\\d+\\.\\d+)".r

private lazy val ingressStrategy: IngressStrategy = {
using(new DefaultKubernetesClient()) {
using(KubernetesRetriever.getK8sClient()) {
client =>
val version = VERSION_REGEXP.findFirstIn(client.getVersion.getGitVersion).get.toDouble
if (version >= 1.19) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.streampark.flink.kubernetes.ingress

import io.fabric8.kubernetes.api.model.{OwnerReference, OwnerReferenceBuilder}
import io.fabric8.kubernetes.client.DefaultKubernetesClient
import io.fabric8.kubernetes.client.KubernetesClient
import org.apache.commons.io.FileUtils
import org.apache.flink.client.program.ClusterClient

Expand Down Expand Up @@ -64,7 +64,7 @@ trait IngressStrategy {
def getOwnerReference(
nameSpace: String,
clusterId: String,
client: DefaultKubernetesClient): OwnerReference = {
client: KubernetesClient): OwnerReference = {

val deployment = client
.apps()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
package org.apache.streampark.flink.kubernetes.ingress

import org.apache.streampark.common.util.Utils
import org.apache.streampark.flink.kubernetes.KubernetesRetriever

import io.fabric8.kubernetes.api.model.networking.v1.IngressBuilder
import io.fabric8.kubernetes.client.DefaultKubernetesClient
import org.apache.flink.client.program.ClusterClient

import scala.collection.JavaConverters._
Expand All @@ -34,7 +34,7 @@ class IngressStrategyV1 extends IngressStrategy {
clusterId: String,
clusterClient: ClusterClient[_]): String = {

Utils.using(new DefaultKubernetesClient) {
Utils.using(KubernetesRetriever.getK8sClient()) {
client =>
Try {
Option(client.network.v1.ingresses.inNamespace(nameSpace).withName(clusterId).get)
Expand All @@ -51,7 +51,7 @@ class IngressStrategyV1 extends IngressStrategy {
}

override def configureIngress(domainName: String, clusterId: String, nameSpace: String): Unit = {
Utils.using(new DefaultKubernetesClient) {
Utils.using(KubernetesRetriever.getK8sClient()) {
client =>
val ownerReference = getOwnerReference(nameSpace, clusterId, client)
val ingress = new IngressBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.streampark.flink.kubernetes.ingress

import org.apache.streampark.common.util.Utils
import org.apache.streampark.flink.kubernetes.KubernetesRetriever

import io.fabric8.kubernetes.api.model.IntOrString
import io.fabric8.kubernetes.api.model.networking.v1beta1.IngressBuilder
Expand All @@ -35,7 +36,7 @@ class IngressStrategyV1beta1 extends IngressStrategy {
clusterId: String,
clusterClient: ClusterClient[_]): String = {

Utils.using(new DefaultKubernetesClient) {
Utils.using(KubernetesRetriever.getK8sClient()) {
client =>
Try {
Option(client.network.v1beta1.ingresses.inNamespace(nameSpace).withName(clusterId).get)
Expand All @@ -51,7 +52,7 @@ class IngressStrategyV1beta1 extends IngressStrategy {
}

override def configureIngress(domainName: String, clusterId: String, nameSpace: String): Unit = {
Utils.using(new DefaultKubernetesClient) {
Utils.using(KubernetesRetriever.getK8sClient()) {
client =>
val ownerReference = getOwnerReference(nameSpace, clusterId, client)
val ingress = new IngressBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class FlinkK8sEventWatcher(implicit watchController: FlinkK8sWatchController)

/** start watcher process */
override def doStart(): Unit = {
k8sClient = Try(KubernetesRetriever.newK8sClient()).getOrElse {
k8sClient = Try(KubernetesRetriever.getK8sClient()).getOrElse {
logError("[flink-k8s] FlinkK8sEventWatcher fails to start.")
return
}
Expand Down
Loading