Commit 30e47d8c authored by Jens Langhammer's avatar Jens Langhammer

tier0: fix initial peer finding, fix Host header being wrong

parent ae2ec757
Pipeline #3861 passed with stage
in 1 minute and 25 seconds
......@@ -22,7 +22,7 @@ import (
func main() {
log.SetLevel(log.DebugLevel)
log.Debugf("Starting p2-tier0 Version %s+r5", internal.Version)
log.Debugf("Starting p2-tier0 Version %s", internal.Version)
k8sc, err := k8s.NewKubernetesContext()
if err != nil {
log.Fatal(err)
......@@ -55,6 +55,7 @@ func main() {
escapedPath := url.QueryEscape(r.URL.Path)
context := cache.CacheContext{
RequestHeader: r.Header,
Host: r.Host,
}
err := localCache.Group.Get(context, escapedPath, groupcache.AllocatingByteSliceSink(&data))
if err != nil {
......
......@@ -27,6 +27,7 @@ type Cache struct {
type CacheContext struct {
groupcache.Context
RequestHeader http.Header
Host string
}
func NewCache(upstream p2.Upstream) Cache {
......@@ -35,9 +36,13 @@ func NewCache(upstream p2.Upstream) Cache {
})
cache := groupcache.NewGroup("tier0", constants.CacheSize, groupcache.GetterFunc(
func(_ctx groupcache.Context, key string, dest groupcache.Sink) error {
if _ctx == nil {
logger.Warningf("Empty context for key '%s'", key)
return nil
}
ctx := _ctx.(CacheContext)
logger.Debug("Fetching key from upstream...")
blob, err := upstream.Fetch(ctx.RequestHeader, key)
logger.Debugf("Fetching upstream key '%s'", key)
blob, err := upstream.Fetch(ctx.Host, key)
if err == nil {
dest.SetBytes(blob.Data)
} else {
......@@ -72,7 +77,7 @@ func (c *Cache) StartCacheServer() {
}
func (c *Cache) SetPeersFromK8s(k8sc k8s.KubernetesContext) {
pods := k8sc.PodsForComponent("cache")
pods := k8sc.PodsForComponent("tier0")
podAddresses := make([]string, 0)
for _, element := range pods.Items {
if element.Status.PodIP != "" {
......
......@@ -27,19 +27,23 @@ type Blob struct {
func NewUpstream(URL string) Upstream {
return Upstream{
URL: URL,
client: http.Client{},
URL: URL,
client: http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
},
}
}
// Fetch Fetch Blob from upstream p2 server
func (u *Upstream) Fetch(header http.Header, key string) (Blob, error) {
func (u *Upstream) Fetch(host string, key string) (Blob, error) {
// Key is Querystring-escaped to circumvent groupcache bugs
realKey, err := url.QueryUnescape(key)
// Build a full request so we can pass the correct Host header
req, err := http.NewRequest("GET", u.URL+realKey, nil)
req.Header = header
req.Host = header.Get("Host")
log.Debugf("Patching host to '%s'", host)
req.Host = host
resp, err := u.client.Do(req)
if err != nil {
return Blob{}, err
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment