Skip to content

Commit 68029e4

Browse files
committed
Proxy API For Mkube
1 parent a805a49 commit 68029e4

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

restapi/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,8 @@ func getSecureFeaturePolicy() string {
228228
func getSecureExpectCTHeader() string {
229229
return env.Get(McsSecureExpectCTHeader, "")
230230
}
231+
232+
// getM3Host returns the hostname of mkube
233+
func getM3Host() string {
234+
return env.Get(McsM3Host, "http://m3:8787")
235+
}

restapi/configure_mcs.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ func FileServerMiddleware(next http.Handler) http.Handler {
164164
switch {
165165
case strings.HasPrefix(r.URL.Path, "/ws"):
166166
serveWS(w, r)
167+
case strings.HasPrefix(r.URL.Path, "/api/v1/clusters"):
168+
serverMkube(w, r)
167169
case strings.HasPrefix(r.URL.Path, "/api"):
168170
next.ServeHTTP(w, r)
169171
default:

restapi/consts.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ const (
4949
McsSecureReferrerPolicy = "MCS_SECURE_REFERRER_POLICY"
5050
McsSecureFeaturePolicy = "MCS_SECURE_FEATURE_POLICY"
5151
McsSecureExpectCTHeader = "MCS_SECURE_EXPECT_CT_HEADER"
52+
McsM3Host = "MCS_M3_HOSTNAME"
5253
)

restapi/mkube.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// This file is part of MinIO Console Server
2+
// Copyright (c) 2020 MinIO, Inc.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package restapi
18+
19+
import (
20+
"bufio"
21+
"errors"
22+
"fmt"
23+
"net/http"
24+
"strings"
25+
26+
apiErrors "github.com/go-openapi/errors"
27+
)
28+
29+
// serverMkube handles calls for mkube
30+
func serverMkube(w http.ResponseWriter, req *http.Request) {
31+
// destination of the request, the mkube server
32+
targetURL := fmt.Sprintf("%s%s", getM3Host(), req.URL.String())
33+
34+
client := &http.Client{}
35+
36+
// set the HTTP method, url, and m3Req body
37+
m3Req, err := http.NewRequest(req.Method, targetURL, req.Body)
38+
if err != nil {
39+
apiErrors.ServeError(w, req, err)
40+
return
41+
}
42+
43+
// set the m3Req headers
44+
m3Req.Header = req.Header
45+
resp, err := client.Do(m3Req)
46+
if err != nil {
47+
if strings.Contains(err.Error(), "connection refused") {
48+
apiErrors.ServeError(w, req, errors.New("service M3 is not available"))
49+
return
50+
}
51+
apiErrors.ServeError(w, req, err)
52+
return
53+
}
54+
defer resp.Body.Close()
55+
// Write the m3 response to the response writer
56+
scanner := bufio.NewScanner(resp.Body)
57+
for i := 0; scanner.Scan() && i < 5; i++ {
58+
w.Write(scanner.Bytes())
59+
}
60+
if err := scanner.Err(); err != nil {
61+
apiErrors.ServeError(w, req, err)
62+
}
63+
64+
}

0 commit comments

Comments
 (0)