-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Signed-off-by: isubasinghe <[email protected]> Signed-off-by: Isitha Subasinghe <[email protected]>
- Loading branch information
1 parent
e9e7c43
commit dbfedbd
Showing
12 changed files
with
325 additions
and
1 deletion.
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,60 @@ | ||
//go:build functional | ||
|
||
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: "argoproj/argosay:v2" | ||
command: [sleep] | ||
args: ["60"] | ||
` | ||
|
||
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,143 @@ | ||
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.LabelParallelismLimit, 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, wfc.throttler.ResetNamespaceParallelism) | ||
}, | ||
|
||
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, wfc.throttler.ResetNamespaceParallelism) | ||
}, | ||
|
||
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, resetFn resetFunc) { | ||
limit, err := extractLimit(ns) | ||
if errors.Is(err, errUnableToExtract) { | ||
resetFn(ns.Name) | ||
log.Infof("removing per-namespace parallelism for %s, reverting to default", ns.Name) | ||
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.LabelParallelismLimit] | ||
newLimit := newNS.GetLabels()[common.LabelParallelismLimit] | ||
return !(oldLimit == newLimit) | ||
} | ||
|
||
func extractLimit(ns *apiv1.Namespace) (int, error) { | ||
labels := ns.GetLabels() | ||
var limitString *string | ||
|
||
for lbl, value := range labels { | ||
if lbl == common.LabelParallelismLimit { | ||
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.