blob: ddc5811d93c2abc6766306c8629b68a16d478442 [file] [log] [blame]
Serge Bazanski5df62ba2023-03-22 17:56:46 +01001// Package roleserve implements the roleserver/“Role Server”.
Serge Bazanski0d937772021-06-17 15:54:40 +02002//
Serge Bazanski6dff6d62022-01-28 18:15:14 +01003// The Role Server runs on every node and is responsible for running all of the
4// node's role dependant services, like the control plane (Consensus/etcd and
5// Curator) and Kubernetes. It watches the node roles as assigned by the
6// cluster's curator, updates the status of the node within the curator, and
7// spawns on-demand services.
Serge Bazanski0d937772021-06-17 15:54:40 +02008//
Serge Bazanski37110c32023-03-01 13:57:27 +00009// .-----------. .--------. Watches .------------.
10// | Cluster |--------->| Role |<----------| Node Roles |
11// | Enrolment | Provides | Server | Updates '------------'
12// '-----------' Data | |----. .-------------.
13// '--------' '----->| Node Status |
14// Spawns | | Spawns '-------------'
15// .-----' '-----.
16// V V
17// .-----------. .------------.
18// | Consensus | | Kubernetes |
19// | & Curator | | |
20// '-----------' '------------'
Serge Bazanski6dff6d62022-01-28 18:15:14 +010021//
22// The internal state of the Role Server (eg. status of services, input from
23// Cluster Enrolment, current node roles as retrieved from the cluster) is
24// stored as in-memory Event Value variables, with some of them being exposed
25// externally for other services to consume (ie. ones that wish to depend on
26// some information managed by the Role Server but which do not need to be
27// spawned on demand by the Role Server). These Event Values and code which acts
28// upon them form a reactive/dataflow-driven model which drives the Role Server
29// logic forward.
30//
31// The Role Server also has to handle the complex bootstrap problem involved in
32// simultaneously accessing the control plane (for node roles and other cluster
33// data) while maintaining (possibly the only one in the cluster) control plane
Serge Bazanskife3d8fd2023-05-30 20:50:09 +020034// instance. This problem is resolved by using the RPC resolver package which
35// allows dynamic reconfiguration of endpoints as the cluster is running.
Serge Bazanski0d937772021-06-17 15:54:40 +020036package roleserve
37
38import (
39 "context"
Serge Bazanski6dff6d62022-01-28 18:15:14 +010040 "crypto/ed25519"
Serge Bazanski0d937772021-06-17 15:54:40 +020041
Serge Bazanskib43d0f02022-06-23 17:32:10 +020042 common "source.monogon.dev/metropolis/node"
Serge Bazanski79208522023-03-28 20:14:58 +020043 "source.monogon.dev/metropolis/node/core/clusternet"
Serge Bazanski5df62ba2023-03-22 17:56:46 +010044 "source.monogon.dev/metropolis/node/core/curator"
Lorenz Brun1de8b182021-12-21 17:15:18 +010045 "source.monogon.dev/metropolis/node/core/identity"
Serge Bazanski0d937772021-06-17 15:54:40 +020046 "source.monogon.dev/metropolis/node/core/localstorage"
47 "source.monogon.dev/metropolis/node/core/network"
Serge Bazanskib43d0f02022-06-23 17:32:10 +020048 "source.monogon.dev/metropolis/node/core/rpc/resolver"
Serge Bazanski37110c32023-03-01 13:57:27 +000049 "source.monogon.dev/metropolis/pkg/event/memory"
Serge Bazanskie012b722023-03-29 17:49:04 +020050 "source.monogon.dev/metropolis/pkg/logtree"
Serge Bazanski0d937772021-06-17 15:54:40 +020051 "source.monogon.dev/metropolis/pkg/supervisor"
Serge Bazanskie012b722023-03-29 17:49:04 +020052
Serge Bazanski6dff6d62022-01-28 18:15:14 +010053 cpb "source.monogon.dev/metropolis/proto/common"
Serge Bazanski0d937772021-06-17 15:54:40 +020054)
55
56// Config is the configuration of the role server.
57type Config struct {
Serge Bazanski0d937772021-06-17 15:54:40 +020058 // StorageRoot is a handle to access all of the Node's storage. This is needed
59 // as the roleserver spawns complex workloads like Kubernetes which need access
60 // to a broad range of storage.
61 StorageRoot *localstorage.Root
62
63 // Network is a handle to the network service, used by workloads.
64 Network *network.Service
Serge Bazanski58ddc092022-06-30 18:23:33 +020065
66 // resolver is the main, long-lived, authenticated cluster resolver that is used
67 // for all subsequent gRPC calls by the subordinates of the roleserver. It is
68 // created early in the roleserver lifecycle, and is seeded with node
Serge Bazanskife3d8fd2023-05-30 20:50:09 +020069 // information from the ProvideXXX methods.
Serge Bazanski58ddc092022-06-30 18:23:33 +020070 Resolver *resolver.Resolver
Serge Bazanskie012b722023-03-29 17:49:04 +020071
72 LogTree *logtree.LogTree
Serge Bazanski0d937772021-06-17 15:54:40 +020073}
74
75// Service is the roleserver/“Role Server” service. See the package-level
76// documentation for more details.
77type Service struct {
78 Config
79
Serge Bazanski1fb2b102023-04-06 10:13:46 +020080 KubernetesStatus memory.Value[*KubernetesStatus]
81 bootstrapData memory.Value[*bootstrapData]
82 localRoles memory.Value[*cpb.NodeRoles]
83 podNetwork memory.Value[*clusternet.Prefixes]
84 clusterDirectorySaved memory.Value[bool]
Serge Bazanskife3d8fd2023-05-30 20:50:09 +020085 localControlPlane memory.Value[*localControlPlane]
86 CuratorConnection memory.Value[*curatorConnection]
Serge Bazanski0d937772021-06-17 15:54:40 +020087
Serge Bazanski6dff6d62022-01-28 18:15:14 +010088 controlPlane *workerControlPlane
89 statusPush *workerStatusPush
Mateusz Zalega32b19292022-05-17 13:26:55 +020090 heartbeat *workerHeartbeat
Serge Bazanski6dff6d62022-01-28 18:15:14 +010091 kubernetes *workerKubernetes
92 rolefetch *workerRoleFetch
Serge Bazanskib40c0082023-03-29 14:28:04 +020093 nodeMgmt *workerNodeMgmt
Serge Bazanski79208522023-03-28 20:14:58 +020094 clusternet *workerClusternet
Serge Bazanski1fb2b102023-04-06 10:13:46 +020095 hostsfile *workerHostsfile
Serge Bazanski54e212a2023-06-14 13:45:11 +020096 metrics *workerMetrics
Serge Bazanski0d937772021-06-17 15:54:40 +020097}
98
99// New creates a Role Server services from a Config.
100func New(c Config) *Service {
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200101 s := &Service{
Serge Bazanski58ddc092022-06-30 18:23:33 +0200102 Config: c,
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200103 }
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100104 s.controlPlane = &workerControlPlane{
105 storageRoot: s.StorageRoot,
106
Serge Bazanskife3d8fd2023-05-30 20:50:09 +0200107 bootstrapData: &s.bootstrapData,
108 localRoles: &s.localRoles,
109 resolver: s.Resolver,
110
111 localControlPlane: &s.localControlPlane,
112 curatorConnection: &s.CuratorConnection,
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100113 }
114
115 s.statusPush = &workerStatusPush{
116 network: s.Network,
117
Serge Bazanskife3d8fd2023-05-30 20:50:09 +0200118 curatorConnection: &s.CuratorConnection,
119 localControlPlane: &s.localControlPlane,
Serge Bazanski1fb2b102023-04-06 10:13:46 +0200120 clusterDirectorySaved: &s.clusterDirectorySaved,
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100121 }
122
Mateusz Zalega32b19292022-05-17 13:26:55 +0200123 s.heartbeat = &workerHeartbeat{
124 network: s.Network,
125
Serge Bazanskife3d8fd2023-05-30 20:50:09 +0200126 curatorConnection: &s.CuratorConnection,
Mateusz Zalega32b19292022-05-17 13:26:55 +0200127 }
128
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100129 s.kubernetes = &workerKubernetes{
130 network: s.Network,
131 storageRoot: s.StorageRoot,
132
133 localRoles: &s.localRoles,
Serge Bazanskife3d8fd2023-05-30 20:50:09 +0200134 localControlPlane: &s.localControlPlane,
135 curatorConnection: &s.CuratorConnection,
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100136
137 kubernetesStatus: &s.KubernetesStatus,
Serge Bazanski79208522023-03-28 20:14:58 +0200138 podNetwork: &s.podNetwork,
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100139 }
140
141 s.rolefetch = &workerRoleFetch{
Serge Bazanski186109c2023-06-21 16:57:36 +0200142 storageRoot: s.StorageRoot,
Serge Bazanskife3d8fd2023-05-30 20:50:09 +0200143 curatorConnection: &s.CuratorConnection,
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100144
145 localRoles: &s.localRoles,
146 }
147
Serge Bazanskib40c0082023-03-29 14:28:04 +0200148 s.nodeMgmt = &workerNodeMgmt{
Serge Bazanskife3d8fd2023-05-30 20:50:09 +0200149 curatorConnection: &s.CuratorConnection,
Serge Bazanskie012b722023-03-29 17:49:04 +0200150 logTree: s.LogTree,
Serge Bazanskib40c0082023-03-29 14:28:04 +0200151 }
Serge Bazanski1fb2b102023-04-06 10:13:46 +0200152
Serge Bazanski79208522023-03-28 20:14:58 +0200153 s.clusternet = &workerClusternet{
154 storageRoot: s.StorageRoot,
155
Serge Bazanskife3d8fd2023-05-30 20:50:09 +0200156 curatorConnection: &s.CuratorConnection,
Serge Bazanski79208522023-03-28 20:14:58 +0200157 podNetwork: &s.podNetwork,
Serge Bazanskib565cc62023-03-30 18:43:51 +0200158 network: s.Network,
Serge Bazanski79208522023-03-28 20:14:58 +0200159 }
Serge Bazanskib40c0082023-03-29 14:28:04 +0200160
Serge Bazanski1fb2b102023-04-06 10:13:46 +0200161 s.hostsfile = &workerHostsfile{
162 storageRoot: s.StorageRoot,
163 network: s.Network,
Serge Bazanskife3d8fd2023-05-30 20:50:09 +0200164 curatorConnection: &s.CuratorConnection,
Serge Bazanski1fb2b102023-04-06 10:13:46 +0200165 clusterDirectorySaved: &s.clusterDirectorySaved,
166 }
167
Serge Bazanski54e212a2023-06-14 13:45:11 +0200168 s.metrics = &workerMetrics{
169 curatorConnection: &s.CuratorConnection,
170 }
171
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100172 return s
Serge Bazanski0d937772021-06-17 15:54:40 +0200173}
174
Serge Bazanskie4a4ce12023-03-22 18:29:54 +0100175func (s *Service) ProvideBootstrapData(privkey ed25519.PrivateKey, iok, cuk, nuk, jkey []byte, icc *curator.Cluster, tpmUsage cpb.NodeTPMUsage) {
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200176 pubkey := privkey.Public().(ed25519.PublicKey)
177 nid := identity.NodeID(pubkey)
178
179 // This is the first time we have the node ID, tell the resolver that it's
180 // available on the loopback interface.
Serge Bazanski58ddc092022-06-30 18:23:33 +0200181 s.Resolver.AddOverride(nid, resolver.NodeByHostPort("127.0.0.1", uint16(common.CuratorServicePort)))
Serge Bazanski90a70a02023-05-30 15:15:27 +0200182 s.Resolver.AddEndpoint(resolver.NodeByHostPort("127.0.0.1", uint16(common.CuratorServicePort)))
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200183
Serge Bazanski37110c32023-03-01 13:57:27 +0000184 s.bootstrapData.Set(&bootstrapData{
Serge Bazanski5df62ba2023-03-22 17:56:46 +0100185 nodePrivateKey: privkey,
186 initialOwnerKey: iok,
187 clusterUnlockKey: cuk,
188 nodeUnlockKey: nuk,
189 nodePrivateJoinKey: jkey,
190 initialClusterConfiguration: icc,
Serge Bazanskie4a4ce12023-03-22 18:29:54 +0100191 nodeTPMUsage: tpmUsage,
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100192 })
Serge Bazanski0d937772021-06-17 15:54:40 +0200193}
194
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100195func (s *Service) ProvideRegisterData(credentials identity.NodeCredentials, directory *cpb.ClusterDirectory) {
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200196 // This is the first time we have the node ID, tell the resolver that it's
197 // available on the loopback interface.
Serge Bazanski58ddc092022-06-30 18:23:33 +0200198 s.Resolver.AddOverride(credentials.ID(), resolver.NodeByHostPort("127.0.0.1", uint16(common.CuratorServicePort)))
Serge Bazanski90a70a02023-05-30 15:15:27 +0200199 // Also tell the resolver about all the existing nodes in the cluster we just
Serge Bazanskife3d8fd2023-05-30 20:50:09 +0200200 // registered into. The directory passed here was used to issue the initial
201 // Register call, which means at least one of the nodes was running the control
202 // plane and thus can be used to seed the rest of the resolver.
Serge Bazanski90a70a02023-05-30 15:15:27 +0200203 for _, n := range directory.Nodes {
Serge Bazanski90a70a02023-05-30 15:15:27 +0200204 for _, addr := range n.Addresses {
Serge Bazanskife3d8fd2023-05-30 20:50:09 +0200205 s.Resolver.AddEndpoint(resolver.NodeAtAddressWithDefaultPort(addr.Host))
Serge Bazanski90a70a02023-05-30 15:15:27 +0200206 }
207 }
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200208
Serge Bazanskife3d8fd2023-05-30 20:50:09 +0200209 s.CuratorConnection.Set(newCuratorConnection(&credentials, s.Resolver))
Serge Bazanski0d937772021-06-17 15:54:40 +0200210}
211
Mateusz Zalega2930e992022-04-25 12:52:35 +0200212func (s *Service) ProvideJoinData(credentials identity.NodeCredentials, directory *cpb.ClusterDirectory) {
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200213 // This is the first time we have the node ID, tell the resolver that it's
214 // available on the loopback interface.
Serge Bazanski58ddc092022-06-30 18:23:33 +0200215 s.Resolver.AddOverride(credentials.ID(), resolver.NodeByHostPort("127.0.0.1", uint16(common.CuratorServicePort)))
Serge Bazanski90a70a02023-05-30 15:15:27 +0200216 // Also tell the resolver about all the existing nodes in the cluster we just
Serge Bazanskife3d8fd2023-05-30 20:50:09 +0200217 // joined into. The directory passed here was used to issue the initial
218 // Join call, which means at least one of the nodes was running the control
219 // plane and thus can be used to seed the rest of the resolver.
Serge Bazanski90a70a02023-05-30 15:15:27 +0200220 for _, n := range directory.Nodes {
Serge Bazanski90a70a02023-05-30 15:15:27 +0200221 for _, addr := range n.Addresses {
Serge Bazanskife3d8fd2023-05-30 20:50:09 +0200222 s.Resolver.AddEndpoint(resolver.NodeAtAddressWithDefaultPort(addr.Host))
Serge Bazanski90a70a02023-05-30 15:15:27 +0200223 }
224 }
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200225
Serge Bazanskife3d8fd2023-05-30 20:50:09 +0200226 s.CuratorConnection.Set(newCuratorConnection(&credentials, s.Resolver))
Serge Bazanski1fb2b102023-04-06 10:13:46 +0200227 s.clusterDirectorySaved.Set(true)
Mateusz Zalega2930e992022-04-25 12:52:35 +0200228}
229
Serge Bazanski0d937772021-06-17 15:54:40 +0200230// Run the Role Server service, which uses intermediary workload launchers to
231// start/stop subordinate services as the Node's roles change.
232func (s *Service) Run(ctx context.Context) error {
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100233 supervisor.Run(ctx, "controlplane", s.controlPlane.run)
234 supervisor.Run(ctx, "kubernetes", s.kubernetes.run)
235 supervisor.Run(ctx, "statuspush", s.statusPush.run)
Mateusz Zalega32b19292022-05-17 13:26:55 +0200236 supervisor.Run(ctx, "heartbeat", s.heartbeat.run)
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100237 supervisor.Run(ctx, "rolefetch", s.rolefetch.run)
Serge Bazanskib40c0082023-03-29 14:28:04 +0200238 supervisor.Run(ctx, "nodemgmt", s.nodeMgmt.run)
Serge Bazanski79208522023-03-28 20:14:58 +0200239 supervisor.Run(ctx, "clusternet", s.clusternet.run)
Serge Bazanski1fb2b102023-04-06 10:13:46 +0200240 supervisor.Run(ctx, "hostsfile", s.hostsfile.run)
Serge Bazanski54e212a2023-06-14 13:45:11 +0200241 supervisor.Run(ctx, "metrics", s.metrics.run)
Serge Bazanski0d937772021-06-17 15:54:40 +0200242 supervisor.Signal(ctx, supervisor.SignalHealthy)
243
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100244 <-ctx.Done()
245 return ctx.Err()
Serge Bazanski0d937772021-06-17 15:54:40 +0200246}