Repository: argoproj/argo
Pull request #4628 (2020-12-02 16:10:38)

fix: Count Workflows with no phase as Pending for metrics
Fixes https://github.com/argoproj/argo/issues/4551

Signed-off-by: Simon Behar <simbeh7@gmail.com>

Checklist:

* [ ] My organization is added to [USERS.md](https://github.com/argoproj/argo/blob/master/USERS.md).

<!--

Please leave your PR in draft if you don't need a review yet. 

To fix failing `CI / Codegen`, run `make codegen`. 

-->



Patch:
diff --git a/workflow/controller/controller.go b/workflow/controller/controller.go
index 40871c9ee..27571f386 100644
--- a/workflow/controller/controller.go
+++ b/workflow/controller/controller.go
@@ -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
diff --git a/workflow/controller/indexes/labels.go b/workflow/controller/indexes/labels.go
index 9979830e9..7baab6a3c 100644
--- a/workflow/controller/indexes/labels.go
+++ b/workflow/controller/indexes/labels.go
@@ -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
 		}
 	}
 }


Test patch:
diff --git a/workflow/controller/indexes/labels_test.go b/workflow/controller/indexes/labels_test.go
index 758178ce7..201bf3fa4 100644
--- a/workflow/controller/indexes/labels_test.go
+++ b/workflow/controller/indexes/labels_test.go
@@ -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) {
@@ -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)})
+	})
 }