Skip to content
Merged
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
9 changes: 4 additions & 5 deletions go/vt/vtctld/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"golang.org/x/net/context"

"vitess.io/vitess/go/acl"
"vitess.io/vitess/go/netutil"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/schemamanager"
Expand All @@ -48,6 +49,7 @@ import (
var (
localCell = flag.String("cell", "", "cell to use")
showTopologyCRUD = flag.Bool("vtctld_show_topology_crud", true, "Controls the display of the CRUD topology actions in the vtctld UI.")
proxyTablets = flag.Bool("proxy_tablets", false, "Setting this true will make vtctld proxy the tablet status instead of redirecting to them")
)

// This file implements a REST-style API for the vtctld web interface.
Expand Down Expand Up @@ -406,13 +408,10 @@ func initAPI(ctx context.Context, ts *topo.Server, actions *ActionRepository, re
MysqlPort: t.MysqlPort,
MasterTermStartTime: t.MasterTermStartTime,
}
tabletPath = fmt.Sprintf("http://%s:%d", t.Tablet.Hostname, t.PortMap["vt"])
if *proxyTablets {
tabletID := fmt.Sprintf("%s-%d", t.Tablet.Alias.Cell, t.Tablet.Alias.Uid)
addRemote(tabletID, tabletPath)
tab.URL = fmt.Sprintf("/vttablet/%s-%d", t.Tablet.Alias.Cell, t.Tablet.Alias.Uid)
tab.URL = fmt.Sprintf("/vttablet/%s-%d", t.Alias.Cell, t.Alias.Uid)
} else {
tab.URL = tabletPath
tab.URL = "http://" + netutil.JoinHostPort(t.Hostname, t.PortMap["vt"])
}
return tab, nil
})
Expand Down
87 changes: 87 additions & 0 deletions go/vt/vtctld/redirection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Copyright 2020 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package vtctld

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"strings"

"vitess.io/vitess/go/netutil"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/topoproto"
)

func initVTTabletRedirection(ts *topo.Server) {
http.HandleFunc("/vttablet/", func(w http.ResponseWriter, r *http.Request) {
splits := strings.SplitN(r.URL.Path, "/", 4)
if len(splits) < 4 {
log.Errorf("Invalid URL: %v", r.URL)
http.NotFound(w, r)
return
}
tabletID := splits[2]
tabletAlias, err := topoproto.ParseTabletAlias(tabletID)
if err != nil {
log.Errorf("Error parsting tablet alias %v: %v", tabletID, err)
http.NotFound(w, r)
return
}
tablet, err := ts.GetTablet(r.Context(), tabletAlias)
if err != nil {
log.Errorf("Error fetching tablet %v: %v", splits[2], err)
http.NotFound(w, r)
return
}
if tablet.Hostname == "" || tablet.PortMap["vt"] == 0 {
log.Errorf("Invalid host/port: %s %d", tablet.Hostname, tablet.PortMap["vt"])
http.NotFound(w, r)
return
}

rp := &httputil.ReverseProxy{}
rp.Director = func(req *http.Request) {
req.URL.Scheme = "http"
req.URL.Host = netutil.JoinHostPort(tablet.Hostname, tablet.PortMap["vt"])
req.URL.Path = "/" + splits[3]
}

prefixPath := fmt.Sprintf("/vttablet/%s/", tabletID)
rp.ModifyResponse = func(r *http.Response) error {
b, _ := ioutil.ReadAll(r.Body)
b = bytes.ReplaceAll(b, []byte(`href="/`), []byte(fmt.Sprintf(`href="%s`, prefixPath)))
b = bytes.ReplaceAll(b, []byte(`href=/`), []byte(fmt.Sprintf(`href=%s`, prefixPath)))
r.Body = ioutil.NopCloser(bytes.NewBuffer(b))
r.Header["Content-Length"] = []string{fmt.Sprint(len(b))}

// Don't forget redirects
loc := r.Header["Location"]
for i, v := range loc {
if strings.HasPrefix(v, "/") {
loc[i] = strings.Replace(v, "/", prefixPath, 1)
}
}
return nil
}

rp.ServeHTTP(w, r)
})
}
3 changes: 3 additions & 0 deletions go/vt/vtctld/vtctld.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,7 @@ func InitVtctld(ts *topo.Server) {

// Init workflow manager.
initWorkflowManager(ts)

// Setup reverse proxy for all vttablets through /vttablet/.
initVTTabletRedirection(ts)
}
81 changes: 0 additions & 81 deletions go/vt/vtctld/vttablet_proxy.go

This file was deleted.