Skip to content

Commit

Permalink
routesrv: reuse skipper.Options
Browse files Browse the repository at this point in the history
Reuse skipper.Options datastructure to avoid duplicate work of adding
the same flags/options to both skipper and routesrv.

Followup on #1873

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
  • Loading branch information
AlexanderYastrebov committed Jun 22, 2023
1 parent 0d5031e commit 47fa773
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 375 deletions.
2 changes: 1 addition & 1 deletion cmd/routesrv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func main() {
cfg := config.NewConfig()
cfg.Parse()
log.SetLevel(cfg.ApplicationLogLevel)
err := routesrv.Run(cfg.ToRouteSrvOptions())
err := routesrv.Run(cfg.ToOptions())
if err != nil {
log.Fatal(err)
}
Expand Down
54 changes: 0 additions & 54 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/net"
"github.com/zalando/skipper/proxy"
routesrv "github.com/zalando/skipper/routesrv"
"github.com/zalando/skipper/swarm"
)

Expand Down Expand Up @@ -658,59 +657,6 @@ func (c *Config) ParseArgs(progname string, args []string) error {
return nil
}

func (c *Config) ToRouteSrvOptions() routesrv.Options {
var whitelistCIDRS []string
if len(c.WhitelistedHealthCheckCIDR) > 0 {
whitelistCIDRS = strings.Split(c.WhitelistedHealthCheckCIDR, ",")
}

options := routesrv.Options{
Address: c.Address,
DefaultFiltersDir: c.DefaultFiltersDir,
DefaultFilters: &eskip.DefaultFilters{Prepend: c.PrependFilters.filters, Append: c.AppendFilters.filters},
KubernetesAllowedExternalNames: c.KubernetesAllowedExternalNames,
KubernetesInCluster: c.KubernetesInCluster,
KubernetesURL: c.KubernetesURL,
KubernetesHealthcheck: c.KubernetesHealthcheck,
KubernetesHTTPSRedirect: c.KubernetesHTTPSRedirect,
KubernetesHTTPSRedirectCode: c.KubernetesHTTPSRedirectCode,
KubernetesIngressClass: c.KubernetesIngressClass,
KubernetesRouteGroupClass: c.KubernetesRouteGroupClass,
KubernetesPathMode: c.KubernetesPathMode,
KubernetesNamespace: c.KubernetesNamespace,
KubernetesEnableEastWest: c.KubernetesEnableEastWest,
KubernetesEastWestDomain: c.KubernetesEastWestDomain,
KubernetesEastWestRangeDomains: c.KubernetesEastWestRangeDomains.values,
KubernetesEastWestRangePredicates: c.KubernetesEastWestRangePredicates,
KubernetesOnlyAllowedExternalNames: c.KubernetesOnlyAllowedExternalNames,
KubernetesRedisServiceNamespace: c.KubernetesRedisServiceNamespace,
KubernetesRedisServiceName: c.KubernetesRedisServiceName,
KubernetesDefaultLoadBalancerAlgorithm: c.KubernetesDefaultLoadBalancerAlgorithm,
OpenTracingBackendNameTag: c.OpentracingBackendNameTag,
OpenTracing: strings.Split(c.OpenTracing, " "),
OriginMarker: c.RouteCreationMetrics,
ReverseSourcePredicate: c.ReverseSourcePredicate,
SourcePollTimeout: time.Duration(c.SourcePollTimeout) * time.Millisecond,
WaitForHealthcheckInterval: c.WaitForHealthcheckInterval,
WhitelistedHealthCheckCIDR: whitelistCIDRS,

// Auth:
EnableOAuth2GrantFlow: c.EnableOAuth2GrantFlow,
OAuth2CallbackPath: c.Oauth2CallbackPath,
}

for _, rcci := range c.CloneRoute {
eskipClone := eskip.NewClone(rcci.Reg, rcci.Repl)
options.CloneRoute = append(options.CloneRoute, eskipClone)
}
for _, rcci := range c.EditRoute {
eskipEdit := eskip.NewEditor(rcci.Reg, rcci.Repl)
options.EditRoute = append(options.EditRoute, eskipEdit)
}

return options
}

func (c *Config) ToOptions() skipper.Options {
var eus []string
if len(c.EtcdUrls) > 0 {
Expand Down
36 changes: 0 additions & 36 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,42 +235,6 @@ func TestToOptions(t *testing.T) {
}
}

func TestToRouteSrvOptions(t *testing.T) {
c := defaultConfigWithoutNil()

// config
c.WhitelistedHealthCheckCIDR = "127.0.0.0/8,10.0.0.0/8"
c.CloneRoute = routeChangerConfig{}
if err := c.CloneRoute.Set("/foo/bar/"); err != nil {
t.Fatalf("Failed to set: %v", err)
}
c.EditRoute = routeChangerConfig{}
if err := c.EditRoute.Set("/foo/bar/"); err != nil {
t.Fatalf("Failed to set: %v", err)
}

if err := validate(c); err != nil {
t.Fatalf("Failed to validate config: %v", err)
}

opt := c.ToRouteSrvOptions()

// validate
if opt.Address != "localhost:8080" {
t.Errorf("Failed to get options Address: %v", opt.Address)
}
if len(opt.WhitelistedHealthCheckCIDR) != 2 {
t.Errorf("Failed to get WhitelistedHealthCheckCIDR: %v", opt.WhitelistedHealthCheckCIDR)
}
if len(opt.CloneRoute) != 1 {
t.Errorf("Failed to get expected clone route: %s", c.CloneRoute)
}
if len(opt.EditRoute) != 1 {
t.Errorf("Failed to get expected edit route: %s", c.EditRoute)
}

}

func Test_Validate(t *testing.T) {
for _, tt := range []struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ r2: PathRegexp("/endpoints") -> enableAccessLog(2,4,5) -> fifo(100,100,"3s") ->
lb := stdlibhttptest.NewServer(pr)
defer lb.Close()

rsvo := routesrv.Options{
rsvo := skipper.Options{
Address: ":8082",
KubernetesURL: lb.URL,
KubernetesRedisServiceNamespace: "skipper",
Expand Down
178 changes: 0 additions & 178 deletions routesrv/options.go

This file was deleted.

36 changes: 5 additions & 31 deletions routesrv/routesrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"

"github.com/zalando/skipper"
"github.com/zalando/skipper/dataclients/kubernetes"
"github.com/zalando/skipper/filters/auth"
"github.com/zalando/skipper/tracing"
Expand All @@ -28,7 +30,7 @@ type RouteServer struct {
// This call does not start data source updates automatically. Kept routes
// will stay in an uninitialized state, till StartUpdates is called and
// in effect data source is queried and routes initialized/updated.
func New(opts Options) (*RouteServer, error) {
func New(opts skipper.Options) (*RouteServer, error) {
rs := &RouteServer{}

opentracingOpts := opts.OpenTracing
Expand All @@ -47,35 +49,7 @@ func New(opts Options) (*RouteServer, error) {
handler.Handle("/routes", b)
handler.Handle("/metrics", promhttp.Handler())

dataclient, err := kubernetes.New(kubernetes.Options{
AllowedExternalNames: opts.KubernetesAllowedExternalNames,
BackendNameTracingTag: opts.OpenTracingBackendNameTag,
DefaultFiltersDir: opts.DefaultFiltersDir,
KubernetesInCluster: opts.KubernetesInCluster,
KubernetesURL: opts.KubernetesURL,
KubernetesNamespace: opts.KubernetesNamespace,
KubernetesEnableEastWest: opts.KubernetesEnableEastWest,
KubernetesEastWestDomain: opts.KubernetesEastWestDomain,
KubernetesEastWestRangeDomains: opts.KubernetesEastWestRangeDomains,
KubernetesEastWestRangePredicates: opts.KubernetesEastWestRangePredicates,
HTTPSRedirectCode: opts.KubernetesHTTPSRedirectCode,
IngressClass: opts.KubernetesIngressClass,
OnlyAllowedExternalNames: opts.KubernetesOnlyAllowedExternalNames,
OriginMarker: opts.OriginMarker,
PathMode: opts.KubernetesPathMode,
ProvideHealthcheck: opts.KubernetesHealthcheck,
ProvideHTTPSRedirect: opts.KubernetesHTTPSRedirect,
ReverseSourcePredicate: opts.ReverseSourcePredicate,
RouteGroupClass: opts.KubernetesRouteGroupClass,
WhitelistedHealthCheckCIDR: opts.WhitelistedHealthCheckCIDR,
ForceKubernetesService: opts.KubernetesForceService,
IngressLabelSelectors: opts.KubernetesIngressLabelSelectors,
ServicesLabelSelectors: opts.KubernetesServicesLabelSelectors,
EndpointsLabelSelectors: opts.KubernetesEndpointsLabelSelectors,
SecretsLabelSelectors: opts.KubernetesSecretsLabelSelectors,
RouteGroupsLabelSelectors: opts.KubernetesRouteGroupsLabelSelectors,
DefaultLoadBalancerAlgorithm: opts.KubernetesDefaultLoadBalancerAlgorithm,
})
dataclient, err := kubernetes.New(opts.KubernetesDataClientOptions())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -165,7 +139,7 @@ func newShutdownFunc(rs *RouteServer) func(delay time.Duration) {
// the server is closed, which can happen due to server startup errors or
// gracefully handled SIGTERM signal. In case of a server startup error,
// the error is returned as is.
func Run(opts Options) error {
func Run(opts skipper.Options) error {
rs, err := New(opts)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 47fa773

Please sign in to comment.