-
Notifications
You must be signed in to change notification settings - Fork 456
/
v1alpha1_runtime.go
234 lines (192 loc) · 7.82 KB
/
v1alpha1_runtime.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package v1alpha1
import (
"context"
"errors"
"fmt"
"log"
"reflect"
"sync"
"sync/atomic"
"time"
"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/safe"
"github.com/google/go-cmp/cmp"
"github.com/siderolabs/talos/internal/app/machined/pkg/runtime"
"github.com/siderolabs/talos/internal/app/machined/pkg/system"
"github.com/siderolabs/talos/internal/app/machined/pkg/system/services"
"github.com/siderolabs/talos/pkg/machinery/config"
"github.com/siderolabs/talos/pkg/machinery/config/types/v1alpha1"
"github.com/siderolabs/talos/pkg/machinery/resources/hardware"
"github.com/siderolabs/talos/pkg/machinery/resources/k8s"
)
// Runtime implements the Runtime interface.
type Runtime struct {
c atomicInterface[config.Provider]
s runtime.State
e runtime.EventStream
l runtime.LoggingManager
rollbackTimerMu sync.Mutex
rollbackTimer *time.Timer
}
// NewRuntime initializes and returns the v1alpha1 runtime.
func NewRuntime(s runtime.State, e runtime.EventStream, l runtime.LoggingManager) *Runtime {
return &Runtime{
s: s,
e: e,
l: l,
}
}
// Config implements the Runtime interface.
func (r *Runtime) Config() config.Config {
return r.c.Load()
}
// ConfigContainer implements the Runtime interface.
func (r *Runtime) ConfigContainer() config.Container {
return r.c.Load()
}
// RollbackToConfigAfter implements the Runtime interface.
func (r *Runtime) RollbackToConfigAfter(timeout time.Duration) error {
cfgProvider := r.c.Load()
r.CancelConfigRollbackTimeout()
r.rollbackTimer = time.AfterFunc(timeout, func() {
log.Println("rolling back the configuration")
if err := r.SetConfig(cfgProvider); err != nil {
log.Printf("config rollback failed %s", err)
}
})
return nil
}
// CancelConfigRollbackTimeout implements the Runtime interface.
func (r *Runtime) CancelConfigRollbackTimeout() {
r.rollbackTimerMu.Lock()
defer r.rollbackTimerMu.Unlock()
if r.rollbackTimer != nil {
r.rollbackTimer.Stop()
r.rollbackTimer = nil
}
}
// SetConfig implements the Runtime interface.
func (r *Runtime) SetConfig(cfg config.Provider) error {
r.c.Store(cfg)
return r.s.V1Alpha2().SetConfig(cfg)
}
// CanApplyImmediate implements the Runtime interface.
func (r *Runtime) CanApplyImmediate(cfg config.Provider) error {
cfgProv := r.c.Load()
if cfgProv == nil {
return errors.New("no current config")
}
currentConfig := cfgProv.RawV1Alpha1()
if currentConfig == nil {
return errors.New("current config is not v1alpha1")
}
newConfig := cfg.RawV1Alpha1()
if newConfig == nil {
return errors.New("new config is not v1alpha1")
}
// copy the config as we're going to modify it
newConfig = newConfig.DeepCopy()
// the config changes allowed to be applied immediately are:
// * .debug
// * .cluster
// * .machine.ca
// * .machine.acceptedCAs
// * .machine.time
// * .machine.certCANs
// * .machine.install
// * .machine.network
// * .machine.sysfs
// * .machine.sysctls
// * .machine.logging
// * .machine.controlplane
// * .machine.kubelet
// * .machine.kernel
// * .machine.registries (note that auth is not applied immediately, containerd limitation)
// * .machine.pods
// * .machine.seccompProfiles
// * .machine.nodeLabels
// * .machine.nodeTaints
// * .machine.features.kubernetesTalosAPIAccess
// * .machine.features.kubePrism
// * .machine.features.localDNS
newConfig.ConfigDebug = currentConfig.ConfigDebug
newConfig.ClusterConfig = currentConfig.ClusterConfig
if newConfig.MachineConfig != nil && currentConfig.MachineConfig != nil {
newConfig.MachineConfig.MachineCA = currentConfig.MachineConfig.MachineCA
newConfig.MachineConfig.MachineAcceptedCAs = currentConfig.MachineConfig.MachineAcceptedCAs
newConfig.MachineConfig.MachineTime = currentConfig.MachineConfig.MachineTime
newConfig.MachineConfig.MachineCertSANs = currentConfig.MachineConfig.MachineCertSANs
newConfig.MachineConfig.MachineInstall = currentConfig.MachineConfig.MachineInstall
newConfig.MachineConfig.MachineNetwork = currentConfig.MachineConfig.MachineNetwork
newConfig.MachineConfig.MachineSysfs = currentConfig.MachineConfig.MachineSysfs
newConfig.MachineConfig.MachineSysctls = currentConfig.MachineConfig.MachineSysctls
newConfig.MachineConfig.MachineLogging = currentConfig.MachineConfig.MachineLogging
newConfig.MachineConfig.MachineControlPlane = currentConfig.MachineConfig.MachineControlPlane
newConfig.MachineConfig.MachineKubelet = currentConfig.MachineConfig.MachineKubelet
newConfig.MachineConfig.MachineKernel = currentConfig.MachineConfig.MachineKernel
newConfig.MachineConfig.MachineRegistries = currentConfig.MachineConfig.MachineRegistries
newConfig.MachineConfig.MachinePods = currentConfig.MachineConfig.MachinePods
newConfig.MachineConfig.MachineSeccompProfiles = currentConfig.MachineConfig.MachineSeccompProfiles
newConfig.MachineConfig.MachineNodeLabels = currentConfig.MachineConfig.MachineNodeLabels
newConfig.MachineConfig.MachineNodeTaints = currentConfig.MachineConfig.MachineNodeTaints
if newConfig.MachineConfig.MachineFeatures != nil && currentConfig.MachineConfig.MachineFeatures != nil {
newConfig.MachineConfig.MachineFeatures.KubernetesTalosAPIAccessConfig = currentConfig.MachineConfig.MachineFeatures.KubernetesTalosAPIAccessConfig
newConfig.MachineConfig.MachineFeatures.KubePrismSupport = currentConfig.MachineConfig.MachineFeatures.KubePrismSupport
newConfig.MachineConfig.MachineFeatures.HostDNSSupport = currentConfig.MachineConfig.MachineFeatures.HostDNSSupport
}
}
if !reflect.DeepEqual(currentConfig, newConfig) {
diff := cmp.Diff(currentConfig, newConfig, cmp.AllowUnexported(v1alpha1.InstallDiskSizeMatcher{}))
return fmt.Errorf("this config change can't be applied in immediate mode\ndiff: %s", diff)
}
return nil
}
// State implements the Runtime interface.
func (r *Runtime) State() runtime.State {
return r.s
}
// Events implements the Runtime interface.
func (r *Runtime) Events() runtime.EventStream {
return r.e
}
// Logging implements the Runtime interface.
func (r *Runtime) Logging() runtime.LoggingManager {
return r.l
}
// NodeName implements the Runtime interface.
func (r *Runtime) NodeName() (string, error) {
nodenameResource, err := r.s.V1Alpha2().Resources().Get(context.Background(), resource.NewMetadata(k8s.NamespaceName, k8s.NodenameType, k8s.NodenameID, resource.VersionUndefined))
if err != nil {
return "", fmt.Errorf("error getting nodename resource: %w", err)
}
return nodenameResource.(*k8s.Nodename).TypedSpec().Nodename, nil
}
// IsBootstrapAllowed checks for CRI to be up, checked in the bootstrap method.
func (r *Runtime) IsBootstrapAllowed() bool {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
svc := &services.CRI{}
if err := system.WaitForService(system.StateEventUp, svc.ID(r)).Wait(ctx); err != nil {
return false
}
return true
}
// GetSystemInformation returns system information resource if it exists.
func (r *Runtime) GetSystemInformation(ctx context.Context) (*hardware.SystemInformation, error) {
return safe.StateGet[*hardware.SystemInformation](ctx, r.State().V1Alpha2().Resources(), hardware.NewSystemInformation(hardware.SystemInformationID).Metadata())
}
// atomicInterface is a typed wrapper around atomic.Value. It's only useful for storing the interfaces, because
// you don't need another layer of indirection (unlike atomic.Pointer[T]) to load the value. For concrete types
// please use atomic.Pointer.
type atomicInterface[T any] struct{ v atomic.Value }
func (a *atomicInterface[T]) Load() T {
if val := a.v.Load(); val != nil {
return val.(T) //nolint:forcetypeassert
}
var zero T
return zero
}
func (a *atomicInterface[T]) Store(v T) { a.v.Store(v) }