Skip to content

Commit

Permalink
Move funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
mikutas committed Nov 9, 2023
1 parent 74bb63a commit b684a0b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 49 deletions.
49 changes: 0 additions & 49 deletions internal/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"sync"
"time"

v1 "k8s.io/api/core/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
restclient "k8s.io/client-go/rest"
clientcmd "k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -94,19 +93,6 @@ func (c *Config) CurrentContextName() (string, error) {
return cfg.CurrentContext, nil
}

func (c *Config) CurrentContextNamespace() (string, error) {
name, err := c.CurrentContextName()
if err != nil {
return "", err
}
context, err := c.GetContext(name)
if err != nil {
return "", err
}

return context.Namespace, nil
}

// GetContext fetch a given context or error if it does not exists.
func (c *Config) GetContext(n string) (*clientcmdapi.Context, error) {
cfg, err := c.RawConfig()
Expand Down Expand Up @@ -292,45 +278,10 @@ func (c *Config) CurrentUserName() (string, error) {
return "", errors.New("unable to locate current user")
}

// CurrentNamespaceName retrieves the active namespace.
func (c *Config) CurrentNamespaceName() (string, error) {
ns, _, err := c.clientConfig().Namespace()

if ns == "default" {
ns, err = c.CurrentContextNamespace()
if ns == "" && err == nil {
return "", errors.New("No namespace specified in context")
}
}

return ns, err
}

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

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

// ----------------------------------------------------------------------------
// Helpers...

// NamespaceNames fetch all available namespaces on current cluster.
func NamespaceNames(nns []v1.Namespace) []string {
nn := make([]string, 0, len(nns))
for _, ns := range nns {
nn = append(nn, ns.Name)
}

return nn
}

func isSet(s *string) bool {
return s != nil && len(*s) != 0
}

func areSet(s *[]string) bool {
return s != nil && len(*s) != 0
}
56 changes: 56 additions & 0 deletions internal/client/ns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package client

import (
"errors"

v1 "k8s.io/api/core/v1"
)

// CurrentContextNamespace retrieves kubecontext's default namespace.
func (c *Config) CurrentContextNamespace() (string, error) {
name, err := c.CurrentContextName()
if err != nil {
return "", err
}
context, err := c.GetContext(name)
if err != nil {
return "", err
}

return context.Namespace, nil
}

// CurrentNamespaceName retrieves the active namespace.
func (c *Config) CurrentNamespaceName() (string, error) {
ns, _, err := c.clientConfig().Namespace()

if ns == "default" {
ns, err = c.CurrentContextNamespace()
if ns == "" && err == nil {
return "", errors.New("No namespace specified in context")
}
}

return ns, err
}

// ----------------------------------------------------------------------------
// Helpers...

// NamespaceNames fetch all available namespaces on current cluster.
func NamespaceNames(nns []v1.Namespace) []string {
nn := make([]string, 0, len(nns))
for _, ns := range nns {
nn = append(nn, ns.Name)
}

return nn
}

func isSet(s *string) bool {
return s != nil && len(*s) != 0
}

func areSet(s *[]string) bool {
return s != nil && len(*s) != 0
}

0 comments on commit b684a0b

Please sign in to comment.