Skip to content

Commit

Permalink
Docker Plugin now totally independent from Kubernetes/Kubectl (#38)
Browse files Browse the repository at this point in the history
* Docker sidecar doesn't need kubectl anymore
  • Loading branch information
Surax98 authored Aug 2, 2023
1 parent 12f9774 commit 6a361d2
Show file tree
Hide file tree
Showing 17 changed files with 369 additions and 346 deletions.
15 changes: 13 additions & 2 deletions cmd/interlink/main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
package main

import (
"context"
"fmt"
"log"
"net/http"

commonIL "github.com/intertwin-eu/interlink/pkg/common"
"github.com/intertwin-eu/interlink/pkg/interlink"
"github.com/sirupsen/logrus"
"github.com/virtual-kubelet/virtual-kubelet/log"
logruslogger "github.com/virtual-kubelet/virtual-kubelet/log/logrus"
)

var Url string

func main() {
var cancel context.CancelFunc

logger := logrus.StandardLogger()
logger.SetLevel(logrus.DebugLevel)
log.L = logruslogger.FromLogrus(logrus.NewEntry(logger))

interlink.Ctx, cancel = context.WithCancel(context.Background())
defer cancel()

commonIL.NewInterLinkConfig()

Expand All @@ -25,6 +36,6 @@ func main() {

err := http.ListenAndServe(":"+commonIL.InterLinkConfigInst.Interlinkport, mutex)
if err != nil {
log.Fatal(err)
log.G(interlink.Ctx).Fatal(err)
}
}
1 change: 0 additions & 1 deletion cmd/sidecars/docker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func main() {
mutex.HandleFunc("/status", docker.StatusHandler)
mutex.HandleFunc("/create", docker.CreateHandler)
mutex.HandleFunc("/delete", docker.DeleteHandler)
mutex.HandleFunc("/setKubeCFG", docker.SetKubeCFGHandler)
err := http.ListenAndServe(":"+commonIL.InterLinkConfigInst.Sidecarport, mutex)

if err != nil {
Expand Down
1 change: 0 additions & 1 deletion cmd/sidecars/slurm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func main() {
mutex.HandleFunc("/status", slurm.StatusHandler)
mutex.HandleFunc("/submit", slurm.SubmitHandler)
mutex.HandleFunc("/stop", slurm.StopHandler)
mutex.HandleFunc("/setKubeCFG", slurm.SetKubeCFGHandler)

err := http.ListenAndServe(":"+commonIL.InterLinkConfigInst.Sidecarport, mutex)
if err != nil {
Expand Down
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
}
24 changes: 14 additions & 10 deletions pkg/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,32 @@ const (
UNKNOWN = 2
)

type PodName struct {
Name string `json:"podname"`
}

type PodStatus struct {
PodStatus uint `json:"podStatus"`
PodName string `json:"podname"`
PodStatus uint `json:"podStatus"`
}

type StatusResponse struct {
PodName []PodName `json:"podname"`
PodStatus []PodStatus `json:"podstatus"`
ReturnVal string `json:"returnVal"`
}

type Request struct {
Pods map[string]*v1.Pod `json:"pods"`
}

type GenericRequestType struct {
Body string `json:"body"`
}

type RetrievedContainer struct {
Name string `json:"name"`
ConfigMaps []v1.ConfigMap `json:"configMaps"`
Secrets []v1.Secret `json:"secrets"`
EmptyDirs []string `json:"emptyDirs"`
}

type RetrievedPodData struct {
Pod v1.Pod `json:"pod"`
Containers []RetrievedContainer `json:"container"`
}

type InterLinkConfig struct {
VKTokenFile string `yaml:"VKTokenFile"`
Interlinkurl string `yaml:"InterlinkURL"`
Expand Down
29 changes: 28 additions & 1 deletion pkg/interlink/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package interlink

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"

commonIL "github.com/intertwin-eu/interlink/pkg/common"
v1 "k8s.io/api/core/v1"
)

func CreateHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -17,7 +19,32 @@ func CreateHandler(w http.ResponseWriter, r *http.Request) {
log.Fatal(err)
}

var req *http.Request
var req *http.Request //request to forward to sidecar
var req2 []*v1.Pod //request for interlink
json.Unmarshal(bodyBytes, &req2)

var retrieved_data []commonIL.RetrievedPodData
for _, pod := range req2 {
data := []commonIL.RetrievedPodData{}
if commonIL.InterLinkConfigInst.ExportPodData {
data, err = getData(pod)
if err != nil {
w.Write([]byte("500"))
return
}
log.Print(data)
}

if data == nil {
data = append(data, commonIL.RetrievedPodData{Pod: *pod})
}

retrieved_data = append(retrieved_data, data...)
}

bodyBytes, err = json.Marshal(retrieved_data)
fmt.Println(retrieved_data)
fmt.Println(string(bodyBytes))
reader := bytes.NewReader(bodyBytes)

switch commonIL.InterLinkConfigInst.Sidecarservice {
Expand Down
95 changes: 95 additions & 0 deletions pkg/interlink/func.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package interlink

import (
"path/filepath"

"github.com/containerd/containerd/log"
commonIL "github.com/intertwin-eu/interlink/pkg/common"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func getData(pod *v1.Pod) ([]commonIL.RetrievedPodData, error) {
var retrieved_data []commonIL.RetrievedPodData
for _, container := range pod.Spec.Containers {
log.G(Ctx).Info("- Retrieving Secrets and ConfigMaps for the Docker Sidecar. Container: " + container.Name)

data, err := retrieve_data(container, pod)
if err != nil {
log.G(Ctx).Error(err)
return nil, err
}

if data.Containers != nil {
data.Pod = *pod
retrieved_data = append(retrieved_data, data)
}
}

return retrieved_data, nil
}

func retrieve_data(container v1.Container, pod *v1.Pod) (commonIL.RetrievedPodData, error) {
retrieved_data := commonIL.RetrievedPodData{}
for _, mount_var := range container.VolumeMounts {
log.G(Ctx).Debug("-- Retrieving data for mountpoint " + mount_var.Name)

var podVolumeSpec *v1.VolumeSource

for _, vol := range pod.Spec.Volumes {

if vol.Name == mount_var.Name {
podVolumeSpec = &vol.VolumeSource
}

if podVolumeSpec != nil && podVolumeSpec.ConfigMap != nil {
log.G(Ctx).Info("--- Retrieving ConfigMap " + podVolumeSpec.ConfigMap.Name)
cmvs := podVolumeSpec.ConfigMap

configMap, err := Clientset.CoreV1().ConfigMaps(pod.Namespace).Get(cmvs.Name, metav1.GetOptions{})

if err != nil {
log.G(Ctx).Error(err)
return commonIL.RetrievedPodData{}, err
} else {
log.G(Ctx).Debug("---- Retrieved ConfigMap " + podVolumeSpec.ConfigMap.Name)
}

if configMap != nil {
if retrieved_data.Containers == nil {
retrieved_data.Containers = append(retrieved_data.Containers, commonIL.RetrievedContainer{Name: container.Name})
}
retrieved_data.Containers[len(retrieved_data.Containers)-1].ConfigMaps = append(retrieved_data.Containers[len(retrieved_data.Containers)-1].ConfigMaps, *configMap)
}

} else if podVolumeSpec != nil && podVolumeSpec.Secret != nil {
log.G(Ctx).Info("--- Retrieving Secret " + podVolumeSpec.Secret.SecretName)
svs := podVolumeSpec.Secret

secret, err := Clientset.CoreV1().Secrets(pod.Namespace).Get(svs.SecretName, metav1.GetOptions{})

if err != nil {
log.G(Ctx).Error(err)
return commonIL.RetrievedPodData{}, err
} else {
log.G(Ctx).Debug("---- Retrieved Secret " + svs.SecretName)
}

if secret.Data != nil {
if retrieved_data.Containers == nil {
retrieved_data.Containers = append(retrieved_data.Containers, commonIL.RetrievedContainer{Name: container.Name})
}
retrieved_data.Containers[len(retrieved_data.Containers)-1].Secrets = append(retrieved_data.Containers[len(retrieved_data.Containers)-1].Secrets, *secret)
}

} else if podVolumeSpec != nil && podVolumeSpec.EmptyDir != nil {
edPath := filepath.Join(commonIL.InterLinkConfigInst.DataRootFolder, pod.Namespace+"-"+string(pod.UID)+"/"+"emptyDirs/"+vol.Name)
if retrieved_data.Containers == nil {
retrieved_data.Containers = append(retrieved_data.Containers, commonIL.RetrievedContainer{Name: container.Name})
}
retrieved_data.Containers[len(retrieved_data.Containers)-1].EmptyDirs = append(retrieved_data.Containers[len(retrieved_data.Containers)-1].EmptyDirs, edPath)
}
}
}
return retrieved_data, nil
}
Loading

0 comments on commit 6a361d2

Please sign in to comment.