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
|
/*
Copyright 2023 The KubeEdge 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 taskexecutor
import (
"encoding/json"
"fmt"
"time"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/v3/mem"
"github.com/kubeedge/kubeedge/common/types"
api "github.com/kubeedge/kubeedge/pkg/apis/fsm/v1alpha1"
"github.com/kubeedge/kubeedge/pkg/util/fsm"
)
const (
MaxCPUUsage float64 = 80
MaxMemUsage float64 = 80
MaxDiskUsage float64 = 80
)
func preCheck(taskReq types.NodeTaskRequest) fsm.Event {
event := fsm.Event{
Type: "Check",
Action: api.ActionSuccess,
Msg: "",
}
data, err := json.Marshal(taskReq.Item)
if err != nil {
event.Action = api.ActionFailure
event.Msg = err.Error()
return event
}
var checkItems types.NodePreCheckRequest
err = json.Unmarshal(data, &checkItems)
if err != nil {
event.Action = api.ActionFailure
event.Msg = err.Error()
return event
}
var failed bool
var checkResult = map[string]string{}
var checkFunc = map[string]func() error{
"cpu": checkCPU,
"mem": checkMem,
"disk": checkDisk,
}
for _, item := range checkItems.CheckItem {
f, ok := checkFunc[item]
if !ok {
checkResult[item] = "check item not support"
continue
}
err = f()
if err != nil {
failed = true
checkResult[item] = err.Error()
continue
}
checkResult[item] = "ok"
}
if !failed {
return event
}
event.Action = api.ActionFailure
result, err := json.Marshal(checkResult)
if err != nil {
event.Msg = err.Error()
return event
}
event.Msg = string(result)
return event
}
func checkCPU() error {
cpuUsage, err := cpu.Percent(100*time.Millisecond, false)
if err != nil {
return err
}
var usage float64
for _, percpu := range cpuUsage {
usage += percpu / float64(len(cpuUsage))
}
if usage > MaxCPUUsage {
return fmt.Errorf("current cpu usage is %f, which exceeds the maximum allowed usage %f", usage, MaxCPUUsage)
}
return nil
}
func checkMem() error {
memInfo, err := mem.VirtualMemory()
if err != nil {
return err
}
memUsedPercent := memInfo.UsedPercent
if memUsedPercent > MaxMemUsage {
return fmt.Errorf("current mem usage is %f, which exceeds the maximum allowed usage %f", memUsedPercent, MaxMemUsage)
}
return nil
}
func checkDisk() error {
partitions, err := disk.Partitions(true)
if err != nil {
return err
}
var failed bool
var diskUsages = map[string]string{}
for _, part := range partitions {
usage, err := disk.Usage(part.Mountpoint)
if err != nil {
failed = true
diskUsages[part.Device] = err.Error()
continue
}
if usage.UsedPercent > MaxDiskUsage {
failed = true
diskUsages[part.Device] = fmt.Sprintf("current disk usage is %f, which exceeds the maximum allowed usage %f", usage.UsedPercent, MaxMemUsage)
continue
}
diskUsages[part.Device] = fmt.Sprintf("%f", usage.UsedPercent)
}
if !failed {
return nil
}
result, err := json.Marshal(diskUsages)
if err != nil {
return err
}
return fmt.Errorf(string(result))
}
func normalInit(types.NodeTaskRequest) fsm.Event {
return fsm.Event{
Type: "Init",
Action: api.ActionSuccess,
Msg: "",
}
}
|