-
Notifications
You must be signed in to change notification settings - Fork 23
[kn-admin] Avoid to print runtime error if kubectl context is not set #70
Comments
@zhanggbj Have you any comments on that? If no objections, I will assign it to myself to do. Thanks! |
@chaozbj good finding! Thanks!
|
/assign |
@zhanggbj After I improved the codes to: // rootCmd represents the base command when called without any subcommands
func NewAdminCommand(params ...pkg.AdminParams) *cobra.Command {
p := &pkg.AdminParams{}
if err := p.Initialize(); err != nil {
fmt.Printf("Failed to create kubernetes client, %+v\n", err)
os.Exit(1)
}
... I found it will break the existing UT codes of root command since no kube config is available during testing. I checked our codes and knative client codes, got two options:
// rootCmd represents the base command when called without any subcommands
func NewAdminCommand(p *pkg.AdminParams) *cobra.Command {
if p == nil {
p = &pkg.AdminParams{}
}
if err := p.Initialize(); err != nil {
fmt.Printf("Failed to create kubernetes client, %+v\n", err)
os.Exit(1)
}
...
Which one do you prefer? Thanks! |
@chaozbj Thanks for the investigation. Briefly, option 2 sounds good to me. |
@zhanggbj Ok, I also like option 2, I will do it, thanks! |
If the kubectl context is not set or run
kubectl config unset current-context
to unset it, we will get runtime error when runkn admin
, for example:I checked the codes in
AdminParams
:I think we can do some improvements for the above functions so that we can avoid outputting runtime error if kubectl context is not set:
Initialize()
, we'd better return error when fail to createClientSet
NewAdminCommand()
, we should check the error fromp.Initialize()
, if error is returned, we can print the error message and exit to run command.The text was updated successfully, but these errors were encountered: