Skip to content

Commit

Permalink
fix: Count Workflows with no phase as Pending for metrics (#4628)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Behar <[email protected]>
  • Loading branch information
simster7 committed Dec 3, 2020
1 parent a2566b9 commit 6175458
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion workflow/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ var indexers = cache.Indexers{
indexes.CronWorkflowIndex: indexes.MetaNamespaceLabelIndexFunc(common.LabelKeyCronWorkflow),
indexes.WorkflowTemplateIndex: indexes.MetaNamespaceLabelIndexFunc(common.LabelKeyWorkflowTemplate),
indexes.SemaphoreConfigIndexName: indexes.WorkflowSemaphoreKeysIndexFunc(),
indexes.WorkflowPhaseIndex: indexes.MetaLabelIndexFunc(common.LabelKeyPhase),
indexes.WorkflowPhaseIndex: indexes.MetaWorkflowPhaseIndexFunc(),
}

// Run starts an Workflow resource controller
Expand Down
10 changes: 7 additions & 3 deletions workflow/controller/indexes/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@ import (

"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/client-go/tools/cache"

"github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo/workflow/common"
)

func MetaNamespaceLabelIndex(namespace, label string) string {
return namespace + "/" + label
}

func MetaLabelIndexFunc(label string) cache.IndexFunc {
func MetaWorkflowPhaseIndexFunc() cache.IndexFunc {
return func(obj interface{}) ([]string, error) {
v, err := meta.Accessor(obj)
if err != nil {
return []string{}, fmt.Errorf("object has no meta: %v", err)
}
if value, exists := v.GetLabels()[label]; exists {
if value, exists := v.GetLabels()[common.LabelKeyPhase]; exists {
return []string{value}, nil
} else {
return []string{}, nil
// If the object doesn't have a phase set, consider it pending
return []string{string(v1alpha1.NodePending)}, nil
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions workflow/controller/indexes/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo/workflow/common"
)

func TestMetaNamespaceLabelIndex(t *testing.T) {
Expand All @@ -30,13 +31,21 @@ func TestMetaNamespaceLabelIndexFunc(t *testing.T) {
assert.ElementsMatch(t, values, []string{"my-ns/my-value"})
})
t.Run("Labelled No Namespace", func(t *testing.T) {
values, err := MetaLabelIndexFunc("my-label")(&wfv1.Workflow{
values, err := MetaWorkflowPhaseIndexFunc()(&wfv1.Workflow{
ObjectMeta: metav1.ObjectMeta{
Namespace: "my-ns",
Labels: map[string]string{"my-label": "my-value"},
Labels: map[string]string{common.LabelKeyPhase: "my-value"},
},
})
assert.NoError(t, err)
assert.ElementsMatch(t, values, []string{"my-value"})
})

t.Run("Labelled No Phase", func(t *testing.T) {
values, err := MetaWorkflowPhaseIndexFunc()(&wfv1.Workflow{
ObjectMeta: metav1.ObjectMeta{},
})
assert.NoError(t, err)
assert.ElementsMatch(t, values, []string{string(wfv1.NodePending)})
})
}

0 comments on commit 6175458

Please sign in to comment.