-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: add recycle grouped workflow run with cron (#143)
Signed-off-by: FogDong <[email protected]>
- Loading branch information
Showing
8 changed files
with
215 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
Copyright 2023 The KubeVela 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 utils | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
"time" | ||
|
||
"github.com/kubevela/workflow/api/v1alpha1" | ||
"github.com/robfig/cron/v3" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
"k8s.io/client-go/util/retry" | ||
"k8s.io/klog/v2" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
) | ||
|
||
// recycleCronJob is the cron job to clean the completed workflow | ||
type recycleCronJob struct { | ||
cli client.Client | ||
duration time.Duration | ||
cron string | ||
label string | ||
} | ||
|
||
// NewRecycleCronJob returns a new recycleCronJob | ||
func NewRecycleCronJob(cli client.Client, duration time.Duration, cron, label string) manager.Runnable { | ||
return &recycleCronJob{ | ||
cli: cli, | ||
duration: duration, | ||
label: label, | ||
cron: cron, | ||
} | ||
} | ||
|
||
func (r *recycleCronJob) Start(ctx context.Context) error { | ||
c, err := r.start(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
defer c.Stop() | ||
<-ctx.Done() | ||
return nil | ||
} | ||
|
||
func (r *recycleCronJob) start(ctx context.Context) (*cron.Cron, error) { | ||
c := cron.New(cron.WithChain( | ||
cron.Recover(cron.DefaultLogger), | ||
)) | ||
if _, err := c.AddFunc(r.cron, func() { | ||
err := retry.OnError(wait.Backoff{ | ||
Steps: 3, | ||
Duration: 1 * time.Minute, | ||
Factor: 5.0, | ||
Jitter: 0.1, | ||
}, func(err error) bool { | ||
// always retry | ||
return true | ||
}, func() error { | ||
if err := r.run(ctx); err != nil { | ||
klog.Errorf("Failed to recycle workflow run: %v", err) | ||
return err | ||
} | ||
klog.Info("Recycle workflow run successfully") | ||
return nil | ||
}) | ||
if err != nil { | ||
klog.Errorf("Failed to recycle workflow runs after 3 tries: %v", err) | ||
} | ||
}); err != nil { | ||
return nil, err | ||
} | ||
c.Start() | ||
return c, nil | ||
} | ||
|
||
func (r *recycleCronJob) run(ctx context.Context) error { | ||
runs := &v1alpha1.WorkflowRunList{} | ||
selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{ | ||
MatchExpressions: []metav1.LabelSelectorRequirement{{Key: r.label, Operator: metav1.LabelSelectorOpExists}}}) | ||
if err != nil { | ||
return err | ||
} | ||
listOpt := &client.ListOptions{LabelSelector: selector} | ||
if err := r.cli.List(ctx, runs, listOpt); err != nil { | ||
return err | ||
} | ||
sort.Sort(runs) | ||
items := make(map[string][]v1alpha1.WorkflowRun) | ||
for _, item := range runs.Items { | ||
if v, ok := item.Labels[r.label]; ok { | ||
items[v] = append(items[v], item) | ||
} | ||
} | ||
for _, l := range items { | ||
for i := 1; i < len(l); i++ { | ||
item := l[i] | ||
if item.Status.Finished && time.Since(item.Status.EndTime.Time) > r.duration { | ||
if err := r.cli.Delete(ctx, &item); err != nil { | ||
klog.Errorf("Failed to delete workflowRun %s/%s, error: %v", item.Namespace, item.Name, err) | ||
} | ||
klog.Info("Successfully recycled completed workflowRun %s/%s", item.Namespace, item.Name) | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
Copyright 2023 The KubeVela 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 utils | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/kubevela/workflow/api/v1alpha1" | ||
) | ||
|
||
func TestRecycleCronJob(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
defer cancel() | ||
r := require.New(t) | ||
for i := 1; i < 7; i++ { | ||
run := &v1alpha1.WorkflowRun{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: fmt.Sprintf("workflow-test-%d", i), | ||
Namespace: "default", | ||
}, | ||
} | ||
if i%5 != 0 { | ||
run.Labels = map[string]string{ | ||
"pipeline.oam.dev/name": "test", | ||
} | ||
} | ||
err := cli.Create(ctx, run) | ||
r.NoError(err) | ||
run.Status.Finished = i%6 != 0 | ||
run.Status.EndTime = metav1.Time{Time: time.Now().AddDate(0, 0, -i)} | ||
err = cli.Status().Update(ctx, run) | ||
r.NoError(err) | ||
defer cli.Delete(ctx, run) | ||
} | ||
|
||
runner := NewRecycleCronJob(cli, time.Hour, "@every 1s", "pipeline.oam.dev/name") | ||
err := runner.Start(ctx) | ||
r.NoError(err) | ||
runs := &v1alpha1.WorkflowRunList{} | ||
err = cli.List(ctx, runs) | ||
r.NoError(err) | ||
r.Equal(3, len(runs.Items)) | ||
} |