/
azure_logcollector.go
418 lines (358 loc) · 13.6 KB
/
azure_logcollector.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
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
// +build e2e
/*
Copyright 2020 The Kubernetes Authors.
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 e2e
import (
"context"
"io/ioutil"
"net/http"
"path/filepath"
"strings"
apierrors "k8s.io/apimachinery/pkg/api/errors"
expv1alpha4 "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1alpha4"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1alpha4"
"sigs.k8s.io/cluster-api/test/framework"
"sigs.k8s.io/cluster-api-provider-azure/api/v1alpha4"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-30/compute"
autorest "github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/azure/auth"
"github.com/pkg/errors"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/controller-runtime/pkg/client"
kinderrors "sigs.k8s.io/kind/pkg/errors"
)
// AzureLogCollector collects logs from a CAPZ workload cluster.
type AzureLogCollector struct{}
var _ framework.ClusterLogCollector = &AzureLogCollector{}
// CollectMachineLog collects logs from a machine.
func (k AzureLogCollector) CollectMachineLog(ctx context.Context, managementClusterClient client.Client, m *clusterv1.Machine, outputPath string) error {
var errors []error
am, err := getAzureMachine(ctx, managementClusterClient, m)
if err != nil {
return err
}
cluster, err := util.GetClusterFromMetadata(ctx, managementClusterClient, m.ObjectMeta)
if err != nil {
return err
}
hostname := getHostname(m, isAzureMachineWindows(am))
if err := collectLogsFromNode(ctx, managementClusterClient, cluster, hostname, isAzureMachineWindows(am), outputPath); err != nil {
errors = append(errors, err)
}
if err := collectVMBootLog(ctx, am, outputPath); err != nil {
errors = append(errors, err)
}
return kinderrors.NewAggregate(errors)
}
// CollectMachinePoolLog collects logs from a machine pool.
func (k AzureLogCollector) CollectMachinePoolLog(ctx context.Context, managementClusterClient client.Client, mp *expv1.MachinePool, outputPath string) error {
var errors []error
var isWindows bool
am, err := getAzureMachinePool(ctx, managementClusterClient, mp)
if err != nil {
if !apierrors.IsNotFound(err) {
return err
}
// Machine pool can be an AzureManagedMachinePool for AKS clusters.
_, err = getAzureManagedMachinePool(ctx, managementClusterClient, mp)
if err != nil {
return err
}
} else {
isWindows = isAzureMachinePoolWindows(am)
}
cluster, err := util.GetClusterFromMetadata(ctx, managementClusterClient, mp.ObjectMeta)
if err != nil {
return err
}
for i, instance := range mp.Spec.ProviderIDList {
hostname := mp.Status.NodeRefs[i].Name
if err := collectLogsFromNode(ctx, managementClusterClient, cluster, hostname, isWindows, filepath.Join(outputPath, hostname)); err != nil {
errors = append(errors, err)
}
if err := collectVMSSBootLog(ctx, instance, filepath.Join(outputPath, hostname)); err != nil {
errors = append(errors, err)
}
}
return kinderrors.NewAggregate(errors)
}
// collectLogsFromNode collects logs from various sources by ssh'ing into the node
func collectLogsFromNode(ctx context.Context, managementClusterClient client.Client, cluster *clusterv1.Cluster, hostname string, isWindows bool, outputPath string) error {
Logf("INFO: Collecting logs for node %s in cluster %s in namespace %s\n", hostname, cluster.Name, cluster.Namespace)
controlPlaneEndpoint := cluster.Spec.ControlPlaneEndpoint.Host
execToPathFn := func(outputFileName, command string, args ...string) func() error {
return func() error {
f, err := fileOnHost(filepath.Join(outputPath, outputFileName))
if err != nil {
return err
}
defer f.Close()
return retryWithExponentialBackOff(func() error {
return execOnHost(controlPlaneEndpoint, hostname, sshPort, f, command, args...)
})
}
}
if isWindows {
// if we initiate to many ssh connections they get dropped (default is 10) so split it up
var errors []error
errors = append(errors, kinderrors.AggregateConcurrent(windowsInfo(execToPathFn)))
errors = append(errors, kinderrors.AggregateConcurrent(windowsK8sLogs(execToPathFn)))
errors = append(errors, kinderrors.AggregateConcurrent(windowsNetworkLogs(execToPathFn)))
return kinderrors.NewAggregate(errors)
}
return kinderrors.AggregateConcurrent(linuxLogs(execToPathFn))
}
func isAzureMachineWindows(am *v1alpha4.AzureMachine) bool {
return am.Spec.OSDisk.OSType == azure.WindowsOS
}
func isAzureMachinePoolWindows(amp *expv1alpha4.AzureMachinePool) bool {
return amp.Spec.Template.OSDisk.OSType == azure.WindowsOS
}
func getHostname(m *clusterv1.Machine, isWindows bool) string {
hostname := m.Spec.InfrastructureRef.Name
if isWindows {
// Windows host name ends up being different than the infra machine name
// due to Windows name limitations in Azure so use ip address instead.
if len(m.Status.Addresses) > 0 {
hostname = m.Status.Addresses[0].Address
} else {
Logf("INFO: Unable to collect logs as node doesn't have addresses")
}
}
return hostname
}
func getAzureMachine(ctx context.Context, managementClusterClient client.Client, m *clusterv1.Machine) (*v1alpha4.AzureMachine, error) {
key := client.ObjectKey{
Namespace: m.Spec.InfrastructureRef.Namespace,
Name: m.Spec.InfrastructureRef.Name,
}
azMachine := &v1alpha4.AzureMachine{}
err := managementClusterClient.Get(ctx, key, azMachine)
return azMachine, err
}
func getAzureMachinePool(ctx context.Context, managementClusterClient client.Client, mp *expv1.MachinePool) (*expv1alpha4.AzureMachinePool, error) {
key := client.ObjectKey{
Namespace: mp.Spec.Template.Spec.InfrastructureRef.Namespace,
Name: mp.Spec.Template.Spec.InfrastructureRef.Name,
}
azMachinePool := &expv1alpha4.AzureMachinePool{}
err := managementClusterClient.Get(ctx, key, azMachinePool)
return azMachinePool, err
}
func getAzureManagedMachinePool(ctx context.Context, managementClusterClient client.Client, mp *expv1.MachinePool) (*expv1alpha4.AzureManagedMachinePool, error) {
key := client.ObjectKey{
Namespace: mp.Spec.Template.Spec.InfrastructureRef.Namespace,
Name: mp.Spec.Template.Spec.InfrastructureRef.Name,
}
azManagedMachinePool := &expv1alpha4.AzureManagedMachinePool{}
err := managementClusterClient.Get(ctx, key, azManagedMachinePool)
return azManagedMachinePool, err
}
func linuxLogs(execToPathFn func(outputFileName string, command string, args ...string) func() error) []func() error {
return []func() error{
execToPathFn(
"journal.log",
"journalctl", "--no-pager", "--output=short-precise",
),
execToPathFn(
"kern.log",
"journalctl", "--no-pager", "--output=short-precise", "-k",
),
execToPathFn(
"kubelet-version.txt",
"kubelet", "--version",
),
execToPathFn(
"kubelet.log",
"journalctl", "--no-pager", "--output=short-precise", "-u", "kubelet.service",
),
execToPathFn(
"containerd.log",
"journalctl", "--no-pager", "--output=short-precise", "-u", "containerd.service",
),
execToPathFn(
"cloud-init.log",
"cat", "/var/log/cloud-init.log",
),
execToPathFn(
"cloud-init-output.log",
"cat", "/var/log/cloud-init-output.log",
),
execToPathFn(
"sentinel-file-dir.txt",
"ls", "/run/cluster-api/",
),
execToPathFn(
"etc/containerd/config.toml",
"cat", "/etc/containerd/config.toml",
),
}
}
func windowsK8sLogs(execToPathFn func(outputFileName string, command string, args ...string) func() error) []func() error {
return []func() error{
execToPathFn(
"hyperv-operation.log",
"Get-WinEvent", "-LogName Microsoft-Windows-Hyper-V-Compute-Operational | Select-Object -Property TimeCreated, Id, LevelDisplayName, Message | Sort-Object TimeCreated | Format-Table -Wrap -Autosize",
),
execToPathFn(
"docker.log",
"get-eventlog", "-LogName Application -Source Docker | Select-Object Index, TimeGenerated, EntryType, Message | Sort-Object Index | Format-Table -Wrap -Autosize",
),
execToPathFn(
"containers.log",
"docker", "ps -a",
),
execToPathFn(
"containers-hcs.log",
"hcsdiag", "list",
),
execToPathFn(
"kubelet.log",
`Get-ChildItem "C:\\var\\log\\kubelet\\" | ForEach-Object { write-output "$_" ;cat "c:\\var\\log\\kubelet\\$_" }`,
),
}
}
func windowsInfo(execToPathFn func(outputFileName string, command string, args ...string) func() error) []func() error {
return []func() error{
execToPathFn(
"reboots.log",
"Get-WinEvent", `-ErrorAction Ignore -FilterHashtable @{logname = 'System'; id = 1074, 1076, 2004, 6005, 6006, 6008 } | Select-Object -Property TimeCreated, Id, LevelDisplayName, Message | Format-Table -Wrap -Autosize`,
),
execToPathFn(
"scm.log",
"Get-WinEvent", `-FilterHashtable @{logname = 'System'; ProviderName = 'Service Control Manager' } | Select-Object -Property TimeCreated, Id, LevelDisplayName, Message | Format-Table -Wrap -Autosize`,
),
execToPathFn(
"pagefile.log",
"Get-CimInstance", "win32_pagefileusage | Format-List *",
),
execToPathFn(
"cloudbase-init-unattend.log",
"get-content 'C:\\Program Files\\Cloudbase Solutions\\Cloudbase-Init\\log\\cloudbase-init-unattend.log'",
),
execToPathFn(
"cloudbase-init.log",
"get-content 'C:\\Program Files\\Cloudbase Solutions\\Cloudbase-Init\\log\\cloudbase-init.log'",
),
execToPathFn(
"services.log",
"get-service",
),
}
}
func windowsNetworkLogs(execToPathFn func(outputFileName string, command string, args ...string) func() error) []func() error {
return []func() error{
execToPathFn(
"network.log",
"Get-HnsNetwork | Select Name, Type, Id, AddressPrefix | Format-Table -Wrap -Autosize",
),
execToPathFn(
"network-detailed.log",
"Get-hnsnetwork | Convertto-json -Depth 20",
),
execToPathFn(
"network-individual-detailed.log",
"Get-hnsnetwork | % { Get-HnsNetwork -Id $_.ID -Detailed } | Convertto-json -Depth 20",
),
execToPathFn(
"hnsendpoints.log",
"Get-HnsEndpoint | Select IpAddress, MacAddress, IsRemoteEndpoint, State",
),
execToPathFn(
"hnsendpolicy-detailed.log",
"Get-hnspolicylist | Convertto-json -Depth 20",
),
execToPathFn(
"ipconfig.log",
"ipconfig /allcompartments /all",
),
execToPathFn(
"ips.log",
"Get-NetIPAddress -IncludeAllCompartments",
),
execToPathFn(
"interfaces.log",
"Get-NetIPInterface -IncludeAllCompartments",
),
execToPathFn(
"hnsdiag.txt",
"hnsdiag list all -d",
),
}
}
// collectVMBootLog collects boot logs of the vm by using azure boot diagnostics.
func collectVMBootLog(ctx context.Context, am *v1alpha4.AzureMachine, outputPath string) error {
Logf("INFO: Collecting boot logs for AzureMachine %s\n", am.GetName())
resourceId := strings.TrimPrefix(*am.Spec.ProviderID, azure.ProviderIDPrefix)
resource, err := autorest.ParseResourceID(resourceId)
if err != nil {
return errors.Wrap(err, "failed to parse resource id")
}
settings, err := auth.GetSettingsFromEnvironment()
if err != nil {
return errors.Wrap(err, "failed to get settings from environment")
}
vmClient := compute.NewVirtualMachinesClient(settings.GetSubscriptionID())
vmClient.Authorizer, err = settings.GetAuthorizer()
if err != nil {
return errors.Wrap(err, "failed to get authorizer")
}
bootDiagnostics, err := vmClient.RetrieveBootDiagnosticsData(ctx, resource.ResourceGroup, resource.ResourceName, nil)
if err != nil {
return errors.Wrap(err, "failed to get boot diagnostics data")
}
return writeBootLog(bootDiagnostics, outputPath)
}
// collectVMSSBootLog collects boot logs of the scale set by using azure boot diagnostics.
func collectVMSSBootLog(ctx context.Context, providerID string, outputPath string) error {
resourceId := strings.TrimPrefix(providerID, azure.ProviderIDPrefix)
v := strings.Split(resourceId, "/")
instanceId := v[len(v)-1]
resourceId = strings.TrimSuffix(resourceId, "/virtualMachines/"+instanceId)
resource, err := autorest.ParseResourceID(resourceId)
if err != nil {
return errors.Wrap(err, "failed to parse resource id")
}
Logf("INFO: Collecting boot logs for VMSS instance %s of scale set %s\n", instanceId, resource.ResourceName)
settings, err := auth.GetSettingsFromEnvironment()
if err != nil {
return errors.Wrap(err, "failed to get settings from environment")
}
vmssClient := compute.NewVirtualMachineScaleSetVMsClient(settings.GetSubscriptionID())
vmssClient.Authorizer, err = settings.GetAuthorizer()
if err != nil {
return errors.Wrap(err, "failed to get authorizer")
}
bootDiagnostics, err := vmssClient.RetrieveBootDiagnosticsData(ctx, resource.ResourceGroup, resource.ResourceName, instanceId, nil)
if err != nil {
return errors.Wrap(err, "failed to get boot diagnostics data")
}
return writeBootLog(bootDiagnostics, outputPath)
}
func writeBootLog(bootDiagnostics compute.RetrieveBootDiagnosticsDataResult, outputPath string) error {
var err error
resp, err := http.Get(*bootDiagnostics.SerialConsoleLogBlobURI)
if err != nil || resp.StatusCode != 200 {
return errors.Wrap(err, "failed to get logs from serial console uri")
}
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "failed to read response body")
}
if err := ioutil.WriteFile(filepath.Join(outputPath, "boot.log"), content, 0644); err != nil {
return errors.Wrap(err, "failed to write response to file")
}
return nil
}