Use mux to handle req; get tail from req
This commit is contained in:
2
Gopkg.lock
generated
2
Gopkg.lock
generated
@@ -403,6 +403,6 @@
|
|||||||
[solve-meta]
|
[solve-meta]
|
||||||
analyzer-name = "dep"
|
analyzer-name = "dep"
|
||||||
analyzer-version = 1
|
analyzer-version = 1
|
||||||
inputs-digest = "e476a3cf3fc7c556db93b5f202d0f578cdca9cda551563756ea30261aff2bd9b"
|
inputs-digest = "db2f3e11453c573c43ded9e17f9e23e76111609d4936482d19a7d18930e1dcce"
|
||||||
solver-name = "gps-cdcl"
|
solver-name = "gps-cdcl"
|
||||||
solver-version = 1
|
solver-version = 1
|
||||||
|
|||||||
@@ -48,3 +48,7 @@
|
|||||||
[[constraint]]
|
[[constraint]]
|
||||||
name = "github.com/xeipuuv/gojsonschema"
|
name = "github.com/xeipuuv/gojsonschema"
|
||||||
revision = "0c8571ac0ce161a5feb57375a9cdf148c98c0f70"
|
revision = "0c8571ac0ce161a5feb57375a9cdf148c98c0f70"
|
||||||
|
|
||||||
|
[[constraint]]
|
||||||
|
name = "github.com/gorilla/mux"
|
||||||
|
version = "1.6.0"
|
||||||
|
|||||||
@@ -198,7 +198,7 @@ func (p *ACIProvider) GetPod(namespace, name string) (*v1.Pod, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetPodLogs returns the logs of a pod by name that is running inside ACI.
|
// GetPodLogs returns the logs of a pod by name that is running inside ACI.
|
||||||
func (p *ACIProvider) GetContainerLogs(namespace, podName, containerName string) (string, error) {
|
func (p *ACIProvider) GetContainerLogs(namespace, podName, containerName string, tail int) (string, error) {
|
||||||
logContent := ""
|
logContent := ""
|
||||||
cg, err := p.aciClient.GetContainerGroup(p.resourceGroup, fmt.Sprintf("%s-%s", namespace, podName))
|
cg, err := p.aciClient.GetContainerGroup(p.resourceGroup, fmt.Sprintf("%s-%s", namespace, podName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -215,7 +215,7 @@ func (p *ACIProvider) GetContainerLogs(namespace, podName, containerName string)
|
|||||||
// get logs from cg
|
// get logs from cg
|
||||||
retry := 10
|
retry := 10
|
||||||
for i := 0; i < retry; i++ {
|
for i := 0; i < retry; i++ {
|
||||||
cLogs, err := p.aciClient.GetContainerLogs(p.resourceGroup, cg.Name, containerName, 10)
|
cLogs, err := p.aciClient.GetContainerLogs(p.resourceGroup, cg.Name, containerName, tail)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
time.Sleep(5000 * time.Millisecond)
|
time.Sleep(5000 * time.Millisecond)
|
||||||
|
|||||||
@@ -140,7 +140,7 @@ func (p *HyperProvider) GetPod(namespace, name string) (*v1.Pod, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetContainerLogs retrieves the logs of a container by name from the provider.
|
// GetContainerLogs retrieves the logs of a container by name from the provider.
|
||||||
func (p *HyperProvider) GetContainerLogs(namespace, podName, containerName string) (string, error) {
|
func (p *HyperProvider) GetContainerLogs(namespace, podName, containerName string, tail int) (string, error) {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,40 +6,64 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
var p Provider
|
var p Provider
|
||||||
|
var r mux.Router
|
||||||
|
|
||||||
|
func NotFound(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Println("404 request not found")
|
||||||
|
http.Error(w, "404 request not found", http.StatusNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
func ApiserverStart(provider Provider) {
|
func ApiserverStart(provider Provider) {
|
||||||
p = provider
|
p = provider
|
||||||
http.HandleFunc("/", ApiServerHandler)
|
|
||||||
certFilePath := os.Getenv("APISERVER_CERT_LOCATION")
|
certFilePath := os.Getenv("APISERVER_CERT_LOCATION")
|
||||||
keyFilePath := os.Getenv("APISERVER_KEY_LOCATION")
|
keyFilePath := os.Getenv("APISERVER_KEY_LOCATION")
|
||||||
port := os.Getenv("KUBELET_PORT")
|
port := os.Getenv("KUBELET_PORT")
|
||||||
addr := fmt.Sprintf(":%s", port)
|
addr := fmt.Sprintf(":%s", port)
|
||||||
err := http.ListenAndServeTLS(addr, certFilePath, keyFilePath, nil)
|
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/containerLogs/{namespace}/{pod}/{container}", ApiServerHandler).Methods("GET")
|
||||||
|
r.NotFoundHandler = http.HandlerFunc(NotFound)
|
||||||
|
|
||||||
|
err := http.ListenAndServeTLS(addr, certFilePath, keyFilePath, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ApiServerHandler(w http.ResponseWriter, req *http.Request) {
|
func ApiServerHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
if req.Method == "GET" {
|
vars := mux.Vars(req)
|
||||||
if strings.ContainsAny(req.RequestURI, "containerLogs" ) {
|
|
||||||
reqParts := strings.Split(req.RequestURI, "/")
|
if len(vars) == 3 {
|
||||||
if len(reqParts) == 5 {
|
namespace := vars["namespace"]
|
||||||
namespace := reqParts[2]
|
pod := vars["pod"]
|
||||||
pod := reqParts[3]
|
container := vars["container"]
|
||||||
container := reqParts[4]
|
tail := 10
|
||||||
podsLogs, err := p.GetContainerLogs(namespace, pod, container)
|
q := req.URL.Query()
|
||||||
if err != nil {
|
queryTail := q.Get("tailLines")
|
||||||
io.WriteString(w, err.Error())
|
if queryTail != "" {
|
||||||
log.Println(err)
|
t, err := strconv.Atoi(queryTail)
|
||||||
} else {
|
if err != nil {
|
||||||
io.WriteString(w, podsLogs)
|
log.Println(err)
|
||||||
}
|
io.WriteString(w, err.Error())
|
||||||
|
} else {
|
||||||
|
tail = t
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
log.Println(tail)
|
||||||
|
podsLogs, err := p.GetContainerLogs(namespace, pod, container, tail)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
io.WriteString(w, err.Error())
|
||||||
|
} else {
|
||||||
|
io.WriteString(w, podsLogs)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Println("404 request pattern not found")
|
||||||
|
NotFound(w, req)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ type Provider interface {
|
|||||||
GetPod(namespace, name string) (*v1.Pod, error)
|
GetPod(namespace, name string) (*v1.Pod, error)
|
||||||
|
|
||||||
// GetContainerLogs retrieves the logs of a container by name from the provider.
|
// GetContainerLogs retrieves the logs of a container by name from the provider.
|
||||||
GetContainerLogs(namespace, podName, containerName string) (string, error)
|
GetContainerLogs(namespace, podName, containerName string, tail int) (string, error)
|
||||||
|
|
||||||
// GetPodStatus retrieves the status of a pod by name from the provider.
|
// GetPodStatus retrieves the status of a pod by name from the provider.
|
||||||
GetPodStatus(namespace, name string) (*v1.PodStatus, error)
|
GetPodStatus(namespace, name string) (*v1.PodStatus, error)
|
||||||
|
|||||||
Reference in New Issue
Block a user