Gracefully handle empty config error when local flag is used
`f.ToRawKubeConfigLoader().Namespace()` throws `ErrEmptyConfig` when default configuration file can not be found, even if it invalid. However, when user explicitly passes `--local` flag, that means we can gracefully handle this error and continue. This commit does that for the commands having `--local` flag.
This commit is contained in:
		@@ -32,6 +32,8 @@ import (
 | 
				
			|||||||
	"k8s.io/apimachinery/pkg/types"
 | 
						"k8s.io/apimachinery/pkg/types"
 | 
				
			||||||
	"k8s.io/apimachinery/pkg/util/json"
 | 
						"k8s.io/apimachinery/pkg/util/json"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"k8s.io/client-go/tools/clientcmd"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"k8s.io/cli-runtime/pkg/genericclioptions"
 | 
						"k8s.io/cli-runtime/pkg/genericclioptions"
 | 
				
			||||||
	"k8s.io/cli-runtime/pkg/genericiooptions"
 | 
						"k8s.io/cli-runtime/pkg/genericiooptions"
 | 
				
			||||||
	"k8s.io/cli-runtime/pkg/printers"
 | 
						"k8s.io/cli-runtime/pkg/printers"
 | 
				
			||||||
@@ -227,7 +229,7 @@ func (flags *AnnotateFlags) ToOptions(f cmdutil.Factory, cmd *cobra.Command, arg
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	options.namespace, options.enforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
 | 
						options.namespace, options.enforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil && !(options.local && clientcmd.IsEmptyConfig(err)) {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	options.builder = f.NewBuilder()
 | 
						options.builder = f.NewBuilder()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -38,6 +38,7 @@ import (
 | 
				
			|||||||
	"k8s.io/cli-runtime/pkg/genericiooptions"
 | 
						"k8s.io/cli-runtime/pkg/genericiooptions"
 | 
				
			||||||
	"k8s.io/cli-runtime/pkg/printers"
 | 
						"k8s.io/cli-runtime/pkg/printers"
 | 
				
			||||||
	"k8s.io/cli-runtime/pkg/resource"
 | 
						"k8s.io/cli-runtime/pkg/resource"
 | 
				
			||||||
 | 
						"k8s.io/client-go/tools/clientcmd"
 | 
				
			||||||
	cmdutil "k8s.io/kubectl/pkg/cmd/util"
 | 
						cmdutil "k8s.io/kubectl/pkg/cmd/util"
 | 
				
			||||||
	"k8s.io/kubectl/pkg/scheme"
 | 
						"k8s.io/kubectl/pkg/scheme"
 | 
				
			||||||
	"k8s.io/kubectl/pkg/util/completion"
 | 
						"k8s.io/kubectl/pkg/util/completion"
 | 
				
			||||||
@@ -204,7 +205,7 @@ func (o *LabelOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []st
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	o.namespace, o.enforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
 | 
						o.namespace, o.enforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil && !(o.local && clientcmd.IsEmptyConfig(err)) {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	o.builder = f.NewBuilder()
 | 
						o.builder = f.NewBuilder()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -40,6 +40,7 @@ import (
 | 
				
			|||||||
	"k8s.io/cli-runtime/pkg/genericiooptions"
 | 
						"k8s.io/cli-runtime/pkg/genericiooptions"
 | 
				
			||||||
	"k8s.io/cli-runtime/pkg/printers"
 | 
						"k8s.io/cli-runtime/pkg/printers"
 | 
				
			||||||
	"k8s.io/cli-runtime/pkg/resource"
 | 
						"k8s.io/cli-runtime/pkg/resource"
 | 
				
			||||||
 | 
						"k8s.io/client-go/tools/clientcmd"
 | 
				
			||||||
	cmdutil "k8s.io/kubectl/pkg/cmd/util"
 | 
						cmdutil "k8s.io/kubectl/pkg/cmd/util"
 | 
				
			||||||
	"k8s.io/kubectl/pkg/scheme"
 | 
						"k8s.io/kubectl/pkg/scheme"
 | 
				
			||||||
	"k8s.io/kubectl/pkg/util/completion"
 | 
						"k8s.io/kubectl/pkg/util/completion"
 | 
				
			||||||
@@ -171,7 +172,7 @@ func (o *PatchOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []st
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	o.namespace, o.enforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
 | 
						o.namespace, o.enforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil && !(o.Local && clientcmd.IsEmptyConfig(err)) {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	o.args = args
 | 
						o.args = args
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -30,6 +30,7 @@ import (
 | 
				
			|||||||
	"k8s.io/cli-runtime/pkg/genericiooptions"
 | 
						"k8s.io/cli-runtime/pkg/genericiooptions"
 | 
				
			||||||
	"k8s.io/cli-runtime/pkg/printers"
 | 
						"k8s.io/cli-runtime/pkg/printers"
 | 
				
			||||||
	"k8s.io/cli-runtime/pkg/resource"
 | 
						"k8s.io/cli-runtime/pkg/resource"
 | 
				
			||||||
 | 
						"k8s.io/client-go/tools/clientcmd"
 | 
				
			||||||
	cmdutil "k8s.io/kubectl/pkg/cmd/util"
 | 
						cmdutil "k8s.io/kubectl/pkg/cmd/util"
 | 
				
			||||||
	"k8s.io/kubectl/pkg/polymorphichelpers"
 | 
						"k8s.io/kubectl/pkg/polymorphichelpers"
 | 
				
			||||||
	"k8s.io/kubectl/pkg/scheme"
 | 
						"k8s.io/kubectl/pkg/scheme"
 | 
				
			||||||
@@ -167,7 +168,7 @@ func (o *SetImageOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args [
 | 
				
			|||||||
	o.PrintObj = printer.PrintObj
 | 
						o.PrintObj = printer.PrintObj
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	cmdNamespace, enforceNamespace, err := f.ToRawKubeConfigLoader().Namespace()
 | 
						cmdNamespace, enforceNamespace, err := f.ToRawKubeConfigLoader().Namespace()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil && !(o.Local && clientcmd.IsEmptyConfig(err)) {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -30,6 +30,7 @@ import (
 | 
				
			|||||||
	"k8s.io/cli-runtime/pkg/genericiooptions"
 | 
						"k8s.io/cli-runtime/pkg/genericiooptions"
 | 
				
			||||||
	"k8s.io/cli-runtime/pkg/printers"
 | 
						"k8s.io/cli-runtime/pkg/printers"
 | 
				
			||||||
	"k8s.io/cli-runtime/pkg/resource"
 | 
						"k8s.io/cli-runtime/pkg/resource"
 | 
				
			||||||
 | 
						"k8s.io/client-go/tools/clientcmd"
 | 
				
			||||||
	cmdutil "k8s.io/kubectl/pkg/cmd/util"
 | 
						cmdutil "k8s.io/kubectl/pkg/cmd/util"
 | 
				
			||||||
	generateversioned "k8s.io/kubectl/pkg/generate/versioned"
 | 
						generateversioned "k8s.io/kubectl/pkg/generate/versioned"
 | 
				
			||||||
	"k8s.io/kubectl/pkg/polymorphichelpers"
 | 
						"k8s.io/kubectl/pkg/polymorphichelpers"
 | 
				
			||||||
@@ -166,7 +167,7 @@ func (o *SetResourcesOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, ar
 | 
				
			|||||||
	o.PrintObj = printer.PrintObj
 | 
						o.PrintObj = printer.PrintObj
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	cmdNamespace, enforceNamespace, err := f.ToRawKubeConfigLoader().Namespace()
 | 
						cmdNamespace, enforceNamespace, err := f.ToRawKubeConfigLoader().Namespace()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil && !(o.Local && clientcmd.IsEmptyConfig(err)) {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -31,6 +31,7 @@ import (
 | 
				
			|||||||
	"k8s.io/cli-runtime/pkg/genericiooptions"
 | 
						"k8s.io/cli-runtime/pkg/genericiooptions"
 | 
				
			||||||
	"k8s.io/cli-runtime/pkg/printers"
 | 
						"k8s.io/cli-runtime/pkg/printers"
 | 
				
			||||||
	"k8s.io/cli-runtime/pkg/resource"
 | 
						"k8s.io/cli-runtime/pkg/resource"
 | 
				
			||||||
 | 
						"k8s.io/client-go/tools/clientcmd"
 | 
				
			||||||
	cmdutil "k8s.io/kubectl/pkg/cmd/util"
 | 
						cmdutil "k8s.io/kubectl/pkg/cmd/util"
 | 
				
			||||||
	"k8s.io/kubectl/pkg/polymorphichelpers"
 | 
						"k8s.io/kubectl/pkg/polymorphichelpers"
 | 
				
			||||||
	"k8s.io/kubectl/pkg/scheme"
 | 
						"k8s.io/kubectl/pkg/scheme"
 | 
				
			||||||
@@ -149,7 +150,7 @@ func (o *SetServiceAccountOptions) Complete(f cmdutil.Factory, cmd *cobra.Comman
 | 
				
			|||||||
	o.PrintObj = printer.PrintObj
 | 
						o.PrintObj = printer.PrintObj
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	cmdNamespace, enforceNamespace, err := f.ToRawKubeConfigLoader().Namespace()
 | 
						cmdNamespace, enforceNamespace, err := f.ToRawKubeConfigLoader().Namespace()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil && !(o.local && clientcmd.IsEmptyConfig(err)) {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if len(args) == 0 {
 | 
						if len(args) == 0 {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -31,6 +31,7 @@ import (
 | 
				
			|||||||
	"k8s.io/cli-runtime/pkg/genericiooptions"
 | 
						"k8s.io/cli-runtime/pkg/genericiooptions"
 | 
				
			||||||
	"k8s.io/cli-runtime/pkg/printers"
 | 
						"k8s.io/cli-runtime/pkg/printers"
 | 
				
			||||||
	"k8s.io/cli-runtime/pkg/resource"
 | 
						"k8s.io/cli-runtime/pkg/resource"
 | 
				
			||||||
 | 
						"k8s.io/client-go/tools/clientcmd"
 | 
				
			||||||
	cmdutil "k8s.io/kubectl/pkg/cmd/util"
 | 
						cmdutil "k8s.io/kubectl/pkg/cmd/util"
 | 
				
			||||||
	"k8s.io/kubectl/pkg/scheme"
 | 
						"k8s.io/kubectl/pkg/scheme"
 | 
				
			||||||
	"k8s.io/kubectl/pkg/util/i18n"
 | 
						"k8s.io/kubectl/pkg/util/i18n"
 | 
				
			||||||
@@ -138,7 +139,7 @@ func (o *SubjectOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	var enforceNamespace bool
 | 
						var enforceNamespace bool
 | 
				
			||||||
	o.namespace, enforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
 | 
						o.namespace, enforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil && !(o.Local && clientcmd.IsEmptyConfig(err)) {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user