-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathtypes.go
More file actions
449 lines (382 loc) · 15.3 KB
/
types.go
File metadata and controls
449 lines (382 loc) · 15.3 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package e2e
import (
"bytes"
"context"
"fmt"
"os/exec"
"os/user"
"reflect"
"strconv"
"strings"
"testing"
"time"
aksnodeconfigv1 "github.com/Azure/agentbaker/aks-node-controller/pkg/gen/aksnodeconfig/v1"
"github.com/Azure/agentbaker/e2e/config"
"github.com/Azure/agentbaker/pkg/agent/datamodel"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v7"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/ssh"
)
type Tags struct {
Name string
ImageName string
OS string
Arch string
NetworkIsolated bool
NonAnonymousACR bool
GPU bool
WASM bool
BootstrapTokenFallback bool
KubeletCustomConfig bool
Scriptless bool
VHDCaching bool
MockAzureChinaCloud bool
VMSeriesCoverageTest bool
}
// MatchesFilters checks if the Tags struct matches all given filters.
// Filters are comma-separated "key=value" pairs (e.g., "gpu=true,os=x64").
// Returns true if all filters match, false otherwise. Errors on invalid input.
func (t Tags) MatchesFilters(filters string) (bool, error) {
return t.matchFilters(filters, true)
}
// MatchesAnyFilter checks if the Tags struct matches at least one of the given filters.
// Filters are comma-separated "key=value" pairs (e.g., "gpu=true,os=x64").
// Returns true if any filter matches, false if none match. Errors on invalid input.
func (t Tags) MatchesAnyFilter(filters string) (bool, error) {
return t.matchFilters(filters, false)
}
// matchFilters is a helper function used by both MatchesFilters and MatchesAnyFilter.
// The 'all' parameter determines whether all filters must match (true) or just any filter (false).
func (t Tags) matchFilters(filters string, all bool) (bool, error) {
if filters == "" {
return true, nil
}
v := reflect.ValueOf(t)
filterPairs := strings.Split(filters, ",")
for _, pair := range filterPairs {
kv := strings.SplitN(pair, "=", 2)
if len(kv) != 2 {
return false, fmt.Errorf("invalid filter format: %s", pair)
}
key := strings.TrimSpace(kv[0])
value := strings.TrimSpace(kv[1])
// Case-insensitive field lookup
field := reflect.Value{}
for i := 0; i < v.NumField(); i++ {
if strings.EqualFold(v.Type().Field(i).Name, key) {
field = v.Field(i)
break
}
}
if !field.IsValid() {
return false, fmt.Errorf("unknown filter key: %s", key)
}
var match bool
switch field.Kind() {
case reflect.String:
match = strings.EqualFold(field.String(), value)
case reflect.Bool:
boolValue, err := strconv.ParseBool(value)
if err != nil {
return false, fmt.Errorf("invalid boolean value for %s: %s", key, value)
}
match = field.Bool() == boolValue
default:
return false, fmt.Errorf("unsupported field type for %s", key)
}
if all && !match {
return false, nil
}
if !all && match {
return true, nil
}
}
return all, nil
}
// Scenario represents an AgentBaker E2E scenario.
type Scenario struct {
// Description is a short description of what the scenario does and tests for
Description string
// Tags are used for filtering scenarios to run based on the tags provided
Tags Tags
// Config contains the configuration of the scenario
Config
// Location is the Azure location where the scenario will run. This can be
// used to override the default location.
Location string
// K8sSystemPoolSKU is the VM size to use for the system nodepool. If empty,
// a default size will be used.
K8sSystemPoolSKU string
// Runtime contains the runtime state of the scenario. It's populated in the beginning of the test run
Runtime *ScenarioRuntime
T testing.TB
}
type ScenarioRuntime struct {
NBC *datamodel.NodeBootstrappingConfiguration
AKSNodeConfig *aksnodeconfigv1.Configuration
Cluster *Cluster
VM *ScenarioVM
VMSSName string
EnableScriptlessNBCCSECmd bool
CSETimingReport *CSETimingReport // eagerly extracted before GA can sweep events
}
type ScenarioVM struct {
KubeName string
VMSS *armcompute.VirtualMachineScaleSet
VM *armcompute.VirtualMachineScaleSetVM
PrivateIP string
SSHClient *ssh.Client
}
// CustomDataWriteFile defines an e2e-only cloud-init write_files entry.
type CustomDataWriteFile struct {
Path string
Permissions string
Owner string
Content string
}
// Config represents the configuration of an AgentBaker E2E scenario.
type Config struct {
// Cluster creates, updates or re-uses an AKS cluster for the scenario
Cluster func(ctx context.Context, request ClusterRequest) (*Cluster, error)
// VHD is the node image used by the scenario.
VHD *config.Image
// BootstrapConfigMutator is a function which mutates the base NodeBootstrappingConfig according to the scenario's requirements
BootstrapConfigMutator func(*Cluster, *datamodel.NodeBootstrappingConfiguration)
// AKSNodeConfigMutator if defined then aks-node-controller will be used to provision nodes
AKSNodeConfigMutator func(*Cluster, *aksnodeconfigv1.Configuration)
// VMConfigMutator is a function which mutates the base VMSS model according to the scenario's requirements
VMConfigMutator func(*armcompute.VirtualMachineScaleSet)
// CustomDataWriteFiles injects additional cloud-init write_files entries into rendered customData.
// This is for e2e-only validation scenarios.
CustomDataWriteFiles []CustomDataWriteFile
// Validator is a function where the scenario can perform any extra validation checks
Validator func(ctx context.Context, s *Scenario)
// SkipDefaultValidation is a flag to indicate whether the common validation (like spawning a pod) should be skipped.
// It shouldn't be used for majority of scenarios, currently only used for preparing VHD in a two-stage scenario
SkipDefaultValidation bool
// SkipSSHConnectivityValidation is a flag to indicate whether the ssh connectivity validation should be skipped.
// It shouldn't be used for majority of scenarios, currently only used for scenarios where the node is not expected to be reachable via ssh
SkipSSHConnectivityValidation bool
// WaitForSSHAfterReboot if set to non-zero duration, SSH connectivity validation will retry with exponential backoff
// for up to this duration when encountering reboot-related errors. This is useful for scenarios where the node
// reboots during provisioning (e.g., MIG-enabled GPU nodes). Default (zero value) means no retry.
WaitForSSHAfterReboot time.Duration
// if VHDCaching is set then a VHD will be created first for the test scenario and then a VM will be created from that VHD.
// The main purpose is to validate VHD Caching logic and ensure a reboot step between basePrep and nodePrep doesn't break anything.
VHDCaching bool
// ExpectedError, when set, indicates that VMSS creation is expected to fail with an error containing this substring.
// The assertion is performed inside the scenario's subtest.
ExpectedError string
// UseNVMe indicates whether to use NVMe-based disk placement/controller. This is required for certain VM sizes (e.g., v6 and v7 series) which only support NVMe disk controllers.
UseNVMe bool
// SkipScriptlessNBC when true prevents the automatic scriptless_nbc sub-test from being generated.
// Use this for scenarios that depend on CSE script execution (e.g., CSE timing validation)
// which is not available in scriptless mode.
SkipScriptlessNBC bool
// EagerCSETimingExtraction when true causes CSE timing events to be extracted
// immediately after SSH is established, before other validators run.
// This prevents the Guest Agent from sweeping events before they can be read.
// Only set this on CSE performance test scenarios.
EagerCSETimingExtraction bool
}
func (s *Scenario) PrepareAKSNodeConfig() {
}
// PrepareVMSSModel mutates the input VirtualMachineScaleSet based on the scenario's VMConfigMutator, if configured.
// This method will also use the scenario's configured VHD selector to modify the input VMSS to reference the correct VHD resource.
func (s *Scenario) PrepareVMSSModel(ctx context.Context, t testing.TB, vmss *armcompute.VirtualMachineScaleSet) {
resourceID, err := CachedPrepareVHD(ctx, GetVHDRequest{
Image: *s.VHD,
Location: s.Location,
})
require.NoError(t, err)
require.NotEmpty(t, resourceID, "VHDSelector.ResourceID")
require.NotNil(t, vmss, "input VirtualMachineScaleSet")
require.NotNil(t, vmss.Properties, "input VirtualMachineScaleSet.Properties")
if s.VMConfigMutator != nil {
s.VMConfigMutator(vmss)
}
if vmss.Properties.VirtualMachineProfile == nil {
vmss.Properties.VirtualMachineProfile = &armcompute.VirtualMachineScaleSetVMProfile{}
}
if vmss.Properties.VirtualMachineProfile.StorageProfile == nil {
vmss.Properties.VirtualMachineProfile.StorageProfile = &armcompute.VirtualMachineScaleSetStorageProfile{}
}
vmss.Properties.VirtualMachineProfile.StorageProfile.ImageReference = &armcompute.ImageReference{
ID: to.Ptr(string(resourceID)),
}
// Override OS disk size if the VHD requires a non-default size.
if s.VHD.OSDiskSizeGB > 0 {
osDisk := vmss.Properties.VirtualMachineProfile.StorageProfile.OSDisk
if osDisk != nil {
osDisk.DiskSizeGB = to.Ptr(s.VHD.OSDiskSizeGB)
}
}
s.updateTags(ctx, vmss)
}
func (s *Scenario) SecureTLSBootstrappingEnabled() bool {
if s.Runtime == nil {
return false
}
if nbc := s.Runtime.NBC; nbc != nil && nbc.SecureTLSBootstrappingConfig.GetEnabled() {
return true
}
if nodeConfig := s.Runtime.AKSNodeConfig; nodeConfig != nil && nodeConfig.BootstrappingConfig.GetBootstrappingAuthMethod() ==
aksnodeconfigv1.BootstrappingAuthMethod_BOOTSTRAPPING_AUTH_METHOD_SECURE_TLS_BOOTSTRAPPING {
return true
}
return false
}
func (s *Scenario) KubeletConfigFileEnabled() bool {
if s.Runtime == nil {
return false
}
if nbc := s.Runtime.NBC; nbc != nil && (nbc.EnableKubeletConfigFile ||
(nbc.AgentPoolProfile != nil && (nbc.AgentPoolProfile.CustomKubeletConfig != nil || nbc.AgentPoolProfile.CustomLinuxOSConfig != nil))) {
return true
}
if nodeConfig := s.Runtime.AKSNodeConfig; nodeConfig != nil && nodeConfig.KubeletConfig != nil && nodeConfig.KubeletConfig.EnableKubeletConfigFile {
return true
}
return false
}
func (s *Scenario) HasServicePrincipalData() bool {
if s.Runtime == nil {
return false
}
if nbc := s.Runtime.NBC; nbc != nil && nbc.ContainerService != nil && nbc.ContainerService.Properties != nil && nbc.ContainerService.Properties.ServicePrincipalProfile != nil {
return nbc.ContainerService.Properties.ServicePrincipalProfile.ClientID != "" && nbc.ContainerService.Properties.ServicePrincipalProfile.Secret != ""
}
if nodeConfig := s.Runtime.AKSNodeConfig; nodeConfig != nil && nodeConfig.AuthConfig != nil {
return nodeConfig.AuthConfig.ServicePrincipalId != "" && nodeConfig.AuthConfig.ServicePrincipalSecret != ""
}
return false
}
func (s *Scenario) GetK8sVersion() string {
if s.Runtime == nil {
return ""
}
if nbc := s.Runtime.NBC; nbc != nil && nbc.ContainerService != nil && nbc.ContainerService.Properties != nil && nbc.ContainerService.Properties.OrchestratorProfile != nil {
return nbc.ContainerService.Properties.OrchestratorProfile.OrchestratorVersion
}
if nodeConfig := s.Runtime.AKSNodeConfig; nodeConfig != nil {
return nodeConfig.GetKubernetesVersion()
}
return ""
}
func (s *Scenario) GetClientPrivateKey() string {
if s.Runtime == nil {
return ""
}
if nbc := s.Runtime.NBC; nbc != nil && nbc.ContainerService != nil && nbc.ContainerService.Properties != nil && nbc.ContainerService.Properties.CertificateProfile != nil {
return nbc.ContainerService.Properties.CertificateProfile.ClientPrivateKey
}
if nodeConfig := s.Runtime.AKSNodeConfig; nodeConfig != nil {
return nodeConfig.GetKubeletConfig().GetKubeletClientKey()
}
return ""
}
func (s *Scenario) GetServicePrincipalSecret() string {
if s.Runtime == nil {
return ""
}
if nbc := s.Runtime.NBC; nbc != nil && nbc.ContainerService != nil && nbc.ContainerService.Properties != nil && nbc.ContainerService.Properties.ServicePrincipalProfile != nil {
return nbc.ContainerService.Properties.ServicePrincipalProfile.Secret
}
if nodeConfig := s.Runtime.AKSNodeConfig; nodeConfig != nil {
return nodeConfig.GetAuthConfig().GetServicePrincipalSecret()
}
return ""
}
func (s *Scenario) GetTLSBootstrapToken() string {
if s.Runtime == nil {
return ""
}
if nbc := s.Runtime.NBC; nbc != nil && nbc.KubeletClientTLSBootstrapToken != nil {
return *nbc.KubeletClientTLSBootstrapToken
}
if nodeConfig := s.Runtime.AKSNodeConfig; nodeConfig != nil {
return nodeConfig.GetBootstrappingConfig().GetTlsBootstrappingToken()
}
return ""
}
func (s *Scenario) updateTags(ctx context.Context, vmss *armcompute.VirtualMachineScaleSet) {
if vmss.Tags == nil {
vmss.Tags = map[string]*string{}
}
// don't clean up VMSS in other tests
if config.Config.KeepVMSS {
vmss.Tags["KEEP_VMSS"] = to.Ptr("true")
}
if config.Config.BuildID != "" {
vmss.Tags[buildIDTagKey] = &config.Config.BuildID
}
owner, err := getLoggedInAzUser()
if err != nil {
owner, err = getLocalUsername()
if err != nil {
owner = "unknown"
}
}
vmss.Tags["owner"] = to.Ptr(owner)
}
func getLoggedInAzUser() (string, error) {
// Define the command and arguments
cmd := exec.Command("az", "account", "show", "--query", "user.name", "-o", "tsv")
// Create a buffer to capture stdout
var out bytes.Buffer
cmd.Stdout = &out
// Run the command
err := cmd.Run()
if err != nil {
return "", err
}
return out.String(), nil
}
func getLocalUsername() (string, error) {
currentUser, err := user.Current()
if err == nil {
return currentUser.Username, nil
}
return "", err
}
func (s *Scenario) IsWindows() bool {
return s.VHD.OS == config.OSWindows
}
func (s *Scenario) IsLinux() bool {
return !s.IsWindows()
}
// IsHostsPluginEnabled returns true if the hosts plugin is explicitly enabled
// via either NBC (traditional) or AKSNodeConfig (scriptless) paths.
func (s *Scenario) IsHostsPluginEnabled() bool {
if s.Runtime.NBC != nil && s.Runtime.NBC.AgentPoolProfile != nil {
return s.Runtime.NBC.AgentPoolProfile.ShouldEnableHostsPlugin()
}
if s.Runtime.AKSNodeConfig != nil && s.Runtime.AKSNodeConfig.LocalDnsProfile != nil {
return s.Runtime.AKSNodeConfig.LocalDnsProfile.EnableLocalDns &&
s.Runtime.AKSNodeConfig.LocalDnsProfile.EnableHostsPlugin
}
return false
}
// GetDefaultFQDNsForValidation returns the public cloud FQDNs to validate in hosts file checks.
// AgentBaker e2e only runs in public cloud, so sovereign cloud branches are unnecessary.
func (s *Scenario) GetDefaultFQDNsForValidation() []string {
return []string{
"mcr.microsoft.com",
"login.microsoftonline.com",
"packages.aks.azure.com",
}
}
// GetContainerRegistryFQDN returns the container registry FQDN for the cloud environment
// determined by the cluster's location. Uses Runtime.Cluster.Model.Location so it works
// for both legacy (NBC) and scriptless (AKSNodeConfig) bootstrap paths.
func (s *Scenario) GetContainerRegistryFQDN() string {
if s.Runtime != nil && s.Runtime.Cluster != nil && s.Runtime.Cluster.Model != nil && s.Runtime.Cluster.Model.Location != nil {
location := strings.ToLower(*s.Runtime.Cluster.Model.Location)
if strings.HasPrefix(location, "china") {
return "mcr.azure.cn"
}
}
// Default to public cloud container registry (also used by Fairfax/US Gov)
return "mcr.microsoft.com"
}