Skip to content

Commit

Permalink
refactor: improve coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
jpts committed Jun 11, 2023
1 parent 27ea48c commit 1dd68dc
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions cmd/kubeletexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,43 @@ import (
"k8s.io/klog/v2"
)

func (c *cliSession) prepKubeletExec() (*http.Request, error) {
func (c *cliSession) getNodeIP() (string, error) {
var ip string

if c.opts.directExecNodeIp != "" {
ip = c.opts.directExecNodeIp
} else {
return c.opts.directExecNodeIp, nil
}

client, err := kubernetes.NewForConfig(c.clientConf)
if err != nil {
return nil, err
}
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{})
if err != nil {
return nil, err
}
res, err := client.CoreV1().Nodes().Get(context.TODO(), c.opts.PodSpec.NodeName, metav1.GetOptions{})
if err != nil {
return "", err
}

for _, addr := range res.Status.Addresses {
if addr.Type == "InternalIP" {
ip = addr.Address
}
for _, addr := range res.Status.Addresses {
if addr.Type == "InternalIP" {
ip = addr.Address
}
}

if ip == "" {
return nil, errors.New("Unable to find Node IP")
}
if ip == "" {
return "", errors.New("Unable to find Node IP")
}

return ip, nil
}

func (c *cliSession) prepKubeletExec() (*http.Request, error) {
nodeIP, err := c.getNodeIP()
if err != nil {
return nil, err
}

u, err := url.Parse(fmt.Sprintf("wss://%s:10250", ip))
u, err := url.Parse(fmt.Sprintf("wss://%s:10250", nodeIP))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -76,7 +85,7 @@ func (c *cliSession) prepKubeletExec() (*http.Request, error) {
if err != nil {
return nil, err
}
klog.V(7).Infof("Making request to kubelet API: %s:10250%s", ip, u.RequestURI())
klog.V(7).Infof("Making request to kubelet API: %s:10250%s", nodeIP, u.RequestURI())

return req, nil

Expand Down

0 comments on commit 1dd68dc

Please sign in to comment.