Skip to content

Commit

Permalink
minors
Browse files Browse the repository at this point in the history
  • Loading branch information
Surax98 committed Jul 28, 2023
1 parent 6697ed0 commit 3ba6f06
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 34 deletions.
Empty file added git
Empty file.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func main() {

localClient := kubernetes.NewForConfigOrDie(kubecfg)

nodeProvider, err := virtualkubelet.NewProvider(cfg.ConfigPath, cfg.NodeName, cfg.OperatingSystem, cfg.InternalIP, cfg.DaemonPort)
nodeProvider, err := virtualkubelet.NewProvider(cfg.ConfigPath, cfg.NodeName, cfg.OperatingSystem, cfg.InternalIP, cfg.DaemonPort, ctx)
if err != nil {
log.G(ctx).Fatal(err)
}
Expand Down
69 changes: 40 additions & 29 deletions pkg/common/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
Expand Down Expand Up @@ -124,7 +125,7 @@ func NewInterLinkConfig() {
}
}

func NewServiceAccount() {
func NewServiceAccount() error {

var sa string
var script string
Expand All @@ -133,8 +134,14 @@ func NewServiceAccount() {
err := os.MkdirAll(path, os.ModePerm)
if err != nil {
log.Println(err)
return err
}
f, err := os.Create(path + "getSAConfig.sh")
if err != nil {
log.Println(err)
return err
}

defer f.Close()

script = "SERVICE_ACCOUNT_NAME=" + InterLinkConfigInst.ServiceAccount + "\n" +
Expand All @@ -156,7 +163,12 @@ func NewServiceAccount() {
"rm ${KUBECONFIG_FILE}.full.tmp\n" +
"rm ${KUBECONFIG_FILE}.tmp"

f.Write([]byte(script))
_, err = f.Write([]byte(script))

if err != nil {
log.Println(err)
return err
}

cmd := []string{path + "getSAConfig.sh"}
shell := exec.ExecTask{
Expand All @@ -167,49 +179,48 @@ func NewServiceAccount() {
execResult, _ := shell.Execute()
if execResult.Stderr != "" {
log.Println(execResult.Stderr)
return errors.New(execResult.Stderr)
}

temp, err := os.ReadFile(path + "kubeconfig-sa")
if err != nil {
log.Println(err)
return err
}

sa = string(temp)
os.Remove(path + "getSAConfig.sh")
os.Remove(path + "kubeconfig-sa")

for {
returnedVal := SendKubeConfig(sa)
if returnedVal == "200" {
break
} else {
fmt.Println(returnedVal)
}
}
}

func SendKubeConfig(body string) string {
var returnValue, _ = json.Marshal("Error")
request := GenericRequestType{Body: body}
var returnValue, _ = json.Marshal("Error")
request := GenericRequestType{Body: sa}

bodyBytes, err := json.Marshal(request)
reader := bytes.NewReader(bodyBytes)
req, err := http.NewRequest(http.MethodPost, InterLinkConfigInst.Interlinkurl+":"+InterLinkConfigInst.Interlinkport+"/setKubeCFG", reader)
bodyBytes, err := json.Marshal(request)
reader := bytes.NewReader(bodyBytes)
req, err := http.NewRequest(http.MethodPost, InterLinkConfigInst.Interlinkurl+":"+InterLinkConfigInst.Interlinkport+"/setKubeCFG", reader)

if err != nil {
log.Println(err)
}
if err != nil {
log.Println(err)
}

token, err := os.ReadFile(InterLinkConfigInst.VKTokenFile) // just pass the file name
req.Header.Add("Authorization", "Bearer "+string(token))
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
time.Sleep(5 * time.Second)
} else {
returnValue, _ = ioutil.ReadAll(resp.Body)
token, err := os.ReadFile(InterLinkConfigInst.VKTokenFile) // just pass the file name
req.Header.Add("Authorization", "Bearer "+string(token))
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
time.Sleep(5 * time.Second)
} else {
returnValue, _ = ioutil.ReadAll(resp.Body)
}

if string(returnValue) == "200" {
return "200"
break
} else {
fmt.Println(string(returnValue))
}
}
return "400"

return nil
}
12 changes: 8 additions & 4 deletions pkg/virtualkubelet/virtualkubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,23 @@ func NewProviderConfig(config VirtualKubeletConfig, nodeName, operatingSystem st
}

// NewProvider creates a new Provider, which implements the PodNotifier interface
func NewProvider(providerConfig, nodeName, operatingSystem string, internalIP string, daemonEndpointPort int32) (*VirtualKubeletProvider, error) {
config, err := loadConfig(providerConfig, nodeName)
func NewProvider(providerConfig, nodeName, operatingSystem string, internalIP string, daemonEndpointPort int32, ctx context.Context) (*VirtualKubeletProvider, error) {
config, err := loadConfig(providerConfig, nodeName, ctx)
if err != nil {
return nil, err
}
return NewProviderConfig(config, nodeName, operatingSystem, internalIP, daemonEndpointPort)
}

// loadConfig loads the given json configuration files and yaml to communicate with InterLink.
func loadConfig(providerConfig, nodeName string) (config VirtualKubeletConfig, err error) {
func loadConfig(providerConfig, nodeName string, ctx context.Context) (config VirtualKubeletConfig, err error) {

commonIL.NewInterLinkConfig()
commonIL.NewServiceAccount()
err = commonIL.NewServiceAccount()

if err != nil {
log.G(ctx).Fatal(err)
}

data, err := ioutil.ReadFile(providerConfig)
if err != nil {
Expand Down

0 comments on commit 3ba6f06

Please sign in to comment.