Skip to content

Commit

Permalink
Introduce resolver module to the CoreDNS plugin
Browse files Browse the repository at this point in the history
Currently, ServiceImport and EndpointSlice processing are in
separate components, with ServiceImports used for resolving
non-headless service queries and EndpointSlice used for
headless services. However, the upcoming ServiceImport
aggregation changes will require direct coordination between
ServiceImports and EndpointSlices for resolving DNS records.
Thus, it makes sense to combine the processing in
'serviceimport.Map' and 'endpointslice.Map ' into a single
component. A new 'resolver' module is introduced to contain
a controller component that watches for ServiceImports and
EndpointSlices and feeds them into a resolver component that
builds internal structs used to provide DNSRecords on request.

Subsequent commits will modify the plugin handler to user the
resolver.

Signed-off-by: Tom Pantelis <[email protected]>
  • Loading branch information
tpantelis committed Feb 28, 2023
1 parent 5de2960 commit 6371e20
Show file tree
Hide file tree
Showing 12 changed files with 2,026 additions and 0 deletions.
403 changes: 403 additions & 0 deletions coredns/resolver/clusterip_service_test.go

Large diffs are not rendered by default.

111 changes: 111 additions & 0 deletions coredns/resolver/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resolver

import (
"github.com/pkg/errors"
"github.com/submariner-io/admiral/pkg/log"
"github.com/submariner-io/admiral/pkg/watcher"
"github.com/submariner-io/lighthouse/coredns/constants"
discovery "k8s.io/api/discovery/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
mcsv1a1 "sigs.k8s.io/mcs-api/pkg/apis/v1alpha1"
)

var logger = log.Logger{Logger: logf.Log.WithName("Resolver")}

type controller struct {
resolver *Interface
stopCh chan struct{}
}

func NewController(r *Interface) *controller {
return &controller{
resolver: r,
stopCh: make(chan struct{}),
}
}

func (c *controller) Start(config watcher.Config) error {
logger.Infof("Starting Resolver Controller")

config.ResourceConfigs = []watcher.ResourceConfig{
{
Name: "EndpointSlice watcher",
ResourceType: &discovery.EndpointSlice{},
SourceNamespace: metav1.NamespaceAll,
SourceLabelSelector: labels.Set(map[string]string{discovery.LabelManagedBy: constants.LabelValueManagedBy}).String(),
Handler: watcher.EventHandlerFuncs{
OnCreateFunc: c.onEndpointSliceCreateOrUpdate,
OnUpdateFunc: c.onEndpointSliceCreateOrUpdate,
OnDeleteFunc: c.onEndpointSliceDelete,
},
},
{
Name: "ServiceImport watcher",
ResourceType: &mcsv1a1.ServiceImport{},
SourceNamespace: metav1.NamespaceAll,
Handler: watcher.EventHandlerFuncs{
OnCreateFunc: c.onServiceImportCreateOrUpdate,
OnUpdateFunc: c.onServiceImportCreateOrUpdate,
OnDeleteFunc: c.onServiceImportDelete,
},
},
}

resourceWatcher, err := watcher.New(&config)
if err != nil {
return errors.Wrap(err, "error creating the resource watcher")
}

err = resourceWatcher.Start(c.stopCh)
if err != nil {
return errors.Wrap(err, "error starting the resource watcher")
}

return nil
}

func (c *controller) Stop() {
close(c.stopCh)

logger.Infof("Resolver Controller stopped")
}

func (c *controller) onEndpointSliceCreateOrUpdate(obj runtime.Object, _ int) bool {
return c.resolver.PutEndpointSlice(obj.(*discovery.EndpointSlice))
}

func (c *controller) onEndpointSliceDelete(obj runtime.Object, _ int) bool {
c.resolver.RemoveEndpointSlice(obj.(*discovery.EndpointSlice))
return false
}

func (c *controller) onServiceImportCreateOrUpdate(obj runtime.Object, _ int) bool {
c.resolver.PutServiceImport(obj.(*mcsv1a1.ServiceImport))
return false
}

func (c *controller) onServiceImportDelete(obj runtime.Object, _ int) bool {
c.resolver.RemoveServiceImport(obj.(*mcsv1a1.ServiceImport))
return false
}
114 changes: 114 additions & 0 deletions coredns/resolver/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resolver_test

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/submariner-io/lighthouse/coredns/resolver"
discovery "k8s.io/api/discovery/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
mcsv1a1 "sigs.k8s.io/mcs-api/pkg/apis/v1alpha1"
)

var _ = Describe("Controller", func() {
t := newTestDriver()

expDNSRecord := resolver.DNSRecord{
IP: serviceIP1,
Ports: []mcsv1a1.ServicePort{port1},
ClusterName: clusterID1,
}

When("a ServiceImport is created", func() {
var serviceImport *mcsv1a1.ServiceImport

BeforeEach(func() {
serviceImport = newClusterServiceImport(namespace1, service1, serviceIP1, clusterID1, port1)
t.createServiceImport(serviceImport)
})

Specify("GetDNSRecords should return the cluster's DNS record when requested", func() {
t.awaitDNSRecordsFound(namespace1, service1, clusterID1, "", false, expDNSRecord)
})

Context("and is subsequently deleted", func() {
Specify("GetDNSRecords should eventually return no DNS record found", func() {
t.awaitDNSRecords(namespace1, service1, clusterID1, "", true)

err := t.serviceImports.Namespace(serviceImport.Namespace).Delete(context.TODO(), serviceImport.Name, metav1.DeleteOptions{})
Expect(err).To(Succeed())

t.awaitDNSRecords(namespace1, service1, clusterID1, "", false)
})
})
})

When("an EndpointSlice is created", func() {
var endpointSlice *discovery.EndpointSlice

JustBeforeEach(func() {
endpointSlice = newClusterIPEndpointSlice(namespace1, service1, clusterID1, serviceIP1, true)
t.createEndpointSlice(endpointSlice)
})

Context("before a ServiceImport", func() {
Specify("GetDNSRecords should eventually deem the cluster healthy and return its DNS record", func() {
Consistently(func() bool {
_, _, found := t.resolver.GetDNSRecords(namespace1, service1, "", "")
return found
}).Should(BeFalse())

t.createServiceImport(newClusterServiceImport(namespace1, service1, serviceIP1, clusterID1, port1))

t.awaitDNSRecordsFound(namespace1, service1, clusterID1, "", false, expDNSRecord)
})
})

Context("after a ServiceImport", func() {
BeforeEach(func() {
t.createServiceImport(newClusterServiceImport(namespace1, service1, serviceIP1, clusterID1, port1))
})

Context("and then deleted", func() {
Specify("GetDNSRecords should eventually deem the cluster unhealthy and return no DNS record", func() {
t.awaitDNSRecordsFound(namespace1, service1, clusterID1, "", false, expDNSRecord)

err := t.endpointSlices.Namespace(namespace1).Delete(context.TODO(), endpointSlice.Name, metav1.DeleteOptions{})
Expect(err).To(Succeed())

t.awaitDNSRecordsFound(namespace1, service1, "", "", false)
})
})

Context("and then updated to unhealthy", func() {
Specify("GetDNSRecords should eventually return no DNS record", func() {
t.awaitDNSRecordsFound(namespace1, service1, clusterID1, "", false, expDNSRecord)

err := t.endpointSlices.Namespace(namespace1).Delete(context.TODO(), endpointSlice.Name, metav1.DeleteOptions{})
Expect(err).To(Succeed())

t.awaitDNSRecordsFound(namespace1, service1, "", "", false)
})
})
})
})
})
Loading

0 comments on commit 6371e20

Please sign in to comment.