Repository: argoproj/argo (go)

Issue:
Unwanted (and invalid) metadata.label when creating wftmpl via argo template create
## Summary 

When creating templates via `argo template create` a label is added to the workflowtemplate which is not in the yaml file. Because the label is in an invalid format, the creation of the workflowtemplate fails.  Using `kubectl apply -f` does succeed in creating the templates. I have only noticed this issue inside a `gitlab-runner` process (via a gitlab deploy job). Likely this issue is due to the combination of the gitlab-runner environment and `argo template create`.

Gitlab sets up a specific k8s environment in the gitlab-runner and (used to) use labels for tracking the k8s resources that are created by gitlab. Likely the label is added to track that these templates were created via a gitlab deployment. Still I find it odd that in the same environment kubectl is able to create the templates, whereas argo is not. It is not clear to me how and where these labels are being added. Is this configured on the client side or is this configured on the server; how could I check (I realize this is a gitlab specific question, not argo related). Any pointers on how to debug this issue further are welcomed.

## Diagnostics
```
$ argo template list -n default
NAME
$ kubectl apply -f https://raw.githubusercontent.com/argoproj/argo/master/examples/workflow-template/templates.yaml -n default
workflowtemplate.argoproj.io/workflow-template-whalesay-template created
workflowtemplate.argoproj.io/workflow-template-random-fail-template created
workflowtemplate.argoproj.io/workflow-template-inner-steps created
workflowtemplate.argoproj.io/workflow-template-inner-dag created
workflowtemplate.argoproj.io/workflow-template-submittable created
$ argo template delete --all -n default
WorkflowTemplate 'workflow-template-inner-dag' deleted
WorkflowTemplate 'workflow-template-inner-steps' deleted
WorkflowTemplate 'workflow-template-random-fail-template' deleted
WorkflowTemplate 'workflow-template-submittable' deleted
WorkflowTemplate 'workflow-template-whalesay-template' deleted
$ argo template create https://raw.githubusercontent.com/argoproj/argo/master/examples/workflow-template/templates.yaml -n default -v
time="2020-09-17T14:05:18+02:00" level=debug msg="CLI version" version="{v2.10.1 2020-09-02T22:53:49Z 854444e47ac00d146cb83d174049bfbb2066bfb2 v2.10.1 clean go1.13.4 gc linux/amd64}"
time="2020-09-17T14:05:18+02:00" level=debug msg="Client options" opts="{{ false false}  0x17084a0 <nil> 0x17084f0}"
time="2020-09-17T14:05:19+02:00" level=debug msg="Getting the template by name"
time="2020-09-17T14:05:19+02:00" level=debug msg="Resolving the template" base="*v1alpha1.Workflow (namespace=,name=)" depth=0 tmpl="*v1alpha1.WorkflowStep (whalesay-template)"
time="2020-09-17T14:05:19+02:00" level=debug msg="Getting the template" base="*v1alpha1.Workflow (namespace=,name=)" depth=0 tmpl="*v1alpha1.WorkflowStep (whalesay-template)"
time="2020-09-17T14:05:19+02:00" level=debug msg="Getting the template by name" base="*v1alpha1.Workflow (namespace=,name=)" depth=0 tmpl="*v1alpha1.WorkflowStep (whalesay-template)"
time="2020-09-17T14:05:19+02:00" level=debug msg="Getting the template by name" base="*v1alpha1.Workflow (namespace=,name=)" depth=0 tmpl="*v1alpha1.WorkflowStep (whalesay-template)"
time="2020-09-17T14:05:19+02:00" level=debug msg="Resolving the template" base="*v1alpha1.Workflow (namespace=,name=)" depth=0 tmpl="*v1alpha1.WorkflowStep (whalesay-template)"
time="2020-09-17T14:05:19+02:00" level=debug msg="Getting the template" base="*v1alpha1.Workflow (namespace=,name=)" depth=0 tmpl="*v1alpha1.WorkflowStep (whalesay-template)"
time="2020-09-17T14:05:19+02:00" level=debug msg="Getting the template by name" base="*v1alpha1.Workflow (namespace=,name=)" depth=0 tmpl="*v1alpha1.WorkflowStep (whalesay-template)"
2020/09/17 14:05:19 Failed to create workflow template: rpc error: code = InvalidArgument desc = WorkflowTemplate.argoproj.io "workflow-template-whalesay-template" is invalid: metadata.labels: Invalid value: "-21007791-staging-imagemanager-21007791-staging-service-account": a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyValue',  or 'my_value',  or '12345', regex used for validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?')
ERROR: Job failed: exit status 1
FATAL: exit status 1
```



```
❯ argo version
argo: v2.10.1
  BuildDate: 2020-09-02T22:53:49Z
  GitCommit: 854444e47ac00d146cb83d174049bfbb2066bfb2
  GitTreeState: clean
  GitTag: v2.10.1
  GoVersion: go1.13.4
  Compiler: gc
  Platform: linux/amd64
```

```yaml
Paste the workflow here, including status:
Just using templates from https://raw.githubusercontent.com/argoproj/argo/master/examples/workflow-template/templates.yaml
```

```
Paste the logs from the workflow controller:
Nothing relevant in status messages, as creation fails already in k8s.
```

---
<!-- Issue Author: Don't delete this message to encourage other users to support your issue! -->
**Message from the maintainers**:

Impacted by this bug? Give it a 👍. We prioritise the issues with the most 👍.


Pull request description:
fix(controller): Deal with hyphen in creator. Fixes #4058
Signed-off-by: Alex Collins <alex_collins@intuit.com>
Fixes #4058

Checklist:

* [x] 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/creator/creator.go b/workflow/creator/creator.go
index 2176c74b0..5a8ffc4ed 100644
--- a/workflow/creator/creator.go
+++ b/workflow/creator/creator.go
@@ -3,6 +3,7 @@ package creator
 import (
 	"context"
 	"regexp"
+	"strings"
 
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 
@@ -18,6 +19,7 @@ func Label(ctx context.Context, obj metav1.Object) {
 		if len(value) > 63 {
 			value = value[len(value)-63:]
 		}
+		value = strings.TrimLeft(value, "-")
 		labels.Label(obj, common.LabelKeyCreator, value)
 	}
 }


Test patch:
diff --git a/workflow/creator/creator_test.go b/workflow/creator/creator_test.go
index cdcdfa4d6..caf6db199 100644
--- a/workflow/creator/creator_test.go
+++ b/workflow/creator/creator_test.go
@@ -35,4 +35,11 @@ func TestLabel(t *testing.T) {
 			assert.Equal(t, strings.Repeat("x", 62)+"y", wf.Labels[common.LabelKeyCreator])
 		}
 	})
+	t.Run("TooLongHyphen", func(t *testing.T) {
+		wf := &wfv1.Workflow{}
+		Label(context.WithValue(context.TODO(), auth.ClaimsKey, &types.Claims{Claims: jwt.Claims{Subject: strings.Repeat("-", 63) + "y"}}), wf)
+		if assert.NotEmpty(t, wf.Labels) {
+			assert.Equal(t, "y", wf.Labels[common.LabelKeyCreator])
+		}
+	})
 }