blob: 8f9bb475c38b64506d29a62913013ab1a4af2423 [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 Bazanski0d937772021-06-17 15:54:40 +020052 "source.monogon.dev/metropolis/pkg/supervisor"
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
69 // information as the first subordinate runs DialCurator().
70 Resolver *resolver.Resolver
Serge Bazanski0d937772021-06-17 15:54:40 +020071}
72
73// Service is the roleserver/“Role Server” service. See the package-level
74// documentation for more details.
75type Service struct {
76 Config
77
Serge Bazanski37110c32023-03-01 13:57:27 +000078 ClusterMembership memory.Value[*ClusterMembership]
79 KubernetesStatus memory.Value[*KubernetesStatus]
80 bootstrapData memory.Value[*bootstrapData]
81 localRoles memory.Value[*cpb.NodeRoles]
Serge Bazanski0d937772021-06-17 15:54:40 +020082
Serge Bazanski6dff6d62022-01-28 18:15:14 +010083 controlPlane *workerControlPlane
84 statusPush *workerStatusPush
Mateusz Zalega32b19292022-05-17 13:26:55 +020085 heartbeat *workerHeartbeat
Serge Bazanski6dff6d62022-01-28 18:15:14 +010086 kubernetes *workerKubernetes
87 rolefetch *workerRoleFetch
Serge Bazanskib40c0082023-03-29 14:28:04 +020088 nodeMgmt *workerNodeMgmt
Serge Bazanski0d937772021-06-17 15:54:40 +020089}
90
91// New creates a Role Server services from a Config.
92func New(c Config) *Service {
Serge Bazanskib43d0f02022-06-23 17:32:10 +020093 s := &Service{
Serge Bazanski58ddc092022-06-30 18:23:33 +020094 Config: c,
Serge Bazanskib43d0f02022-06-23 17:32:10 +020095 }
Serge Bazanski6dff6d62022-01-28 18:15:14 +010096 s.controlPlane = &workerControlPlane{
97 storageRoot: s.StorageRoot,
98
99 bootstrapData: &s.bootstrapData,
100 clusterMembership: &s.ClusterMembership,
101 localRoles: &s.localRoles,
Serge Bazanski58ddc092022-06-30 18:23:33 +0200102 resolver: s.Resolver,
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100103 }
104
105 s.statusPush = &workerStatusPush{
106 network: s.Network,
107
108 clusterMembership: &s.ClusterMembership,
109 }
110
Mateusz Zalega32b19292022-05-17 13:26:55 +0200111 s.heartbeat = &workerHeartbeat{
112 network: s.Network,
113
114 clusterMembership: &s.ClusterMembership,
115 }
116
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100117 s.kubernetes = &workerKubernetes{
118 network: s.Network,
119 storageRoot: s.StorageRoot,
120
121 localRoles: &s.localRoles,
122 clusterMembership: &s.ClusterMembership,
123
124 kubernetesStatus: &s.KubernetesStatus,
125 }
126
127 s.rolefetch = &workerRoleFetch{
128 clusterMembership: &s.ClusterMembership,
129
130 localRoles: &s.localRoles,
131 }
132
Serge Bazanskib40c0082023-03-29 14:28:04 +0200133 s.nodeMgmt = &workerNodeMgmt{
134 clusterMembership: &s.ClusterMembership,
135 }
136
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100137 return s
Serge Bazanski0d937772021-06-17 15:54:40 +0200138}
139
Mateusz Zalega2930e992022-04-25 12:52:35 +0200140func (s *Service) ProvideBootstrapData(privkey ed25519.PrivateKey, iok, cuk, nuk, jkey []byte) {
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200141 pubkey := privkey.Public().(ed25519.PublicKey)
142 nid := identity.NodeID(pubkey)
143
144 // This is the first time we have the node ID, tell the resolver that it's
145 // available on the loopback interface.
Serge Bazanski58ddc092022-06-30 18:23:33 +0200146 s.Resolver.AddOverride(nid, resolver.NodeByHostPort("127.0.0.1", uint16(common.CuratorServicePort)))
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200147
Serge Bazanski37110c32023-03-01 13:57:27 +0000148 s.ClusterMembership.Set(&ClusterMembership{
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200149 pubkey: pubkey,
Serge Bazanski58ddc092022-06-30 18:23:33 +0200150 resolver: s.Resolver,
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100151 })
Serge Bazanski37110c32023-03-01 13:57:27 +0000152 s.bootstrapData.Set(&bootstrapData{
Mateusz Zalega2930e992022-04-25 12:52:35 +0200153 nodePrivateKey: privkey,
154 initialOwnerKey: iok,
155 clusterUnlockKey: cuk,
156 nodeUnlockKey: nuk,
157 nodePrivateJoinKey: jkey,
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100158 })
Serge Bazanski0d937772021-06-17 15:54:40 +0200159}
160
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100161func (s *Service) ProvideRegisterData(credentials identity.NodeCredentials, directory *cpb.ClusterDirectory) {
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200162 // This is the first time we have the node ID, tell the resolver that it's
163 // available on the loopback interface.
Serge Bazanski58ddc092022-06-30 18:23:33 +0200164 s.Resolver.AddOverride(credentials.ID(), resolver.NodeByHostPort("127.0.0.1", uint16(common.CuratorServicePort)))
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200165
Serge Bazanski37110c32023-03-01 13:57:27 +0000166 s.ClusterMembership.Set(&ClusterMembership{
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100167 remoteCurators: directory,
168 credentials: &credentials,
169 pubkey: credentials.PublicKey(),
Serge Bazanski58ddc092022-06-30 18:23:33 +0200170 resolver: s.Resolver,
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100171 })
Serge Bazanski0d937772021-06-17 15:54:40 +0200172}
173
Mateusz Zalega2930e992022-04-25 12:52:35 +0200174func (s *Service) ProvideJoinData(credentials identity.NodeCredentials, directory *cpb.ClusterDirectory) {
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200175 // This is the first time we have the node ID, tell the resolver that it's
176 // available on the loopback interface.
Serge Bazanski58ddc092022-06-30 18:23:33 +0200177 s.Resolver.AddOverride(credentials.ID(), resolver.NodeByHostPort("127.0.0.1", uint16(common.CuratorServicePort)))
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200178
Serge Bazanski37110c32023-03-01 13:57:27 +0000179 s.ClusterMembership.Set(&ClusterMembership{
Mateusz Zalega2930e992022-04-25 12:52:35 +0200180 remoteCurators: directory,
181 credentials: &credentials,
182 pubkey: credentials.PublicKey(),
Serge Bazanski58ddc092022-06-30 18:23:33 +0200183 resolver: s.Resolver,
Mateusz Zalega2930e992022-04-25 12:52:35 +0200184 })
185}
186
Serge Bazanski0d937772021-06-17 15:54:40 +0200187// Run the Role Server service, which uses intermediary workload launchers to
188// start/stop subordinate services as the Node's roles change.
189func (s *Service) Run(ctx context.Context) error {
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100190 supervisor.Run(ctx, "controlplane", s.controlPlane.run)
191 supervisor.Run(ctx, "kubernetes", s.kubernetes.run)
192 supervisor.Run(ctx, "statuspush", s.statusPush.run)
Mateusz Zalega32b19292022-05-17 13:26:55 +0200193 supervisor.Run(ctx, "heartbeat", s.heartbeat.run)
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100194 supervisor.Run(ctx, "rolefetch", s.rolefetch.run)
Serge Bazanskib40c0082023-03-29 14:28:04 +0200195 supervisor.Run(ctx, "nodemgmt", s.nodeMgmt.run)
Serge Bazanski0d937772021-06-17 15:54:40 +0200196 supervisor.Signal(ctx, supervisor.SignalHealthy)
197
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100198 <-ctx.Done()
199 return ctx.Err()
Serge Bazanski0d937772021-06-17 15:54:40 +0200200}