-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: enable per-namespace parallelism
Signed-off-by: isubasinghe <isitha@pipekit.io>
- Loading branch information
1 parent
aaa4c26
commit 8501244
Showing
12 changed files
with
321 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package e2e | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/argoproj/argo-workflows/v3/test/e2e/fixtures" | ||
) | ||
|
||
type NamespaceParallelismSuite struct { | ||
fixtures.E2ESuite | ||
} | ||
|
||
const wf = `apiVersion: argoproj.io/v1alpha1 | ||
kind: Workflow | ||
metadata: | ||
generateName: hello-world- | ||
labels: | ||
workflows.argoproj.io/archive-strategy: "false" | ||
annotations: | ||
workflows.argoproj.io/description: | | ||
This is a simple hello world example. | ||
spec: | ||
entrypoint: hello-world | ||
templates: | ||
- name: hello-world | ||
container: | ||
image: busybox | ||
command: [sleep] | ||
args: ["240"] | ||
` | ||
|
||
func (s *NamespaceParallelismSuite) TestNamespaceParallelism() { | ||
|
||
s.Given(). | ||
Workflow(wf). | ||
When(). | ||
AddNamespaceLimit("1"). | ||
SubmitWorkflow(). | ||
WaitForWorkflow(fixtures.ToStart) | ||
|
||
time.Sleep(time.Second * 5) | ||
wf := s.Given(). | ||
Workflow(wf). | ||
When(). | ||
SubmitWorkflow(). | ||
WaitForWorkflow(fixtures.ToBePending).GetWorkflow() | ||
t := s.T() | ||
assert.Equal(t, "Workflow processing has been postponed because too many workflows are already running", wf.Status.Message) | ||
} | ||
|
||
func TestNamespaceParallelismSuite(t *testing.T) { | ||
suite.Run(t, new(NamespaceParallelismSuite)) | ||
} |
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,141 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"time" | ||
|
||
"errors" | ||
|
||
"github.com/sirupsen/logrus" | ||
apiv1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/selection" | ||
"k8s.io/apimachinery/pkg/watch" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/cache" | ||
|
||
"github.com/argoproj/argo-workflows/v3/workflow/common" | ||
) | ||
|
||
var ( | ||
limitReq, _ = labels.NewRequirement(common.LabelNamespaceLimit, selection.Exists, nil) | ||
nsResyncPeriod = 5 * time.Minute | ||
errUnableToExtract = errors.New("was unable to extract limit") | ||
) | ||
|
||
type updateFunc = func(string, int) | ||
type resetFunc = func(string) | ||
|
||
func (wfc *WorkflowController) newNamespaceInformer(ctx context.Context, kubeclientset kubernetes.Interface) (cache.SharedIndexInformer, error) { | ||
|
||
c := kubeclientset.CoreV1().Namespaces() | ||
logger := logrus.WithField("scope", "ns_watcher") | ||
|
||
labelSelector := labels.NewSelector(). | ||
Add(*limitReq) | ||
|
||
listFunc := func(opts metav1.ListOptions) (runtime.Object, error) { | ||
opts.LabelSelector = labelSelector.String() | ||
return c.List(ctx, opts) | ||
} | ||
|
||
watchFunc := func(opts metav1.ListOptions) (watch.Interface, error) { | ||
opts.Watch = true | ||
opts.LabelSelector = labelSelector.String() | ||
return c.Watch(ctx, opts) | ||
} | ||
|
||
source := &cache.ListWatch{ListFunc: listFunc, WatchFunc: watchFunc} | ||
informer := cache.NewSharedIndexInformer(source, &apiv1.Namespace{}, nsResyncPeriod, cache.Indexers{}) | ||
|
||
_, err := informer.AddEventHandler( | ||
cache.ResourceEventHandlerFuncs{ | ||
AddFunc: func(obj interface{}) { | ||
ns, err := nsFromObj(obj) | ||
if err != nil { | ||
return | ||
} | ||
updateNS(logger, ns, wfc.throttler.UpdateNamespaceParallelism) | ||
}, | ||
|
||
UpdateFunc: func(old, newVal interface{}) { | ||
ns, err := nsFromObj(newVal) | ||
if err != nil { | ||
return | ||
} | ||
oldNs, err := nsFromObj(old) | ||
if err == nil && !limitChanged(oldNs, ns) { | ||
return | ||
} | ||
updateNS(logger, ns, wfc.throttler.UpdateNamespaceParallelism) | ||
}, | ||
|
||
DeleteFunc: func(obj interface{}) { | ||
ns, err := nsFromObj(obj) | ||
if err != nil { | ||
return | ||
} | ||
deleteNS(logger, ns, wfc.throttler.ResetNamespaceParallelism) | ||
}, | ||
}, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return informer, nil | ||
} | ||
|
||
func deleteNS(log *logrus.Entry, ns *apiv1.Namespace, resetFn resetFunc) { | ||
log.Infof("reseting the namespace parallelism limits for %s due to deletion event", ns.Name) | ||
resetFn(ns.Name) | ||
} | ||
|
||
func updateNS(log *logrus.Entry, ns *apiv1.Namespace, updateFn updateFunc) { | ||
limit, err := extractLimit(ns) | ||
if errors.Is(err, errUnableToExtract) { | ||
return | ||
} else if err != nil { | ||
log.Errorf("was unable to extract the limit due to: %s", err) | ||
return | ||
} | ||
log.Infof("changing namespace parallelism in %s to %d", ns.Name, limit) | ||
updateFn(ns.Name, limit) | ||
} | ||
|
||
func nsFromObj(obj interface{}) (*apiv1.Namespace, error) { | ||
ns, ok := obj.(*apiv1.Namespace) | ||
if !ok { | ||
return nil, errors.New("was unable to convert to namespace") | ||
} | ||
return ns, nil | ||
} | ||
|
||
func limitChanged(old *apiv1.Namespace, newNS *apiv1.Namespace) bool { | ||
oldLimit := old.GetLabels()[common.LabelNamespaceLimit] | ||
newLimit := newNS.GetLabels()[common.LabelNamespaceLimit] | ||
return !(oldLimit == newLimit) | ||
} | ||
|
||
func extractLimit(ns *apiv1.Namespace) (int, error) { | ||
labels := ns.GetLabels() | ||
var limitString *string | ||
|
||
for lbl, value := range labels { | ||
if lbl == common.LabelNamespaceLimit { | ||
limitString = &value | ||
break | ||
} | ||
} | ||
if limitString == nil { | ||
return 0, errUnableToExtract | ||
} | ||
|
||
integerValue, err := strconv.Atoi(*limitString) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return integerValue, nil | ||
} |
Oops, something went wrong.