Skip to content

Commit

Permalink
Merge pull request #155 from Surax98/fixing_squash
Browse files Browse the repository at this point in the history
Functions/types description and minor fix
  • Loading branch information
dciangot authored Feb 28, 2024
2 parents 31bee38 + 92a45db commit 4ca7404
Show file tree
Hide file tree
Showing 21 changed files with 110 additions and 41 deletions.
1 change: 1 addition & 0 deletions cmd/interlink/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func main() {
mutex.HandleFunc("/getLogs", interLinkAPIs.GetLogsHandler)
mutex.HandleFunc("/updateCache", interlink.UpdateCacheHandler)
err = http.ListenAndServe(":"+commonIL.InterLinkConfigInst.Interlinkport, mutex)

if err != nil {
log.G(interlink.Ctx).Fatal(err)
}
Expand Down
1 change: 1 addition & 0 deletions examples/interlink-slurm/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@

Check [here](/docs/docs/tutorial-users/01-quick-start.md) for a Quick Start tutorial!
3 changes: 3 additions & 0 deletions pkg/common/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ var InterLinkConfigInst InterLinkConfig
var Clientset *kubernetes.Clientset

// TODO: implement factory design

// NewInterLinkConfig returns a variable of type InterLinkConfig, used in many other functions and the first encountered error.
func NewInterLinkConfig() (InterLinkConfig, error) {
if !InterLinkConfigInst.set {
var path string
Expand Down Expand Up @@ -124,6 +126,7 @@ func NewInterLinkConfig() (InterLinkConfig, error) {
return InterLinkConfigInst, nil
}

// PingInterLink pings the InterLink API and returns true if there's an answer. The second return value is given by the answer provided by the API.
func PingInterLink(ctx context.Context) (bool, int, error) {
log.G(ctx).Info("Pinging: " + InterLinkConfigInst.Interlinkurl + ":" + InterLinkConfigInst.Interlinkport + "/pinglink")
retVal := -1
Expand Down
32 changes: 7 additions & 25 deletions pkg/common/types.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,41 @@
package common

import (
"io/fs"
"time"

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

// PodCreateRequests is a struct holding data for a create request. Retrieved ConfigMaps and Secrets are held along the Pod description itself.
type PodCreateRequests struct {
Pod v1.Pod `json:"pod"`
ConfigMaps []v1.ConfigMap `json:"configmaps"`
Secrets []v1.Secret `json:"secrets"`
}

// PodStatus is a simplified v1.Pod struct, holding only necessary variables to uniquely identify a job/service in the sidecar. It is used to request
type PodStatus struct {
PodName string `json:"name"`
PodUID string `json:"UID"`
PodNamespace string `json:"namespace"`
Containers []v1.ContainerStatus `json:"containers"`
}

type PodStatusWithMap struct {
PodName string `json:"name"`
PodUID string `json:"UID"`
PodNamespace string `json:"namespace"`
Containers map[string]v1.ContainerStatus `json:"containers"`
}

// RetrievedContainer is used in InterLink to rearrange data structure in a suitable way for the sidecar
type RetrievedContainer struct {
Name string `json:"name"`
ConfigMaps []v1.ConfigMap `json:"configMaps"`
Secrets []v1.Secret `json:"secrets"`
EmptyDirs []string `json:"emptyDirs"`
}

// RetrievedPoData is used in InterLink to rearrange data structure in a suitable way for the sidecar
type RetrievedPodData struct {
Pod v1.Pod `json:"pod"`
Containers []RetrievedContainer `json:"container"`
}

type ConfigMapSecret struct {
Key string `json:"Key"`
Value string `json:"Value"`
Path string `json:"Path"`
Kind string `json:"Kind"`
Mode fs.FileMode `json:"Mode"`
}

// InterLinkConfig holds the whole configuration
type InterLinkConfig struct {
VKConfigPath string `yaml:"VKConfigPath"`
VKTokenFile string `yaml:"VKTokenFile"`
Expand Down Expand Up @@ -74,15 +63,7 @@ type InterLinkConfig struct {
set bool
}

type ServiceAccount struct {
Name string
Token string
CA string
URL string
ClusterName string
Config string
}

// ContainerLogOpts is a struct in which it is possible to specify options to retrieve logs from the sidecar
type ContainerLogOpts struct {
Tail int `json:"Tail"`
LimitBytes int `json:"Bytes"`
Expand All @@ -93,6 +74,7 @@ type ContainerLogOpts struct {
SinceTime time.Time `json:"SinceTime"`
}

// LogStruct is needed to identify the job/container running on the sidecar to retrieve the logs from. Using ContainerLogOpts struct allows to specify more options on how to collect logs
type LogStruct struct {
Namespace string `json:"Namespace"`
PodUID string `json:"PodUID"`
Expand Down
1 change: 1 addition & 0 deletions pkg/interlink/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
commonIL "github.com/intertwin-eu/interlink/pkg/common"
)

// CreateHandler collects and rearranges all needed ConfigMaps/Secrets/EmptyDirs to ship them to the sidecar, then sends a response to the client
func (h *InterLinkHandler) CreateHandler(w http.ResponseWriter, r *http.Request) {
log.G(Ctx).Info("InterLink: received Create call")

Expand Down
7 changes: 7 additions & 0 deletions pkg/interlink/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
commonIL "github.com/intertwin-eu/interlink/pkg/common"
)

// DeleteHandler deletes the cached status for the provided Pod and forwards the request to the sidecar
func (h *InterLinkHandler) DeleteHandler(w http.ResponseWriter, r *http.Request) {
log.G(Ctx).Info("InterLink: received Delete call")

Expand All @@ -37,6 +38,12 @@ func (h *InterLinkHandler) DeleteHandler(w http.ResponseWriter, r *http.Request)

deleteCachedStatus(string(pod.UID))
req, err = http.NewRequest(http.MethodPost, h.Config.Sidecarurl+":"+h.Config.Sidecarport+"/delete", reader)
if err != nil {
statusCode = http.StatusInternalServerError
w.WriteHeader(statusCode)
log.G(Ctx).Error(err)
return
}

req.Header.Set("Content-Type", "application/json")
log.G(Ctx).Info("InterLink: forwarding Delete call to sidecar")
Expand Down
17 changes: 11 additions & 6 deletions pkg/interlink/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type MutexStatuses struct {

var PodStatuses MutexStatuses

// getData retrieves ConfigMaps, Secrets and EmptyDirs from the provided pod by calling the retrieveData function.
// The config is needed by the retrieveData function.
// The function aggregates the return values of retrieveData function in a commonIL.RetrievedPodData variable and returns it, along with the first encountered error.
func getData(config commonIL.InterLinkConfig, pod commonIL.PodCreateRequests) (commonIL.RetrievedPodData, error) {
log.G(Ctx).Debug(pod.ConfigMaps)
var retrievedData commonIL.RetrievedPodData
Expand All @@ -35,6 +38,9 @@ func getData(config commonIL.InterLinkConfig, pod commonIL.PodCreateRequests) (c
return retrievedData, nil
}

// retrieveData retrieves ConfigMaps, Secrets and EmptyDirs.
// The config is needed to specify the EmptyDirs mounting point.
// It returns the retrieved data in a variable of type commonIL.RetrievedContainer and the first encountered error.
func retrieveData(config commonIL.InterLinkConfig, pod commonIL.PodCreateRequests, container v1.Container) (commonIL.RetrievedContainer, error) {
retrievedData := commonIL.RetrievedContainer{}
for _, mountVar := range container.VolumeMounts {
Expand Down Expand Up @@ -76,12 +82,14 @@ func retrieveData(config commonIL.InterLinkConfig, pod commonIL.PodCreateRequest
return retrievedData, nil
}

// deleteCachedStatus locks the map PodStatuses and delete the uid key from that map
func deleteCachedStatus(uid string) {
PodStatuses.mu.Lock()
delete(PodStatuses.Statuses, uid)
PodStatuses.mu.Unlock()
}

// checkIfCached checks if the uid key is present in the PodStatuses map and returns a bool
func checkIfCached(uid string) bool {
_, ok := PodStatuses.Statuses[uid]

Expand All @@ -92,16 +100,13 @@ func checkIfCached(uid string) bool {
}
}

// updateStatuses locks and updates the PodStatuses map with the statuses contained in the returnedStatuses slice
func updateStatuses(returnedStatuses []commonIL.PodStatus) {
PodStatuses.mu.Lock()

for _, new := range returnedStatuses {
log.G(Ctx).Info(PodStatuses.Statuses, new)
if checkIfCached(new.PodUID) {
PodStatuses.Statuses[new.PodUID] = new
} else {
PodStatuses.Statuses[new.PodUID] = new
}
//log.G(Ctx).Debug(PodStatuses.Statuses, new)
PodStatuses.Statuses[new.PodUID] = new
}

PodStatuses.mu.Unlock()
Expand Down
4 changes: 2 additions & 2 deletions pkg/interlink/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (h *InterLinkHandler) GetLogsHandler(w http.ResponseWriter, r *http.Request
} else {
w.Write([]byte("Both SinceSeconds and SinceTime set. Set only one of them"))
}
log.G(Ctx).Error(errors.New("Check Opts configurations"))
log.G(Ctx).Error(errors.New("check opts configurations"))
return
}

Expand All @@ -54,7 +54,7 @@ func (h *InterLinkHandler) GetLogsHandler(w http.ResponseWriter, r *http.Request
return
}
reader := bytes.NewReader(bodyBytes)
req, err := http.NewRequest(http.MethodPost, h.Config.Sidecarurl+":"+h.Config.Sidecarport+"/getLogs", reader)
req, err := http.NewRequest(http.MethodGet, h.Config.Sidecarurl+":"+h.Config.Sidecarport+"/getLogs", reader)
if err != nil {
log.G(Ctx).Fatal(err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/interlink/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/containerd/containerd/log"
)

// Ping is just a very basic Ping function
func (h *InterLinkHandler) Ping(w http.ResponseWriter, r *http.Request) {
log.G(Ctx).Info("InterLink: received Ping call")
w.WriteHeader(http.StatusOK)
Expand Down
1 change: 1 addition & 0 deletions pkg/interlink/updateCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/containerd/containerd/log"
)

// UpdateCacheHandler is responsible for deleting not-available-anymore Pods on the Virtual Kubelet from the InterLink caching structure
func UpdateCacheHandler(w http.ResponseWriter, r *http.Request) {
log.G(Ctx).Info("InterLink: received UpdateCache call")

Expand Down
1 change: 1 addition & 0 deletions pkg/sidecars/docker/Create.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
commonIL "github.com/intertwin-eu/interlink/pkg/common"
)

// CreateHandler creates a Docker Container based on data provided by the InterLink API.
func (h *SidecarHandler) CreateHandler(w http.ResponseWriter, r *http.Request) {
log.G(h.Ctx).Info("Docker Sidecar: received Create call")
var execReturn exec.ExecResult
Expand Down
1 change: 1 addition & 0 deletions pkg/sidecars/docker/Delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
v1 "k8s.io/api/core/v1"
)

// DeleteHandler stops and deletes Docker containers from provided data
func (h *SidecarHandler) DeleteHandler(w http.ResponseWriter, r *http.Request) {
log.G(h.Ctx).Info("Docker Sidecar: received Delete call")
var execReturn exec.ExecResult
Expand Down
1 change: 1 addition & 0 deletions pkg/sidecars/docker/GetLogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
commonIL "github.com/intertwin-eu/interlink/pkg/common"
)

// GetLogsHandler performs a Docker logs command and returns its manipulated output
func (h *SidecarHandler) GetLogsHandler(w http.ResponseWriter, r *http.Request) {
log.G(h.Ctx).Info("Docker Sidecar: received GetLogs call")
var req commonIL.LogStruct
Expand Down
1 change: 1 addition & 0 deletions pkg/sidecars/docker/Status.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
commonIL "github.com/intertwin-eu/interlink/pkg/common"
)

// StatusHandler checks Docker Container's status by running docker ps -af command and returns that status
func (h *SidecarHandler) StatusHandler(w http.ResponseWriter, r *http.Request) {
log.G(h.Ctx).Info("Docker Sidecar: received GetStatus call")
var resp []commonIL.PodStatus
Expand Down
7 changes: 7 additions & 0 deletions pkg/sidecars/docker/aux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type SidecarHandler struct {
Ctx context.Context
}

// prepareMounts iterates along the struct provided in the data parameter and checks for ConfigMaps, Secrets and EmptyDirs to be mounted.
// For each element found, the mountData function is called.
// It returns a string composed as the docker -v command to bind mount directories and files and the first encountered error.
func prepareMounts(Ctx context.Context, config commonIL.InterLinkConfig, data []commonIL.RetrievedPodData, container v1.Container) (string, error) {
log.G(Ctx).Info("- Preparing mountpoints for " + container.Name)
mountedData := ""
Expand Down Expand Up @@ -79,6 +82,10 @@ func prepareMounts(Ctx context.Context, config commonIL.InterLinkConfig, data []
return mountedData, nil
}

// mountData is called by prepareMounts and creates files and directory according to their definition in the pod structure.
// The data parameter is an interface and it can be of type v1.ConfigMap, v1.Secret and string (for the empty dir).
// Returns a string which is a bind mount of the file/directory. Example: path/to/file/on/host:path/to/file/in/container.
// It also returns the first encountered error.
func mountData(Ctx context.Context, config commonIL.InterLinkConfig, pod v1.Pod, data interface{}, container v1.Container) ([]string, error) {
wd, err := os.Getwd()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/sidecars/slurm/Create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
commonIL "github.com/intertwin-eu/interlink/pkg/common"
)

// SubmitHandler generates and submits a SLURM batch script according to provided data.
// 1 Pod = 1 Job. If a Pod has multiple containers, every container is a line with it's parameters in the SLURM script.
func (h *SidecarHandler) SubmitHandler(w http.ResponseWriter, r *http.Request) {
log.G(h.Ctx).Info("Slurm Sidecar: received Submit call")
statusCode := http.StatusOK
Expand Down
1 change: 1 addition & 0 deletions pkg/sidecars/slurm/Delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
v1 "k8s.io/api/core/v1"
)

// StopHandler runs a scancel command, updating JIDs and cached statuses
func (h *SidecarHandler) StopHandler(w http.ResponseWriter, r *http.Request) {
log.G(h.Ctx).Info("Slurm Sidecar: received Stop call")
statusCode := http.StatusOK
Expand Down
2 changes: 2 additions & 0 deletions pkg/sidecars/slurm/GetLogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
commonIL "github.com/intertwin-eu/interlink/pkg/common"
)

// GetLogsHandler reads Jobs' output file to return what's logged inside.
// What's returned is based on the provided parameters (Tail/LimitBytes/Timestamps/etc)
func (h *SidecarHandler) GetLogsHandler(w http.ResponseWriter, r *http.Request) {
log.G(h.Ctx).Info("Docker Sidecar: received GetLogs call")
var req commonIL.LogStruct
Expand Down
1 change: 1 addition & 0 deletions pkg/sidecars/slurm/Status.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
commonIL "github.com/intertwin-eu/interlink/pkg/common"
)

// StatusHandler performs a squeue --me and uses regular expressions to get the running Jobs' status
func (h *SidecarHandler) StatusHandler(w http.ResponseWriter, r *http.Request) {
var req []*v1.Pod
var resp []commonIL.PodStatus
Expand Down
Loading

0 comments on commit 4ca7404

Please sign in to comment.