Add support for multiple gloo namespaces in one External DNS instance

This commit is contained in:
megum1n 2023-03-15 02:33:00 +01:00
parent dd61d0f5d8
commit 3d48d66c91
No known key found for this signature in database
GPG Key ID: 2604BA0B7D864F76
6 changed files with 47 additions and 43 deletions

View File

@ -25,7 +25,7 @@ spec:
image: registry.k8s.io/external-dns/external-dns:v0.13.2 image: registry.k8s.io/external-dns/external-dns:v0.13.2
args: args:
- --source=gloo-proxy - --source=gloo-proxy
- --gloo-namespace=custom-gloo-system # gloo system namespace. Omit to use the default (gloo-system) - --gloo-namespaces=custom-gloo-system # comma separated gloo system namespace list. Omit to use the default (gloo-system)
- --provider=aws - --provider=aws
- --registry=txt - --registry=txt
- --txt-owner-id=my-identifier - --txt-owner-id=my-identifier
@ -93,7 +93,7 @@ spec:
image: registry.k8s.io/external-dns/external-dns:v0.13.2 image: registry.k8s.io/external-dns/external-dns:v0.13.2
args: args:
- --source=gloo-proxy - --source=gloo-proxy
- --gloo-namespace=custom-gloo-system # gloo system namespace. Omit to use the default (gloo-system) - --gloo-namespaces=custom-gloo-system # comma separated gloo system namespace list. Omit to use the default (gloo-system)
- --provider=aws - --provider=aws
- --registry=txt - --registry=txt
- --txt-owner-id=my-identifier - --txt-owner-id=my-identifier

View File

@ -134,7 +134,7 @@ func main() {
CFUsername: cfg.CFUsername, CFUsername: cfg.CFUsername,
CFPassword: cfg.CFPassword, CFPassword: cfg.CFPassword,
ContourLoadBalancerService: cfg.ContourLoadBalancerService, ContourLoadBalancerService: cfg.ContourLoadBalancerService,
GlooNamespace: cfg.GlooNamespace, GlooNamespaces: cfg.GlooNamespaces,
SkipperRouteGroupVersion: cfg.SkipperRouteGroupVersion, SkipperRouteGroupVersion: cfg.SkipperRouteGroupVersion,
RequestTimeout: cfg.RequestTimeout, RequestTimeout: cfg.RequestTimeout,
DefaultTargets: cfg.DefaultTargets, DefaultTargets: cfg.DefaultTargets,

View File

@ -48,7 +48,7 @@ type Config struct {
RequestTimeout time.Duration RequestTimeout time.Duration
DefaultTargets []string DefaultTargets []string
ContourLoadBalancerService string ContourLoadBalancerService string
GlooNamespace string GlooNamespaces string
SkipperRouteGroupVersion string SkipperRouteGroupVersion string
Sources []string Sources []string
Namespace string Namespace string
@ -207,7 +207,7 @@ var defaultConfig = &Config{
RequestTimeout: time.Second * 30, RequestTimeout: time.Second * 30,
DefaultTargets: []string{}, DefaultTargets: []string{},
ContourLoadBalancerService: "heptio-contour/contour", ContourLoadBalancerService: "heptio-contour/contour",
GlooNamespace: "gloo-system", GlooNamespaces: "gloo-system",
SkipperRouteGroupVersion: "zalando.org/v1", SkipperRouteGroupVersion: "zalando.org/v1",
Sources: nil, Sources: nil,
Namespace: "", Namespace: "",
@ -398,7 +398,7 @@ func (cfg *Config) ParseFlags(args []string) error {
app.Flag("contour-load-balancer", "The fully-qualified name of the Contour load balancer service. (default: heptio-contour/contour)").Default("heptio-contour/contour").StringVar(&cfg.ContourLoadBalancerService) app.Flag("contour-load-balancer", "The fully-qualified name of the Contour load balancer service. (default: heptio-contour/contour)").Default("heptio-contour/contour").StringVar(&cfg.ContourLoadBalancerService)
// Flags related to Gloo // Flags related to Gloo
app.Flag("gloo-namespace", "Gloo namespace. (default: gloo-system)").Default("gloo-system").StringVar(&cfg.GlooNamespace) app.Flag("gloo-namespaces", "Coma separated list of gloo namespaces. (default: gloo-system)").Default("gloo-system").StringVar(&cfg.GlooNamespaces)
// Flags related to Skipper RouteGroup // Flags related to Skipper RouteGroup
app.Flag("skipper-routegroup-groupversion", "The resource version for skipper routegroup").Default(source.DefaultRoutegroupVersion).StringVar(&cfg.SkipperRouteGroupVersion) app.Flag("skipper-routegroup-groupversion", "The resource version for skipper routegroup").Default(source.DefaultRoutegroupVersion).StringVar(&cfg.SkipperRouteGroupVersion)

View File

@ -36,7 +36,7 @@ var (
KubeConfig: "", KubeConfig: "",
RequestTimeout: time.Second * 30, RequestTimeout: time.Second * 30,
ContourLoadBalancerService: "heptio-contour/contour", ContourLoadBalancerService: "heptio-contour/contour",
GlooNamespace: "gloo-system", GlooNamespaces: "gloo-system",
SkipperRouteGroupVersion: "zalando.org/v1", SkipperRouteGroupVersion: "zalando.org/v1",
Sources: []string{"service"}, Sources: []string{"service"},
Namespace: "", Namespace: "",
@ -136,7 +136,7 @@ var (
KubeConfig: "/some/path", KubeConfig: "/some/path",
RequestTimeout: time.Second * 77, RequestTimeout: time.Second * 77,
ContourLoadBalancerService: "heptio-contour-other/contour-other", ContourLoadBalancerService: "heptio-contour-other/contour-other",
GlooNamespace: "gloo-not-system", GlooNamespaces: "gloo-not-system,gloo-second-system",
SkipperRouteGroupVersion: "zalando.org/v2", SkipperRouteGroupVersion: "zalando.org/v2",
Sources: []string{"service", "ingress", "connector"}, Sources: []string{"service", "ingress", "connector"},
Namespace: "namespace", Namespace: "namespace",
@ -266,7 +266,7 @@ func TestParseFlags(t *testing.T) {
"--kubeconfig=/some/path", "--kubeconfig=/some/path",
"--request-timeout=77s", "--request-timeout=77s",
"--contour-load-balancer=heptio-contour-other/contour-other", "--contour-load-balancer=heptio-contour-other/contour-other",
"--gloo-namespace=gloo-not-system", "--gloo-namespaces=gloo-not-system,gloo-second-system",
"--skipper-routegroup-groupversion=zalando.org/v2", "--skipper-routegroup-groupversion=zalando.org/v2",
"--source=service", "--source=service",
"--source=ingress", "--source=ingress",
@ -391,7 +391,7 @@ func TestParseFlags(t *testing.T) {
"EXTERNAL_DNS_KUBECONFIG": "/some/path", "EXTERNAL_DNS_KUBECONFIG": "/some/path",
"EXTERNAL_DNS_REQUEST_TIMEOUT": "77s", "EXTERNAL_DNS_REQUEST_TIMEOUT": "77s",
"EXTERNAL_DNS_CONTOUR_LOAD_BALANCER": "heptio-contour-other/contour-other", "EXTERNAL_DNS_CONTOUR_LOAD_BALANCER": "heptio-contour-other/contour-other",
"EXTERNAL_DNS_GLOO_NAMESPACE": "gloo-not-system", "EXTERNAL_DNS_GLOO_NAMESPACES": "gloo-not-system,gloo-second-system",
"EXTERNAL_DNS_SKIPPER_ROUTEGROUP_GROUPVERSION": "zalando.org/v2", "EXTERNAL_DNS_SKIPPER_ROUTEGROUP_GROUPVERSION": "zalando.org/v2",
"EXTERNAL_DNS_SOURCE": "service\ningress\nconnector", "EXTERNAL_DNS_SOURCE": "service\ningress\nconnector",
"EXTERNAL_DNS_NAMESPACE": "namespace", "EXTERNAL_DNS_NAMESPACE": "namespace",

View File

@ -81,15 +81,16 @@ type proxyVirtualHostMetadataSource struct {
type glooSource struct { type glooSource struct {
dynamicKubeClient dynamic.Interface dynamicKubeClient dynamic.Interface
kubeClient kubernetes.Interface kubeClient kubernetes.Interface
glooNamespace string glooNamespaces []string
} }
// NewGlooSource creates a new glooSource with the given config // NewGlooSource creates a new glooSource with the given config
func NewGlooSource(dynamicKubeClient dynamic.Interface, kubeClient kubernetes.Interface, glooNamespace string) (Source, error) { func NewGlooSource(dynamicKubeClient dynamic.Interface, kubeClient kubernetes.Interface, glooNamespaces string) (Source, error) {
return &glooSource{ return &glooSource{
dynamicKubeClient, dynamicKubeClient,
kubeClient, kubeClient,
glooNamespace, strings.Split(glooNamespaces, ","),
}, nil }, nil
} }
@ -100,33 +101,36 @@ func (gs *glooSource) AddEventHandler(ctx context.Context, handler func()) {
func (gs *glooSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error) { func (gs *glooSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error) {
endpoints := []*endpoint.Endpoint{} endpoints := []*endpoint.Endpoint{}
proxies, err := gs.dynamicKubeClient.Resource(proxyGVR).Namespace(gs.glooNamespace).List(ctx, metav1.ListOptions{}) for _, ns := range gs.glooNamespaces {
if err != nil { proxies, err := gs.dynamicKubeClient.Resource(proxyGVR).Namespace(ns).List(ctx, metav1.ListOptions{})
return nil, err if err != nil {
} return nil, err
for _, obj := range proxies.Items { }
proxy := proxy{} for _, obj := range proxies.Items {
jsonString, err := obj.MarshalJSON() proxy := proxy{}
if err != nil { jsonString, err := obj.MarshalJSON()
return nil, err if err != nil {
} return nil, err
err = json.Unmarshal(jsonString, &proxy) }
if err != nil { err = json.Unmarshal(jsonString, &proxy)
return nil, err if err != nil {
} return nil, err
log.Debugf("Gloo: Find %s proxy", proxy.Metadata.Name) }
proxyTargets, err := gs.proxyTargets(ctx, proxy.Metadata.Name) log.Debugf("Gloo: Find %s proxy", proxy.Metadata.Name)
if err != nil { proxyTargets, err := gs.proxyTargets(ctx, proxy.Metadata.Name, ns)
return nil, err if err != nil {
} return nil, err
log.Debugf("Gloo[%s]: Find %d target(s) (%+v)", proxy.Metadata.Name, len(proxyTargets), proxyTargets) }
proxyEndpoints, err := gs.generateEndpointsFromProxy(ctx, &proxy, proxyTargets) log.Debugf("Gloo[%s]: Find %d target(s) (%+v)", proxy.Metadata.Name, len(proxyTargets), proxyTargets)
if err != nil { proxyEndpoints, err := gs.generateEndpointsFromProxy(ctx, &proxy, proxyTargets)
return nil, err if err != nil {
} return nil, err
log.Debugf("Gloo[%s]: Generate %d endpoint(s)", proxy.Metadata.Name, len(proxyEndpoints)) }
endpoints = append(endpoints, proxyEndpoints...) log.Debugf("Gloo[%s]: Generate %d endpoint(s)", proxy.Metadata.Name, len(proxyEndpoints))
endpoints = append(endpoints, proxyEndpoints...)
}
} }
return endpoints, nil return endpoints, nil
} }
@ -168,8 +172,8 @@ func (gs *glooSource) annotationsFromProxySource(ctx context.Context, virtualHos
return annotations, nil return annotations, nil
} }
func (gs *glooSource) proxyTargets(ctx context.Context, name string) (endpoint.Targets, error) { func (gs *glooSource) proxyTargets(ctx context.Context, name string, namespace string) (endpoint.Targets, error) {
svc, err := gs.kubeClient.CoreV1().Services(gs.glooNamespace).Get(ctx, name, metav1.GetOptions{}) svc, err := gs.kubeClient.CoreV1().Services(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -67,7 +67,7 @@ type Config struct {
CFUsername string CFUsername string
CFPassword string CFPassword string
ContourLoadBalancerService string ContourLoadBalancerService string
GlooNamespace string GlooNamespaces string
SkipperRouteGroupVersion string SkipperRouteGroupVersion string
RequestTimeout time.Duration RequestTimeout time.Duration
DefaultTargets []string DefaultTargets []string
@ -288,7 +288,7 @@ func BuildWithConfig(ctx context.Context, source string, p ClientGenerator, cfg
if err != nil { if err != nil {
return nil, err return nil, err
} }
return NewGlooSource(dynamicClient, kubernetesClient, cfg.GlooNamespace) return NewGlooSource(dynamicClient, kubernetesClient, cfg.GlooNamespaces)
case "openshift-route": case "openshift-route":
ocpClient, err := p.OpenShiftClient() ocpClient, err := p.OpenShiftClient()
if err != nil { if err != nil {