blob: fe29abd5fc713a533ea1b841821eea05607387f6 [file] [log] [blame]
Serge Bazanski42e61c62021-03-18 15:07:18 +01001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
Serge Bazanskia959cbd2021-06-17 15:56:51 +020017// cluster implements low-level clustering logic, especially logic regarding to
18// bootstrapping, registering into and joining a cluster. Its goal is to provide
19// the rest of the node code with the following:
20// - A mounted plaintext storage.
21// - Node credentials/identity.
22// - A locally running etcd server if the node is supposed to run one, and a
23// client connection to that etcd cluster if so.
24// - The state of the cluster as seen by the node, to enable code to respond to
25// node lifecycle changes.
Serge Bazanski42e61c62021-03-18 15:07:18 +010026package cluster
27
28import (
Serge Bazanskia959cbd2021-06-17 15:56:51 +020029 "context"
Leopold Schabela5545282021-12-04 23:29:44 +010030 "encoding/base64"
Serge Bazanskia959cbd2021-06-17 15:56:51 +020031 "errors"
Serge Bazanski42e61c62021-03-18 15:07:18 +010032 "fmt"
Leopold Schabela5545282021-12-04 23:29:44 +010033 "io"
34 "net/http"
Lorenz Brun764a2de2021-11-22 16:26:36 +010035 "os"
Serge Bazanskia959cbd2021-06-17 15:56:51 +020036 "sync"
Serge Bazanski42e61c62021-03-18 15:07:18 +010037
Leopold Schabela5545282021-12-04 23:29:44 +010038 "github.com/cenkalti/backoff/v4"
Serge Bazanskia959cbd2021-06-17 15:56:51 +020039 "google.golang.org/protobuf/proto"
40
41 "source.monogon.dev/metropolis/node/core/consensus"
42 "source.monogon.dev/metropolis/node/core/localstorage"
43 "source.monogon.dev/metropolis/node/core/network"
44 "source.monogon.dev/metropolis/pkg/event/memory"
45 "source.monogon.dev/metropolis/pkg/supervisor"
46 apb "source.monogon.dev/metropolis/proto/api"
47 ppb "source.monogon.dev/metropolis/proto/private"
Serge Bazanski42e61c62021-03-18 15:07:18 +010048)
49
Serge Bazanskia959cbd2021-06-17 15:56:51 +020050type state struct {
51 mu sync.RWMutex
Serge Bazanski42e61c62021-03-18 15:07:18 +010052
Serge Bazanskia959cbd2021-06-17 15:56:51 +020053 oneway bool
Serge Bazanski42e61c62021-03-18 15:07:18 +010054
Serge Bazanskia959cbd2021-06-17 15:56:51 +020055 configuration *ppb.SealedConfiguration
Serge Bazanski42e61c62021-03-18 15:07:18 +010056}
57
Serge Bazanskia959cbd2021-06-17 15:56:51 +020058type Manager struct {
59 storageRoot *localstorage.Root
60 networkService *network.Service
61 status memory.Value
62
63 state
64
65 // consensus is the spawned etcd/consensus service, if the Manager brought
66 // up a Node that should run one.
67 consensus *consensus.Service
68}
69
70// NewManager creates a new cluster Manager. The given localstorage Root must
71// be places, but not yet started (and will be started as the Manager makes
72// progress). The given network Service must already be running.
73func NewManager(storageRoot *localstorage.Root, networkService *network.Service) *Manager {
74 return &Manager{
75 storageRoot: storageRoot,
76 networkService: networkService,
77
78 state: state{},
79 }
80}
81
82func (m *Manager) lock() (*state, func()) {
83 m.mu.Lock()
84 return &m.state, m.mu.Unlock
85}
86
87func (m *Manager) rlock() (*state, func()) {
88 m.mu.RLock()
89 return &m.state, m.mu.RUnlock
90}
91
92// Run is the runnable of the Manager, to be started using the Supervisor. It
93// is one-shot, and should not be restarted.
94func (m *Manager) Run(ctx context.Context) error {
95 state, unlock := m.lock()
96 if state.oneway {
97 unlock()
98 // TODO(q3k): restart the entire system if this happens
99 return fmt.Errorf("cannot restart cluster manager")
100 }
101 state.oneway = true
102 unlock()
103
Lorenz Brun6c35e972021-12-14 03:08:23 +0100104 configuration, err := m.storageRoot.ESP.Metropolis.SealedConfiguration.Unseal()
Serge Bazanskia959cbd2021-06-17 15:56:51 +0200105 if err == nil {
106 supervisor.Logger(ctx).Info("Sealed configuration present. attempting to join cluster")
107 return m.join(ctx, configuration)
108 }
109
110 if !errors.Is(err, localstorage.ErrNoSealed) {
111 return fmt.Errorf("unexpected sealed config error: %w", err)
112 }
113
114 supervisor.Logger(ctx).Info("No sealed configuration, looking for node parameters")
115
116 params, err := m.nodeParams(ctx)
117 if err != nil {
118 return fmt.Errorf("no parameters available: %w", err)
119 }
120
121 switch inner := params.Cluster.(type) {
122 case *apb.NodeParameters_ClusterBootstrap_:
123 return m.bootstrap(ctx, inner.ClusterBootstrap)
124 case *apb.NodeParameters_ClusterRegister_:
125 return m.register(ctx, inner.ClusterRegister)
126 default:
127 return fmt.Errorf("node parameters misconfigured: neither cluster_bootstrap nor cluster_register set")
128 }
129}
130
Serge Bazanskia959cbd2021-06-17 15:56:51 +0200131func (m *Manager) nodeParamsFWCFG(ctx context.Context) (*apb.NodeParameters, error) {
Lorenz Brun764a2de2021-11-22 16:26:36 +0100132 bytes, err := os.ReadFile("/sys/firmware/qemu_fw_cfg/by_name/dev.monogon.metropolis/parameters.pb/raw")
Serge Bazanskia959cbd2021-06-17 15:56:51 +0200133 if err != nil {
134 return nil, fmt.Errorf("could not read firmware enrolment file: %w", err)
135 }
136
137 config := apb.NodeParameters{}
138 err = proto.Unmarshal(bytes, &config)
139 if err != nil {
140 return nil, fmt.Errorf("could not unmarshal: %v", err)
141 }
142
143 return &config, nil
144}
145
Leopold Schabela5545282021-12-04 23:29:44 +0100146// nodeParamsGCPMetadata attempts to retrieve the node parameters from the
147// GCP metadata service. Returns nil if the metadata service is available,
148// but no node parameters are specified.
149func (m *Manager) nodeParamsGCPMetadata(ctx context.Context) (*apb.NodeParameters, error) {
150 const metadataURL = "http://169.254.169.254/computeMetadata/v1/instance/attributes/metropolis-node-params"
151 req, err := http.NewRequestWithContext(ctx, "GET", metadataURL, nil)
152 if err != nil {
153 return nil, fmt.Errorf("could not create request: %w", err)
154 }
155 req.Header.Set("Metadata-Flavor", "Google")
156 resp, err := http.DefaultClient.Do(req)
157 if err != nil {
158 return nil, fmt.Errorf("HTTP request failed: %w", err)
159 }
160 defer resp.Body.Close()
161 if resp.StatusCode != http.StatusOK {
162 if resp.StatusCode == http.StatusNotFound {
163 return nil, nil
164 }
165 return nil, fmt.Errorf("non-200 status code: %d", resp.StatusCode)
166 }
167 decoded, err := io.ReadAll(base64.NewDecoder(base64.StdEncoding, resp.Body))
168 if err != nil {
169 return nil, fmt.Errorf("cannot decode base64: %w", err)
170 }
171 config := apb.NodeParameters{}
172 err = proto.Unmarshal(decoded, &config)
173 if err != nil {
174 return nil, fmt.Errorf("failed unmarshalling NodeParameters: %w", err)
175 }
176 return &config, nil
177}
178
Serge Bazanskia959cbd2021-06-17 15:56:51 +0200179func (m *Manager) nodeParams(ctx context.Context) (*apb.NodeParameters, error) {
Leopold Schabela5545282021-12-04 23:29:44 +0100180 boardName, err := getDMIBoardName()
181 if err != nil {
182 supervisor.Logger(ctx).Warningf("Could not get board name, cannot detect platform: %v", err)
183 }
184 supervisor.Logger(ctx).Infof("Board name: %q", boardName)
185
186 // When running on GCP, attempt to retrieve the node parameters from the
187 // metadata server first. Retry until we get a response, since we need to
188 // wait for the network service to assign an IP address first.
189 if isGCPInstance(boardName) {
190 var params *apb.NodeParameters
191 op := func() error {
192 supervisor.Logger(ctx).Info("Running on GCP, attempting to retrieve node parameters from metadata server")
193 params, err = m.nodeParamsGCPMetadata(ctx)
194 return err
195 }
196 err := backoff.Retry(op, backoff.WithContext(backoff.NewExponentialBackOff(), ctx))
197 if err != nil {
198 supervisor.Logger(ctx).Errorf("Failed to retrieve node parameters: %v", err)
199 }
200 if params != nil {
201 supervisor.Logger(ctx).Info("Retrieved parameters from GCP metadata server")
202 return params, nil
203 }
204 supervisor.Logger(ctx).Infof("\"metropolis-node-params\" metadata not found")
205 }
206
Serge Bazanskia959cbd2021-06-17 15:56:51 +0200207 // Retrieve node parameters from qemu's fwcfg interface or ESP.
208 // TODO(q3k): probably abstract this away and implement per platform/build/...
209 paramsFWCFG, err := m.nodeParamsFWCFG(ctx)
210 if err != nil {
211 supervisor.Logger(ctx).Warningf("Could not retrieve node parameters from qemu fwcfg: %v", err)
212 paramsFWCFG = nil
213 } else {
214 supervisor.Logger(ctx).Infof("Retrieved node parameters from qemu fwcfg")
215 }
Lorenz Brun6c35e972021-12-14 03:08:23 +0100216 paramsESP, err := m.storageRoot.ESP.Metropolis.NodeParameters.Unmarshal()
Serge Bazanskia959cbd2021-06-17 15:56:51 +0200217 if err != nil {
218 supervisor.Logger(ctx).Warningf("Could not retrieve node parameters from ESP: %v", err)
219 paramsESP = nil
220 } else {
221 supervisor.Logger(ctx).Infof("Retrieved node parameters from ESP")
222 }
223 if paramsFWCFG == nil && paramsESP == nil {
224 return nil, fmt.Errorf("could not find node parameters in ESP or qemu fwcfg")
225 }
226 if paramsFWCFG != nil && paramsESP != nil {
227 supervisor.Logger(ctx).Warningf("Node parameters found both in both ESP and qemu fwcfg, using the latter")
228 return paramsFWCFG, nil
229 } else if paramsFWCFG != nil {
230 return paramsFWCFG, nil
231 } else {
232 return paramsESP, nil
233 }
234}
235
236func (m *Manager) join(ctx context.Context, cfg *ppb.SealedConfiguration) error {
237 return fmt.Errorf("unimplemented")
238}