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/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)) + } + }) + } +} 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)