-
Notifications
You must be signed in to change notification settings - Fork 13
/
bootstrap.go
143 lines (127 loc) · 4.99 KB
/
bootstrap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2023 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bootstrap
import (
"context"
"errors"
"os"
"strings"
"time"
"github.com/gardener/etcd-wrapper/internal/types"
"github.com/gardener/etcd-wrapper/internal/brclient"
"github.com/gardener/etcd-wrapper/internal/util"
"go.etcd.io/etcd/embed"
"go.uber.org/zap"
)
const (
defaultBackupRestoreMaxRetries = 5
defaultBackOffBetweenRetries = 1 * time.Second
)
// EtcdInitializer is an interface for methods to be used to initialize etcd
type EtcdInitializer interface {
Run(context.Context) (*embed.Config, error)
}
type initializer struct {
brClient brclient.BackupRestoreClient
logger *zap.Logger
}
// NewEtcdInitializer creates and returns an EtcdInitializer object
func NewEtcdInitializer(brConfig *types.BackupRestoreConfig, logger *zap.Logger) (EtcdInitializer, error) {
// Validate backup-restore configuration
if err := brConfig.Validate(); err != nil {
return nil, err
}
//create backup-restore client
brClient, err := brclient.NewDefaultClient(*brConfig)
if err != nil {
return nil, err
}
return &initializer{
brClient: brClient,
logger: logger,
}, nil
}
// Run initializes the etcd and gets the etcd configuration
func (i *initializer) Run(ctx context.Context) (*embed.Config, error) {
var (
err error
initStatus brclient.InitStatus
)
for initStatus != brclient.Successful {
if initStatus, err = i.brClient.GetInitializationStatus(ctx); err != nil {
i.logger.Error("error while fetching initialization status", zap.Error(err))
}
i.logger.Info("Fetched initialization status", zap.String("Status", initStatus.String()))
if initStatus == brclient.New {
validationMode := determineValidationMode(types.DefaultExitCodeFilePath, i.logger)
i.logger.Info("Fetched initialization status is `New`. Triggering etcd initialization with validation mode", zap.Any("mode", validationMode))
if err = i.brClient.TriggerInitialization(ctx, validationMode); err != nil {
i.logger.Error("error while triggering initialization to backup-restore", zap.Error(err))
}
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(defaultBackOffBetweenRetries):
}
}
i.logger.Info("Etcd initialization succeeded")
return i.tryGetEtcdConfig(ctx, defaultBackupRestoreMaxRetries, defaultBackOffBetweenRetries)
}
// CaptureExitCode captures the exit signal into a file `exit_code`
func CaptureExitCode(signal os.Signal, exitCodeFilePath string) error {
if signal == nil {
return nil
}
interruptSignal := []byte(signal.String())
return os.WriteFile(exitCodeFilePath, interruptSignal, 0644)
}
// CleanupExitCode removes the `exit_code` file
func CleanupExitCode(exitCodeFilePath string) error {
err := os.Remove(exitCodeFilePath)
if errors.Is(err, os.ErrNotExist) {
return nil
}
return err
}
func (i *initializer) tryGetEtcdConfig(ctx context.Context, maxRetries int, interval time.Duration) (*embed.Config, error) {
// Get etcd config only
opResult := util.Retry[string](ctx, i.logger, "GetEtcdConfig", func() (string, error) {
return i.brClient.GetEtcdConfig(ctx)
}, maxRetries, interval, util.AlwaysRetry)
if opResult.IsErr() {
return nil, opResult.Err
}
etcdConfigFilePath := opResult.Value
i.logger.Info("Fetched and written etcd configuration", zap.String("path", etcdConfigFilePath))
return embed.ConfigFromFile(etcdConfigFilePath)
}
func determineValidationMode(exitCodeFilePath string, logger *zap.Logger) brclient.ValidationType {
var err error
if _, err = os.Stat(exitCodeFilePath); err == nil {
data, err := os.ReadFile(exitCodeFilePath)
if err != nil {
logger.Error("error in reading exitCodeFile, assuming full-validation to be done.", zap.String("exitCodeFilePath", exitCodeFilePath), zap.Error(err))
return brclient.FullValidation
}
validationMarker := strings.TrimSpace(string(data))
if validationMarker == "terminated" || validationMarker == "interrupt" {
logger.Info("last captured exit code read, assuming sanity validation to be done.", zap.String("exitCodeFilePath", exitCodeFilePath), zap.String("signal-captured", validationMarker))
return brclient.SanityValidation
}
}
logger.Error("error in checking if exitCodeFile exists, assuming full-validation to be done.", zap.String("exitCodeFilePath", exitCodeFilePath), zap.Error(err))
// Full validation if error
return brclient.FullValidation
}