Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sosivio): add the ability to query sosivio API #362

Open
wants to merge 14 commits into
base: release/2.17
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions http/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/portainer/agent/http/handler/kubernetesproxy"
"github.com/portainer/agent/http/handler/nomadproxy"
"github.com/portainer/agent/http/handler/ping"
"github.com/portainer/agent/http/handler/sosivio"
"github.com/portainer/agent/http/handler/websocket"
"github.com/portainer/agent/http/proxy"
"github.com/portainer/agent/http/security"
Expand All @@ -40,6 +41,7 @@ type Handler struct {
webSocketHandler *websocket.Handler
hostHandler *host.Handler
pingHandler *ping.Handler
sosivioHandler *sosivio.Handler
containerPlatform agent.ContainerPlatform
}

Expand Down Expand Up @@ -78,6 +80,7 @@ func NewHandler(config *Config) *Handler {
webSocketHandler: websocket.NewHandler(config.ClusterService, config.RuntimeConfiguration, notaryService, config.KubeClient),
hostHandler: host.NewHandler(config.SystemService, agentProxy, notaryService),
pingHandler: ping.NewHandler(),
sosivioHandler: sosivio.NewHandler(notaryService),
containerPlatform: config.ContainerPlatform,
}
}
Expand Down Expand Up @@ -119,6 +122,8 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, request *http.Request) {
h.kubernetesProxyHandler.ServeHTTP(rw, request)
case strings.HasPrefix(request.URL.Path, "/nomad"):
h.nomadProxyHandler.ServeHTTP(rw, request)
case strings.HasPrefix(request.URL.Path, "/sosivio"):
h.sosivioHandler.ServeHTTP(rw, request)
case strings.HasPrefix(request.URL.Path, "/"):
h.dockerProxyHandler.ServeHTTP(rw, request)
}
Expand Down
28 changes: 28 additions & 0 deletions http/handler/sosivio/commandcenter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package sosivio

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

httperror "github.com/portainer/libhttp/error"
)

// GET request on /sosivio/commandcenter
func (handler *Handler) commandCenter(rw http.ResponseWriter, r *http.Request) *httperror.HandlerError {

// TODO: REVIEW-POC-SOSIVIO
// Make use of a proper HTTP client here to manage timeouts
// Alternatively, a proxy can probably be used to handle ALL Sosivio related requests.
resp, err := http.Get("http://poc-api.portainer.svc.cluster.local:8088/api/v1/commandcenter?" + r.URL.RawQuery)
if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to query Sosivio API", err}
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to parse Sosivio API response", err}
}

return Raw(rw, body)
}
41 changes: 41 additions & 0 deletions http/handler/sosivio/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package sosivio

import (
"net/http"

"github.com/gorilla/mux"
"github.com/portainer/agent/http/security"
httperror "github.com/portainer/libhttp/error"
)

// Handler is the HTTP handler used to handle Sosivio operations.
type Handler struct {
*mux.Router
}

// NewHandler returns a pointer to an Handler
// It sets the associated handle functions for all the Sosivio related HTTP endpoints.
func NewHandler(notaryService *security.NotaryService) *Handler {
h := &Handler{
Router: mux.NewRouter(),
}

h.Handle("/sosivio/namespaces",
notaryService.DigitalSignatureVerification(httperror.LoggerHandler(h.namespaces))).Methods(http.MethodGet)
h.Handle("/sosivio/pods",
notaryService.DigitalSignatureVerification(httperror.LoggerHandler(h.pods))).Methods(http.MethodGet)
h.Handle("/sosivio/nodes",
notaryService.DigitalSignatureVerification(httperror.LoggerHandler(h.nodes))).Methods(http.MethodGet)
h.Handle("/sosivio/commandcenter",
notaryService.DigitalSignatureVerification(httperror.LoggerHandler(h.commandCenter))).Methods(http.MethodGet)
return h
}

// TODO: REVIEW-POC-SOSIVIO
// Port to libhttp.
// Raw returns raw data. Returns a pointer to a
// HandlerError if encoding fails.
func Raw(rw http.ResponseWriter, data []byte) *httperror.HandlerError {
rw.Write(data)
return nil
}
28 changes: 28 additions & 0 deletions http/handler/sosivio/namespaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package sosivio

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

httperror "github.com/portainer/libhttp/error"
)

// GET request on /sosivio/namespaces
func (handler *Handler) namespaces(rw http.ResponseWriter, r *http.Request) *httperror.HandlerError {

// TODO: REVIEW-POC-SOSIVIO
// Make use of a proper HTTP client here to manage timeouts
// Alternatively, a proxy can probably be used to handle ALL Sosivio related requests.
resp, err := http.Get("http://poc-api.portainer.svc.cluster.local:8088/api/v1/namespaces?" + r.URL.RawQuery)
if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to query Sosivio API", err}
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to parse Sosivio API response", err}
}

return Raw(rw, body)
}
28 changes: 28 additions & 0 deletions http/handler/sosivio/nodes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package sosivio

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

httperror "github.com/portainer/libhttp/error"
)

// GET request on /sosivio/nodes
func (handler *Handler) nodes(rw http.ResponseWriter, r *http.Request) *httperror.HandlerError {

// TODO: REVIEW-POC-SOSIVIO
// Make use of a proper HTTP client here to manage timeouts
// Alternatively, a proxy can probably be used to handle ALL Sosivio related requests.
resp, err := http.Get("http://poc-api.portainer.svc.cluster.local:8088/api/v1/node?" + r.URL.RawQuery)
if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to query Sosivio API", err}
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to parse Sosivio API response", err}
}

return Raw(rw, body)
}
28 changes: 28 additions & 0 deletions http/handler/sosivio/pods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package sosivio

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

httperror "github.com/portainer/libhttp/error"
)

// GET request on /sosivio/pods
func (handler *Handler) pods(rw http.ResponseWriter, r *http.Request) *httperror.HandlerError {

// TODO: REVIEW-POC-SOSIVIO
// Make use of a proper HTTP client here to manage timeouts
// Alternatively, a proxy can probably be used to handle ALL Sosivio related requests.
resp, err := http.Get("http://poc-api.portainer.svc.cluster.local:8088/api/v1/pod?" + r.URL.RawQuery)
if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to query Sosivio API", err}
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to parse Sosivio API response", err}
}

return Raw(rw, body)
}