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

routesrv: discover redis shards without fetching full cluster state #2956

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
4 changes: 0 additions & 4 deletions cmd/routesrv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ func main() {
log.Fatalf("Error processing config: %s", err)
}

if !cfg.KubernetesIngress {
log.Fatalf("-kubernetes flag required")
}

log.SetLevel(cfg.ApplicationLogLevel)
if cfg.ApplicationLogJSONEnabled {
log.SetFormatter(&log.JSONFormatter{})
Expand Down
19 changes: 15 additions & 4 deletions routesrv/redishandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,22 @@ func (rh *RedisHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
w.Write(address)
}

func getRedisAddresses(opts *skipper.Options, kdc *kubernetes.Client, m metrics.Metrics) func() ([]byte, error) {
func getRedisAddresses(opts *skipper.Options, kdc *kubernetes.Client, loaded bool, m metrics.Metrics) func() ([]byte, error) {
return func() ([]byte, error) {
a := kdc.GetEndpointAddresses(opts.KubernetesRedisServiceNamespace, opts.KubernetesRedisServiceName)
log.Debugf("Redis updater called and found %d redis endpoints: %v", len(a), a)
var (
a []string
err error
)
if loaded {
a = kdc.GetEndpointAddresses(opts.KubernetesRedisServiceNamespace, opts.KubernetesRedisServiceName)
log.Debugf("GetEndpointAddresses found %d redis endpoints", len(a))
} else {
a, err = kdc.LoadEndpointAddresses(opts.KubernetesRedisServiceNamespace, opts.KubernetesRedisServiceName)
log.Debugf("LoadEndpointAddresses found %d redis endpoints, err: %v", len(a), err)
if err != nil {
return nil, fmt.Errorf("failed to load redis endpoints: %w", err)
}
}
m.UpdateGauge("redis_endpoints", float64(len(a)))

result := RedisEndpoints{
Expand Down
39 changes: 28 additions & 11 deletions routesrv/routesrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package routesrv

import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
Expand All @@ -15,8 +14,10 @@ import (

"github.com/zalando/skipper"
"github.com/zalando/skipper/dataclients/kubernetes"
"github.com/zalando/skipper/dataclients/routestring"
"github.com/zalando/skipper/filters/auth"
"github.com/zalando/skipper/metrics"
"github.com/zalando/skipper/routing"
"github.com/zalando/skipper/tracing"
)

Expand Down Expand Up @@ -86,14 +87,20 @@ func New(opts skipper.Options) (*RouteServer, error) {
supportHandler.Handle("/debug/pprof/", metricsHandler)
}

if !opts.Kubernetes {
return nil, fmt.Errorf(`option "Kubernetes" is required`)
var (
kdc *kubernetes.Client
dataclient routing.DataClient
)
if opts.Kubernetes {
kdc, err = kubernetes.New(opts.KubernetesDataClientOptions())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: close

if err != nil {
return nil, err
}
dataclient = kdc
} else {
dataclient, _ = routestring.New("")
}

dataclient, err := kubernetes.New(opts.KubernetesDataClientOptions())
if err != nil {
return nil, err
}
var oauthConfig *auth.OAuthConfig
if opts.EnableOAuth2GrantFlow /* explicitly enable grant flow */ {
oauthConfig = &auth.OAuthConfig{}
Expand All @@ -104,12 +111,22 @@ func New(opts skipper.Options) (*RouteServer, error) {
// in case we have kubernetes dataclient and we can detect redis instances, we patch redisOptions
if opts.KubernetesRedisServiceNamespace != "" && opts.KubernetesRedisServiceName != "" {
log.Infof("Use endpoints %s/%s to fetch updated redis shards", opts.KubernetesRedisServiceNamespace, opts.KubernetesRedisServiceName)

rh = &RedisHandler{}
_, err := dataclient.LoadAll()
if err != nil {
return nil, err
if kdc != nil {
if _, err := kdc.LoadAll(); err != nil {
return nil, err
}
rh.AddrUpdater = getRedisAddresses(&opts, kdc, true, m)
} else {
kdc, err := kubernetes.New(opts.KubernetesDataClientOptions())
if err != nil {
return nil, err
}
// defer kdc.Close()

rh.AddrUpdater = getRedisAddresses(&opts, kdc, false, m)
}
rh.AddrUpdater = getRedisAddresses(&opts, dataclient, m)
mux.Handle("/swarm/redis/shards", rh)
}

Expand Down
Loading