Skip to content

Commit

Permalink
Logs handler fix + upgrade go and vk (#49)
Browse files Browse the repository at this point in the history
* update vk version + podHandlers

* pod vendors

* fix vk nodename + http hande fixes

* ioutil depprecated + main http server above main

---------

Co-authored-by: Diego Ciangottini <[email protected]>
  • Loading branch information
dciangot and dciangot authored Aug 30, 2023
1 parent cd2b686 commit 141378c
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 25 deletions.
40 changes: 40 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,46 @@ func main() {
// fmt.Println("pods:", pods[pod].Name)
// }

// start podHandler
handlerPodConfig := api.PodHandlerConfig{
GetContainerLogs: nodeProvider.GetLogs,
GetPods: nodeProvider.GetPods,
GetStatsSummary: nodeProvider.GetStatsSummary,
}

mux := http.NewServeMux()

podRoutes := api.PodHandlerConfig{
GetContainerLogs: handlerPodConfig.GetContainerLogs,
GetStatsSummary: handlerPodConfig.GetStatsSummary,
GetPods: handlerPodConfig.GetPods,
}

api.AttachPodRoutes(podRoutes, mux, true)

parsedIP := net.ParseIP(os.Getenv("POD_IP"))
retriever := newSelfSignedCertificateRetriever(cfg.NodeName, parsedIP)

server := &http.Server{
Addr: fmt.Sprintf("0.0.0.0:%d", 10255),
Handler: mux,
ReadHeaderTimeout: 10 * time.Second, // Required to limit the effects of the Slowloris attack.
TLSConfig: &tls.Config{
GetCertificate: retriever,
MinVersion: tls.VersionTLS12,
},
}

go func() {
log.G(ctx).Infof("Starting the virtual kubelet HTTPs server listening on %q", server.Addr)

// Key and certificate paths are not specified, since already configured as part of the TLSConfig.
if err := server.ListenAndServeTLS("", ""); err != nil {
log.G(ctx).Errorf("Failed to start the HTTPs server: %v", err)
os.Exit(1)
}
}()

pc, err := node.NewPodController(podControllerConfig) // <-- instatiates the pod controller
if err != nil {
log.G(ctx).Fatal(err)
Expand Down
3 changes: 1 addition & 2 deletions pkg/common/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
Expand Down Expand Up @@ -192,7 +191,7 @@ func NewServiceAccount() error {
time.Sleep(5 * time.Second)
continue
} else {
returnValue, _ = ioutil.ReadAll(resp.Body)
returnValue, _ = os.ReadAll(resp.Body)

Check failure on line 194 in pkg/common/func.go

View workflow job for this annotation

GitHub Actions / goreleaser

undefined: os.ReadAll
}

if resp.StatusCode == http.StatusOK {
Expand Down
5 changes: 2 additions & 3 deletions pkg/interlink/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package interlink
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"time"

Expand All @@ -14,7 +13,7 @@ import (

func CreateHandler(w http.ResponseWriter, r *http.Request) {
log.G(Ctx).Info("InterLink: received Create call")
bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := os.ReadAll(r.Body)
statusCode := http.StatusOK
if err != nil {
statusCode = http.StatusInternalServerError
Expand Down Expand Up @@ -91,7 +90,7 @@ func CreateHandler(w http.ResponseWriter, r *http.Request) {
log.G(Ctx).Debug(statusCode)
}

returnValue, _ := ioutil.ReadAll(resp.Body)
returnValue, _ := os.ReadAll(resp.Body)
log.G(Ctx).Debug(string(returnValue))
w.WriteHeader(statusCode)
w.Write(returnValue)
Expand Down
6 changes: 3 additions & 3 deletions pkg/interlink/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package interlink
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"os"

"github.com/containerd/containerd/log"
commonIL "github.com/intertwin-eu/interlink/pkg/common"
Expand All @@ -13,7 +13,7 @@ import (

func DeleteHandler(w http.ResponseWriter, r *http.Request) {
log.G(Ctx).Info("InterLink: received Delete call")
bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := os.ReadAll(r.Body)
statusCode := http.StatusOK

if err != nil {
Expand Down Expand Up @@ -41,7 +41,7 @@ func DeleteHandler(w http.ResponseWriter, r *http.Request) {
return
}

returnValue, _ := ioutil.ReadAll(resp.Body)
returnValue, _ := os.ReadAll(resp.Body)
statusCode = resp.StatusCode

if statusCode != http.StatusOK {
Expand Down
3 changes: 1 addition & 2 deletions pkg/interlink/kubeCFG.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package interlink

import (
"io/ioutil"
"net/http"
"os"

Expand All @@ -15,7 +14,7 @@ func SetKubeCFGHandler(w http.ResponseWriter, r *http.Request) {
path := "/tmp/.kube/"
statusCode := http.StatusOK

bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := os.ReadAll(r.Body)
if err != nil {
statusCode = http.StatusInternalServerError
w.WriteHeader(statusCode)
Expand Down
6 changes: 3 additions & 3 deletions pkg/interlink/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package interlink

import (
"bytes"
"io/ioutil"
"io"
"net/http"
"strconv"

Expand All @@ -13,7 +13,7 @@ import (
func StatusHandler(w http.ResponseWriter, r *http.Request) {
statusCode := http.StatusOK
log.G(Ctx).Info("InterLink: received GetStatus call")
bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
log.G(Ctx).Fatal(err)
}
Expand All @@ -38,7 +38,7 @@ func StatusHandler(w http.ResponseWriter, r *http.Request) {
statusCode = http.StatusInternalServerError
}

returnValue, _ := ioutil.ReadAll(resp.Body)
returnValue, _ := io.ReadAll(resp.Body)
log.G(Ctx).Debug("InterLink: status " + string(returnValue))

w.WriteHeader(statusCode)
Expand Down
8 changes: 4 additions & 4 deletions pkg/sidecars/docker/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package docker

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"os"
"strings"
Expand All @@ -19,7 +19,7 @@ func StatusHandler(w http.ResponseWriter, r *http.Request) {
var req []*v1.Pod
statusCode := http.StatusOK

bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
statusCode = http.StatusInternalServerError
w.WriteHeader(statusCode)
Expand Down Expand Up @@ -86,7 +86,7 @@ func CreateHandler(w http.ResponseWriter, r *http.Request) {
log.G(Ctx).Info("Docker Sidecar: received Create call")
var execReturn exec.ExecResult
statusCode := http.StatusOK
bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
statusCode = http.StatusInternalServerError
w.WriteHeader(statusCode)
Expand Down Expand Up @@ -201,7 +201,7 @@ func DeleteHandler(w http.ResponseWriter, r *http.Request) {
log.G(Ctx).Info("Docker Sidecar: received Delete call")
var execReturn exec.ExecResult
statusCode := http.StatusOK
bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := io.ReadAll(r.Body)

if err != nil {
statusCode = http.StatusInternalServerError
Expand Down
8 changes: 4 additions & 4 deletions pkg/sidecars/slurm/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package slurm

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"os"
"strings"
Expand All @@ -20,7 +20,7 @@ func SubmitHandler(w http.ResponseWriter, r *http.Request) {
log.G(Ctx).Info("Slurm Sidecar: received Submit call")
statusCode := http.StatusOK

bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
statusCode = http.StatusInternalServerError
w.WriteHeader(statusCode)
Expand Down Expand Up @@ -128,7 +128,7 @@ func StopHandler(w http.ResponseWriter, r *http.Request) {
log.G(Ctx).Info("Slurm Sidecar: received Stop call")
statusCode := http.StatusOK

bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
statusCode = http.StatusInternalServerError
w.WriteHeader(statusCode)
Expand Down Expand Up @@ -172,7 +172,7 @@ func StatusHandler(w http.ResponseWriter, r *http.Request) {
statusCode := http.StatusOK
log.G(Ctx).Info("Slurm Sidecar: received GetStatus call")

bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
statusCode = http.StatusInternalServerError
w.WriteHeader(statusCode)
Expand Down
8 changes: 4 additions & 4 deletions pkg/virtualkubelet/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"strconv"
Expand Down Expand Up @@ -47,7 +47,7 @@ func createRequest(pods []*v1.Pod, token string) ([]byte, error) {
return nil, errors.New("Unexpected error occured while creating Pods. Status code: " + strconv.Itoa(resp.StatusCode) + ". Check InterLink's logs for further informations")
} else {
log.G(context.Background()).Info(string(returnValue))
returnValue, err = ioutil.ReadAll(resp.Body)
returnValue, err = io.ReadAll(resp.Body)
if err != nil {
log.L.Error(err)
return nil, err
Expand Down Expand Up @@ -84,7 +84,7 @@ func deleteRequest(pods []*v1.Pod, token string) ([]byte, error) {
if statusCode != http.StatusOK {
return nil, errors.New("Unexpected error occured while deleting Pods. Status code: " + strconv.Itoa(resp.StatusCode) + ". Check InterLink's logs for further informations")
} else {
returnValue, _ = ioutil.ReadAll(resp.Body)
returnValue, _ = os.ReadAll(resp.Body)
log.G(context.Background()).Info(string(returnValue))
var response []commonIL.PodStatus
err = json.Unmarshal(returnValue, &response)
Expand Down Expand Up @@ -124,7 +124,7 @@ func statusRequest(podsList []*v1.Pod, token string) ([]byte, error) {
if resp.StatusCode != http.StatusOK {
return nil, errors.New("Unexpected error occured while getting status. Status code: " + strconv.Itoa(resp.StatusCode) + ". Check InterLink's logs for further informations")
} else {
returnValue, _ = ioutil.ReadAll(resp.Body)
returnValue, _ = io.ReadAll(resp.Body)
if err != nil {
log.L.Error(err)
return nil, err
Expand Down

0 comments on commit 141378c

Please sign in to comment.