-
Notifications
You must be signed in to change notification settings - Fork 450
/
changes.go
238 lines (193 loc) · 7.14 KB
/
changes.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
235
236
237
238
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package operatingsystemconfig
import (
"errors"
"fmt"
"slices"
"github.com/spf13/afero"
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
nodeagentv1alpha1 "github.com/gardener/gardener/pkg/nodeagent/apis/config/v1alpha1"
)
var decoder runtime.Decoder
func init() {
scheme := runtime.NewScheme()
utilruntime.Must(extensionsv1alpha1.AddToScheme(scheme))
decoder = serializer.NewCodecFactory(scheme).UniversalDeserializer()
}
func extractOSCFromSecret(secret *corev1.Secret) (*extensionsv1alpha1.OperatingSystemConfig, []byte, string, error) {
oscRaw, ok := secret.Data[nodeagentv1alpha1.DataKeyOperatingSystemConfig]
if !ok {
return nil, nil, "", fmt.Errorf("no %s key found in OSC secret", nodeagentv1alpha1.DataKeyOperatingSystemConfig)
}
osc := &extensionsv1alpha1.OperatingSystemConfig{}
if err := runtime.DecodeInto(decoder, oscRaw, osc); err != nil {
return nil, nil, "", fmt.Errorf("unable to decode OSC from secret data key %s: %w", nodeagentv1alpha1.DataKeyOperatingSystemConfig, err)
}
return osc, oscRaw, secret.Annotations[nodeagentv1alpha1.AnnotationKeyChecksumDownloadedOperatingSystemConfig], nil
}
type operatingSystemConfigChanges struct {
units units
files files
}
type units struct {
changed []changedUnit
deleted []extensionsv1alpha1.Unit
}
type changedUnit struct {
extensionsv1alpha1.Unit
dropIns dropIns
}
type dropIns struct {
changed []extensionsv1alpha1.DropIn
deleted []extensionsv1alpha1.DropIn
}
type files struct {
changed []extensionsv1alpha1.File
deleted []extensionsv1alpha1.File
}
func computeOperatingSystemConfigChanges(fs afero.Afero, newOSC *extensionsv1alpha1.OperatingSystemConfig) (*operatingSystemConfigChanges, error) {
changes := &operatingSystemConfigChanges{}
// osc.files and osc.unit.files should be changed the same way by OSC controller.
// The reason for assigning files to units is the detection of changes which require the restart of a unit.
newOSCFiles := collectAllFiles(newOSC)
oldOSCRaw, err := fs.ReadFile(lastAppliedOperatingSystemConfigFilePath)
if err != nil {
if !errors.Is(err, afero.ErrFileNotFound) {
return nil, fmt.Errorf("error reading last applied OSC from file path %s: %w", lastAppliedOperatingSystemConfigFilePath, err)
}
var unitChanges []changedUnit
for _, unit := range mergeUnits(newOSC.Spec.Units, newOSC.Status.ExtensionUnits) {
unitChanges = append(unitChanges, changedUnit{
Unit: unit,
dropIns: dropIns{changed: unit.DropIns},
})
}
changes.files.changed = newOSCFiles
changes.units.changed = unitChanges
return changes, nil
}
oldOSC := &extensionsv1alpha1.OperatingSystemConfig{}
if err := runtime.DecodeInto(decoder, oldOSCRaw, oldOSC); err != nil {
return nil, fmt.Errorf("unable to decode the old OSC read from file path %s: %w", lastAppliedOperatingSystemConfigFilePath, err)
}
oldOSCFiles := collectAllFiles(oldOSC)
// File changes have to be computed in one step for all files,
// because moving a file from osc.unit.files to osc.files or vice versa should not result in a change and a delete event.
changes.files = computeFileDiffs(oldOSCFiles, newOSCFiles)
changes.units = computeUnitDiffs(
mergeUnits(oldOSC.Spec.Units, oldOSC.Status.ExtensionUnits),
mergeUnits(newOSC.Spec.Units, newOSC.Status.ExtensionUnits),
changes.files,
)
return changes, nil
}
func computeUnitDiffs(oldUnits, newUnits []extensionsv1alpha1.Unit, fileDiffs files) units {
var u units
var changedFiles = sets.New[string]()
// Only changed files are relevant here. Deleted files must be removed from `Unit.FilePaths` too which leads to a semantic difference.
for _, file := range fileDiffs.changed {
changedFiles.Insert(file.Path)
}
for _, oldUnit := range oldUnits {
if !slices.ContainsFunc(newUnits, func(newUnit extensionsv1alpha1.Unit) bool {
return oldUnit.Name == newUnit.Name
}) {
u.deleted = append(u.deleted, oldUnit)
}
}
for _, newUnit := range newUnits {
oldUnitIndex := slices.IndexFunc(oldUnits, func(oldUnit extensionsv1alpha1.Unit) bool {
return oldUnit.Name == newUnit.Name
})
var fileContentChanged bool
for _, filePath := range newUnit.FilePaths {
if changedFiles.Has(filePath) {
fileContentChanged = true
}
}
if oldUnitIndex == -1 {
u.changed = append(u.changed, changedUnit{
Unit: newUnit,
dropIns: dropIns{changed: newUnit.DropIns},
})
} else if !apiequality.Semantic.DeepEqual(oldUnits[oldUnitIndex], newUnit) || fileContentChanged {
var d dropIns
for _, oldDropIn := range oldUnits[oldUnitIndex].DropIns {
if !slices.ContainsFunc(newUnit.DropIns, func(newDropIn extensionsv1alpha1.DropIn) bool {
return oldDropIn.Name == newDropIn.Name
}) {
d.deleted = append(d.deleted, oldDropIn)
}
}
for _, newDropIn := range newUnit.DropIns {
oldDropInIndex := slices.IndexFunc(oldUnits[oldUnitIndex].DropIns, func(oldDropIn extensionsv1alpha1.DropIn) bool {
return oldDropIn.Name == newDropIn.Name
})
if oldDropInIndex == -1 || !apiequality.Semantic.DeepEqual(oldUnits[oldUnitIndex].DropIns[oldDropInIndex], newDropIn) {
d.changed = append(d.changed, newDropIn)
continue
}
}
u.changed = append(u.changed, changedUnit{
Unit: newUnit,
dropIns: d,
})
}
}
return u
}
func computeFileDiffs(oldFiles, newFiles []extensionsv1alpha1.File) files {
var f files
for _, oldFile := range oldFiles {
if !slices.ContainsFunc(newFiles, func(newFile extensionsv1alpha1.File) bool {
return oldFile.Path == newFile.Path
}) {
f.deleted = append(f.deleted, oldFile)
}
}
for _, newFile := range newFiles {
oldFileIndex := slices.IndexFunc(oldFiles, func(oldFile extensionsv1alpha1.File) bool {
return oldFile.Path == newFile.Path
})
if oldFileIndex == -1 || !apiequality.Semantic.DeepEqual(oldFiles[oldFileIndex], newFile) {
f.changed = append(f.changed, newFile)
continue
}
}
return f
}
func mergeUnits(specUnits, statusUnits []extensionsv1alpha1.Unit) []extensionsv1alpha1.Unit {
var out []extensionsv1alpha1.Unit
for _, unit := range append(specUnits, statusUnits...) {
unitIndex := slices.IndexFunc(out, func(existingUnit extensionsv1alpha1.Unit) bool {
return existingUnit.Name == unit.Name
})
if unitIndex == -1 {
out = append(out, unit)
continue
}
if unit.Enable != nil {
out[unitIndex].Enable = unit.Enable
}
if unit.Command != nil {
out[unitIndex].Command = unit.Command
}
if unit.Content != nil {
out[unitIndex].Content = unit.Content
}
out[unitIndex].DropIns = append(out[unitIndex].DropIns, unit.DropIns...)
out[unitIndex].FilePaths = append(out[unitIndex].FilePaths, unit.FilePaths...)
}
return out
}
func collectAllFiles(osc *extensionsv1alpha1.OperatingSystemConfig) []extensionsv1alpha1.File {
return append(osc.Spec.Files, osc.Status.ExtensionFiles...)
}