diff --git a/client.go b/client.go index b6c8e1b98f..061256e135 100644 --- a/client.go +++ b/client.go @@ -8,7 +8,6 @@ import ( ) const ( - DefaultNamespace = "faas" DefaultRegistry = "docker.io" DefaultRuntime = "go" DefaultTrigger = "http" diff --git a/cmd/completion_util.go b/cmd/completion_util.go index 3b6db6624e..bc0cf1d1eb 100644 --- a/cmd/completion_util.go +++ b/cmd/completion_util.go @@ -6,14 +6,13 @@ import ( "os/user" "path" - "github.com/boson-project/faas" "github.com/boson-project/faas/buildpacks" "github.com/boson-project/faas/knative" "github.com/spf13/cobra" ) func CompleteFunctionList(cmd *cobra.Command, args []string, toComplete string) (strings []string, directive cobra.ShellCompDirective) { - lister, err := knative.NewLister(faas.DefaultNamespace) + lister, err := knative.NewLister("") if err != nil { directive = cobra.ShellCompDirectiveError return diff --git a/cmd/delete.go b/cmd/delete.go index e59827e84b..67e469f416 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -12,6 +12,7 @@ import ( func init() { root.AddCommand(deleteCmd) deleteCmd.Flags().StringP("path", "p", cwd(), "Path to the project which should be deleted - $FAAS_PATH") + deleteCmd.Flags().StringP("namespace", "n", "", "Override namespace in which to search for Functions. Default is to use currently active underlying platform setting - $FAAS_NAMESPACE") deleteCmd.Flags().BoolP("yes", "y", false, "When in interactive mode (attached to a TTY), skip prompting the user. - $FAAS_YES") } @@ -21,14 +22,14 @@ var deleteCmd = &cobra.Command{ Long: `Removes the deployed Function by name, by explicit path, or by default for the current directory. No local files are deleted.`, SuggestFor: []string{"remove", "rm", "del"}, ValidArgsFunction: CompleteFunctionList, - PreRunE: bindEnv("path", "yes"), + PreRunE: bindEnv("path", "yes", "namespace"), RunE: runDelete, } func runDelete(cmd *cobra.Command, args []string) (err error) { config := newDeleteConfig(args).Prompt() - remover := knative.NewRemover() + remover := knative.NewRemover(config.Namespace) remover.Verbose = config.Verbose function := faas.Function{Root: config.Path, Name: config.Name} @@ -41,10 +42,11 @@ func runDelete(cmd *cobra.Command, args []string) (err error) { } type deleteConfig struct { - Name string - Path string - Verbose bool - Yes bool + Name string + Namespace string + Path string + Verbose bool + Yes bool } // newDeleteConfig returns a config populated from the current execution context @@ -55,10 +57,11 @@ func newDeleteConfig(args []string) deleteConfig { name = args[0] } return deleteConfig{ - Path: viper.GetString("path"), - Name: deriveName(name, viper.GetString("path")), // args[0] or derived - Verbose: viper.GetBool("verbose"), // defined on root - Yes: viper.GetBool("yes"), + Path: viper.GetString("path"), + Namespace: viper.GetString("namespace"), + Name: deriveName(name, viper.GetString("path")), // args[0] or derived + Verbose: viper.GetBool("verbose"), // defined on root + Yes: viper.GetBool("yes"), } } diff --git a/knative/remover.go b/knative/remover.go index 5563ac15d7..e4fa77c412 100644 --- a/knative/remover.go +++ b/knative/remover.go @@ -3,16 +3,16 @@ package knative import ( "bytes" "fmt" - "github.com/boson-project/faas" "github.com/boson-project/faas/k8s" "io" + "k8s.io/client-go/tools/clientcmd" commands "knative.dev/client/pkg/kn/commands" "os" "time" ) -func NewRemover() *Remover { - return &Remover{Namespace: faas.DefaultNamespace} +func NewRemover(namespaceOverride string) *Remover { + return &Remover{Namespace: namespaceOverride} } type Remover struct { @@ -41,6 +41,11 @@ func (remover *Remover) Remove(name string) (err error) { if err != nil { return err } + if remover.Namespace == "" { + loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() + clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{}) + remover.Namespace, _, _ = clientConfig.Namespace() + } client, err := p.NewServingClient(remover.Namespace) if err != nil { return fmt.Errorf("remover failed to create new serving client: %v", err)