Skip to content

Commit

Permalink
feat: centralise session init
Browse files Browse the repository at this point in the history
  • Loading branch information
jpts committed Jun 12, 2023
1 parent 598a747 commit 6a3ed7b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 32 deletions.
30 changes: 24 additions & 6 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,28 @@ const (
type cliSession struct {
opts Options
clientConf *rest.Config
k8sClient *kubernetes.Clientset
namespace string
}

func NewCliSession(o *Options) (*cliSession, error) {
c := &cliSession{
opts: *o,
}

err := c.prepConfig()
if err != nil {
return nil, err
}

c.k8sClient, err = kubernetes.NewForConfig(c.clientConf)
if err != nil {
return nil, err
}

return c, nil
}

// prep the session
func (c *cliSession) prepConfig() error {
var cfg clientcmd.ClientConfig
Expand Down Expand Up @@ -92,13 +111,12 @@ func (c *cliSession) prepConfig() error {

c.clientConf.UserAgent = fmt.Sprintf("kubectl-execws/%s", releaseVersion)

if !c.opts.noSanityCheck {
client, err := kubernetes.NewForConfig(c.clientConf)
if err != nil {
return err
}
return nil
}

res, err := client.CoreV1().Pods(c.namespace).Get(context.TODO(), c.opts.Pod, metav1.GetOptions{})
func (c *cliSession) sanityCheck() error {
if !c.opts.noSanityCheck {
res, err := c.k8sClient.CoreV1().Pods(c.namespace).Get(context.TODO(), c.opts.Pod, metav1.GetOptions{})
if err != nil {
return err
}
Expand Down
8 changes: 1 addition & 7 deletions cmd/kubeletexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/url"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
)

Expand All @@ -19,12 +18,7 @@ func (c *cliSession) getNodeIP() (string, error) {
return c.opts.directExecNodeIp, nil
}

client, err := kubernetes.NewForConfig(c.clientConf)
if err != nil {
return "", err
}

res, err := client.CoreV1().Nodes().Get(context.TODO(), c.opts.PodSpec.NodeName, metav1.GetOptions{})
res, err := c.k8sClient.CoreV1().Nodes().Get(context.TODO(), c.opts.PodSpec.NodeName, metav1.GetOptions{})
if err != nil {
return "", err
}
Expand Down
37 changes: 18 additions & 19 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,23 @@ var rootCmd = &cobra.Command{
}
}

s := &cliSession{
opts: Options{
Command: command,
Container: container,
Kconfig: kconfig,
Namespace: namespace,
Object: object,
Pod: pod,
Stdin: stdinFlag,
TTY: tty,
noSanityCheck: noSanityCheck,
noTLSVerify: noTLSVerify,
directExec: directExec,
directExecNodeIp: directExecNodeIp,
},
opts := Options{
Command: command,
Container: container,
Kconfig: kconfig,
Namespace: namespace,
Object: object,
Pod: pod,
Stdin: stdinFlag,
TTY: tty,
noSanityCheck: noSanityCheck,
noTLSVerify: noTLSVerify,
directExec: directExec,
directExecNodeIp: directExecNodeIp,
}
s, err := NewCliSession(&opts)
if err != nil {
return err
}

if s.opts.noSanityCheck && s.opts.directExec {
Expand All @@ -91,10 +93,7 @@ var rootCmd = &cobra.Command{
flag.Set("v", fmt.Sprint(loglevel))
flag.Set("stderrthreshold", fmt.Sprint(loglevel))

err := s.prepConfig()
if err != nil {
return err
}
s.sanityCheck()

var req *http.Request
if s.opts.directExec {
Expand Down

0 comments on commit 6a3ed7b

Please sign in to comment.