From 47fa7738385da180e79692a914880248231fd238 Mon Sep 17 00:00:00 2001 From: Alexander Yastrebov Date: Wed, 21 Jun 2023 14:50:32 +0200 Subject: [PATCH] routesrv: reuse skipper.Options 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 --- cmd/routesrv/main.go | 2 +- config/config.go | 54 ------------ config/config_test.go | 36 -------- redis_test.go | 2 +- routesrv/options.go | 178 -------------------------------------- routesrv/routesrv.go | 36 ++------ routesrv/routesrv_test.go | 77 ++++++++--------- skipper.go | 68 ++++++++------- 8 files changed, 78 insertions(+), 375 deletions(-) delete mode 100644 routesrv/options.go diff --git a/cmd/routesrv/main.go b/cmd/routesrv/main.go index ffe9f7e948..b9d651a914 100644 --- a/cmd/routesrv/main.go +++ b/cmd/routesrv/main.go @@ -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) } diff --git a/config/config.go b/config/config.go index 3e24e2c6b1..71273ad347 100644 --- a/config/config.go +++ b/config/config.go @@ -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" ) @@ -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 { diff --git a/config/config_test.go b/config/config_test.go index bbac7b5b28..f70172a3ab 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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 diff --git a/redis_test.go b/redis_test.go index 2b0258d5c8..3e1da28155 100644 --- a/redis_test.go +++ b/redis_test.go @@ -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", diff --git a/routesrv/options.go b/routesrv/options.go deleted file mode 100644 index 85954181b8..0000000000 --- a/routesrv/options.go +++ /dev/null @@ -1,178 +0,0 @@ -package routesrv - -import ( - "regexp" - "time" - - "github.com/zalando/skipper/dataclients/kubernetes" - "github.com/zalando/skipper/eskip" - "github.com/zalando/skipper/filters" -) - -// Options for initializing/running RouteServer -type Options struct { - // Network address that routesrv should listen on. - Address string - - // Polling timeout of the routing data source - SourcePollTimeout time.Duration - - // WaitForHealthcheckInterval sets the time that skipper waits - // for the loadbalancer in front to become unhealthy. Defaults - // to 0. - WaitForHealthcheckInterval time.Duration - - // OpenTracing enables tracing - OpenTracing []string - - // If set makes skipper authenticate with the kubernetes API server with service account assigned to the - // skipper POD. - // If omitted skipper will rely on kubectl proxy to authenticate with API server - KubernetesInCluster bool - - // Kubernetes API base URL. Only makes sense if KubernetesInCluster is set to false. If omitted and - // skipper is not running in-cluster, the default API URL will be used. - KubernetesURL string - - // KubernetesHealthcheck, when Kubernetes ingress is set, indicates - // whether an automatic healthcheck route should be generated. The - // generated route will report healthyness when the Kubernetes API - // calls are successful. The healthcheck endpoint is accessible from - // internal IPs, with the path /kube-system/healthz. - KubernetesHealthcheck bool - - // KubernetesHTTPSRedirect, when Kubernetes ingress is set, indicates - // whether an automatic redirect route should be generated to redirect - // HTTP requests to their HTTPS equivalent. The generated route will - // match requests with the X-Forwarded-Proto and X-Forwarded-Port, - // expected to be set by the load-balancer. - KubernetesHTTPSRedirect bool - - // KubernetesHTTPSRedirectCode overrides the default redirect code (308) - // when used together with -kubernetes-https-redirect. - KubernetesHTTPSRedirectCode int - - // KubernetesIngressClass is a regular expression, that will make - // skipper load only the ingress resources that have a matching - // kubernetes.io/ingress.class annotation. For backwards compatibility, - // the ingresses without an annotation, or an empty annotation, will - // be loaded, too. - KubernetesIngressClass string - - // KubernetesRouteGroupClass is a regular expression, that will make skipper - // load only the RouteGroup resources that have a matching - // zalando.org/routegroup.class annotation. Any RouteGroups without the - // annotation, or which an empty annotation, will be loaded too. - KubernetesRouteGroupClass string - - // PathMode controls the default interpretation of ingress paths in cases - // when the ingress doesn't specify it with an annotation. - KubernetesPathMode kubernetes.PathMode - - // KubernetesNamespace is used to switch between monitoring ingresses in the cluster-scope or limit - // the ingresses to only those in the specified namespace. Defaults to "" which means monitor ingresses - // in the cluster-scope. - KubernetesNamespace string - - // *DEPRECATED* KubernetesEnableEastWest enables cluster internal service to service communication, aka east-west traffic - KubernetesEnableEastWest bool - - // *DEPRECATED* KubernetesEastWestDomain sets the cluster internal domain used to create additional routes in skipper, defaults to skipper.cluster.local - KubernetesEastWestDomain string - - // KubernetesEastWestRangeDomains set the the cluster internal domains for - // east west traffic. Identified routes to such domains will include - // the KubernetesEastWestRangePredicates. - KubernetesEastWestRangeDomains []string - - // KubernetesEastWestRangePredicates set the Predicates that will be - // appended to routes identified as to KubernetesEastWestRangeDomains. - KubernetesEastWestRangePredicates []*eskip.Predicate - - // KubernetesOnlyAllowedExternalNames will enable validation of ingress external names and route groups network - // backend addresses, explicit LB endpoints validation against the list of patterns in - // AllowedExternalNames. - KubernetesOnlyAllowedExternalNames bool - - // KubernetesAllowedExternalNames contains regexp patterns of those domain names that are allowed to be - // used with external name services (type=ExternalName). - KubernetesAllowedExternalNames []*regexp.Regexp - - // KubernetesIngressLabelSelectors is a map of kubernetes labels to their values that must be present on a resource to be loaded - // by the client. A label and its value on an Ingress must be match exactly to be loaded by Skipper. - // If the value is irrelevant for a given configuration, it can be left empty. The default - // value is no labels required. - KubernetesIngressLabelSelectors map[string]string - - // KubernetesServicesLabelSelectors is a map of kubernetes labels to their values that must be present on a resource to be loaded - // by the client. - KubernetesServicesLabelSelectors map[string]string - - // KubernetesEndpointsLabelSelectors is a map of kubernetes labels to their values that must be present on a resource to be loaded - // by the client. - KubernetesEndpointsLabelSelectors map[string]string - - // KubernetesSecretsLabelSelectors is a map of kubernetes labels to their values that must be present on a resource to be loaded - // by the client. - KubernetesSecretsLabelSelectors map[string]string - - // KubernetesRouteGroupsLabelSelectors is a map of kubernetes labels to their values that must be present on a resource to be loaded - // by the client. - KubernetesRouteGroupsLabelSelectors map[string]string - - // KubernetesForceService overrides the default Skipper functionality to route traffic using Kubernetes Endpoints, - // instead using Kubernetes Services. - KubernetesForceService bool - - // KubernetesRedisServiceNamespace to be used to lookup ring shards dynamically - KubernetesRedisServiceNamespace string - - // KubernetesRedisServiceName to be used to lookup ring shards dynamically - KubernetesRedisServiceName string - - // KubernetesDefaultLoadBalancerAlgorithm sets the default load balancer algorithm for Kubernetes services - KubernetesDefaultLoadBalancerAlgorithm string - - // WhitelistedHealthcheckCIDR appends the whitelisted IP Range to the inernalIPS range for healthcheck purposes - WhitelistedHealthCheckCIDR []string - - // ReverseSourcePredicate enables the automatic use of IP - // whitelisting in different places to use the reversed way of - // identifying a client IP within the X-Forwarded-For - // header. Amazon's ALB for example writes the client IP to - // the last item of the string list of the X-Forwarded-For - // header, in this case you want to set this to true. - ReverseSourcePredicate bool - - // Default filters directory enables default filters mechanism and sets the directory where the filters are located - DefaultFiltersDir string - - // DefaultFilters enables appending/prepending filters to all routes - DefaultFilters *eskip.DefaultFilters - - // OriginMarker is *deprecated* and not used anymore. It will be deleted in v1. - OriginMarker bool - - // List of custom filter specifications. - CustomFilters []filters.Spec - - // OpenTracingBackendNameTag enables an additional tracing tag containing a backend name - // for a route when it's available (e.g. for RouteGroups) - OpenTracingBackendNameTag bool - - // EnableOAuth2GrantFlow, enables OAuth2 Grant Flow filter - EnableOAuth2GrantFlow bool - - // OAuth2CallbackPath contains the path where the OAuth2 callback requests with the - // authorization code should be redirected to. Defaults to /.well-known/oauth2-callback - OAuth2CallbackPath string - - // CloneRoute is a slice of PreProcessors that will be applied to all routes - // automatically. They will clone all matching routes and apply changes to the - // cloned routes. - CloneRoute []*eskip.Clone - - // EditRoute will be applied to all routes automatically and - // will apply changes to all matching routes. - EditRoute []*eskip.Editor -} diff --git a/routesrv/routesrv.go b/routesrv/routesrv.go index 49ed896c30..b980281feb 100644 --- a/routesrv/routesrv.go +++ b/routesrv/routesrv.go @@ -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" @@ -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 @@ -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 } @@ -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 diff --git a/routesrv/routesrv_test.go b/routesrv/routesrv_test.go index 93879847e1..406cf13d6f 100644 --- a/routesrv/routesrv_test.go +++ b/routesrv/routesrv_test.go @@ -16,7 +16,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" - "github.com/zalando/skipper/dataclients/kubernetes" + "github.com/zalando/skipper" "github.com/zalando/skipper/dataclients/kubernetes/kubernetestest" "github.com/zalando/skipper/eskip" "github.com/zalando/skipper/logging/loggingtest" @@ -69,14 +69,13 @@ func loadKubeYAML(t *testing.T, path string) io.Reader { } func newRouteServer(t *testing.T, kubeServer *httptest.Server) *routesrv.RouteServer { - return newRouteServerWithOptions(t, routesrv.Options{ - SourcePollTimeout: pollInterval, - KubernetesURL: kubeServer.URL, - KubernetesDefaultLoadBalancerAlgorithm: kubernetes.DefaultLoadBalancerAlgorithm, + return newRouteServerWithOptions(t, skipper.Options{ + SourcePollTimeout: pollInterval, + KubernetesURL: kubeServer.URL, }) } -func newRouteServerWithOptions(t *testing.T, o routesrv.Options) *routesrv.RouteServer { +func newRouteServerWithOptions(t *testing.T, o skipper.Options) *routesrv.RouteServer { t.Helper() rs, err := routesrv.New(o) if err != nil { @@ -222,12 +221,11 @@ func TestRedisIPs(t *testing.T) { ks, _ := newKubeServer(t, loadKubeYAML(t, "testdata/redis.yaml")) ks.Start() defer ks.Close() - rs := newRouteServerWithOptions(t, routesrv.Options{ - SourcePollTimeout: pollInterval, - KubernetesURL: ks.URL, - KubernetesRedisServiceNamespace: "namespace1", - KubernetesRedisServiceName: "service1", - KubernetesDefaultLoadBalancerAlgorithm: kubernetes.DefaultLoadBalancerAlgorithm, + rs := newRouteServerWithOptions(t, skipper.Options{ + SourcePollTimeout: pollInterval, + KubernetesURL: ks.URL, + KubernetesRedisServiceNamespace: "namespace1", + KubernetesRedisServiceName: "service1", }) w := getRedisURLs(rs) @@ -243,10 +241,9 @@ func TestFetchedIngressRoutesAreServedInEskipFormat(t *testing.T) { ks, _ := newKubeServer(t, loadKubeYAML(t, "testdata/ing-v1-lb-target-multi.yaml")) ks.Start() defer ks.Close() - rs := newRouteServerWithOptions(t, routesrv.Options{ - SourcePollTimeout: pollInterval, - KubernetesURL: ks.URL, - KubernetesDefaultLoadBalancerAlgorithm: kubernetes.DefaultLoadBalancerAlgorithm, + rs := newRouteServerWithOptions(t, skipper.Options{ + SourcePollTimeout: pollInterval, + KubernetesURL: ks.URL, }) rs.StartUpdates() @@ -321,10 +318,9 @@ func TestRoutesWithDefaultFilters(t *testing.T) { ks, _ := newKubeServer(t, loadKubeYAML(t, "testdata/lb-target-multi.yaml")) ks.Start() defer ks.Close() - rs := newRouteServerWithOptions(t, routesrv.Options{ - SourcePollTimeout: pollInterval, - KubernetesURL: ks.URL, - KubernetesDefaultLoadBalancerAlgorithm: kubernetes.DefaultLoadBalancerAlgorithm, + rs := newRouteServerWithOptions(t, skipper.Options{ + SourcePollTimeout: pollInterval, + KubernetesURL: ks.URL, DefaultFilters: &eskip.DefaultFilters{ Prepend: []*eskip.Filter{ { @@ -363,12 +359,11 @@ func TestRoutesWithOAuth2Callback(t *testing.T) { ks, _ := newKubeServer(t, loadKubeYAML(t, "testdata/lb-target-multi.yaml")) ks.Start() defer ks.Close() - rs := newRouteServerWithOptions(t, routesrv.Options{ - SourcePollTimeout: pollInterval, - KubernetesURL: ks.URL, - KubernetesDefaultLoadBalancerAlgorithm: kubernetes.DefaultLoadBalancerAlgorithm, - EnableOAuth2GrantFlow: true, - OAuth2CallbackPath: "/.well-known/oauth2-callback", + rs := newRouteServerWithOptions(t, skipper.Options{ + SourcePollTimeout: pollInterval, + KubernetesURL: ks.URL, + EnableOAuth2GrantFlow: true, + OAuth2CallbackPath: "/.well-known/oauth2-callback", }) rs.StartUpdates() @@ -393,11 +388,10 @@ func TestRoutesWithEastWest(t *testing.T) { ks, _ := newKubeServer(t, loadKubeYAML(t, "testdata/internal-host-explicit-route-predicate.yaml")) ks.Start() defer ks.Close() - rs := newRouteServerWithOptions(t, routesrv.Options{ - SourcePollTimeout: pollInterval, - KubernetesURL: ks.URL, - KubernetesDefaultLoadBalancerAlgorithm: kubernetes.DefaultLoadBalancerAlgorithm, - KubernetesEastWestRangeDomains: []string{"ingress.cluster.local"}, + rs := newRouteServerWithOptions(t, skipper.Options{ + SourcePollTimeout: pollInterval, + KubernetesURL: ks.URL, + KubernetesEastWestRangeDomains: []string{"ingress.cluster.local"}, KubernetesEastWestRangePredicates: []*eskip.Predicate{ { Name: "ClientIP", @@ -539,10 +533,9 @@ func TestESkipBytesHandlerWithXCount(t *testing.T) { ks, _ := newKubeServer(t, loadKubeYAML(t, "testdata/lb-target-multi.yaml")) ks.Start() defer ks.Close() - rs := newRouteServerWithOptions(t, routesrv.Options{ - SourcePollTimeout: pollInterval, - KubernetesURL: ks.URL, - KubernetesDefaultLoadBalancerAlgorithm: kubernetes.DefaultLoadBalancerAlgorithm, + rs := newRouteServerWithOptions(t, skipper.Options{ + SourcePollTimeout: pollInterval, + KubernetesURL: ks.URL, }) rs.StartUpdates() @@ -570,10 +563,9 @@ func TestRoutesWithEditRoute(t *testing.T) { ks, _ := newKubeServer(t, loadKubeYAML(t, "testdata/lb-target-multi.yaml")) ks.Start() defer ks.Close() - rs := newRouteServerWithOptions(t, routesrv.Options{ - SourcePollTimeout: pollInterval, - KubernetesURL: ks.URL, - KubernetesDefaultLoadBalancerAlgorithm: kubernetes.DefaultLoadBalancerAlgorithm, + rs := newRouteServerWithOptions(t, skipper.Options{ + SourcePollTimeout: pollInterval, + KubernetesURL: ks.URL, EditRoute: []*eskip.Editor{ eskip.NewEditor(regexp.MustCompile("Host[(](.*)[)]"), "HostAny($1)"), }, @@ -601,10 +593,9 @@ func TestRoutesWithCloneRoute(t *testing.T) { ks, _ := newKubeServer(t, loadKubeYAML(t, "testdata/lb-target-multi.yaml")) ks.Start() defer ks.Close() - rs := newRouteServerWithOptions(t, routesrv.Options{ - SourcePollTimeout: pollInterval, - KubernetesURL: ks.URL, - KubernetesDefaultLoadBalancerAlgorithm: kubernetes.DefaultLoadBalancerAlgorithm, + rs := newRouteServerWithOptions(t, skipper.Options{ + SourcePollTimeout: pollInterval, + KubernetesURL: ks.URL, CloneRoute: []*eskip.Clone{ eskip.NewClone(regexp.MustCompile("Host"), "HostAny"), }, diff --git a/skipper.go b/skipper.go index 6ac7514b9d..006a326fad 100644 --- a/skipper.go +++ b/skipper.go @@ -890,6 +890,39 @@ type Options struct { LuaSources []string } +func (o *Options) KubernetesDataClientOptions() kubernetes.Options { + return kubernetes.Options{ + AllowedExternalNames: o.KubernetesAllowedExternalNames, + BackendNameTracingTag: o.OpenTracingBackendNameTag, + DefaultFiltersDir: o.DefaultFiltersDir, + KubernetesInCluster: o.KubernetesInCluster, + KubernetesURL: o.KubernetesURL, + KubernetesNamespace: o.KubernetesNamespace, + KubernetesEnableEastWest: o.KubernetesEnableEastWest, + KubernetesEastWestDomain: o.KubernetesEastWestDomain, + KubernetesEastWestRangeDomains: o.KubernetesEastWestRangeDomains, + KubernetesEastWestRangePredicates: o.KubernetesEastWestRangePredicates, + HTTPSRedirectCode: o.KubernetesHTTPSRedirectCode, + IngressClass: o.KubernetesIngressClass, + IngressLabelSelectors: o.KubernetesIngressLabelSelectors, + ServicesLabelSelectors: o.KubernetesServicesLabelSelectors, + EndpointsLabelSelectors: o.KubernetesEndpointsLabelSelectors, + SecretsLabelSelectors: o.KubernetesSecretsLabelSelectors, + RouteGroupsLabelSelectors: o.KubernetesRouteGroupsLabelSelectors, + OnlyAllowedExternalNames: o.KubernetesOnlyAllowedExternalNames, + OriginMarker: o.EnableRouteCreationMetrics, + PathMode: o.KubernetesPathMode, + ProvideHealthcheck: o.KubernetesHealthcheck, + ProvideHTTPSRedirect: o.KubernetesHTTPSRedirect, + ReverseSourcePredicate: o.ReverseSourcePredicate, + RouteGroupClass: o.KubernetesRouteGroupClass, + WhitelistedHealthCheckCIDR: o.WhitelistedHealthCheckCIDR, + ForceKubernetesService: o.KubernetesForceService, + BackendTrafficAlgorithm: o.KubernetesBackendTrafficAlgorithm, + DefaultLoadBalancerAlgorithm: o.KubernetesDefaultLoadBalancerAlgorithm, + } +} + type serverErrorLogWriter struct{} func (*serverErrorLogWriter) Write(p []byte) (int, error) { @@ -973,37 +1006,10 @@ func createDataClients(o Options, cr *certregistry.CertRegistry) ([]routing.Data } if o.Kubernetes { - kubernetesClient, err := kubernetes.New(kubernetes.Options{ - AllowedExternalNames: o.KubernetesAllowedExternalNames, - BackendNameTracingTag: o.OpenTracingBackendNameTag, - DefaultFiltersDir: o.DefaultFiltersDir, - KubernetesInCluster: o.KubernetesInCluster, - KubernetesURL: o.KubernetesURL, - KubernetesNamespace: o.KubernetesNamespace, - KubernetesEnableEastWest: o.KubernetesEnableEastWest, - KubernetesEastWestDomain: o.KubernetesEastWestDomain, - KubernetesEastWestRangeDomains: o.KubernetesEastWestRangeDomains, - KubernetesEastWestRangePredicates: o.KubernetesEastWestRangePredicates, - DefaultLoadBalancerAlgorithm: o.KubernetesDefaultLoadBalancerAlgorithm, - HTTPSRedirectCode: o.KubernetesHTTPSRedirectCode, - IngressClass: o.KubernetesIngressClass, - IngressLabelSelectors: o.KubernetesIngressLabelSelectors, - ServicesLabelSelectors: o.KubernetesServicesLabelSelectors, - EndpointsLabelSelectors: o.KubernetesEndpointsLabelSelectors, - SecretsLabelSelectors: o.KubernetesSecretsLabelSelectors, - RouteGroupsLabelSelectors: o.KubernetesRouteGroupsLabelSelectors, - OnlyAllowedExternalNames: o.KubernetesOnlyAllowedExternalNames, - OriginMarker: o.EnableRouteCreationMetrics, - PathMode: o.KubernetesPathMode, - ProvideHealthcheck: o.KubernetesHealthcheck, - ProvideHTTPSRedirect: o.KubernetesHTTPSRedirect, - ReverseSourcePredicate: o.ReverseSourcePredicate, - RouteGroupClass: o.KubernetesRouteGroupClass, - WhitelistedHealthCheckCIDR: o.WhitelistedHealthCheckCIDR, - ForceKubernetesService: o.KubernetesForceService, - BackendTrafficAlgorithm: o.KubernetesBackendTrafficAlgorithm, - CertificateRegistry: cr, - }) + kops := o.KubernetesDataClientOptions() + kops.CertificateRegistry = cr + + kubernetesClient, err := kubernetes.New(kops) if err != nil { return nil, err }