blob: 0d0997dcfdf75a75bf5a66b4de8aa7a67aa2e7d5 [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 Bazanski0d937772021-06-17 15:54:40 +020088}
89
90// New creates a Role Server services from a Config.
91func New(c Config) *Service {
Serge Bazanskib43d0f02022-06-23 17:32:10 +020092 s := &Service{
Serge Bazanski58ddc092022-06-30 18:23:33 +020093 Config: c,
Serge Bazanskib43d0f02022-06-23 17:32:10 +020094 }
Serge Bazanski6dff6d62022-01-28 18:15:14 +010095 s.controlPlane = &workerControlPlane{
96 storageRoot: s.StorageRoot,
97
98 bootstrapData: &s.bootstrapData,
99 clusterMembership: &s.ClusterMembership,
100 localRoles: &s.localRoles,
Serge Bazanski58ddc092022-06-30 18:23:33 +0200101 resolver: s.Resolver,
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100102 }
103
104 s.statusPush = &workerStatusPush{
105 network: s.Network,
106
107 clusterMembership: &s.ClusterMembership,
108 }
109
Mateusz Zalega32b19292022-05-17 13:26:55 +0200110 s.heartbeat = &workerHeartbeat{
111 network: s.Network,
112
113 clusterMembership: &s.ClusterMembership,
114 }
115
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100116 s.kubernetes = &workerKubernetes{
117 network: s.Network,
118 storageRoot: s.StorageRoot,
119
120 localRoles: &s.localRoles,
121 clusterMembership: &s.ClusterMembership,
122
123 kubernetesStatus: &s.KubernetesStatus,
124 }
125
126 s.rolefetch = &workerRoleFetch{
127 clusterMembership: &s.ClusterMembership,
128
129 localRoles: &s.localRoles,
130 }
131
132 return s
Serge Bazanski0d937772021-06-17 15:54:40 +0200133}
134
Mateusz Zalega2930e992022-04-25 12:52:35 +0200135func (s *Service) ProvideBootstrapData(privkey ed25519.PrivateKey, iok, cuk, nuk, jkey []byte) {
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200136 pubkey := privkey.Public().(ed25519.PublicKey)
137 nid := identity.NodeID(pubkey)
138
139 // This is the first time we have the node ID, tell the resolver that it's
140 // available on the loopback interface.
Serge Bazanski58ddc092022-06-30 18:23:33 +0200141 s.Resolver.AddOverride(nid, resolver.NodeByHostPort("127.0.0.1", uint16(common.CuratorServicePort)))
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200142
Serge Bazanski37110c32023-03-01 13:57:27 +0000143 s.ClusterMembership.Set(&ClusterMembership{
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200144 pubkey: pubkey,
Serge Bazanski58ddc092022-06-30 18:23:33 +0200145 resolver: s.Resolver,
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100146 })
Serge Bazanski37110c32023-03-01 13:57:27 +0000147 s.bootstrapData.Set(&bootstrapData{
Mateusz Zalega2930e992022-04-25 12:52:35 +0200148 nodePrivateKey: privkey,
149 initialOwnerKey: iok,
150 clusterUnlockKey: cuk,
151 nodeUnlockKey: nuk,
152 nodePrivateJoinKey: jkey,
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100153 })
Serge Bazanski0d937772021-06-17 15:54:40 +0200154}
155
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100156func (s *Service) ProvideRegisterData(credentials identity.NodeCredentials, directory *cpb.ClusterDirectory) {
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200157 // This is the first time we have the node ID, tell the resolver that it's
158 // available on the loopback interface.
Serge Bazanski58ddc092022-06-30 18:23:33 +0200159 s.Resolver.AddOverride(credentials.ID(), resolver.NodeByHostPort("127.0.0.1", uint16(common.CuratorServicePort)))
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200160
Serge Bazanski37110c32023-03-01 13:57:27 +0000161 s.ClusterMembership.Set(&ClusterMembership{
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100162 remoteCurators: directory,
163 credentials: &credentials,
164 pubkey: credentials.PublicKey(),
Serge Bazanski58ddc092022-06-30 18:23:33 +0200165 resolver: s.Resolver,
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100166 })
Serge Bazanski0d937772021-06-17 15:54:40 +0200167}
168
Mateusz Zalega2930e992022-04-25 12:52:35 +0200169func (s *Service) ProvideJoinData(credentials identity.NodeCredentials, directory *cpb.ClusterDirectory) {
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200170 // This is the first time we have the node ID, tell the resolver that it's
171 // available on the loopback interface.
Serge Bazanski58ddc092022-06-30 18:23:33 +0200172 s.Resolver.AddOverride(credentials.ID(), resolver.NodeByHostPort("127.0.0.1", uint16(common.CuratorServicePort)))
Serge Bazanskib43d0f02022-06-23 17:32:10 +0200173
Serge Bazanski37110c32023-03-01 13:57:27 +0000174 s.ClusterMembership.Set(&ClusterMembership{
Mateusz Zalega2930e992022-04-25 12:52:35 +0200175 remoteCurators: directory,
176 credentials: &credentials,
177 pubkey: credentials.PublicKey(),
Serge Bazanski58ddc092022-06-30 18:23:33 +0200178 resolver: s.Resolver,
Mateusz Zalega2930e992022-04-25 12:52:35 +0200179 })
180}
181
Serge Bazanski0d937772021-06-17 15:54:40 +0200182// Run the Role Server service, which uses intermediary workload launchers to
183// start/stop subordinate services as the Node's roles change.
184func (s *Service) Run(ctx context.Context) error {
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100185 supervisor.Run(ctx, "controlplane", s.controlPlane.run)
186 supervisor.Run(ctx, "kubernetes", s.kubernetes.run)
187 supervisor.Run(ctx, "statuspush", s.statusPush.run)
Mateusz Zalega32b19292022-05-17 13:26:55 +0200188 supervisor.Run(ctx, "heartbeat", s.heartbeat.run)
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100189 supervisor.Run(ctx, "rolefetch", s.rolefetch.run)
Serge Bazanski0d937772021-06-17 15:54:40 +0200190 supervisor.Signal(ctx, supervisor.SignalHealthy)
191
Serge Bazanski6dff6d62022-01-28 18:15:14 +0100192 <-ctx.Done()
193 return ctx.Err()
Serge Bazanski0d937772021-06-17 15:54:40 +0200194}