Skip to content

Commit 0d8daf2

Browse files
authored
Merge pull request #796 from hzxuzhonghu/start-type
some cleanups and rename on start type
2 parents b47fbcd + daf352a commit 0d8daf2

File tree

4 files changed

+43
-31
lines changed

4 files changed

+43
-31
lines changed

daemon/manager/manager.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ func Execute(configs *options.BootstrapConfigs) error {
113113
log.Info("start cni successfully")
114114

115115
setupSignalHandler()
116-
bpf.SetCloseStatus()
116+
// set exit type, which can be used by bpf loader to decide whether to cleanup bpf prog
117+
bpf.SetExitType()
117118
return nil
118119
}
119120

pkg/bpf/bpf.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ type BpfLoader struct {
5858
obj *BpfKmesh
5959
workloadObj *BpfKmeshWorkload
6060
bpfLogLevel *ebpf.Map
61-
VersionMap *ebpf.Map
61+
versionMap *ebpf.Map
6262
}
6363

6464
func NewBpfLoader(config *options.BpfConfig) *BpfLoader {
6565
return &BpfLoader{
6666
config: config,
67-
VersionMap: NewVersionMap(config),
67+
versionMap: NewVersionMap(config),
6868
}
6969
}
7070

@@ -119,7 +119,7 @@ func StartMda() error {
119119
func (l *BpfLoader) Start(config *options.BpfConfig) error {
120120
var err error
121121

122-
if l.VersionMap == nil {
122+
if l.versionMap == nil {
123123
return fmt.Errorf("NewVersionMap failed")
124124
}
125125

@@ -173,12 +173,12 @@ func StopMda() error {
173173

174174
func (l *BpfLoader) Stop() {
175175
var err error
176-
if GetStartType() == Restart {
176+
if GetExitType() == Restart {
177177
log.Infof("kmesh restart, not clean bpf map and prog")
178178
return
179179
}
180180

181-
Close(l.VersionMap)
181+
closeMap(l.versionMap)
182182

183183
if l.config.AdsEnabled() {
184184
C.deserial_uninit()
@@ -206,14 +206,15 @@ func NewVersionMap(config *options.BpfConfig) *ebpf.Map {
206206
var versionPath string
207207
var versionMap *ebpf.Map
208208
if config.AdsEnabled() {
209-
versionPath = filepath.Join(config.BpfFsPath + constants.VersionPath)
209+
versionPath = filepath.Join(config.BpfFsPath, constants.VersionPath)
210210
} else if config.WdsEnabled() {
211-
versionPath = filepath.Join(config.BpfFsPath + constants.WorkloadVersionPath)
211+
versionPath = filepath.Join(config.BpfFsPath, constants.WorkloadVersionPath)
212212
}
213213

214+
versionMapPinPath := filepath.Join(versionPath, "kmesh_version")
214215
_, err := os.Stat(versionPath)
215216
if err == nil {
216-
versionMap = recoverVersionMap(config, versionPath)
217+
versionMap = recoverVersionMap(versionMapPinPath)
217218
if versionMap != nil {
218219
SetStartStatus(versionMap)
219220
}
@@ -229,8 +230,7 @@ func NewVersionMap(config *options.BpfConfig) *ebpf.Map {
229230
}
230231

231232
// Make sure the directory about to use is clean
232-
kmeshBpfPath := filepath.Dir(versionPath)
233-
err = os.RemoveAll(kmeshBpfPath)
233+
err = os.RemoveAll(versionPath)
234234
if err != nil {
235235
log.Errorf("Clean bpf maps and progs failed, err is:%v", err)
236236
return nil
@@ -255,7 +255,7 @@ func NewVersionMap(config *options.BpfConfig) *ebpf.Map {
255255
return nil
256256
}
257257

258-
err = m.Pin(filepath.Join(versionPath + "/kmesh_version"))
258+
err = m.Pin(versionMapPinPath)
259259
if err != nil {
260260
log.Errorf("kmesh_version pin failed: %v", err)
261261
return nil
@@ -288,14 +288,14 @@ func getOldVersionFromMap(m *ebpf.Map, key uint32) uint32 {
288288
return value
289289
}
290290

291-
func recoverVersionMap(config *options.BpfConfig, versionPath string) *ebpf.Map {
291+
func recoverVersionMap(pinPath string) *ebpf.Map {
292292
opts := &ebpf.LoadPinOptions{
293293
ReadOnly: false,
294294
WriteOnly: false,
295295
Flags: 0,
296296
}
297297

298-
versionMap, err := ebpf.LoadPinnedMap(filepath.Join(versionPath+"/kmesh_version"), opts)
298+
versionMap, err := ebpf.LoadPinnedMap(pinPath, opts)
299299
if err != nil {
300300
log.Infof("kmesh version map loadfailed: %v, start normally", err)
301301

@@ -306,17 +306,17 @@ func recoverVersionMap(config *options.BpfConfig, versionPath string) *ebpf.Map
306306
return versionMap
307307
}
308308

309-
func Close(m *ebpf.Map) {
309+
func closeMap(m *ebpf.Map) {
310310
var err error
311311
err = m.Unpin()
312312
if err != nil {
313-
log.Errorf("Failed to Unpin kmesh_version :%v", err)
313+
log.Errorf("Failed to unpin kmesh_version: %v", err)
314314
}
315315

316316
err = m.Close()
317317
if err != nil {
318-
log.Errorf("Failed to Close kmesh_version :%v", err)
318+
log.Errorf("Failed to close kmesh_version: %v", err)
319319
}
320320

321-
log.Infof("Clean kmesh_version map and bpf prog")
321+
log.Infof("cleaned kmesh_version map")
322322
}

pkg/bpf/bpf_restart.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ import (
3838
* Normal: a normal new start
3939
* Restart: reusing the previous kmesh configuration
4040
* Update: upgrading kmesh and reusing part of previous kmesh configuration
41-
* Close Kmesh:
41+
* Exit Kmesh:
4242
* Normal: normal close, cleanup all the bpf prog and maps
4343
* Restart: not clean kmesh configuration and bpf map, for next launch
44-
* Update: not clean part of kmesh configuration, for next launch
44+
* Update: not supported yet
4545
*/
4646

4747
type StartType uint8
@@ -52,8 +52,12 @@ const (
5252
Update
5353
)
5454

55+
// kmeshStartType is used during bootstrap to indicate how to initiate bpf prog and maps
5556
var kmeshStartType StartType
5657

58+
// kmeshStartType is used during exit to indicate how to cleanup bpf prog and maps
59+
var kmeshExitType StartType
60+
5761
func GetStartType() StartType {
5862
return kmeshStartType
5963
}
@@ -62,7 +66,15 @@ func SetStartType(Status StartType) {
6266
kmeshStartType = Status
6367
}
6468

65-
func inferRestartStatus() StartType {
69+
func SetExitType() {
70+
kmeshExitType = inferNextStartType()
71+
}
72+
73+
func GetExitType() StartType {
74+
return kmeshExitType
75+
}
76+
77+
func inferNextStartType() StartType {
6678
clientset, err := utils.GetK8sclient()
6779
if err != nil {
6880
return Normal
@@ -80,10 +92,6 @@ func inferRestartStatus() StartType {
8092
return Normal
8193
}
8294

83-
func SetCloseStatus() {
84-
SetStartType(inferRestartStatus())
85-
}
86-
8795
func SetStartStatus(versionMap *ebpf.Map) {
8896
var GitVersion uint32
8997
hash.Reset()
@@ -94,6 +102,10 @@ func SetStartStatus(versionMap *ebpf.Map) {
94102
if GitVersion == oldGitVersion {
95103
log.Infof("kmesh start with Restart, load bpf maps and prog from last")
96104
SetStartType(Restart)
105+
} else if oldGitVersion == 0 {
106+
// version not found, it is a fresh start
107+
log.Infof("kmesh start with Normal")
108+
SetStartType(Normal)
97109
} else {
98110
log.Infof("kmesh start with Update")
99111
SetStartType(Update)

pkg/bpf/bpf_restart_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ func runTestNormal(t *testing.T) {
7575
assert.ErrorIsf(t, err, nil, "bpfLoader start failed %v", err)
7676
}
7777
assert.Equal(t, Normal, GetStartType(), "set kmesh start status failed")
78-
SetCloseStatus()
79-
assert.Equal(t, Normal, GetStartType(), "set kmesh close status failed")
78+
79+
SetExitType()
8080
bpfLoader.Stop()
8181
}
8282

@@ -89,24 +89,23 @@ func runTestRestart(t *testing.T) {
8989
assert.ErrorIsf(t, err, nil, "bpfLoader start failed %v", err)
9090
}
9191
assert.Equal(t, Normal, GetStartType(), "set kmesh start status:Normal failed")
92-
SetStartType(Restart)
92+
kmeshExitType = Restart
9393
bpfLoader.Stop()
94-
assert.Equal(t, Restart, GetStartType(), "set kmesh close status:Restart failed")
9594

9695
if config.AdsEnabled() {
9796
versionPath = filepath.Join(config.BpfFsPath + "/bpf_kmesh/map/")
9897
} else if config.WdsEnabled() {
9998
versionPath = filepath.Join(config.BpfFsPath + "/bpf_kmesh_workload/map/")
10099
}
101100
_, err := os.Stat(versionPath)
102-
assert.ErrorIsf(t, err, nil, "bpfLoader Stop failed,versionPath is not exist: %v", err)
101+
assert.ErrorIsf(t, err, nil, "bpfLoader Stop failed, versionPath is not exist: %v", err)
103102

104103
// Restart
105104
bpfLoader = NewBpfLoader(&config)
106105
if err := bpfLoader.Start(&config); err != nil {
107106
assert.ErrorIsf(t, err, nil, "bpfLoader start failed %v", err)
108107
}
109108
assert.Equal(t, Restart, GetStartType(), "set kmesh start status:Restart failed")
110-
SetStartType(Normal)
109+
kmeshExitType = Normal
111110
bpfLoader.Stop()
112111
}

0 commit comments

Comments
 (0)