Make CLI functions return error instead of fatal out (#1379)
Before this PR, CLI functions fatal out when encountering error instead of returning it. Such behavior makes it hard to reuse the functions nor writing unit tests. This PR aims to make functions return errors instead of error out.
This commit is contained in:
@@ -30,11 +30,11 @@ import (
|
||||
"github.com/fission/fission/pkg/controller/client"
|
||||
ferror "github.com/fission/fission/pkg/error"
|
||||
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd/httptrigger"
|
||||
_package "github.com/fission/fission/pkg/fission-cli/cmd/package"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd/spec"
|
||||
"github.com/fission/fission/pkg/fission-cli/log"
|
||||
"github.com/fission/fission/pkg/fission-cli/consolemsg"
|
||||
"github.com/fission/fission/pkg/fission-cli/util"
|
||||
"github.com/fission/fission/pkg/types"
|
||||
)
|
||||
|
||||
@@ -50,8 +50,12 @@ type CreateSubCommand struct {
|
||||
}
|
||||
|
||||
func Create(flags cli.Input) error {
|
||||
c, err := util.GetServer(flags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts := CreateSubCommand{
|
||||
client: cmd.GetServer(flags),
|
||||
client: c,
|
||||
}
|
||||
return opts.do(flags)
|
||||
}
|
||||
@@ -79,10 +83,10 @@ func (opts *CreateSubCommand) complete(flags cli.Input) error {
|
||||
toSpec = true
|
||||
opts.specFile = fmt.Sprintf("function-%v.yaml", fnName)
|
||||
}
|
||||
specDir := cmd.GetSpecDir(flags)
|
||||
specDir := util.GetSpecDir(flags)
|
||||
|
||||
// check for unique function names within a namespace
|
||||
metadata, err := cmd.GetMetadata("name", "fnNamespace", flags)
|
||||
metadata, err := util.GetMetadata("name", "fnNamespace", flags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -110,7 +114,7 @@ func (opts *CreateSubCommand) complete(flags cli.Input) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resourceReq, err := cmd.GetResourceReqs(flags, &apiv1.ResourceRequirements{})
|
||||
resourceReq, err := util.GetResourceReqs(flags, &apiv1.ResourceRequirements{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -129,7 +133,7 @@ func (opts *CreateSubCommand) complete(flags cli.Input) error {
|
||||
pkgMetadata = &pkg.Metadata
|
||||
envName = pkg.Spec.Environment.Name
|
||||
if envName != flags.String("env") {
|
||||
log.Warn("Function's environment is different than package's environment, package's environment will be used for creating function")
|
||||
consolemsg.Warn("Function's environment is different than package's environment, package's environment will be used for creating function")
|
||||
}
|
||||
envNamespace = pkg.Spec.Environment.Namespace
|
||||
} else {
|
||||
@@ -147,7 +151,7 @@ func (opts *CreateSubCommand) complete(flags cli.Input) error {
|
||||
})
|
||||
if err != nil {
|
||||
if e, ok := err.(ferror.Error); ok && e.Code == ferror.ErrorNotFound {
|
||||
log.Warn(fmt.Sprintf("Environment \"%v\" does not exist. Please create the environment before executing the function. \nFor example: `fission env create --name %v --envns %v --image <image>`\n", envName, envName, envNamespace))
|
||||
consolemsg.Warn(fmt.Sprintf("Environment \"%v\" does not exist. Please create the environment before executing the function. \nFor example: `fission env create --name %v --envns %v --image <image>`\n", envName, envName, envNamespace))
|
||||
} else {
|
||||
return errors.Wrap(err, "error retrieving environment information")
|
||||
}
|
||||
@@ -192,7 +196,7 @@ func (opts *CreateSubCommand) complete(flags cli.Input) error {
|
||||
})
|
||||
if err != nil {
|
||||
if k8serrors.IsNotFound(err) {
|
||||
log.Warn(fmt.Sprintf("Secret %s not found in Namespace: %s. Secret needs to be present in the same namespace as function", secretName, fnNamespace))
|
||||
consolemsg.Warn(fmt.Sprintf("Secret %s not found in Namespace: %s. Secret needs to be present in the same namespace as function", secretName, fnNamespace))
|
||||
} else {
|
||||
return errors.Wrap(err, "error checking secret")
|
||||
}
|
||||
@@ -216,7 +220,7 @@ func (opts *CreateSubCommand) complete(flags cli.Input) error {
|
||||
})
|
||||
if err != nil {
|
||||
if k8serrors.IsNotFound(err) {
|
||||
log.Warn(fmt.Sprintf("ConfigMap %s not found in Namespace: %s. ConfigMap needs to be present in the same namespace as function", cfgMapName, fnNamespace))
|
||||
consolemsg.Warn(fmt.Sprintf("ConfigMap %s not found in Namespace: %s. ConfigMap needs to be present in the same namespace as function", cfgMapName, fnNamespace))
|
||||
} else {
|
||||
return errors.Wrap(err, "error checking configmap")
|
||||
}
|
||||
@@ -353,7 +357,7 @@ func getInvokeStrategy(flags cli.Input, existingInvokeStrategy *fv1.InvokeStrate
|
||||
}
|
||||
|
||||
if flags.IsSet("mincpu") || flags.IsSet("maxcpu") || flags.IsSet("minmemory") || flags.IsSet("maxmemory") {
|
||||
log.Warn("To limit CPU/Memory for function with executor type \"poolmgr\", please specify resources limits when creating environment")
|
||||
consolemsg.Warn("To limit CPU/Memory for function with executor type \"poolmgr\", please specify resources limits when creating environment")
|
||||
}
|
||||
strategy = &fv1.InvokeStrategy{
|
||||
StrategyType: fv1.StrategyTypeExecution,
|
||||
|
||||
@@ -23,7 +23,7 @@ import (
|
||||
|
||||
"github.com/fission/fission/pkg/controller/client"
|
||||
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd"
|
||||
"github.com/fission/fission/pkg/fission-cli/util"
|
||||
)
|
||||
|
||||
type DeleteSubCommand struct {
|
||||
@@ -31,14 +31,18 @@ type DeleteSubCommand struct {
|
||||
}
|
||||
|
||||
func Delete(flags cli.Input) error {
|
||||
c, err := util.GetServer(flags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts := DeleteSubCommand{
|
||||
client: cmd.GetServer(flags),
|
||||
client: c,
|
||||
}
|
||||
return opts.do(flags)
|
||||
}
|
||||
|
||||
func (opts *DeleteSubCommand) do(flags cli.Input) error {
|
||||
m, err := cmd.GetMetadata("name", "fnNamespace", flags)
|
||||
m, err := util.GetMetadata("name", "fnNamespace", flags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ import (
|
||||
|
||||
"github.com/fission/fission/pkg/controller/client"
|
||||
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd"
|
||||
"github.com/fission/fission/pkg/fission-cli/util"
|
||||
)
|
||||
|
||||
type GetSubCommand struct {
|
||||
@@ -32,14 +32,18 @@ type GetSubCommand struct {
|
||||
}
|
||||
|
||||
func Get(flags cli.Input) error {
|
||||
c, err := util.GetServer(flags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts := GetSubCommand{
|
||||
client: cmd.GetServer(flags),
|
||||
client: c,
|
||||
}
|
||||
return opts.do(flags)
|
||||
}
|
||||
|
||||
func (opts *GetSubCommand) do(flags cli.Input) error {
|
||||
m, err := cmd.GetMetadata("name", "fnNamespace", flags)
|
||||
m, err := util.GetMetadata("name", "fnNamespace", flags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ import (
|
||||
|
||||
"github.com/fission/fission/pkg/controller/client"
|
||||
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd"
|
||||
"github.com/fission/fission/pkg/fission-cli/util"
|
||||
)
|
||||
|
||||
type GetMetaSubCommand struct {
|
||||
@@ -33,14 +33,18 @@ type GetMetaSubCommand struct {
|
||||
}
|
||||
|
||||
func GetMeta(flags cli.Input) error {
|
||||
c, err := util.GetServer(flags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts := GetMetaSubCommand{
|
||||
client: cmd.GetServer(flags),
|
||||
client: c,
|
||||
}
|
||||
return opts.do(flags)
|
||||
}
|
||||
|
||||
func (opts *GetMetaSubCommand) do(flags cli.Input) error {
|
||||
m, err := cmd.GetMetadata("name", "fnNamespace", flags)
|
||||
m, err := util.GetMetadata("name", "fnNamespace", flags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ import (
|
||||
|
||||
"github.com/fission/fission/pkg/controller/client"
|
||||
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd"
|
||||
"github.com/fission/fission/pkg/fission-cli/util"
|
||||
)
|
||||
|
||||
type ListSubCommand struct {
|
||||
@@ -34,8 +34,12 @@ type ListSubCommand struct {
|
||||
}
|
||||
|
||||
func List(flags cli.Input) error {
|
||||
c, err := util.GetServer(flags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts := ListSubCommand{
|
||||
client: cmd.GetServer(flags),
|
||||
client: c,
|
||||
}
|
||||
return opts.do(flags)
|
||||
}
|
||||
|
||||
@@ -25,7 +25,6 @@ import (
|
||||
|
||||
"github.com/fission/fission/pkg/controller/client"
|
||||
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd"
|
||||
"github.com/fission/fission/pkg/fission-cli/logdb"
|
||||
"github.com/fission/fission/pkg/fission-cli/util"
|
||||
)
|
||||
@@ -35,14 +34,18 @@ type LogSubCommand struct {
|
||||
}
|
||||
|
||||
func Log(flags cli.Input) error {
|
||||
c, err := util.GetServer(flags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts := LogSubCommand{
|
||||
client: cmd.GetServer(flags),
|
||||
client: c,
|
||||
}
|
||||
return opts.do(flags)
|
||||
}
|
||||
|
||||
func (opts *LogSubCommand) do(flags cli.Input) error {
|
||||
m, err := cmd.GetMetadata("name", "fnNamespace", flags)
|
||||
m, err := util.GetMetadata("name", "fnNamespace", flags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -66,8 +69,13 @@ func (opts *LogSubCommand) do(flags cli.Input) error {
|
||||
return errors.Wrap(err, "error getting function")
|
||||
}
|
||||
|
||||
server, err := util.GetApplicationUrl("application=fission-api")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// request the controller to establish a proxy server to the database.
|
||||
logDB, err := logdb.GetLogDB(dbType, util.GetServerUrl())
|
||||
logDB, err := logdb.GetLogDB(dbType, server)
|
||||
if err != nil {
|
||||
return errors.New("failed to connect log database")
|
||||
}
|
||||
|
||||
@@ -30,7 +30,6 @@ import (
|
||||
|
||||
"github.com/fission/fission/pkg/controller/client"
|
||||
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd/httptrigger"
|
||||
"github.com/fission/fission/pkg/fission-cli/util"
|
||||
)
|
||||
@@ -40,14 +39,18 @@ type TestSubCommand struct {
|
||||
}
|
||||
|
||||
func Test(flags cli.Input) error {
|
||||
c, err := util.GetServer(flags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts := TestSubCommand{
|
||||
client: cmd.GetServer(flags),
|
||||
client: c,
|
||||
}
|
||||
return opts.do(flags)
|
||||
}
|
||||
|
||||
func (opts *TestSubCommand) do(flags cli.Input) error {
|
||||
m, err := cmd.GetMetadata("name", "fnNamespace", flags)
|
||||
m, err := util.GetMetadata("name", "fnNamespace", flags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -55,8 +58,10 @@ func (opts *TestSubCommand) do(flags cli.Input) error {
|
||||
routerURL := os.Getenv("FISSION_ROUTER")
|
||||
if len(routerURL) == 0 {
|
||||
// Portforward to the fission router
|
||||
localRouterPort := util.SetupPortForward(util.GetFissionNamespace(),
|
||||
"application=fission-router")
|
||||
localRouterPort, err := util.SetupPortForward(util.GetFissionNamespace(), "application=fission-router")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
routerURL = "127.0.0.1:" + localRouterPort
|
||||
} else {
|
||||
routerURL = strings.TrimPrefix(routerURL, "http://")
|
||||
@@ -158,7 +163,12 @@ func printPodLogs(flags cli.Input) error {
|
||||
return errors.New("need --name argument.")
|
||||
}
|
||||
|
||||
queryURL, err := url.Parse(util.GetServerUrl())
|
||||
u, err := util.GetApplicationUrl("application=fission-api")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
queryURL, err := url.Parse(u)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error parsing the base URL")
|
||||
}
|
||||
|
||||
@@ -26,9 +26,9 @@ import (
|
||||
fv1 "github.com/fission/fission/pkg/apis/fission.io/v1"
|
||||
"github.com/fission/fission/pkg/controller/client"
|
||||
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd"
|
||||
_package "github.com/fission/fission/pkg/fission-cli/cmd/package"
|
||||
"github.com/fission/fission/pkg/fission-cli/log"
|
||||
"github.com/fission/fission/pkg/fission-cli/consolemsg"
|
||||
"github.com/fission/fission/pkg/fission-cli/util"
|
||||
"github.com/fission/fission/pkg/types"
|
||||
)
|
||||
|
||||
@@ -38,8 +38,12 @@ type UpdateSubCommand struct {
|
||||
}
|
||||
|
||||
func Update(flags cli.Input) error {
|
||||
c, err := util.GetServer(flags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts := UpdateSubCommand{
|
||||
client: cmd.GetServer(flags),
|
||||
client: c,
|
||||
}
|
||||
return opts.do(flags)
|
||||
}
|
||||
@@ -67,7 +71,7 @@ func (opts *UpdateSubCommand) complete(flags cli.Input) error {
|
||||
}
|
||||
fnNamespace := flags.String("fnNamespace")
|
||||
|
||||
m, err := cmd.GetMetadata("name", "fnNamespace", flags)
|
||||
m, err := util.GetMetadata("name", "fnNamespace", flags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -127,7 +131,7 @@ func (opts *UpdateSubCommand) complete(flags cli.Input) error {
|
||||
Name: secretName,
|
||||
})
|
||||
if k8serrors.IsNotFound(err) {
|
||||
log.Warn(fmt.Sprintf("secret %s not found in Namespace: %s. Secret needs to be present in the same namespace as function", secretName, fnNamespace))
|
||||
consolemsg.Warn(fmt.Sprintf("secret %s not found in Namespace: %s. Secret needs to be present in the same namespace as function", secretName, fnNamespace))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,7 +155,7 @@ func (opts *UpdateSubCommand) complete(flags cli.Input) error {
|
||||
Name: cfgMapName,
|
||||
})
|
||||
if k8serrors.IsNotFound(err) {
|
||||
log.Warn(fmt.Sprintf("ConfigMap %s not found in Namespace: %s. ConfigMap needs to be present in the same namespace as the function", cfgMapName, fnNamespace))
|
||||
consolemsg.Warn(fmt.Sprintf("ConfigMap %s not found in Namespace: %s. ConfigMap needs to be present in the same namespace as the function", cfgMapName, fnNamespace))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,7 +211,7 @@ func (opts *UpdateSubCommand) complete(flags cli.Input) error {
|
||||
}
|
||||
}
|
||||
|
||||
resReqs, err := cmd.GetResourceReqs(flags, &function.Spec.Resources)
|
||||
resReqs, err := util.GetResourceReqs(flags, &function.Spec.Resources)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -267,7 +271,7 @@ func (opts *UpdateSubCommand) complete(flags cli.Input) error {
|
||||
}
|
||||
|
||||
if function.Spec.Environment.Name != pkg.Spec.Environment.Name {
|
||||
log.Warn("Function's environment is different than package's environment, package's environment will be used for updating function")
|
||||
consolemsg.Warn("Function's environment is different than package's environment, package's environment will be used for updating function")
|
||||
function.Spec.Environment.Name = pkg.Spec.Environment.Name
|
||||
function.Spec.Environment.Namespace = pkg.Spec.Environment.Namespace
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user