add namespace param for fn and env (#2556)
This PR moves fission CLI as closer as possible to kubectl command behaviour. We have improved namespace handling behaviour across CLI. * add namespace param for fn and env * use common fn for ns check * update validation * default namespace for httpTrigger, env and package, config and triggers * use default ns * add namespace filter to spec * add forceNamespace flag * set current namespace * add default namespace in config * add namespace specific destroy * add all namespace in the list of resources * add namespace as global tag * use %s instead of %v * add test cases for namespace * use ns in get all functions
This commit is contained in:
@@ -104,11 +104,8 @@ func KubifyName(old string) string {
|
||||
return newName
|
||||
}
|
||||
|
||||
// GetKubernetesClient builds a new kubernetes client. If the KUBECONFIG
|
||||
// environment variable is empty or doesn't exist, ~/.kube/config is used for
|
||||
// the kube config path
|
||||
func GetKubernetesClient(kubeContext string) (*restclient.Config, kubernetes.Interface, error) {
|
||||
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
|
||||
func getLoadingRules() (loadingRules *clientcmd.ClientConfigLoadingRules, err error) {
|
||||
loadingRules = clientcmd.NewDefaultClientConfigLoadingRules()
|
||||
|
||||
kubeConfigPath := os.Getenv("KUBECONFIG")
|
||||
if len(kubeConfigPath) == 0 {
|
||||
@@ -126,7 +123,7 @@ func GetKubernetesClient(kubeContext string) (*restclient.Config, kubernetes.Int
|
||||
kubeConfigPath = filepath.Join(homeDir, ".kube", "config")
|
||||
|
||||
if _, err := os.Stat(kubeConfigPath); os.IsNotExist(err) {
|
||||
return nil, nil, errors.New("Couldn't find kubeconfig file. " +
|
||||
return nil, errors.New("Couldn't find kubeconfig file. " +
|
||||
"Set the KUBECONFIG environment variable to your kubeconfig's path.")
|
||||
}
|
||||
loadingRules.ExplicitPath = kubeConfigPath
|
||||
@@ -134,6 +131,17 @@ func GetKubernetesClient(kubeContext string) (*restclient.Config, kubernetes.Int
|
||||
} else {
|
||||
console.Verbose(2, "Using kubeconfig from environment %q", kubeConfigPath)
|
||||
}
|
||||
return loadingRules, nil
|
||||
}
|
||||
|
||||
// GetKubernetesClient builds a new kubernetes client. If the KUBECONFIG
|
||||
// environment variable is empty or doesn't exist, ~/.kube/config is used for
|
||||
// the kube config path
|
||||
func GetKubernetesClient(kubeContext string) (*restclient.Config, kubernetes.Interface, error) {
|
||||
loadingRules, err := getLoadingRules()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
|
||||
loadingRules, &clientcmd.ConfigOverrides{CurrentContext: kubeContext}).ClientConfig()
|
||||
@@ -149,6 +157,25 @@ func GetKubernetesClient(kubeContext string) (*restclient.Config, kubernetes.Int
|
||||
return config, clientset, nil
|
||||
}
|
||||
|
||||
// GetKubernetesNamespace builds a new kubernetes client. If the KUBECONFIG
|
||||
// environment variable is empty or doesn't exist, ~/.kube/config is used for
|
||||
// the kube config path
|
||||
func GetKubernetesNamespace(kubeContext string) (currentNS string, err error) {
|
||||
loadingRules, err := getLoadingRules()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
config1, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
|
||||
loadingRules, &clientcmd.ConfigOverrides{CurrentContext: kubeContext}).RawConfig()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "Failed to build Kubernetes config")
|
||||
}
|
||||
currentNS = config1.Contexts[config1.CurrentContext].Namespace
|
||||
|
||||
return currentNS, nil
|
||||
}
|
||||
|
||||
// given a list of functions, this checks if the functions actually exist on the cluster
|
||||
func CheckFunctionExistence(client client.Interface, functions []string, fnNamespace string) (err error) {
|
||||
fnMissing := make([]string, 0)
|
||||
@@ -459,3 +486,34 @@ func GetStorageURL(ctx context.Context, kubeContext string) (*url.URL, error) {
|
||||
|
||||
return serverURL, nil
|
||||
}
|
||||
|
||||
func GetResourceNamespace(input cli.Input, deprecatedFlag string) (namespace, currentNS string, err error) {
|
||||
namespace = input.String(deprecatedFlag)
|
||||
currentNS = namespace
|
||||
|
||||
if input.String(flagkey.Namespace) != "" {
|
||||
namespace = input.String(flagkey.Namespace)
|
||||
currentNS = namespace
|
||||
return namespace, currentNS, err
|
||||
}
|
||||
console.Verbose(2, "Namespace from user %s ", namespace)
|
||||
|
||||
if namespace == "" {
|
||||
if os.Getenv("FISSION_DEFAULT_NAMESPACE") != "" {
|
||||
currentNS = os.Getenv("FISSION_DEFAULT_NAMESPACE")
|
||||
} else {
|
||||
kubeContext := input.String(flagkey.KubeContext)
|
||||
currentNS, err = GetKubernetesNamespace(kubeContext)
|
||||
if err != nil {
|
||||
return namespace, currentNS, err
|
||||
}
|
||||
}
|
||||
if currentNS == "" {
|
||||
return namespace, currentNS, errors.Errorf("either set current-context or provide namespace with --namespace flag")
|
||||
}
|
||||
}
|
||||
|
||||
console.Verbose(2, "Namespace final %s ", currentNS)
|
||||
|
||||
return namespace, currentNS, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user