Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 1 | // 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 | |
| 17 | package node |
| 18 | |
| 19 | import ( |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 20 | "context" |
Serge Bazanski | cdb8c78 | 2020-02-17 12:34:02 +0100 | [diff] [blame] | 21 | "fmt" |
Lorenz Brun | 52f7f29 | 2020-06-24 16:42:02 +0200 | [diff] [blame^] | 22 | "os" |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 23 | |
Lorenz Brun | 52f7f29 | 2020-06-24 16:42:02 +0200 | [diff] [blame^] | 24 | "git.monogon.dev/source/nexantic.git/core/internal/storage" |
| 25 | |
Serge Bazanski | 7ba3152 | 2020-02-03 16:08:19 +0100 | [diff] [blame] | 26 | "google.golang.org/grpc/codes" |
| 27 | "google.golang.org/grpc/status" |
Lorenz Brun | a4ea9d0 | 2019-10-31 11:40:30 +0100 | [diff] [blame] | 28 | |
Serge Bazanski | cdb8c78 | 2020-02-17 12:34:02 +0100 | [diff] [blame] | 29 | "git.monogon.dev/source/nexantic.git/core/generated/api" |
| 30 | "git.monogon.dev/source/nexantic.git/core/internal/common" |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | var ( |
Serge Bazanski | 7ba3152 | 2020-02-03 16:08:19 +0100 | [diff] [blame] | 34 | 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 Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 39 | ) |
| 40 | |
| 41 | func (s *SmalltownNode) CurrentState() common.SmalltownState { |
| 42 | return s.state |
| 43 | } |
| 44 | |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 45 | // InitializeNode contains functionality that needs to be executed regardless of what the node does |
| 46 | // later on |
Serge Bazanski | cdb8c78 | 2020-02-17 12:34:02 +0100 | [diff] [blame] | 47 | func (s *SmalltownNode) InitializeNode(ctx context.Context) (*api.NewNodeInfo, string, error) { |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 48 | globalUnlockKey, err := s.Storage.InitializeData() |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 49 | if err != nil { |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 50 | return nil, "", err |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Serge Bazanski | cdb8c78 | 2020-02-17 12:34:02 +0100 | [diff] [blame] | 53 | nodeIP, err := s.Network.GetIP(ctx, true) |
| 54 | if err != nil { |
| 55 | return nil, "", fmt.Errorf("could not get IP: %v", err) |
| 56 | } |
Lorenz Brun | a4ea9d0 | 2019-10-31 11:40:30 +0100 | [diff] [blame] | 57 | |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 58 | nodeCert, nodeID, err := s.generateNodeID() |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 59 | if err != nil { |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 60 | return nil, "", err |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 63 | return &api.NewNodeInfo{ |
| 64 | EnrolmentConfig: s.enrolmentConfig, |
Lorenz Brun | 52f7f29 | 2020-06-24 16:42:02 +0200 | [diff] [blame^] | 65 | Ip: *nodeIP, |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 66 | IdCert: nodeCert, |
| 67 | GlobalUnlockKey: globalUnlockKey, |
| 68 | }, nodeID, nil |
| 69 | } |
| 70 | |
Lorenz Brun | 52f7f29 | 2020-06-24 16:42:02 +0200 | [diff] [blame^] | 71 | func (s *SmalltownNode) JoinCluster(ctx context.Context, req *api.JoinClusterRequest) (*api.JoinClusterResponse, error) { |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 72 | if s.state != common.StateEnrollMode { |
| 73 | return nil, ErrNotInJoinMode |
| 74 | } |
| 75 | |
| 76 | s.logger.Info("Joining Consenus") |
| 77 | |
Lorenz Brun | 52f7f29 | 2020-06-24 16:42:02 +0200 | [diff] [blame^] | 78 | 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 Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 87 | config := s.Consensus.GetConfig() |
Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 88 | config.Name = s.hostname |
Lorenz Brun | 52f7f29 | 2020-06-24 16:42:02 +0200 | [diff] [blame^] | 89 | config.InitialCluster = req.InitialCluster |
| 90 | config.DataDir = dataPath |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 91 | s.Consensus.SetConfig(config) |
Lorenz Brun | 52f7f29 | 2020-06-24 16:42:02 +0200 | [diff] [blame^] | 92 | |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 93 | if err := s.Consensus.WriteCertificateFiles(req.Certs); err != nil { |
| 94 | return nil, err |
Lorenz Brun | a4ea9d0 | 2019-10-31 11:40:30 +0100 | [diff] [blame] | 95 | } |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 96 | |
| 97 | // Start consensus |
| 98 | err = s.Consensus.Start() |
| 99 | if err != nil { |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 100 | return nil, err |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 103 | s.state = common.StateJoined |
Lorenz Brun | 52f7f29 | 2020-06-24 16:42:02 +0200 | [diff] [blame^] | 104 | go s.Containerd.Run()(context.TODO()) |
| 105 | s.Kubernetes.Start() |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 106 | |
| 107 | s.logger.Info("Joined cluster. Node is now syncing.") |
| 108 | |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 109 | return &api.JoinClusterResponse{}, nil |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 110 | } |