Repository: fission/fission (language: go)

Issue:
poolmgr cannot create deployment due to long name
<!-- Please answer these questions before submitting your issue. Thanks! -->

<!-- Documentation URL: https://fission.io/docs -->
<!-- Troubleshooting guide: https://fission.io/docs/trouble-shooting/ -->

**Fission/Kubernetes version**
1.15.1/1.20.15+k3s1

**Kubernetes platform (e.g. Google Kubernetes Engine)**
k3s

**Describe the bug**
<!--A clear and concise description of what the bug is.-->
Poolmgr deployment can fail to be created if the environment name or namespace the environment is in is too long.

**To Reproduce**

<!-- Please provide steps for reproducing the error. -->
1. Create an environment with a long name (mine is 50 characters)
2. Run a function in said environment

**Expected result**
<!--A clear and concise description of what you expected to happen.-->
poolmgr deployment is created

**Actual result**
deployment fails to create

**Screenshots/Dump file**
<!--If applicable, add screenshots/fission dump file to help explain your problem.-->
The problem is basically the same as for #2009 
https://github.com/fission/fission/blob/a1cbce810ebce938aefb8d9c883d3942c8d7a01c/pkg/executor/executortype/poolmgr/gp_deployment.go#L37

If either the environment name or the namespace are long and combine to be too long the deployment name exceeds the limit.

<pre>
{"level":"error","ts":"2022-03-11T03:56:18.634Z","logger":"triggerset.http_trigger_set.XXX-XXX","caller":"httputil/reverseproxy.go:295","msg":"error sending request to function","trace_id":"f86fc17feb32cb1f8f23d3e2a6313262","error":" - Internal error - [xxx-xxx] error creating service for function: Deployment.apps \"poolmgr-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-default-33243\" is invalid: spec.template.spec.containers[2].image: Required value","function":{"namespace":"default","name":"xxx-xxx"},"status":"Internal Server Error","code":500,"stacktrace":"net/http/httputil.(*ReverseProxy).ServeHTTP\n\t/usr/local/Cellar/go@1.16/1.16.11/libexec/src/net/http/httputil/reverseproxy.go:295\ngithub.com/fission/fission/pkg/router.functionHandler.handler\n\tpkg/router/functionHandler.go:529\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/Cellar/go@1.16/1.16.11/libexec/src/net/http/server.go:2050\ngo.opentelemetry.io/contrib/instrumentation/net/http/otelhttp.(*Handler).ServeHTTP\n\t/Users/sanket/go/pkg/mod/go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp@v0.26.0/handler.go:179\ngithub.com/gorilla/mux.(*Router).ServeHTTP\n\t/Users/sanket/go/pkg/mod/github.com/gorilla/mux@v1.8.0/mux.go:210\ngithub.com/fission/fission/pkg/router.(*mutableRouter).ServeHTTP\n\tpkg/router/mutablemux.go:52\ngo.opentelemetry.io/contrib/instrumentation/net/http/otelhttp.(*Handler).ServeHTTP\n\t/Users/sanket/go/pkg/mod/go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp@v0.26.0/handler.go:179\nnet/http.serverHandler.ServeHTTP\n\t/usr/local/Cellar/go@1.16/1.16.11/libexec/src/net/http/server.go:2868\nnet/http.(*conn).serve\n\t/usr/local/Cellar/go@1.16/1.16.11/libexec/src/net/http/server.go:1933"}
</pre>

**Additional context**
I don't recommend relying on any specific convention/naming pattern for identifying an object. It would be better to use labels to identify.



Discussion / hints:

Fix patch:
diff --git a/pkg/executor/executortype/poolmgr/gp_deployment.go b/pkg/executor/executortype/poolmgr/gp_deployment.go
index cfccc51ac0..4696c2d2e9 100644
--- a/pkg/executor/executortype/poolmgr/gp_deployment.go
+++ b/pkg/executor/executortype/poolmgr/gp_deployment.go
@@ -32,16 +32,34 @@ import (
 )
 
 // getPoolName returns a unique name of an environment
-func (gp *GenericPool) getPoolName(env *fv1.Environment) string {
+func getPoolName(env *fv1.Environment) string {
 	// TODO: get rid of resource version here
-	return strings.ToLower(fmt.Sprintf("poolmgr-%v-%v-%v", env.ObjectMeta.Name, env.ObjectMeta.Namespace, env.ObjectMeta.ResourceVersion))
+	var envPodName string
+
+	min := func(a, b int) int {
+		if a > b {
+			return b
+		}
+		return a
+	}
+
+	//To fit the 63 character limit
+	if len(env.ObjectMeta.Name)+len(env.ObjectMeta.Namespace) < 37 {
+		envPodName = env.ObjectMeta.Name + "-" + env.ObjectMeta.Namespace
+	} else {
+		nameLength := min(len(env.ObjectMeta.Name), 18)
+		namespaceLength := min(len(env.ObjectMeta.Namespace), 18)
+		envPodName = env.ObjectMeta.Name[:nameLength] + "-" + env.ObjectMeta.Namespace[:namespaceLength]
+	}
+
+	return "poolmgr-" + strings.ToLower(fmt.Sprintf("%s-%s", envPodName, env.ResourceVersion))
 }
 
 func (gp *GenericPool) genDeploymentMeta(env *fv1.Environment) metav1.ObjectMeta {
 	deployLabels := gp.getEnvironmentPoolLabels(env)
 	deployAnnotations := gp.getDeployAnnotations(env)
 	return metav1.ObjectMeta{
-		Name:        gp.getPoolName(env),
+		Name:        getPoolName(env),
 		Labels:      deployLabels,
 		Annotations: deployAnnotations,
 	}


Test patch:
diff --git a/pkg/executor/executortype/poolmgr/gp_deployment_test.go b/pkg/executor/executortype/poolmgr/gp_deployment_test.go
new file mode 100644
index 0000000000..a0eb3d3813
--- /dev/null
+++ b/pkg/executor/executortype/poolmgr/gp_deployment_test.go
@@ -0,0 +1,74 @@
+/*
+Copyright 2016 The Fission Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package poolmgr
+
+import (
+	"fmt"
+	"testing"
+
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+
+	fv1 "github.com/fission/fission/pkg/apis/core/v1"
+)
+
+func TestGetPoolName(t *testing.T) {
+	tests := []struct {
+		name string
+		env  *fv1.Environment
+		want string
+	}{
+		{
+			"Under character limit",
+			&fv1.Environment{
+				TypeMeta: metav1.TypeMeta{
+					Kind:       fv1.CRD_NAME_ENVIRONMENT,
+					APIVersion: fv1.CRD_VERSION,
+				},
+				ObjectMeta: metav1.ObjectMeta{
+					Name:            "test",
+					Namespace:       "testns",
+					ResourceVersion: "2517",
+				},
+			},
+			"poolmgr-test-testns-2517",
+		},
+		{
+			"Over character limit",
+			&fv1.Environment{
+				TypeMeta: metav1.TypeMeta{
+					Kind:       fv1.CRD_NAME_ENVIRONMENT,
+					APIVersion: fv1.CRD_VERSION,
+				},
+				ObjectMeta: metav1.ObjectMeta{
+					Name:            "justtryingtoincreasethenumberofcharactersinthisstring",
+					Namespace:       "checkingifthegetpoolfunctionworkswithcharactersmorethan18",
+					ResourceVersion: "2518",
+				},
+			},
+			"poolmgr-justtryingtoincrea-checkingifthegetpo-2518",
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if got := getPoolName(tt.env); got != tt.want {
+				t.Errorf("getPoolName() = %s, want = %s len(getPoolName()) = %x len(want) = %x", got, tt.want, len(got), len(tt.want))
+			} else {
+				fmt.Printf("getPoolName() = %s,length of string = %x", got, len(got))
+			}
+		})
+	}
+}