blob: a1969bf8df3d389a648ca14a00bab3e630e08518 [file] [log] [blame]
Serge Bazanski0d937772021-06-17 15:54:40 +02001// package roleserve implements the roleserver/“Role Server”.
2//
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
34// instance. The state of of resolution of this bootstrap problem is maintained
35// within ClusterMembership, which contains critical information about the
36// control plane, like the information required to connect to a Curator (local
37// or remote). It is updated both by external processes (ie. data from the
38// Cluster Enrolment) as well as logic responsible for spawning the control
39// plane.
Serge Bazanski0d937772021-06-17 15:54:40 +020040package roleserve
41
42import (
43 "context"
Serge Bazanski6dff6d62022-01-28 18:15:14 +010044 "crypto/ed25519"
Serge Bazanski0d937772021-06-17 15:54:40 +020045
Serge Bazanskib43d0f02022-06-23 17:32:10 +020046 common "source.monogon.dev/metropolis/node"
Lorenz Brun1de8b182021-12-21 17:15:18 +010047 "source.monogon.dev/metropolis/node/core/identity"
Serge Bazanski0d937772021-06-17 15:54:40 +020048 "source.monogon.dev/metropolis/node/core/localstorage"
49 "source.monogon.dev/metropolis/node/core/network"
Serge Bazanskib43d0f02022-06-23 17:32:10 +020050 "source.monogon.dev/metropolis/node/core/rpc/resolver"
Serge Bazanski37110c32023-03-01 13:57:27 +000051 "source.monogon.dev/metropolis/pkg/event/memory"
Serge Bazanskie012b722023-03-29 17:49:04 +020052 "source.monogon.dev/metropolis/pkg/logtree"
Serge Bazanski0d937772021-06-17 15:54:40 +020053 "source.monogon.dev/metropolis/pkg/supervisor"
Serge Bazanskie012b722023-03-29 17:49:04 +020054
Serge Bazanski6dff6d62022-01-28 18:15:14 +010055 cpb "source.monogon.dev/metropolis/proto/common"
Serge Bazanski0d937772021-06-17 15:54:40 +020056)
57
58// Config is the configuration of the role server.
59type Config struct {
Serge Bazanski0d937772021-06-17 15:54:40 +020060 // StorageRoot is a handle to access all of the Node's storage. This is needed
61 // as the roleserver spawns complex workloads like Kubernetes which need access
62 // to a broad range of storage.
63 StorageRoot *localstorage.Root
64
65 // Network is a handle to the network service, used by workloads.
66 Network *network.Service
Serge Bazanski58ddc092022-06-30 18:23:33 +020067
68 // resolver is the main, long-lived, authenticated cluster resolver that is used
69 // for all subsequent gRPC calls by the subordinates of the roleserver. It is
70 // created early in the roleserver lifecycle, and is seeded with node
71 // information as the first subordinate runs DialCurator().
72 Resolver *resolver.Resolver
Serge Bazanskie012b722023-03-29 17:49:04 +020073
74 LogTree *logtree.LogTree
Serge Bazanski0d937772021-06-17 15:54:40 +020075}
76
77// Service is the roleserver/“Role Server” service. See the package-level
78// documentation for more details.
79type Service struct {
80 Config
81
Serge Bazanski37110c32023-03-01 13:57:27 +000082 ClusterMembership memory.Value[*ClusterMembership]
83 KubernetesStatus memory.Value[*KubernetesStatus]
84 bootstrapData memory.Value[*bootstrapData]
85 localRoles memory.Value[*cpb.NodeRoles]
Serge Bazanski0d937772021-06-17 15:54:40 +020086
Serge Bazanski6dff6d62022-01-28 18:15:14 +010087 controlPlane *workerControlPlane
88 statusPush *workerStatusPush
Mateusz Zalega32b19292022-05-17 13:26:55 +020089 heartbeat *workerHeartbeat
Serge Bazanski6dff6d62022-01-28 18:15:14 +010090 kubernetes *workerKubernetes
91 rolefetch *workerRoleFetch
Serge Bazanskib40c0082023-03-29 14:28:04 +020092 nodeMgmt *workerNodeMgmt
Serge Bazanski0d937772021-06-17 15:54:40 +020093}
94
95// New creates a Role Server services from a Config.
96func New(c Config) *Service {
Serge Bazanskib43d0f02022-06-23 17:32:10 +020097 s := &Service{
Serge Bazanski58ddc092022-06-30 18:23:33 +020098 Config: c,
Serge Bazanskib43d0f02022-06-23 17:32:10 +020099 }
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100100 s.controlPlane = &workerControlPlane{
101 storageRoot: s.StorageRoot,
102
103 bootstrapData: &s.bootstrapData,
104 clusterMembership: &s.ClusterMembership,
105 localRoles: &s.localRoles,
Serge Bazanski58ddc092022-06-30 18:23:33 +0200106 resolver: s.Resolver,
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100107 }
108
109 s.statusPush = &workerStatusPush{
110 network: s.Network,
111
112 clusterMembership: &s.ClusterMembership,
113 }
114
Mateusz Zalega32b19292022-05-17 13:26:55 +0200115 s.heartbeat = &workerHeartbeat{
116 network: s.Network,
117
118 clusterMembership: &s.ClusterMembership,
119 }
120
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100121 s.kubernetes = &workerKubernetes{
122 network: s.Network,
123 storageRoot: s.StorageRoot,
124
125 localRoles: &s.localRoles,
126 clusterMembership: &s.ClusterMembership,
127
128 kubernetesStatus: &s.KubernetesStatus,
129 }
130
131 s.rolefetch = &workerRoleFetch{
132 clusterMembership: &s.ClusterMembership,
133
134 localRoles: &s.localRoles,
135 }
136
Serge Bazanskib40c0082023-03-29 14:28:04 +0200137 s.nodeMgmt = &workerNodeMgmt{
138 clusterMembership: &s.ClusterMembership,
Serge Bazanskie012b722023-03-29 17:49:04 +0200139 logTree: s.LogTree,
Serge Bazanskib40c0082023-03-29 14:28:04 +0200140 }
141
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100142 return s
Serge Bazanski0d937772021-06-17 15:54:40 +0200143}
144
Mateusz Zalega2930e992022-04-25 12:52:35 +0200145func (s *Service) ProvideBootstrapData(privkey ed25519.PrivateKey, iok, cuk, nuk, jkey []byte) {
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200146 pubkey := privkey.Public().(ed25519.PublicKey)
147 nid := identity.NodeID(pubkey)
148
149 // This is the first time we have the node ID, tell the resolver that it's
150 // available on the loopback interface.
Serge Bazanski58ddc092022-06-30 18:23:33 +0200151 s.Resolver.AddOverride(nid, resolver.NodeByHostPort("127.0.0.1", uint16(common.CuratorServicePort)))
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200152
Serge Bazanski37110c32023-03-01 13:57:27 +0000153 s.ClusterMembership.Set(&ClusterMembership{
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200154 pubkey: pubkey,
Serge Bazanski58ddc092022-06-30 18:23:33 +0200155 resolver: s.Resolver,
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100156 })
Serge Bazanski37110c32023-03-01 13:57:27 +0000157 s.bootstrapData.Set(&bootstrapData{
Mateusz Zalega2930e992022-04-25 12:52:35 +0200158 nodePrivateKey: privkey,
159 initialOwnerKey: iok,
160 clusterUnlockKey: cuk,
161 nodeUnlockKey: nuk,
162 nodePrivateJoinKey: jkey,
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100163 })
Serge Bazanski0d937772021-06-17 15:54:40 +0200164}
165
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100166func (s *Service) ProvideRegisterData(credentials identity.NodeCredentials, directory *cpb.ClusterDirectory) {
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200167 // This is the first time we have the node ID, tell the resolver that it's
168 // available on the loopback interface.
Serge Bazanski58ddc092022-06-30 18:23:33 +0200169 s.Resolver.AddOverride(credentials.ID(), resolver.NodeByHostPort("127.0.0.1", uint16(common.CuratorServicePort)))
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200170
Serge Bazanski37110c32023-03-01 13:57:27 +0000171 s.ClusterMembership.Set(&ClusterMembership{
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100172 remoteCurators: directory,
173 credentials: &credentials,
174 pubkey: credentials.PublicKey(),
Serge Bazanski58ddc092022-06-30 18:23:33 +0200175 resolver: s.Resolver,
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100176 })
Serge Bazanski0d937772021-06-17 15:54:40 +0200177}
178
Mateusz Zalega2930e992022-04-25 12:52:35 +0200179func (s *Service) ProvideJoinData(credentials identity.NodeCredentials, directory *cpb.ClusterDirectory) {
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200180 // This is the first time we have the node ID, tell the resolver that it's
181 // available on the loopback interface.
Serge Bazanski58ddc092022-06-30 18:23:33 +0200182 s.Resolver.AddOverride(credentials.ID(), 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.ClusterMembership.Set(&ClusterMembership{
Mateusz Zalega2930e992022-04-25 12:52:35 +0200185 remoteCurators: directory,
186 credentials: &credentials,
187 pubkey: credentials.PublicKey(),
Serge Bazanski58ddc092022-06-30 18:23:33 +0200188 resolver: s.Resolver,
Mateusz Zalega2930e992022-04-25 12:52:35 +0200189 })
190}
191
Serge Bazanski0d937772021-06-17 15:54:40 +0200192// Run the Role Server service, which uses intermediary workload launchers to
193// start/stop subordinate services as the Node's roles change.
194func (s *Service) Run(ctx context.Context) error {
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100195 supervisor.Run(ctx, "controlplane", s.controlPlane.run)
196 supervisor.Run(ctx, "kubernetes", s.kubernetes.run)
197 supervisor.Run(ctx, "statuspush", s.statusPush.run)
Mateusz Zalega32b19292022-05-17 13:26:55 +0200198 supervisor.Run(ctx, "heartbeat", s.heartbeat.run)
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100199 supervisor.Run(ctx, "rolefetch", s.rolefetch.run)
Serge Bazanskib40c0082023-03-29 14:28:04 +0200200 supervisor.Run(ctx, "nodemgmt", s.nodeMgmt.run)
Serge Bazanski0d937772021-06-17 15:54:40 +0200201 supervisor.Signal(ctx, supervisor.SignalHealthy)
202
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100203 <-ctx.Done()
204 return ctx.Err()
Serge Bazanski0d937772021-06-17 15:54:40 +0200205}