Skip to content

Commit d0ceb85

Browse files
authored
feat(host): support pod post cleanup configuration (#22836)
1 parent b4c7932 commit d0ceb85

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

pkg/apis/compute/pod.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ const (
3636
)
3737

3838
const (
39-
POD_METADATA_CRI_ID = "cri_id"
40-
POD_METADATA_CRI_CONFIG = "cri_config"
41-
POD_METADATA_PORT_MAPPINGS = "port_mappings"
39+
POD_METADATA_CRI_ID = "cri_id"
40+
POD_METADATA_CRI_CONFIG = "cri_config"
41+
POD_METADATA_PORT_MAPPINGS = "port_mappings"
42+
POD_METADATA_POST_STOP_CLEANUP_CONFIG = "post_stop_cleanup_config"
4243
)
4344

4445
type PodContainerCreateInput struct {
@@ -164,3 +165,7 @@ func ValidatePodLogOptions(opts *PodLogOptions) error {
164165
}
165166
return nil
166167
}
168+
169+
type PodPostStopCleanupConfig struct {
170+
Dirs []string `json:"dirs"`
171+
}

pkg/hostman/guestman/pod.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,22 @@ func (s *sPodGuestInstance) getCreateParams() (jsonutils.JSONObject, error) {
600600
return jsonutils.ParseString(createParamsStr)
601601
}
602602

603+
func (s *sPodGuestInstance) getPostStopCleanupConfig() (*computeapi.PodPostStopCleanupConfig, error) {
604+
data, ok := s.GetDesc().Metadata[computeapi.POD_METADATA_POST_STOP_CLEANUP_CONFIG]
605+
if !ok {
606+
return nil, nil
607+
}
608+
jsonData, err := jsonutils.ParseString(data)
609+
if err != nil {
610+
return nil, errors.Wrapf(err, "parse to json")
611+
}
612+
input := new(computeapi.PodPostStopCleanupConfig)
613+
if err := jsonData.Unmarshal(input); err != nil {
614+
return nil, errors.Wrapf(err, "unmarshal to pod post stop cleanup config")
615+
}
616+
return input, nil
617+
}
618+
603619
func (s *sPodGuestInstance) getPodCreateParams() (*computeapi.PodCreateInput, error) {
604620
createParams, err := s.getCreateParams()
605621
if err != nil {
@@ -1130,6 +1146,25 @@ func (s *sPodGuestInstance) stopPod(ctx context.Context, timeout int64) error {
11301146
return err
11311147
}
11321148
ReleaseGuestCpuset(s.manager, s)
1149+
if err := s.postStopCleanup(ctx); err != nil {
1150+
return errors.Wrapf(err, "post stop cleanup")
1151+
}
1152+
return nil
1153+
}
1154+
1155+
func (s *sPodGuestInstance) postStopCleanup(ctx context.Context) error {
1156+
config, err := s.getPostStopCleanupConfig()
1157+
if err != nil {
1158+
return errors.Wrapf(err, "get post stop cleanup config")
1159+
}
1160+
if config == nil {
1161+
return nil
1162+
}
1163+
for _, dir := range config.Dirs {
1164+
if err := os.RemoveAll(dir); err != nil {
1165+
return errors.Wrapf(err, "remove dir %s", dir)
1166+
}
1167+
}
11331168
return nil
11341169
}
11351170

0 commit comments

Comments
 (0)