-
Notifications
You must be signed in to change notification settings - Fork 1
/
KubernetesServices.scala
172 lines (160 loc) · 6.45 KB
/
KubernetesServices.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package tech.unisonui.providers.container.actors
import akka.actor.typed.scaladsl.Behaviors
import akka.actor.typed.{ActorSystem, Behavior}
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.HttpRequest
import akka.http.scaladsl.unmarshalling.Unmarshaller
import akka.stream.scaladsl.SourceQueueWithComplete
import com.typesafe.scalalogging.LazyLogging
import skuber.{Service => KubernetesService}
import tech.unisonui.grpc.ReflectionClient
import tech.unisonui.models._
import tech.unisonui.providers.container.settings._
import scala.concurrent.{ExecutionContext, Future}
import scala.util.chaining._
import scala.util.control.Exception.allCatch
object KubernetesServices extends LazyLogging {
private final case class OpenApiService(address: String,
specificationPath: String,
useProxyValue: Boolean)
private final case class ServiceFoundWithFile(
id: String,
service: String,
openApi: Option[OpenApiService],
grpc: Option[Service.Grpc.Server])
private val Namespace = "namespace"
sealed trait Message
object Message {
final case class Add(namespace: String, services: List[KubernetesService])
extends Message
}
def apply(settingsLabels: Labels,
queue: SourceQueueWithComplete[ServiceEvent],
servicesByNamespaces: Map[String, List[KubernetesService]] =
Map.empty)(implicit
system: ActorSystem[_],
executionContext: ExecutionContext,
reflectionClient: ReflectionClient): Behavior[Message] =
Behaviors.receive { (_, message) =>
message match {
case Message.Add(namespace, newServices) =>
val filteredServices = newServices.filter(service =>
settingsLabels.extractLabels(service.metadata.labels).isDefined)
servicesByNamespaces
.get(namespace)
.fold(filteredServices) { services =>
services
.filter(service => !filteredServices.contains(service))
.flatMap(createEndpoint(settingsLabels, _))
.foreach { service =>
queue.offer(ServiceEvent.ServiceDown(service.id))
}
filteredServices
.filter(service => !services.contains(service))
}
.pipe(notifyServiceEvent(_, settingsLabels, namespace, queue))
apply(settingsLabels,
queue,
servicesByNamespaces + (namespace -> filteredServices))
}
}
private def notifyServiceEvent(
services: List[KubernetesService],
settingsLabels: Labels,
namespace: String,
queue: SourceQueueWithComplete[ServiceEvent])(implicit
system: ActorSystem[_],
executionContext: ExecutionContext,
reflectionClient: ReflectionClient) =
services
.flatMap(createEndpoint(settingsLabels, _))
.foreach {
case ServiceFoundWithFile(id,
serviceName,
openApiService,
grpcServer) =>
for {
_ <- openApiService.fold(Future.unit) {
case OpenApiService(address, specificationPath, useProxy) =>
downloadFile(
queue,
Service
.OpenApi(id,
serviceName,
address,
Map(Metadata.Provider -> "container",
Metadata.File -> specificationPath,
Namespace -> namespace),
useProxy = useProxy)
)
}
_ <- loadSchema(queue, id, serviceName, namespace, grpcServer)
} yield ()
}
private def createEndpoint(
settingsLabels: Labels,
service: KubernetesService): List[ServiceFoundWithFile] =
settingsLabels
.extractLabels(service.metadata.labels)
.map { case Labels(_, maybeOpenApi, maybeGrpc) =>
val openApiService = maybeOpenApi.map {
case OpenApiLabels(port, protocol, specificationPath, useProxy) =>
val address =
s"$protocol://${service.copySpec.clusterIP}:${port.toInt}$specificationPath"
val useProxyValue =
allCatch.opt(useProxy.toBoolean).getOrElse(false)
OpenApiService(address, specificationPath, useProxyValue)
}
val grpc = maybeGrpc.map { case GrpcLabels(port, tls) =>
Service.Grpc.Server(service.copySpec.clusterIP,
port.toInt,
allCatch.opt(tls.toBoolean).getOrElse(false))
}
ServiceFoundWithFile(service.uid, service.name, openApiService, grpc)
}
.toList
private def downloadFile(queue: SourceQueueWithComplete[ServiceEvent],
openapi: Service.OpenApi)(implicit
system: ActorSystem[_],
executionContext: ExecutionContext): Future[Unit] =
Http()
.singleRequest(HttpRequest(uri = openapi.file))
.flatMap { response =>
Unmarshaller.stringUnmarshaller(response.entity)
}
.flatMap { content =>
queue
.offer(ServiceEvent.ServiceUp(openapi.copy(file = content)))
.map(_ => ())
}
.recover { throwable =>
logger.warn("There was an error while download the file {}", throwable)
}
private def loadSchema(queue: SourceQueueWithComplete[ServiceEvent],
id: String,
serviceName: String,
namespace: String,
grpcServer: Option[Service.Grpc.Server])(implicit
reflectionClient: ReflectionClient,
executionContext: ExecutionContext): Future[Unit] =
grpcServer.fold(Future.unit) { server =>
val address = s"${server.address}:${server.port}"
reflectionClient
.loadSchema(server)
.flatMap { schema =>
val metadata = Map(
Metadata.Provider -> "container",
Metadata.File -> address,
Namespace -> namespace
)
queue
.offer(ServiceEvent.ServiceUp(Service
.Grpc(id, serviceName, schema, Map(address -> server), metadata)))
.map(_ => ())
}
.recover { throwable =>
logger.warn("There was an error while retrieving the schema {}",
throwable)
}
}
}