Skip to content

Commit

Permalink
bugfix: DeleteObj task returns error (#41)
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Shmulevich <[email protected]>
  • Loading branch information
dmitsh authored May 17, 2024
1 parent e3d5e67 commit b7df34c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
4 changes: 3 additions & 1 deletion pkg/engine/delete_object_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ func (task *DeleteObjTask) Exec(ctx context.Context) error {
return err
}

task.log.V(4).Info("Deleting objects", "GVR", info.GVR.String(), "names", info.Names)

for _, name := range info.Names {
err := task.client.Resource(info.GVR).Delete(ctx, name, v1.DeleteOptions{})
err = task.client.Resource(info.GVR).Namespace(info.Namespace).Delete(ctx, name, v1.DeleteOptions{})
if err != nil {
return err
}
Expand Down
20 changes: 10 additions & 10 deletions pkg/engine/submit_object_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (task *SubmitObjTask) Exec(ctx context.Context) error {
return fmt.Errorf("%s: failed to get object type: %v", task.ID(), err)
}

objs, podCount, podRegexp, err := task.getGenericObjects(regObjParams)
objs, names, podCount, podRegexp, err := task.getGenericObjects(regObjParams)
if err != nil {
return err
}
Expand All @@ -135,13 +135,13 @@ func (task *SubmitObjTask) Exec(ctx context.Context) error {
}

return task.accessor.SetObjInfo(task.taskID,
NewObjInfo([]string{objs[0].Metadata.Name}, objs[0].Metadata.Namespace, regObjParams.gvr, podCount, podRegexp...))
NewObjInfo(names, objs[0].Metadata.Namespace, regObjParams.gvr, podCount, podRegexp...))
}

func (task *SubmitObjTask) getGenericObjects(regObjParams *RegisterObjParams) ([]GenericObject, int, []string, error) {
func (task *SubmitObjTask) getGenericObjects(regObjParams *RegisterObjParams) ([]GenericObject, []string, int, []string, error) {
names, err := utils.GenerateNames(regObjParams.NameFormat, task.Count, task.Params)
if err != nil {
return nil, 0, nil, fmt.Errorf("%s: failed to generate object names: %v", task.ID(), err)
return nil, nil, 0, nil, fmt.Errorf("%s: failed to generate object names: %v", task.ID(), err)
}

objs := make([]GenericObject, task.Count)
Expand All @@ -152,17 +152,17 @@ func (task *SubmitObjTask) getGenericObjects(regObjParams *RegisterObjParams) ([

data, err := utils.ExecTemplate(regObjParams.objTpl, task.Params)
if err != nil {
return nil, 0, nil, err
return nil, nil, 0, nil, err
}

if err = yaml.Unmarshal(data, &objs[i]); err != nil {
return nil, 0, nil, err
return nil, nil, 0, nil, err
}

if regObjParams.podNameTpl != nil {
data, err = utils.ExecTemplate(regObjParams.podNameTpl, task.Params)
if err != nil {
return nil, 0, nil, err
return nil, nil, 0, nil, err
}
re := strings.Trim(strings.TrimSpace(string(data)), "\"")
podRegexp = append(podRegexp, re)
Expand All @@ -173,18 +173,18 @@ func (task *SubmitObjTask) getGenericObjects(regObjParams *RegisterObjParams) ([
if regObjParams.podCountTpl != nil {
data, err := utils.ExecTemplate(regObjParams.podCountTpl, task.Params)
if err != nil {
return nil, 0, nil, err
return nil, nil, 0, nil, err
}
str := string(data)
podCount, err = strconv.Atoi(str)
if err != nil {
return nil, 0, nil, fmt.Errorf("%s: failed to convert pod count %s to int: %v", task.ID(), str, err)
return nil, nil, 0, nil, fmt.Errorf("%s: failed to convert pod count %s to int: %v", task.ID(), str, err)
}
podCount *= task.Count
}
task.log.V(4).Info("Generating object specs", "podCount", podCount, "podRegexp", podRegexp)

return objs, podCount, podRegexp, nil
return objs, names, podCount, podRegexp, nil
}

func (obj *GenericObject) UnmarshalYAML(unmarshal func(interface{}) error) error {
Expand Down
6 changes: 5 additions & 1 deletion pkg/engine/submit_object_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func TestNewSubmitObjTask(t *testing.T) {
err string
task *SubmitObjTask
objs []GenericObject
names []string
podCount int
podRegexp []string
}{
Expand Down Expand Up @@ -178,6 +179,7 @@ func TestNewSubmitObjTask(t *testing.T) {
Spec: spec,
},
},
names: []string{"job1"},
podRegexp: []string{},
},
{
Expand Down Expand Up @@ -232,6 +234,7 @@ func TestNewSubmitObjTask(t *testing.T) {
Spec: spec,
},
},
names: []string{"job1", "job2"},
podCount: 4,
podRegexp: []string{"job1-test-[0-9]+", "job2-test-[0-9]+"},
},
Expand Down Expand Up @@ -280,9 +283,10 @@ func TestNewSubmitObjTask(t *testing.T) {
require.NoError(t, err)
}

objs, podCount, podRegexp, err := task.getGenericObjects(tc.regObjParams)
objs, names, podCount, podRegexp, err := task.getGenericObjects(tc.regObjParams)
require.NoError(t, err)
require.Equal(t, tc.objs, objs)
require.Equal(t, tc.names, names)
require.Equal(t, tc.podCount, podCount)
require.Equal(t, tc.podRegexp, podRegexp)
}
Expand Down
4 changes: 4 additions & 0 deletions resources/tests/volcano/test-job.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ tasks:
refTaskId: job
status: Running
timeout: 5s
- id: delete
type: DeleteObj
params:
refTaskId: job

0 comments on commit b7df34c

Please sign in to comment.