forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchanges_test.go
More file actions
213 lines (186 loc) · 10.6 KB
/
changes_test.go
File metadata and controls
213 lines (186 loc) · 10.6 KB
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
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package operatingsystemconfig_test
import (
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
gomegatypes "github.com/onsi/gomega/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubeletconfigv1beta1 "k8s.io/kubelet/config/v1beta1"
"k8s.io/utils/ptr"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
. "github.com/gardener/gardener/pkg/nodeagent/controller/operatingsystemconfig"
)
var _ = Describe("Changes", func() {
DescribeTable("#ComputeKubeletConfigChange", func(oldKubeletConfig, newKubeletConfig *kubeletconfigv1beta1.KubeletConfiguration, expectedCPUManagerPolicyChange, expectedConfigChange bool, errMatcher gomegatypes.GomegaMatcher) {
configChange, cpuManagerPolicyChange, err := ComputeKubeletConfigChange(oldKubeletConfig, newKubeletConfig)
Expect(err).To(errMatcher)
Expect(cpuManagerPolicyChange).To(Equal(expectedCPUManagerPolicyChange))
Expect(configChange).To(Equal(expectedConfigChange))
},
Entry("no change", &kubeletconfigv1beta1.KubeletConfiguration{}, &kubeletconfigv1beta1.KubeletConfiguration{}, false, false, BeNil()),
Entry("changed cpu manager policy", &kubeletconfigv1beta1.KubeletConfiguration{CPUManagerPolicy: "static"}, &kubeletconfigv1beta1.KubeletConfiguration{CPUManagerPolicy: "none"}, true, false, BeNil()),
Entry("invalid kubeReserved CPU", &kubeletconfigv1beta1.KubeletConfiguration{CPUManagerPolicy: "static", KubeReserved: map[string]string{"cpu": "aoeu"}, SystemReserved: map[string]string{"cpu": "100m"}}, &kubeletconfigv1beta1.KubeletConfiguration{CPUManagerPolicy: "none", KubeReserved: map[string]string{"cpu": "100m"}}, true, false, MatchError(ContainSubstring("failed to parse"))),
Entry("changed kubeReserved CPU", &kubeletconfigv1beta1.KubeletConfiguration{CPUManagerPolicy: "static", KubeReserved: map[string]string{"cpu": "100m"}}, &kubeletconfigv1beta1.KubeletConfiguration{CPUManagerPolicy: "none", KubeReserved: map[string]string{"cpu": "200m"}}, true, true, BeNil()),
Entry("changed kubeReserved memory", &kubeletconfigv1beta1.KubeletConfiguration{KubeReserved: map[string]string{"memory": "100Mi"}}, &kubeletconfigv1beta1.KubeletConfiguration{KubeReserved: map[string]string{"memory": "200Mi"}}, false, true, BeNil()),
Entry("changed kubeReserved ephemeral-storage", &kubeletconfigv1beta1.KubeletConfiguration{KubeReserved: map[string]string{"ephemeral-storage": "100Gi"}}, &kubeletconfigv1beta1.KubeletConfiguration{KubeReserved: map[string]string{"ephemeral-storage": "200Gi"}}, false, true, BeNil()),
Entry("changed kubeReserved PID", &kubeletconfigv1beta1.KubeletConfiguration{KubeReserved: map[string]string{"pid": "10k"}}, &kubeletconfigv1beta1.KubeletConfiguration{KubeReserved: map[string]string{"pid": "20k"}}, false, true, BeNil()),
Entry("changed evictionHard memory.available", &kubeletconfigv1beta1.KubeletConfiguration{CPUManagerPolicy: "static", EvictionHard: map[string]string{"memory.available": "100Mi"}}, &kubeletconfigv1beta1.KubeletConfiguration{CPUManagerPolicy: "none", EvictionHard: map[string]string{"memory.available": "200Mi"}}, true, true, BeNil()),
Entry("changed evictionHard imagefs.available", &kubeletconfigv1beta1.KubeletConfiguration{EvictionHard: map[string]string{"imagefs.available": "100Mi"}}, &kubeletconfigv1beta1.KubeletConfiguration{EvictionHard: map[string]string{"imagefs.available": "200Mi"}}, false, true, BeNil()),
Entry("changed evictionHard imagefs.inodesFree", &kubeletconfigv1beta1.KubeletConfiguration{EvictionHard: map[string]string{"imagefs.inodesFree": "1k"}}, &kubeletconfigv1beta1.KubeletConfiguration{EvictionHard: map[string]string{"imagefs.inodesFree": "2k"}}, false, true, BeNil()),
Entry("changed evictionHard nodefs.available", &kubeletconfigv1beta1.KubeletConfiguration{EvictionHard: map[string]string{"nodefs.available": "100Mi"}}, &kubeletconfigv1beta1.KubeletConfiguration{EvictionHard: map[string]string{"nodefs.available": "200Mi"}}, false, true, BeNil()),
Entry("changed evictionHard nodefs.inodesFree", &kubeletconfigv1beta1.KubeletConfiguration{EvictionHard: map[string]string{"nodefs.inodesFree": "1k"}}, &kubeletconfigv1beta1.KubeletConfiguration{EvictionHard: map[string]string{"nodefs.inodesFree": "2k"}}, false, true, BeNil()),
Entry("some other field changed in evictionHard", &kubeletconfigv1beta1.KubeletConfiguration{EvictionHard: map[string]string{"foo": "bar"}}, &kubeletconfigv1beta1.KubeletConfiguration{EvictionHard: map[string]string{"foo": "baz"}}, false, false, BeNil()),
)
Describe("#IsOsVersionUpToDate", func() {
var (
currentOSVersion *string
newOSC *extensionsv1alpha1.OperatingSystemConfig
)
BeforeEach(func() {
currentOSVersion = nil
newOSC = &extensionsv1alpha1.OperatingSystemConfig{
Spec: extensionsv1alpha1.OperatingSystemConfigSpec{
InPlaceUpdates: &extensionsv1alpha1.InPlaceUpdates{
OperatingSystemVersion: "1.2.0",
},
},
}
})
It("should return false if current OS version is nil", func() {
changed, err := IsOsVersionUpToDate(currentOSVersion, newOSC)
Expect(err).To(MatchError(ContainSubstring("current OS version is nil")))
Expect(changed).To(BeFalse())
})
It("should return true if the OS version is up to date", func() {
currentOSVersion = ptr.To("1.2")
changed, err := IsOsVersionUpToDate(currentOSVersion, newOSC)
Expect(err).NotTo(HaveOccurred())
Expect(changed).To(BeTrue())
currentOSVersion = ptr.To("1.2.0")
changed, err = IsOsVersionUpToDate(currentOSVersion, newOSC)
Expect(err).NotTo(HaveOccurred())
Expect(changed).To(BeTrue())
currentOSVersion = ptr.To("1.2.0-foo.12")
newOSC.Spec.InPlaceUpdates.OperatingSystemVersion = "1.2.0"
changed, err = IsOsVersionUpToDate(currentOSVersion, newOSC)
Expect(err).NotTo(HaveOccurred())
Expect(changed).To(BeTrue())
})
It("should return false if the OS version is not up to date", func() {
currentOSVersion = ptr.To("1.1.0")
changed, err := IsOsVersionUpToDate(currentOSVersion, newOSC)
Expect(err).NotTo(HaveOccurred())
Expect(changed).To(BeFalse())
currentOSVersion = ptr.To("1.2.0")
newOSC.Spec.InPlaceUpdates.OperatingSystemVersion = "1.2.1"
changed, err = IsOsVersionUpToDate(currentOSVersion, newOSC)
Expect(err).NotTo(HaveOccurred())
Expect(changed).To(BeFalse())
})
It("should return an error if the OS version in the new OSC is invalid", func() {
newOSC.Spec.InPlaceUpdates.OperatingSystemVersion = "invalid"
currentOSVersion = ptr.To("1.2.0")
changed, err := IsOsVersionUpToDate(currentOSVersion, newOSC)
Expect(err).To(MatchError(ContainSubstring("failed comparing current OS version")))
Expect(changed).To(BeFalse())
})
})
Describe("ComputeCredentialsRotationChanges", func() {
var (
oldOSC, newOSC *extensionsv1alpha1.OperatingSystemConfig
timeNow = time.Now().UTC()
)
BeforeEach(func() {
oldOSC = &extensionsv1alpha1.OperatingSystemConfig{
Spec: extensionsv1alpha1.OperatingSystemConfigSpec{
InPlaceUpdates: &extensionsv1alpha1.InPlaceUpdates{
CredentialsRotation: &extensionsv1alpha1.CredentialsRotation{
CertificateAuthorities: &extensionsv1alpha1.CARotation{
LastInitiationTime: &metav1.Time{Time: timeNow.Add(-time.Hour)},
},
ServiceAccountKey: &extensionsv1alpha1.ServiceAccountKeyRotation{
LastInitiationTime: &metav1.Time{Time: timeNow.Add(-time.Hour)},
},
},
},
},
}
newOSC = oldOSC.DeepCopy()
newOSC.Spec.InPlaceUpdates.CredentialsRotation.CertificateAuthorities.LastInitiationTime = &metav1.Time{Time: timeNow}
newOSC.Spec.InPlaceUpdates.CredentialsRotation.ServiceAccountKey.LastInitiationTime = &metav1.Time{Time: timeNow}
})
It("should return false if CredentialsRotation is nil in the new OSC", func() {
oldOSC.Spec.InPlaceUpdates.CredentialsRotation = nil
newOSC.Spec.InPlaceUpdates.CredentialsRotation = nil
caRotation, saKeyRotation := ComputeCredentialsRotationChanges(oldOSC, newOSC)
Expect(caRotation).To(BeFalse())
Expect(saKeyRotation).To(BeFalse())
})
It("should return true if the CredentialsRotation is nil in the old OSC", func() {
oldOSC.Spec.InPlaceUpdates.CredentialsRotation = nil
caRotation, saKeyRotation := ComputeCredentialsRotationChanges(oldOSC, newOSC)
Expect(caRotation).To(BeTrue())
Expect(saKeyRotation).To(BeTrue())
})
It("should return true if the lastInitiationTimes of rotations are changed", func() {
caRotation, saKeyRotation := ComputeCredentialsRotationChanges(oldOSC, newOSC)
Expect(caRotation).To(BeTrue())
Expect(saKeyRotation).To(BeTrue())
})
It("should return false if the lastInitiationTimes of rotations are not changed", func() {
oldOSC = newOSC.DeepCopy()
caRotation, saKeyRotation := ComputeCredentialsRotationChanges(oldOSC, newOSC)
Expect(caRotation).To(BeFalse())
Expect(saKeyRotation).To(BeFalse())
})
})
Describe("CollectAllFiles", func() {
It("should return all files when NodeName is empty", func() {
osc := &extensionsv1alpha1.OperatingSystemConfig{
Spec: extensionsv1alpha1.OperatingSystemConfigSpec{
Files: []extensionsv1alpha1.File{
{Path: "/etc/foo", HostName: nil},
{Path: "/etc/bar", HostName: nil},
},
},
Status: extensionsv1alpha1.OperatingSystemConfigStatus{
ExtensionFiles: []extensionsv1alpha1.File{
{Path: "/etc/baz", HostName: nil},
},
},
}
Expect(CollectAllFiles(osc, "node-1")).To(ConsistOf(
extensionsv1alpha1.File{Path: "/etc/foo", HostName: nil},
extensionsv1alpha1.File{Path: "/etc/bar", HostName: nil},
extensionsv1alpha1.File{Path: "/etc/baz", HostName: nil},
))
})
It("should filter files by NodeName", func() {
osc := &extensionsv1alpha1.OperatingSystemConfig{
Spec: extensionsv1alpha1.OperatingSystemConfigSpec{
Files: []extensionsv1alpha1.File{
{Path: "/etc/foo", HostName: ptr.To("node-1")},
{Path: "/etc/bar", HostName: ptr.To("node-2")},
{Path: "/etc/all", HostName: nil},
},
},
Status: extensionsv1alpha1.OperatingSystemConfigStatus{
ExtensionFiles: []extensionsv1alpha1.File{
{Path: "/etc/baz", HostName: ptr.To("node-1")},
{Path: "/etc/qux", HostName: ptr.To("node-3")},
{Path: "/etc/sts", HostName: nil},
},
},
}
Expect(CollectAllFiles(osc, "node-1")).To(ConsistOf(
extensionsv1alpha1.File{Path: "/etc/foo", HostName: ptr.To("node-1")},
extensionsv1alpha1.File{Path: "/etc/baz", HostName: ptr.To("node-1")},
extensionsv1alpha1.File{Path: "/etc/all", HostName: nil},
extensionsv1alpha1.File{Path: "/etc/sts", HostName: nil},
))
})
})
})