Skip to content

Commit

Permalink
fix #2488
Browse files Browse the repository at this point in the history
  • Loading branch information
derailed committed Jan 18, 2024
1 parent a543f47 commit 76a8297
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
23 changes: 10 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,25 +357,20 @@ type (
)

func initK8sFlagCompletion() {
conn := client.NewConfig(k8sFlags)
cfg, err := conn.RawConfig()
if err != nil {
log.Error().Err(err).Msgf("k8s config getter failed")
}

_ = rootCmd.RegisterFlagCompletionFunc("context", k8sFlagCompletion(&cfg, func(cfg *api.Config) map[string]*api.Context {
_ = rootCmd.RegisterFlagCompletionFunc("context", k8sFlagCompletion(func(cfg *api.Config) map[string]*api.Context {
return cfg.Contexts
}))

_ = rootCmd.RegisterFlagCompletionFunc("cluster", k8sFlagCompletion(&cfg, func(cfg *api.Config) map[string]*api.Cluster {
_ = rootCmd.RegisterFlagCompletionFunc("cluster", k8sFlagCompletion(func(cfg *api.Config) map[string]*api.Cluster {
return cfg.Clusters
}))

_ = rootCmd.RegisterFlagCompletionFunc("user", k8sFlagCompletion(&cfg, func(cfg *api.Config) map[string]*api.AuthInfo {
_ = rootCmd.RegisterFlagCompletionFunc("user", k8sFlagCompletion(func(cfg *api.Config) map[string]*api.AuthInfo {
return cfg.AuthInfos
}))

_ = rootCmd.RegisterFlagCompletionFunc("namespace", func(cmd *cobra.Command, args []string, s string) ([]string, cobra.ShellCompDirective) {
conn := client.NewConfig(k8sFlags)
if c, err := client.InitConnection(conn); err == nil {
if nss, err := c.ValidNamespaceNames(); err == nil {
return filterFlagCompletions(nss, s)
Expand All @@ -386,13 +381,15 @@ func initK8sFlagCompletion() {
})
}

func k8sFlagCompletion[T any](cfg *api.Config, picker k8sPickerFn[T]) completeFn {
func k8sFlagCompletion[T any](picker k8sPickerFn[T]) completeFn {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if cfg == nil {
return nil, cobra.ShellCompDirectiveError
conn := client.NewConfig(k8sFlags)
cfg, err := conn.RawConfig()
if err != nil {
log.Error().Err(err).Msgf("k8s config getter failed")
}

return filterFlagCompletions(picker(cfg), toComplete)
return filterFlagCompletions(picker(&cfg), toComplete)
}
}

Expand Down
7 changes: 3 additions & 4 deletions internal/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ const (
// Config tracks a kubernetes configuration.
type Config struct {
flags *genericclioptions.ConfigFlags
mutex *sync.RWMutex
mx sync.RWMutex
}

// NewConfig returns a new k8s config or an error if the flags are invalid.
func NewConfig(f *genericclioptions.ConfigFlags) *Config {
return &Config{
flags: f,
mutex: &sync.RWMutex{},
}
}

Expand Down Expand Up @@ -299,8 +298,8 @@ func (c *Config) CurrentNamespaceName() (string, error) {

// ConfigAccess return the current kubeconfig api server access configuration.
func (c *Config) ConfigAccess() (clientcmd.ConfigAccess, error) {
c.mutex.RLock()
defer c.mutex.RUnlock()
c.mx.RLock()
defer c.mx.RUnlock()

return c.clientConfig().ConfigAccess(), nil
}
Expand Down

0 comments on commit 76a8297

Please sign in to comment.