blob: a9e841c91908df0f672237c2c960ba63f87c4949 [file] [log] [blame]
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +02001// 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
17package node
18
19import (
Lorenz Brunaa6b7342019-12-12 02:55:02 +010020 "context"
Serge Bazanskicdb8c782020-02-17 12:34:02 +010021 "fmt"
Lorenz Brun52f7f292020-06-24 16:42:02 +020022 "os"
Lorenz Brunaa6b7342019-12-12 02:55:02 +010023
Lorenz Brun52f7f292020-06-24 16:42:02 +020024 "git.monogon.dev/source/nexantic.git/core/internal/storage"
25
Serge Bazanski7ba31522020-02-03 16:08:19 +010026 "google.golang.org/grpc/codes"
27 "google.golang.org/grpc/status"
Lorenz Bruna4ea9d02019-10-31 11:40:30 +010028
Serge Bazanskicdb8c782020-02-17 12:34:02 +010029 "git.monogon.dev/source/nexantic.git/core/generated/api"
30 "git.monogon.dev/source/nexantic.git/core/internal/common"
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020031)
32
33var (
Serge Bazanski7ba31522020-02-03 16:08:19 +010034 ErrConsensusAlreadyProvisioned = status.Error(codes.FailedPrecondition, "consensus is already provisioned; make sure the data folder is empty")
35 ErrAlreadySetup = status.Error(codes.FailedPrecondition, "node is already set up")
36 ErrNotInJoinMode = status.Error(codes.FailedPrecondition, "node is not in the cluster join mode")
37 ErrTrustNotInitialized = status.Error(codes.FailedPrecondition, "trust backend not initialized")
38 ErrStorageNotInitialized = status.Error(codes.FailedPrecondition, "storage not initialized")
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020039)
40
41func (s *SmalltownNode) CurrentState() common.SmalltownState {
42 return s.state
43}
44
Lorenz Brunaa6b7342019-12-12 02:55:02 +010045// InitializeNode contains functionality that needs to be executed regardless of what the node does
46// later on
Serge Bazanskicdb8c782020-02-17 12:34:02 +010047func (s *SmalltownNode) InitializeNode(ctx context.Context) (*api.NewNodeInfo, string, error) {
Lorenz Brunaa6b7342019-12-12 02:55:02 +010048 globalUnlockKey, err := s.Storage.InitializeData()
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020049 if err != nil {
Lorenz Brunaa6b7342019-12-12 02:55:02 +010050 return nil, "", err
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020051 }
52
Serge Bazanskicdb8c782020-02-17 12:34:02 +010053 nodeIP, err := s.Network.GetIP(ctx, true)
54 if err != nil {
55 return nil, "", fmt.Errorf("could not get IP: %v", err)
56 }
Lorenz Bruna4ea9d02019-10-31 11:40:30 +010057
Lorenz Brunaa6b7342019-12-12 02:55:02 +010058 nodeCert, nodeID, err := s.generateNodeID()
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020059 if err != nil {
Lorenz Brunaa6b7342019-12-12 02:55:02 +010060 return nil, "", err
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020061 }
62
Lorenz Brunaa6b7342019-12-12 02:55:02 +010063 return &api.NewNodeInfo{
64 EnrolmentConfig: s.enrolmentConfig,
Lorenz Brun52f7f292020-06-24 16:42:02 +020065 Ip: *nodeIP,
Lorenz Brunaa6b7342019-12-12 02:55:02 +010066 IdCert: nodeCert,
67 GlobalUnlockKey: globalUnlockKey,
68 }, nodeID, nil
69}
70
Lorenz Brun52f7f292020-06-24 16:42:02 +020071func (s *SmalltownNode) JoinCluster(ctx context.Context, req *api.JoinClusterRequest) (*api.JoinClusterResponse, error) {
Lorenz Brunaa6b7342019-12-12 02:55:02 +010072 if s.state != common.StateEnrollMode {
73 return nil, ErrNotInJoinMode
74 }
75
76 s.logger.Info("Joining Consenus")
77
Lorenz Brun52f7f292020-06-24 16:42:02 +020078 dataPath, err := s.Storage.GetPathInPlace(storage.PlaceData, "etcd")
79 if err != nil {
80 return nil, status.Errorf(codes.Unavailable, "Data partition not available: %v", err)
81 }
82
83 if err := os.MkdirAll(dataPath, 0600); err != nil {
84 return nil, status.Errorf(codes.Internal, "Cannot create path on data partition: %v", err)
85 }
86
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020087 config := s.Consensus.GetConfig()
Leopold Schabel68c58752019-11-14 21:00:59 +010088 config.Name = s.hostname
Lorenz Brun52f7f292020-06-24 16:42:02 +020089 config.InitialCluster = req.InitialCluster
90 config.DataDir = dataPath
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020091 s.Consensus.SetConfig(config)
Lorenz Brun52f7f292020-06-24 16:42:02 +020092
Lorenz Brunaa6b7342019-12-12 02:55:02 +010093 if err := s.Consensus.WriteCertificateFiles(req.Certs); err != nil {
94 return nil, err
Lorenz Bruna4ea9d02019-10-31 11:40:30 +010095 }
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020096
97 // Start consensus
98 err = s.Consensus.Start()
99 if err != nil {
Lorenz Brunaa6b7342019-12-12 02:55:02 +0100100 return nil, err
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +0200101 }
102
Lorenz Brunaa6b7342019-12-12 02:55:02 +0100103 s.state = common.StateJoined
Lorenz Brun52f7f292020-06-24 16:42:02 +0200104 go s.Containerd.Run()(context.TODO())
105 s.Kubernetes.Start()
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +0200106
107 s.logger.Info("Joined cluster. Node is now syncing.")
108
Lorenz Brunaa6b7342019-12-12 02:55:02 +0100109 return &api.JoinClusterResponse{}, nil
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +0200110}