-
Notifications
You must be signed in to change notification settings - Fork 13
/
volume_module.go
114 lines (95 loc) · 3.15 KB
/
volume_module.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
package pods
import (
"regexp"
"strings"
"time"
"github.com/alipay/container-observability-service/pkg/reason/modules"
"github.com/alipay/container-observability-service/pkg/reason/share"
"github.com/alipay/container-observability-service/pkg/reason/utils"
"github.com/alipay/container-observability-service/pkg/shares"
utils2 "github.com/alipay/container-observability-service/pkg/utils"
v1 "k8s.io/api/core/v1"
)
var (
InvalidMountConfig = "InvalidMountConfig" // 用户自身问题 hostPath卷配置错误
AccessError = "AccessError" // 权限错误or延迟
NFSError = "NFSError" // NFS版本或转换协议不支持
NoSpaceLeftOnDevice = "NoSpaceLeftOnDevice" // 磁盘空间不足
FailedMount = "FailedMount" // 其他原因
)
func init() {
modules.ShareModuleFactory.Register(share.VOLUME, func() modules.DeliveryModule {
return modules.NewDAGDeliveryModule(share.VOLUME, VolumeReason)
})
}
func VolumeReason(auditEvents []*shares.AuditEvent, beginTime *time.Time, endTime *time.Time) (result string, hasError bool) {
podYaml := utils.GetPodYamlFromHyperEvents(auditEvents, endTime)
if podYaml == nil {
return "", false
}
defer utils2.IgnorePanic("analyze_volume_mount")
SuccessfullyMountedVolumes := map[string]bool{}
eventLen := len(auditEvents)
for idx := eventLen - 1; idx >= 0; idx-- {
hyEvent := auditEvents[idx]
if endTime != nil && hyEvent.Event.StageTimestamp.After(*endTime) {
continue
}
reason := ""
if hyEvent.Reason != "" {
reason = hyEvent.Reason
}
msg := ""
ev, ok := hyEvent.ResponseRuntimeObj.(*v1.Event)
if ok && ev != nil {
msg = ev.Message
}
//v1.16版本
if reason == "SuccessfulCreatePodSandBox" {
return "", false
}
//1.14版本没有明确信息,可以依赖是否开始镜像操作
if reason == "Pulling" || reason == "Pulled" {
return "", false
}
// 1.16后若EnableEventEnhancement 不报此reason
if reason == "SuccessfulAttachOrMountVolume" {
return "", false
}
//regUnmounted := regexp.MustCompile("unmounted volumes=\\[(.*?)\\]")
//unmountedVolumes := regUnmounted.FindStringSubmatch(msg)
if reason != "MountVolume" {
continue
}
var volume string
regVolume := regexp.MustCompile("\\[(.*?)\\]")
volumeMatches := regVolume.FindStringSubmatch(msg)
if len(volumeMatches) >= 2 {
volume = volumeMatches[1]
}
if reason == "MountVolume" && strings.Contains(msg, "Successfully mounted") {
SuccessfullyMountedVolumes[volume] = true
continue
}
if reason == "MountVolume" && strings.Contains(msg, "Failed mounted") {
// 如果已经成功挂载,跳过本次分析
if SuccessfullyMountedVolumes[volume] == true {
continue
}
if strings.Contains(msg, "hostPath type check failed") {
return InvalidMountConfig, true
}
if strings.Contains(msg, "no relationship found between node") {
return AccessError, true
}
if strings.Contains(msg, "requested NFS version or transport protocol is not supported") {
return NFSError, true
}
if strings.Contains(msg, "no space left on device") {
return NoSpaceLeftOnDevice, true
}
return FailedMount, true
}
}
return "", false
}