Repository: knative-sandbox/kn-plugin-func
Pull request #761 (2022-01-13 17:45:57)

Support git scp url format

knative-prow-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative-sandbox/kn-plugin-func/pull/761#" title="Author self-approved">matejvasek</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative-sandbox%2Fkn-plugin-func).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/knative-sandbox/kn-plugin-func/blob/main/OWNERS)~~ [matejvasek]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/function_git.go b/function_git.go
index b6a5a332..f35a539e 100644
--- a/function_git.go
+++ b/function_git.go
@@ -18,6 +18,9 @@ type Git struct {
 func validateGit(git Git, mandatoryGit bool) (errors []string) {
 	if git.URL != nil {
 		_, err := giturls.ParseTransport(*git.URL)
+		if err != nil {
+			_, err = giturls.ParseScp(*git.URL)
+		}
 		if err != nil {
 			errMsg := fmt.Sprintf("specified option \"git.url=%s\" is not valid", *git.URL)
 
diff --git a/k8s/labels/labels.go b/k8s/labels/labels.go
new file mode 100644
index 00000000..8fd8dee2
--- /dev/null
+++ b/k8s/labels/labels.go
@@ -0,0 +1,13 @@
+package labels
+
+const (
+	FunctionKey        = "function.knative.dev"
+	FunctionValue      = "true"
+	FunctionRuntimeKey = "function.knative.dev/runtime"
+	FunctionNameKey    = "function.knative.dev/name"
+
+	// --- handle usage of deprecated labels
+	DeprecatedFunctionKey        = "boson.dev/function"
+	DeprecatedFunctionRuntimeKey = "boson.dev/runtime"
+	// --- end of handling usage of deprecated runtime labels
+)
diff --git a/k8s/persistent_volumes.go b/k8s/persistent_volumes.go
index 54929968..465d05a7 100644
--- a/k8s/persistent_volumes.go
+++ b/k8s/persistent_volumes.go
@@ -17,7 +17,7 @@ func GetPersistentVolumeClaim(ctx context.Context, name, namespaceOverride strin
 	return client.CoreV1().PersistentVolumeClaims(namespace).Get(ctx, name, metav1.GetOptions{})
 }
 
-func CreatePersistentVolumeClaim(ctx context.Context, name, namespaceOverride string, accessMode corev1.PersistentVolumeAccessMode, resourceRequest resource.Quantity) (err error) {
+func CreatePersistentVolumeClaim(ctx context.Context, name, namespaceOverride string, labels map[string]string, accessMode corev1.PersistentVolumeAccessMode, resourceRequest resource.Quantity) (err error) {
 	client, namespace, err := NewClientAndResolvedNamespace(namespaceOverride)
 	if err != nil {
 		return
@@ -27,6 +27,7 @@ func CreatePersistentVolumeClaim(ctx context.Context, name, namespaceOverride st
 		ObjectMeta: metav1.ObjectMeta{
 			Name:      name,
 			Namespace: namespace,
+			Labels:    labels,
 		},
 		Spec: corev1.PersistentVolumeClaimSpec{
 			AccessModes: []corev1.PersistentVolumeAccessMode{accessMode},
diff --git a/k8s/role_bidings.go b/k8s/role_bidings.go
index a4db5bdd..290ddd08 100644
--- a/k8s/role_bidings.go
+++ b/k8s/role_bidings.go
@@ -7,7 +7,7 @@ import (
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 )
 
-func CreateRoleBindingForServiceAccount(ctx context.Context, name, namespaceOverride, serviceAccountName, roleKind, roleName string) (err error) {
+func CreateRoleBindingForServiceAccount(ctx context.Context, name, namespaceOverride string, labels map[string]string, serviceAccountName, roleKind, roleName string) (err error) {
 	client, namespace, err := NewClientAndResolvedNamespace(namespaceOverride)
 	if err != nil {
 		return
@@ -15,7 +15,8 @@ func CreateRoleBindingForServiceAccount(ctx context.Context, name, namespaceOver
 
 	rb := &rbacv1.RoleBinding{
 		ObjectMeta: metav1.ObjectMeta{
-			Name: name,
+			Name:   name,
+			Labels: labels,
 		},
 		Subjects: []rbacv1.Subject{
 			{
diff --git a/k8s/secrets.go b/k8s/secrets.go
index d906713e..04500d5e 100644
--- a/k8s/secrets.go
+++ b/k8s/secrets.go
@@ -36,7 +36,7 @@ func ListSecretsNames(ctx context.Context, namespaceOverride string) (names []st
 	return
 }
 
-func CreateDockerRegistrySecret(ctx context.Context, name, namespaceOverride, username, password, server string) (err error) {
+func CreateDockerRegistrySecret(ctx context.Context, name, namespaceOverride string, labels map[string]string, username, password, server string) (err error) {
 	client, namespace, err := NewClientAndResolvedNamespace(namespaceOverride)
 	if err != nil {
 		return
@@ -46,6 +46,7 @@ func CreateDockerRegistrySecret(ctx context.Context, name, namespaceOverride, us
 		ObjectMeta: metav1.ObjectMeta{
 			Name:      name,
 			Namespace: namespace,
+			Labels:    labels,
 		},
 		Type: corev1.SecretTypeDockerConfigJson,
 		Data: map[string][]byte{},
diff --git a/k8s/service_accounts.go b/k8s/service_accounts.go
index 3312f5fc..5f9a676b 100644
--- a/k8s/service_accounts.go
+++ b/k8s/service_accounts.go
@@ -7,7 +7,7 @@ import (
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 )
 
-func CreateServiceAccountWithSecret(ctx context.Context, name, namespaceOverride, secretName string) (err error) {
+func CreateServiceAccountWithSecret(ctx context.Context, name, namespaceOverride string, labels map[string]string, secretName string) (err error) {
 	client, namespace, err := NewClientAndResolvedNamespace(namespaceOverride)
 	if err != nil {
 		return
@@ -17,6 +17,7 @@ func CreateServiceAccountWithSecret(ctx context.Context, name, namespaceOverride
 		ObjectMeta: metav1.ObjectMeta{
 			Name:      name,
 			Namespace: namespace,
+			Labels:    labels,
 		},
 		Secrets: []corev1.ObjectReference{
 			{
diff --git a/knative/deployer.go b/knative/deployer.go
index cefc6795..77118747 100644
--- a/knative/deployer.go
+++ b/knative/deployer.go
@@ -22,6 +22,7 @@ import (
 
 	fn "knative.dev/kn-plugin-func"
 	"knative.dev/kn-plugin-func/k8s"
+	"knative.dev/kn-plugin-func/k8s/labels"
 )
 
 const LIVENESS_ENDPOINT = "/health/liveness"
@@ -349,10 +350,14 @@ func updateService(f fn.Function, newEnv []corev1.EnvVar, newEnvFrom []corev1.En
 //     value: {{ env:MY_ENV }}
 func processLabels(f fn.Function) (map[string]string, error) {
 	labels := map[string]string{
-		"boson.dev/function":           "true",
-		"boson.dev/runtime":            f.Runtime,
-		"function.knative.dev":         "true",
-		"function.knative.dev/runtime": f.Runtime,
+		labels.FunctionKey:        labels.FunctionValue,
+		labels.FunctionNameKey:    f.Name,
+		labels.FunctionRuntimeKey: f.Runtime,
+
+		// --- handle usage of deprecated labels (`boson.dev/function`, `boson.dev/runtime`)
+		labels.DeprecatedFunctionKey:        labels.FunctionValue,
+		labels.DeprecatedFunctionRuntimeKey: f.Runtime,
+		// --- end of handling usage of deprecated runtime labels
 	}
 	for _, label := range f.Labels {
 		if label.Key != nil && label.Value != nil {
diff --git a/knative/lister.go b/knative/lister.go
index 433bff7e..da8ad8aa 100644
--- a/knative/lister.go
+++ b/knative/lister.go
@@ -9,14 +9,7 @@ import (
 
 	fn "knative.dev/kn-plugin-func"
 	"knative.dev/kn-plugin-func/k8s"
-)
-
-const (
-	labelFunctionKey           = "function.knative.dev"
-	deprecatedLabelFunctionKey = "boson.dev/function"
-	labelFunctionValue         = "true"
-	labelRuntimeKey            = "function.knative.dev/runtime"
-	deprecatedLabelRuntimeKey  = "boson.dev/runtime"
+	"knative.dev/kn-plugin-func/k8s/labels"
 )
 
 type Lister struct {
@@ -43,13 +36,13 @@ func (l *Lister) List(ctx context.Context) (items []fn.ListItem, err error) {
 		return
 	}
 
-	lst, err := client.ListServices(ctx, clientservingv1.WithLabel(labelFunctionKey, labelFunctionValue))
+	lst, err := client.ListServices(ctx, clientservingv1.WithLabel(labels.FunctionKey, labels.FunctionValue))
 	if err != nil {
 		return
 	}
 
 	// --- handle usage of deprecated function labels (`boson.dev/function`)
-	lstDeprecated, err := client.ListServices(ctx, clientservingv1.WithLabel(deprecatedLabelFunctionKey, labelFunctionValue))
+	lstDeprecated, err := client.ListServices(ctx, clientservingv1.WithLabel(labels.DeprecatedFunctionKey, labels.FunctionValue))
 	if err != nil {
 		return
 	}
@@ -82,10 +75,10 @@ func (l *Lister) List(ctx context.Context) (items []fn.ListItem, err error) {
 
 		// --- handle usage of deprecated runtime labels (`boson.dev/runtime`)
 		runtimeLabel := ""
-		if val, ok := f.Labels[labelRuntimeKey]; ok {
+		if val, ok := f.Labels[labels.FunctionRuntimeKey]; ok {
 			runtimeLabel = val
 		} else {
-			runtimeLabel = f.Labels[deprecatedLabelRuntimeKey]
+			runtimeLabel = f.Labels[labels.DeprecatedFunctionRuntimeKey]
 		}
 		// --- end of handling usage of deprecated runtime labels
 
diff --git a/pipelines/resources/tekton/task/func-deploy/0.1/func-deploy.yaml b/pipelines/resources/tekton/task/func-deploy/0.1/func-deploy.yaml
index 4af24c14..a7aeee5c 100644
--- a/pipelines/resources/tekton/task/func-deploy/0.1/func-deploy.yaml
+++ b/pipelines/resources/tekton/task/func-deploy/0.1/func-deploy.yaml
@@ -26,7 +26,7 @@ spec:
     args:
       - "deploy"
       - "--verbose"
-      - "--build=false"
+      - "--build=disabled"
       - "--push=false"
       - "--path"
       - "$(params.path)"
diff --git a/pipelines/tekton/pipeplines_provider.go b/pipelines/tekton/pipeplines_provider.go
index 784cd9f0..b8a4969d 100644
--- a/pipelines/tekton/pipeplines_provider.go
+++ b/pipelines/tekton/pipeplines_provider.go
@@ -19,6 +19,7 @@ import (
 	fn "knative.dev/kn-plugin-func"
 	"knative.dev/kn-plugin-func/docker"
 	"knative.dev/kn-plugin-func/k8s"
+	"knative.dev/kn-plugin-func/k8s/labels"
 	"knative.dev/kn-plugin-func/knative"
 	"knative.dev/pkg/apis"
 )
@@ -83,14 +84,17 @@ func (pp *PipelinesProvider) Run(ctx context.Context, f fn.Function) error {
 		return err
 	}
 
-	err = k8s.CreatePersistentVolumeClaim(ctx, getPipelinePvcName(f), pp.namespace, corev1.ReadWriteOnce, *resource.NewQuantity(DefaultPersistentVolumeClaimSize, resource.DecimalSI))
+	// let's specify labels that will be applied to every resouce that is created for a Pipeline
+	labels := map[string]string{labels.FunctionNameKey: f.Name}
+
+	err = k8s.CreatePersistentVolumeClaim(ctx, getPipelinePvcName(f), pp.namespace, labels, corev1.ReadWriteOnce, *resource.NewQuantity(DefaultPersistentVolumeClaimSize, resource.DecimalSI))
 	if err != nil {
 		if !errors.IsAlreadyExists(err) {
 			return fmt.Errorf("problem creating persistent volume claim: %v", err)
 		}
 	}
 
-	_, err = client.Pipelines(pp.namespace).Create(ctx, generatePipeline(f), metav1.CreateOptions{})
+	_, err = client.Pipelines(pp.namespace).Create(ctx, generatePipeline(f, labels), metav1.CreateOptions{})
 	if err != nil {
 		if !errors.IsAlreadyExists(err) {
 			if errors.IsNotFound(err) {
@@ -118,7 +122,7 @@ func (pp *PipelinesProvider) Run(ctx context.Context, f fn.Function) error {
 			registry = authn.DefaultAuthKey
 		}
 
-		err = k8s.CreateDockerRegistrySecret(ctx, getPipelineSecretName(f), pp.namespace, creds.Username, creds.Password, registry)
+		err = k8s.CreateDockerRegistrySecret(ctx, getPipelineSecretName(f), pp.namespace, labels, creds.Username, creds.Password, registry)
 		if err != nil {
 			return err
 		}
@@ -126,7 +130,7 @@ func (pp *PipelinesProvider) Run(ctx context.Context, f fn.Function) error {
 		return fmt.Errorf("problem in creating secret: %v", err)
 	}
 
-	err = k8s.CreateServiceAccountWithSecret(ctx, getPipelineBuilderServiceAccountName(f), pp.namespace, getPipelineSecretName(f))
+	err = k8s.CreateServiceAccountWithSecret(ctx, getPipelineBuilderServiceAccountName(f), pp.namespace, labels, getPipelineSecretName(f))
 	if err != nil {
 		if !errors.IsAlreadyExists(err) {
 			return fmt.Errorf("problem in creating service account: %v", err)
@@ -134,7 +138,7 @@ func (pp *PipelinesProvider) Run(ctx context.Context, f fn.Function) error {
 	}
 
 	// using ClusterRole `knative-serving-namespaced-admin` that should be present on the cluster after the installation of Knative Serving
-	err = k8s.CreateRoleBindingForServiceAccount(ctx, getPipelineDeployerRoleBindingName(f), pp.namespace, getPipelineBuilderServiceAccountName(f), "ClusterRole", "knative-serving-namespaced-admin")
+	err = k8s.CreateRoleBindingForServiceAccount(ctx, getPipelineDeployerRoleBindingName(f), pp.namespace, labels, getPipelineBuilderServiceAccountName(f), "ClusterRole", "knative-serving-namespaced-admin")
 	if err != nil {
 		if !errors.IsAlreadyExists(err) {
 			return fmt.Errorf("problem in creating role biding: %v", err)
@@ -142,7 +146,7 @@ func (pp *PipelinesProvider) Run(ctx context.Context, f fn.Function) error {
 	}
 
 	pp.progressListener.Increment("Running Pipeline with the Function")
-	pr, err := client.PipelineRuns(pp.namespace).Create(ctx, generatePipelineRun(f), metav1.CreateOptions{})
+	pr, err := client.PipelineRuns(pp.namespace).Create(ctx, generatePipelineRun(f, labels), metav1.CreateOptions{})
 	if err != nil {
 		return fmt.Errorf("problem in creating pipeline run: %v", err)
 	}
diff --git a/pipelines/tekton/resources.go b/pipelines/tekton/resources.go
index 1a19f5cd..ad41184a 100644
--- a/pipelines/tekton/resources.go
+++ b/pipelines/tekton/resources.go
@@ -10,7 +10,7 @@ import (
 	fn "knative.dev/kn-plugin-func"
 )
 
-func generatePipeline(f fn.Function) *pplnv1beta1.Pipeline {
+func generatePipeline(f fn.Function, labels map[string]string) *pplnv1beta1.Pipeline {
 	pipelineName := getPipelineName(f)
 
 	params := []pplnv1beta1.ParamSpec{
@@ -52,7 +52,8 @@ func generatePipeline(f fn.Function) *pplnv1beta1.Pipeline {
 
 	return &pplnv1beta1.Pipeline{
 		ObjectMeta: v1.ObjectMeta{
-			Name: pipelineName,
+			Name:   pipelineName,
+			Labels: labels,
 		},
 		Spec: pplnv1beta1.PipelineSpec{
 			Params:     params,
@@ -62,7 +63,7 @@ func generatePipeline(f fn.Function) *pplnv1beta1.Pipeline {
 	}
 }
 
-func generatePipelineRun(f fn.Function) *pplnv1beta1.PipelineRun {
+func generatePipelineRun(f fn.Function, labels map[string]string) *pplnv1beta1.PipelineRun {
 
 	revision := ""
 	if f.Git.Revision != nil {
@@ -76,6 +77,7 @@ func generatePipelineRun(f fn.Function) *pplnv1beta1.PipelineRun {
 	return &pplnv1beta1.PipelineRun{
 		ObjectMeta: v1.ObjectMeta{
 			GenerateName: fmt.Sprintf("%s-run-", getPipelineName(f)),
+			Labels:       labels,
 		},
 
 		Spec: pplnv1beta1.PipelineRunSpec{


Test patch:
diff --git a/function_git_unit_test.go b/function_git_unit_test.go
index 0f155523..4ac34414 100644
--- a/function_git_unit_test.go
+++ b/function_git_unit_test.go
@@ -15,13 +15,21 @@ func Test_validateGit(t *testing.T) {
 		errs         int
 	}{
 		{
-			"correct 'Git - only URL",
+			"correct 'Git - only URL https",
 			Git{
 				URL: ptr.String("https://myrepo/foo.git"),
 			},
 			true,
 			0,
 		},
+		{
+			"correct 'Git - only URL scp",
+			Git{
+				URL: ptr.String("git@myrepo:foo.git"),
+			},
+			true,
+			0,
+		},
 		{
 			"correct 'Git - URL + revision",
 			Git{