Skip to content

Commit 057c161

Browse files
author
Adriano Santos
committed
feat: add plugins behaviour and initial monitor plugin structure
1 parent fc50cef commit 057c161

File tree

7 files changed

+267
-3
lines changed

7 files changed

+267
-3
lines changed

spawn_operator/spawn_operator/lib/spawn_operator.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ defmodule SpawnOperator do
9292
Map.get(annotations, "spawn-eigr.io/actors-global-backpressure-min-demand", "-1"),
9393
actors_global_backpressure_enabled:
9494
Map.get(annotations, "spawn-eigr.io/actors-global-backpressure-enabled", "true"),
95-
grpc_include_protos_path: Map.get(annotations, "spawn-eigr.io/grpc-include-protos-path", "/shared/protos"),
95+
grpc_include_protos_path:
96+
Map.get(annotations, "spawn-eigr.io/grpc-include-protos-path", "/shared/protos")
9697
}
9798
end
9899
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
defmodule SpawnOperator.K8s.Plugins.Monitor do
2+
@moduledoc false
3+
@behaviour SpawnOperator.K8s.Plugins
4+
5+
@default_certs_volume [
6+
%{
7+
"name" => "certs",
8+
"secret" => %{"secretName" => "tls-certs", "optional" => true}
9+
}
10+
]
11+
12+
@default_monitor_envs [
13+
%{
14+
"name" => "RELEASE_NAME",
15+
"value" => "monitor"
16+
},
17+
%{
18+
"name" => "NAMESPACE",
19+
"valueFrom" => %{"fieldRef" => %{"fieldPath" => "metadata.namespace"}}
20+
},
21+
%{
22+
"name" => "POD_NAME",
23+
"valueFrom" => %{"fieldRef" => %{"fieldPath" => "metadata.name"}}
24+
},
25+
%{
26+
"name" => "POD_NAMESPACE",
27+
"valueFrom" => %{"fieldRef" => %{"fieldPath" => "metadata.namespace"}}
28+
},
29+
%{
30+
"name" => "POD_IP",
31+
"valueFrom" => %{"fieldRef" => %{"fieldPath" => "status.podIP"}}
32+
},
33+
%{
34+
"name" => "RELEASE_DISTRIBUTION",
35+
"value" => "name"
36+
},
37+
%{
38+
"name" => "RELEASE_NODE",
39+
"value" => "$(RELEASE_NAME)@$(POD_IP)"
40+
}
41+
]
42+
43+
@default_monitor_image "gcr.io/eigr/spawn-monitor:2.0.0-RC4"
44+
45+
@default_monitor_resources [
46+
%{
47+
"requests" => %{
48+
"cpu" => "100m",
49+
"memory" => "80Mi",
50+
"ephemeral-storage" => "1M"
51+
}
52+
}
53+
]
54+
55+
@impl true
56+
def manifest(
57+
%{
58+
system: system,
59+
namespace: ns,
60+
name: name,
61+
params: params,
62+
labels: _labels,
63+
annotations: annotations
64+
} = _resource,
65+
opts
66+
) do
67+
manifests = [
68+
create_service(system, ns, name),
69+
create_deployment(system, ns, name, params, annotations, opts)
70+
]
71+
72+
{:ok, manifests}
73+
end
74+
75+
defp create_service(system, ns, name) do
76+
monitor_name = "#{system}-monitor"
77+
78+
%{
79+
"apiVersion" => "v1",
80+
"kind" => "Service",
81+
"metadata" => %{
82+
"name" => monitor_name,
83+
"namespace" => ns,
84+
"labels" => %{"app" => monitor_name, "actor-system" => system}
85+
},
86+
"spec" => %{
87+
"ports" => [
88+
%{"name" => "monitor-http", "port" => 8090, "targetPort" => 8090}
89+
],
90+
"selector" => %{"app" => monitor_name, "actor-system" => system}
91+
}
92+
}
93+
end
94+
95+
defp create_deployment(system, ns, name, params, annotations, opts) do
96+
monitor_name = "#{system}-monitor"
97+
replicas = 1
98+
99+
%{
100+
"apiVersion" => "apps/v1",
101+
"kind" => "Deployment",
102+
"metadata" => %{
103+
"name" => monitor_name,
104+
"namespace" => ns,
105+
"labels" => %{"app" => monitor_name, "actor-system" => system}
106+
},
107+
"spec" => %{
108+
"replicas" => replicas,
109+
"selector" => %{
110+
"matchLabels" => %{"actor-system" => system}
111+
},
112+
"strategy" => %{
113+
"type" => "RollingUpdate",
114+
"rollingUpdate" => %{
115+
"maxSurge" => "50%",
116+
"maxUnavailable" => 0
117+
}
118+
},
119+
"template" => %{
120+
"metadata" => %{
121+
"annotations" => %{
122+
"prometheus.io/port" => "8090",
123+
"prometheus.io/path" => "/metrics",
124+
"prometheus.io/scrape" => "true"
125+
},
126+
"labels" => %{
127+
"app" => monitor_name,
128+
"actor-system" => system
129+
}
130+
},
131+
"spec" => %{
132+
"affinity" => build_affinity(system, name),
133+
"containers" =>
134+
get_containers(
135+
system,
136+
name,
137+
annotations
138+
),
139+
"initContainers" => [
140+
%{
141+
"name" => "init-certificates",
142+
"image" => "#{annotations.proxy_init_container_image_tag}",
143+
"args" => [
144+
"--environment",
145+
:prod,
146+
"--secret",
147+
"tls-certs",
148+
"--namespace",
149+
"#{ns}",
150+
"--service",
151+
"#{system}",
152+
"--to",
153+
"#{ns}"
154+
],
155+
"env" => [
156+
%{
157+
"name" => "RELEASE_DISTRIBUTION",
158+
"value" => "none"
159+
}
160+
]
161+
}
162+
],
163+
"serviceAccountName" => "#{system}-sa",
164+
"volumes" => @default_certs_volume
165+
}
166+
}
167+
}
168+
}
169+
end
170+
171+
defp build_affinity(system, app_name) do
172+
%{
173+
"podAffinity" => %{
174+
"preferredDuringSchedulingIgnoredDuringExecution" => [
175+
%{
176+
"weight" => 50,
177+
"podAffinityTerm" => %{
178+
"labelSelector" => %{
179+
"matchExpressions" => [
180+
%{
181+
"key" => "actor-system",
182+
"operator" => "In",
183+
"values" => [
184+
system
185+
]
186+
}
187+
]
188+
},
189+
"topologyKey" => "kubernetes.io/hostname"
190+
}
191+
}
192+
]
193+
},
194+
"podAntiAffinity" => %{
195+
"preferredDuringSchedulingIgnoredDuringExecution" => [
196+
%{
197+
"weight" => 100,
198+
"podAffinityTerm" => %{
199+
"labelSelector" => %{
200+
"matchExpressions" => [
201+
%{
202+
"key" => "app",
203+
"operator" => "In",
204+
"values" => [
205+
app_name
206+
]
207+
}
208+
]
209+
},
210+
"topologyKey" => "kubernetes.io/hostname"
211+
}
212+
}
213+
]
214+
}
215+
}
216+
end
217+
218+
defp get_containers(system, name, annotations) do
219+
proxy_http_port = 8090
220+
221+
monitor_ports = [
222+
%{"containerPort" => 4369, "name" => "epmd"},
223+
%{"containerPort" => proxy_http_port, "name" => "monitor-http"}
224+
]
225+
226+
monitor_container =
227+
%{
228+
"name" => "monitor",
229+
"image" => @default_monitor_image,
230+
"env" => @default_monitor_envs,
231+
"envFrom" => [
232+
%{
233+
"configMapRef" => %{
234+
"name" => "#{name}-sidecar-cm"
235+
}
236+
},
237+
%{
238+
"secretRef" => %{
239+
"name" => "#{system}-secret"
240+
}
241+
}
242+
],
243+
"ports" => monitor_ports,
244+
"resources" => @default_monitor_resources
245+
}
246+
247+
[
248+
monitor_container
249+
]
250+
end
251+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule SpawnOperator.K8s.Plugins do
2+
@moduledoc false
3+
4+
@type resource :: map()
5+
@type manifest :: map()
6+
@type opts :: Keyword.t()
7+
8+
@callback manifest(resource(), opts()) :: {:ok, list(manifest())} | :error
9+
end

spawn_operator/spawn_operator/lib/spawn_operator/k8s/proxy/configmap/sidecar_configmap.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ defmodule SpawnOperator.K8s.Proxy.CM.Configmap do
160160
"ACTORS_GLOBAL_BACKPRESSURE_MIN_DEMAND" =>
161161
annotations.actors_global_backpressure_min_demand,
162162
"ACTORS_GLOBAL_BACKPRESSURE_ENABLED" => annotations.actors_global_backpressure_enabled,
163-
"PROXY_GRPC_INCLUDE_PROTOS_PATH" => annotations.grpc_include_protos_path,
163+
"PROXY_GRPC_INCLUDE_PROTOS_PATH" => annotations.grpc_include_protos_path
164164
}
165165
}
166166
end

spawn_operator/spawn_operator/lib/spawn_operator/k8s/proxy/deployment.ex

-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ defmodule SpawnOperator.K8s.Proxy.Deployment do
6666
"name" => "shared-volume",
6767
"emptyDir" => %{}
6868
}
69-
7069

7170
@default_certs_volume %{
7271
"name" => "certs",

spawn_operator/spawn_operator/lib/spawn_operator/operator.ex

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ defmodule SpawnOperator.Operator do
4444
]
4545
end
4646

47+
@impl true
4748
def controllers(watch_namespace, _opts) do
4849
[
4950
%{

spawn_operator/spawn_operator/mix.lock

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
"decorator": {:hex, :decorator, "1.4.0", "a57ac32c823ea7e4e67f5af56412d12b33274661bb7640ec7fc882f8d23ac419", [:mix], [], "hexpm", "0a07cedd9083da875c7418dea95b78361197cf2bf3211d743f6f7ce39656597f"},
2020
"delta_crdt": {:hex, :delta_crdt, "0.6.4", "79d235eef82a58bb0cb668bc5b9558d2e65325ccb46b74045f20b36fd41671da", [:mix], [{:merkle_map, "~> 0.2.0", [hex: :merkle_map, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4a81f579c06aeeb625db54c6c109859a38aa00d837e3e7f8ac27b40cea34885a"},
2121
"ecto": {:hex, :ecto, "3.12.5", "4a312960ce612e17337e7cefcf9be45b95a3be6b36b6f94dfb3d8c361d631866", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "6eb18e80bef8bb57e17f5a7f068a1719fbda384d40fc37acb8eb8aeca493b6ea"},
22+
"ecto_mysql_extras": {:hex, :ecto_mysql_extras, "0.6.3", "fdd487de7f7fd949f2e599fd205cda23bc47c34fdf8e972088b912aea45eb0e4", [:mix], [{:ecto_sql, "~> 3.7", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:myxql, "~> 0.5", [hex: :myxql, repo: "hexpm", optional: false]}, {:table_rex, "~> 3.1 or ~> 4.0", [hex: :table_rex, repo: "hexpm", optional: true]}], "hexpm", "24bd4f41e621f434c1d8312e4d76a513828f59cbc35191865e574dbfd893cd80"},
23+
"ecto_psql_extras": {:hex, :ecto_psql_extras, "0.8.3", "0c1df205bd051eaf599b3671e75356b121aa71eac09b63ecf921cb1a080c072e", [:mix], [{:ecto_sql, "~> 3.7", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:postgrex, "> 0.16.0 and < 0.20.0", [hex: :postgrex, repo: "hexpm", optional: false]}, {:table_rex, "~> 3.1.1 or ~> 4.0.0", [hex: :table_rex, repo: "hexpm", optional: false]}], "hexpm", "d0e35ea160359e759a2993a00c3a5389a9ca7ece6df5d0753fa927f988c7351a"},
2224
"ecto_sql": {:hex, :ecto_sql, "3.12.1", "c0d0d60e85d9ff4631f12bafa454bc392ce8b9ec83531a412c12a0d415a3a4d0", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.12", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.7", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.19 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "aff5b958a899762c5f09028c847569f7dfb9cc9d63bdb8133bff8a5546de6bf5"},
2325
"ed25519": {:hex, :ed25519, "1.4.1", "479fb83c3e31987c9cad780e6aeb8f2015fb5a482618cdf2a825c9aff809afc4", [:mix], [], "hexpm", "0dacb84f3faa3d8148e81019ca35f9d8dcee13232c32c9db5c2fb8ff48c80ec7"},
2426
"elixir_make": {:hex, :elixir_make, "0.8.3", "d38d7ee1578d722d89b4d452a3e36bcfdc644c618f0d063b874661876e708683", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:certifi, "~> 2.0", [hex: :certifi, repo: "hexpm", optional: true]}], "hexpm", "5c99a18571a756d4af7a4d89ca75c28ac899e6103af6f223982f09ce44942cc9"},
@@ -81,6 +83,7 @@
8183
"salsa20": {:hex, :salsa20, "1.0.4", "404cbea1fa8e68a41bcc834c0a2571ac175580fec01cc38cc70c0fb9ffc87e9b", [:mix], [], "hexpm", "745ddcd8cfa563ddb0fd61e7ce48d5146279a2cf7834e1da8441b369fdc58ac6"},
8284
"shards": {:hex, :shards, "1.1.0", "ed3032e63ae99f0eaa6d012b8b9f9cead48b9a810b3f91aeac266cfc4118eff6", [:make, :rebar3], [], "hexpm", "1d188e565a54a458a7a601c2fd1e74f5cfeba755c5a534239266d28b7ff124c7"},
8385
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},
86+
"table_rex": {:hex, :table_rex, "4.0.0", "3c613a68ebdc6d4d1e731bc973c233500974ec3993c99fcdabb210407b90959b", [:mix], [], "hexpm", "c35c4d5612ca49ebb0344ea10387da4d2afe278387d4019e4d8111e815df8f55"},
8487
"telemetry": {:hex, :telemetry, "1.3.0", "fedebbae410d715cf8e7062c96a1ef32ec22e764197f70cda73d82778d61e7a2", [:rebar3], [], "hexpm", "7015fc8919dbe63764f4b4b87a95b7c0996bd539e0d499be6ec9d7f3875b79e6"},
8588
"telemetry_metrics": {:hex, :telemetry_metrics, "1.0.0", "29f5f84991ca98b8eb02fc208b2e6de7c95f8bb2294ef244a176675adc7775df", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f23713b3847286a534e005126d4c959ebcca68ae9582118ce436b521d1d47d5d"},
8689
"telemetry_metrics_prometheus_core": {:hex, :telemetry_metrics_prometheus_core, "1.2.1", "c9755987d7b959b557084e6990990cb96a50d6482c683fb9622a63837f3cd3d8", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.6 or ~> 1.0", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "5e2c599da4983c4f88a33e9571f1458bf98b0cf6ba930f1dc3a6e8cf45d5afb6"},

0 commit comments

Comments
 (0)