Skip to content

Commit bede95a

Browse files
committed
Proxy API For Mkube
1 parent be5cd7f commit bede95a

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ 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+
client := &http.Client{}
169+
serverMkube(client, w, r)
167170
case strings.HasPrefix(r.URL.Path, "/api"):
168171
next.ServeHTTP(w, r)
169172
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: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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(client *http.Client, 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+
// set the HTTP method, url, and m3Req body
35+
m3Req, err := http.NewRequest(req.Method, targetURL, req.Body)
36+
if err != nil {
37+
apiErrors.ServeError(w, req, err)
38+
return
39+
}
40+
41+
// set the m3Req headers
42+
m3Req.Header = req.Header
43+
resp, err := client.Do(m3Req)
44+
if err != nil {
45+
if strings.Contains(err.Error(), "connection refused") {
46+
apiErrors.ServeError(w, req, errors.New("service M3 is not available"))
47+
return
48+
}
49+
apiErrors.ServeError(w, req, err)
50+
return
51+
}
52+
defer resp.Body.Close()
53+
// Write the m3 response to the response writer
54+
scanner := bufio.NewScanner(resp.Body)
55+
for i := 0; scanner.Scan() && i < 5; i++ {
56+
w.Write(scanner.Bytes())
57+
}
58+
if err := scanner.Err(); err != nil {
59+
apiErrors.ServeError(w, req, err)
60+
}
61+
62+
}

restapi/mkube_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
"net/http"
21+
"net/http/httptest"
22+
"testing"
23+
)
24+
25+
func Test_serverMkube(t *testing.T) {
26+
27+
// Start a local HTTP server
28+
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
29+
rw.Write([]byte(`OK`))
30+
}))
31+
// Close the server when test finishes
32+
defer server.Close()
33+
w := httptest.NewRecorder()
34+
type args struct {
35+
client *http.Client
36+
w http.ResponseWriter
37+
req *http.Request
38+
}
39+
tests := []struct {
40+
name string
41+
args args
42+
}{
43+
{
44+
name: "Successful request",
45+
args: args{
46+
client: server.Client(),
47+
w: w,
48+
req: &http.Request{},
49+
},
50+
},
51+
}
52+
for _, tt := range tests {
53+
t.Run(tt.name, func(t *testing.T) {
54+
})
55+
}
56+
}

0 commit comments

Comments
 (0)