Repository: fission/fission
Pull request #2482 (2022-07-12 11:45:44)

Generate package within 63 character limit when creating function
<!--  Thanks for sending a pull request! We request you provide detailed description as much as possible. -->

## Description
While creating a package if package name is more than 63 characters, then package get failed.
This fix will allow to create a package even if package name is more than 63 characters by taking first 26 characters as package name and appending unique id to it, and will trim rest of the characters of package name.

## Which issue(s) this PR fixes:

Fixes # https://fissionio.slack.com/archives/C3LUX6BBP/p1657148131481119

## Testing
Created a simple function with function name less than 63 characters.
Created a function with function name more than 63 characters.
Created a function using spec with function name less than 63 characters.
Created a function using spec with function name more than 63 characters.
Created a package manually with package name less than 63 characters.
Created a package manually with package name more than 63 characters.

## Checklist:
<!-- Please tick following checkboxes as per your understanding. -->
- [x] I ran tests as well as code linting locally to verify my changes. 
- [x] I have done manual verification of my changes, changes working as expected.
- [x] I have added new tests to cover my changes.
- [x] My changes follow contributing guidelines of Fission.
- [ ] I have signed all of my commits.


codecov[bot]: # [Codecov](https://codecov.io/gh/fission/fission/pull/2482?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fission) Report
> Merging [#2482](https://codecov.io/gh/fission/fission/pull/2482?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fission) (5fbf99e) into [master](https://codecov.io/gh/fission/fission/commit/583834059469df2d266abb4abd5fc9f5ea88a955?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fission) (5838340) will **increase** coverage by `0.13%`.
> The diff coverage is `42.85%`.

```diff
@@            Coverage Diff             @@
##           master    #2482      +/-   ##
==========================================
+ Coverage   18.97%   19.10%   +0.13%     
==========================================
  Files          62       62              
  Lines        6872     6829      -43     
==========================================
+ Hits         1304     1305       +1     
+ Misses       5463     5420      -43     
+ Partials      105      104       -1     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `19.10% <42.85%> (+0.13%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fission#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/fission/fission/pull/2482?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fission) | Coverage Δ | |
|---|---|---|
| [pkg/fission-cli/cmd/function/create.go](https://codecov.io/gh/fission/fission/pull/2482/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fission#diff-cGtnL2Zpc3Npb24tY2xpL2NtZC9mdW5jdGlvbi9jcmVhdGUuZ28=) | `25.94% <42.85%> (+0.30%)` | :arrow_up: |
| [pkg/router/router.go](https://codecov.io/gh/fission/fission/pull/2482/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fission#diff-cGtnL3JvdXRlci9yb3V0ZXIuZ28=) | `0.00% <0.00%> (ø)` | |
| [pkg/storagesvc/storagesvc.go](https://codecov.io/gh/fission/fission/pull/2482/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fission#diff-cGtnL3N0b3JhZ2VzdmMvc3RvcmFnZXN2Yy5nbw==) | `0.00% <0.00%> (ø)` | |
| [pkg/storagesvc/stowClient.go](https://codecov.io/gh/fission/fission/pull/2482/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fission#diff-cGtnL3N0b3JhZ2VzdmMvc3Rvd0NsaWVudC5nbw==) | `0.00% <0.00%> (ø)` | |
| [pkg/router/functionHandler.go](https://codecov.io/gh/fission/fission/pull/2482/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fission#diff-cGtnL3JvdXRlci9mdW5jdGlvbkhhbmRsZXIuZ28=) | `10.92% <0.00%> (+0.02%)` | :arrow_up: |
| [pkg/router/httpTriggers.go](https://codecov.io/gh/fission/fission/pull/2482/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fission#diff-cGtnL3JvdXRlci9odHRwVHJpZ2dlcnMuZ28=) | `0.76% <0.00%> (+0.02%)` | :arrow_up: |
| [pkg/storagesvc/client/client.go](https://codecov.io/gh/fission/fission/pull/2482/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fission#diff-cGtnL3N0b3JhZ2VzdmMvY2xpZW50L2NsaWVudC5nbw==) | `49.24% <0.00%> (+0.33%)` | :arrow_up: |

:mega: Codecov can now indicate which changes are the most critical in Pull Requests. [Learn more](https://about.codecov.io/product/feature/runtime-insights/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fission)


Patch:
diff --git a/pkg/fission-cli/cmd/function/create.go b/pkg/fission-cli/cmd/function/create.go
index 430198a8..ba008c94 100644
--- a/pkg/fission-cli/cmd/function/create.go
+++ b/pkg/fission-cli/cmd/function/create.go
@@ -221,7 +221,7 @@ func (opts *CreateSubCommand) complete(input cli.Input) error {
 		if err != nil {
 			return errors.Wrap(err, "error generating uuid")
 		}
-		pkgName := fmt.Sprintf("%v-%v", fnName, id.String())
+		pkgName := generatePackageName(fnName, id.String())
 
 		// create new package in the same namespace as the function.
 		pkgMetadata, err = _package.CreatePackage(input, opts.Client(), pkgName, fnNamespace, envName, envNamespace,
@@ -324,6 +324,23 @@ func (opts *CreateSubCommand) complete(input cli.Input) error {
 	return nil
 }
 
+// generatePackgeName => will return package name by appending id in function name and will make sure that package name will never be more than length of 63 characters.
+func generatePackageName(fnName string, id string) string {
+	var (
+		lenFnName       int = len(fnName)
+		lenId           int = len(id)
+		lastIndexOfChar int
+	)
+	if lenFnName+lenId <= 62 {
+		return fmt.Sprintf("%v-%v", fnName, id)
+	}
+
+	lastIndexOfChar = lenFnName - (lenFnName + lenId - 62)
+	pkgName := fmt.Sprintf("%v-%v", fnName[:lastIndexOfChar], id)
+	console.Info(fmt.Sprintf("Generated package %v from function to acceptable character limit", pkgName))
+	return pkgName
+}
+
 // run write the resource to a spec file or create a fission CRD with remote fission server.
 // It also prints warning/error if necessary.
 func (opts *CreateSubCommand) run(input cli.Input) error {
diff --git a/pkg/fission-cli/cmd/package/create.go b/pkg/fission-cli/cmd/package/create.go
index 833f7ac0..6fae780f 100644
--- a/pkg/fission-cli/cmd/package/create.go
+++ b/pkg/fission-cli/cmd/package/create.go
@@ -62,6 +62,7 @@ func (opts *CreateSubCommand) run(input cli.Input) error {
 			console.Warn(fmt.Sprintf("--%v will be soon marked as required flag, see 'help' for details", flagkey.HtName))
 		}
 	}
+
 	pkgNamespace := input.String(flagkey.NamespacePackage)
 	envName := input.String(flagkey.PkgEnvironment)
 	envNamespace := input.String(flagkey.NamespaceEnvironment)
@@ -85,6 +86,11 @@ func (opts *CreateSubCommand) run(input cli.Input) error {
 	var specDir, specFile string
 
 	if input.Bool(flagkey.SpecSave) {
+		// since package CRD created using --spec, not validate by k8s. So we need to validate it and make sure package name is not more than 63 characters.
+		if len(pkgName) > 63 {
+			return errors.Errorf("error creating package: package name %v, must be no more than 63 characters", pkgName)
+		}
+
 		specDir = util.GetSpecDir(input)
 		specIgnore := util.GetSpecIgnore(input)
 		fr, err := spec.ReadSpecs(specDir, specIgnore, false)


Test patch:
diff --git a/pkg/fission-cli/cmd/function/create_test.go b/pkg/fission-cli/cmd/function/create_test.go
new file mode 100644
index 00000000..0b6b2a72
--- /dev/null
+++ b/pkg/fission-cli/cmd/function/create_test.go
@@ -0,0 +1,42 @@
+package function
+
+import (
+	"testing"
+
+	uuid "github.com/satori/go.uuid"
+)
+
+func TestGeneratePackageName(t *testing.T) {
+	for _, test := range []struct {
+		name     string
+		fnName   string
+		expected int
+	}{
+		{
+			name:     "name of package should be less than or equal to 63 characters, if function name is equal to 26 characters",
+			fnName:   "test-function-with-26-char",
+			expected: 63,
+		},
+		{
+			name:     "name of package should be less than or equal to 63 characters, if function name is more than 26 characters",
+			fnName:   "testfunctionwithmorethan26character",
+			expected: 63,
+		},
+		{
+			name:     "name of package should be less than or equal to 63 characters, if function name is less than 26 characters",
+			fnName:   "fission-function",
+			expected: 63,
+		},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			id, err := uuid.NewV4()
+			if err != nil {
+				t.Fatal(err)
+			}
+			pkgName := generatePackageName(test.fnName, id.String())
+			if len(pkgName) > test.expected {
+				t.Errorf("expected len of package to be %v, got %v", test.expected, len(pkgName))
+			}
+		})
+	}
+}