Skip to content

Commit 1a7c7ee

Browse files
committed
Iniialize global config in the CoreDNS plugin setup
Signed-off-by: Tom Pantelis <[email protected]>
1 parent c066578 commit 1a7c7ee

File tree

5 files changed

+118
-62
lines changed

5 files changed

+118
-62
lines changed

coredns/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/onsi/gomega v1.38.2
1111
github.com/pkg/errors v0.9.1
1212
github.com/prometheus/client_golang v1.23.2
13-
github.com/submariner-io/admiral v0.22.0-m2.0.20251014134744-5203e0e79bf9
13+
github.com/submariner-io/admiral v0.22.0-m2.0.20251020132010-7ac697e3ece9
1414
k8s.io/api v0.34.1
1515
k8s.io/apimachinery v0.34.1
1616
k8s.io/client-go v0.34.1

coredns/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
297297
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
298298
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
299299
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
300-
github.com/submariner-io/admiral v0.22.0-m2.0.20251014134744-5203e0e79bf9 h1:z0NazUBuNEM7MUx0vtkZFlRC9iye/0/vL7N6hdQ0kmI=
301-
github.com/submariner-io/admiral v0.22.0-m2.0.20251014134744-5203e0e79bf9/go.mod h1:wHfXJrUF1hzyDRsZvJ8MGB/Tc7CEwRMLMQt8C4onvNg=
300+
github.com/submariner-io/admiral v0.22.0-m2.0.20251020132010-7ac697e3ece9 h1:VJpxfUh5jeanWsd/orDN/0ID87NdaEqYtmf+YBKeeLM=
301+
github.com/submariner-io/admiral v0.22.0-m2.0.20251020132010-7ac697e3ece9/go.mod h1:w2mFuBeiXcUCtITrlEp5KiG+x55l0+F6oIK5r34jUvQ=
302302
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
303303
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
304304
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=

coredns/plugin/lighthouse.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package lighthouse
2121
import (
2222
"errors"
2323

24+
"github.com/coredns/caddy"
2425
"github.com/coredns/coredns/plugin"
2526
"github.com/coredns/coredns/plugin/pkg/fall"
2627
"github.com/submariner-io/admiral/pkg/log"
@@ -50,3 +51,46 @@ type Lighthouse struct {
5051
}
5152

5253
var _ plugin.Handler = &Lighthouse{}
54+
55+
func (lh *Lighthouse) configure(c *caddy.Controller) error {
56+
if c.Next() {
57+
lh.Zones = c.RemainingArgs()
58+
if len(lh.Zones) == 0 {
59+
lh.Zones = make([]string, len(c.ServerBlockKeys))
60+
copy(lh.Zones, c.ServerBlockKeys)
61+
}
62+
63+
for i, str := range lh.Zones {
64+
hosts := plugin.Host(str).NormalizeExact()
65+
if hosts == nil {
66+
logger.Infof("Failed to normalize zone %q", str)
67+
68+
lh.Zones[i] = ""
69+
70+
continue
71+
}
72+
73+
lh.Zones[i] = hosts[0]
74+
}
75+
76+
for c.NextBlock() {
77+
switch c.Val() {
78+
case "fallthrough":
79+
lh.Fall.SetZonesFromArgs(c.RemainingArgs())
80+
case "ttl":
81+
t, err := parseTTL(c)
82+
if err != nil {
83+
return err
84+
}
85+
86+
lh.TTL = t
87+
default:
88+
if c.Val() != "}" {
89+
return c.Errf("unknown property '%s'", c.Val()) //nolint:wrapcheck // No need to wrap this.
90+
}
91+
}
92+
}
93+
}
94+
95+
return nil
96+
}

coredns/plugin/setup.go

Lines changed: 39 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,24 @@ import (
2323
"os"
2424
"strconv"
2525
"strings"
26+
"syscall"
2627

2728
"github.com/coredns/caddy"
2829
"github.com/coredns/coredns/core/dnsserver"
2930
"github.com/coredns/coredns/plugin"
3031
"github.com/pkg/errors"
32+
"github.com/submariner-io/admiral/pkg/configmap"
33+
"github.com/submariner-io/admiral/pkg/global"
34+
"github.com/submariner-io/admiral/pkg/names"
35+
"github.com/submariner-io/admiral/pkg/resource"
3136
"github.com/submariner-io/admiral/pkg/watcher"
3237
"github.com/submariner-io/lighthouse/coredns/gateway"
3338
"github.com/submariner-io/lighthouse/coredns/resolver"
3439
discovery "k8s.io/api/discovery/v1"
3540
"k8s.io/apimachinery/pkg/api/meta"
3641
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
37-
"k8s.io/client-go/dynamic"
42+
"k8s.io/apimachinery/pkg/util/wait"
43+
"k8s.io/client-go/kubernetes"
3844
"k8s.io/client-go/kubernetes/scheme"
3945
"k8s.io/client-go/rest"
4046
"k8s.io/client-go/tools/clientcmd"
@@ -47,13 +53,11 @@ var (
4753
kubeconfig string
4854
)
4955

50-
// Hooks for unit tests.
5156
var (
5257
buildKubeConfigFunc = clientcmd.BuildConfigFromFlags
5358

54-
newDynamicClient = func(c *rest.Config) (dynamic.Interface, error) {
55-
client, err := dynamic.NewForConfig(c)
56-
return client, errors.Wrap(err, "error creating a dynamic client")
59+
newK8sClient = func(cfg *rest.Config) (kubernetes.Interface, error) {
60+
return kubernetes.NewForConfig(cfg)
5761
}
5862

5963
restMapper meta.RESTMapper
@@ -95,83 +99,64 @@ func lighthouseParse(c *caddy.Controller) (*Lighthouse, error) {
9599

96100
gwController := gateway.NewController()
97101

98-
localClient, err := newDynamicClient(cfg)
102+
localClient, err := resource.NewDynamicClient(cfg)
99103
if err != nil {
100104
return nil, errors.Wrap(err, "error creating local client")
101105
}
102106

107+
k8sClient, err := newK8sClient(cfg)
108+
if err != nil {
109+
return nil, errors.Wrap(err, "error creating K8s client")
110+
}
111+
103112
lh := &Lighthouse{
104113
TTL: defaultTTL,
105114
ClusterStatus: gwController,
106115
Resolver: resolver.New(gwController, localClient),
107116
SupportedIPFamilies: determineSupportedAddressTypes(),
108117
}
109118

110-
err = gwController.Start(localClient)
111-
if err != nil {
112-
return nil, errors.Wrap(err, "error starting the Gateway controller")
113-
}
114-
115119
resolverController := resolver.NewController(lh.Resolver)
116120

117-
err = resolverController.Start(&watcher.Config{
118-
RestConfig: cfg,
119-
Client: localClient,
120-
RestMapper: restMapper,
121-
})
122-
if err != nil {
123-
return nil, errors.Wrap(err, "error starting the resolver controller")
124-
}
121+
stopCh := make(chan struct{})
122+
ctx := wait.ContextForChannel(stopCh)
125123

126124
c.OnShutdown(func() error {
125+
close(stopCh)
127126
gwController.Stop()
128127
resolverController.Stop()
129128

130129
return nil
131130
})
132131

133-
// Changed `for` to `if` to satisfy golint:
134-
// SA4004: the surrounding loop is unconditionally terminated (staticcheck)
135-
if c.Next() {
136-
lh.Zones = c.RemainingArgs()
137-
if len(lh.Zones) == 0 {
138-
lh.Zones = make([]string, len(c.ServerBlockKeys))
139-
copy(lh.Zones, c.ServerBlockKeys)
140-
}
132+
submNamespace := os.Getenv("SUBMARINER_NAMESPACE")
141133

142-
for i, str := range lh.Zones {
143-
hosts := plugin.Host(str).NormalizeExact()
144-
if hosts == nil {
145-
logger.Infof("Failed to normalize zone %q", str)
134+
configMap, err := configmap.Get(ctx, resource.ForConfigMap(k8sClient, submNamespace), names.LighthouseCoreDNSComponent)
135+
if err != nil {
136+
return nil, errors.Wrap(err, "error retrieving ConfigMap")
137+
}
146138

147-
lh.Zones[i] = ""
139+
global.Init(configMap)
148140

149-
continue
150-
}
141+
configmap.WatchAndSignalOnChange(ctx, k8sClient, submNamespace, syscall.SIGINT, names.ServiceDiscoveryComponent)
151142

152-
lh.Zones[i] = hosts[0]
153-
}
143+
err = gwController.Start(localClient)
144+
if err != nil {
145+
return nil, errors.Wrap(err, "error starting the Gateway controller")
146+
}
154147

155-
for c.NextBlock() {
156-
switch c.Val() {
157-
case "fallthrough":
158-
lh.Fall.SetZonesFromArgs(c.RemainingArgs())
159-
case "ttl":
160-
t, err := parseTTL(c)
161-
if err != nil {
162-
return nil, err
163-
}
164-
165-
lh.TTL = t
166-
default:
167-
if c.Val() != "}" {
168-
return nil, c.Errf("unknown property '%s'", c.Val()) //nolint:wrapcheck // No need to wrap this.
169-
}
170-
}
171-
}
148+
err = resolverController.Start(&watcher.Config{
149+
RestConfig: cfg,
150+
Client: localClient,
151+
RestMapper: restMapper,
152+
})
153+
if err != nil {
154+
return nil, errors.Wrap(err, "error starting the resolver controller")
172155
}
173156

174-
return lh, nil
157+
err = lh.configure(c)
158+
159+
return lh, err
175160
}
176161

177162
func determineSupportedAddressTypes() []k8snet.IPFamily {

coredns/plugin/setup_internal_test.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,18 @@ import (
2929
"github.com/miekg/dns"
3030
. "github.com/onsi/ginkgo/v2"
3131
. "github.com/onsi/gomega"
32+
"github.com/submariner-io/admiral/pkg/global"
33+
"github.com/submariner-io/admiral/pkg/names"
34+
"github.com/submariner-io/admiral/pkg/resource"
3235
"github.com/submariner-io/admiral/pkg/syncer/test"
36+
corev1 "k8s.io/api/core/v1"
3337
discovery "k8s.io/api/discovery/v1"
38+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3439
"k8s.io/apimachinery/pkg/runtime/schema"
3540
"k8s.io/client-go/dynamic"
3641
fakeClient "k8s.io/client-go/dynamic/fake"
42+
"k8s.io/client-go/kubernetes"
43+
k8sfake "k8s.io/client-go/kubernetes/fake"
3744
"k8s.io/client-go/kubernetes/scheme"
3845
"k8s.io/client-go/rest"
3946
k8snet "k8s.io/utils/net"
@@ -51,6 +58,8 @@ func (f *fakeHandler) Name() string {
5158
}
5259

5360
var _ = Describe("Plugin setup", func() {
61+
os.Setenv("SUBMARINER_NAMESPACE", "submariner-operator")
62+
5463
BeforeEach(func() {
5564
os.Unsetenv("SUBMARINER_CLUSTERCIDR")
5665

@@ -66,18 +75,18 @@ var _ = Describe("Plugin setup", func() {
6675
Resource: "submariners",
6776
}
6877

69-
newDynamicClient = func(_ *rest.Config) (dynamic.Interface, error) {
78+
resource.NewDynamicClient = func(_ *rest.Config) (dynamic.Interface, error) {
7079
return fakeClient.NewSimpleDynamicClientWithCustomListKinds(scheme.Scheme, map[schema.GroupVersionResource]string{
7180
gatewaysGVR: "GatewayList",
7281
submarinersGVR: "SubmarinersList",
7382
}), nil
7483
}
7584

76-
restMapper = test.GetRESTMapperFor(&discovery.EndpointSlice{}, &mcsv1a1.ServiceImport{})
77-
})
85+
newK8sClient = func(_ *rest.Config) (kubernetes.Interface, error) {
86+
return k8sfake.NewClientset(), nil
87+
}
7888

79-
AfterEach(func() {
80-
newDynamicClient = nil
89+
restMapper = test.GetRESTMapperFor(&discovery.EndpointSlice{}, &mcsv1a1.ServiceImport{})
8190
})
8291

8392
Context("Parsing correct configurations", testCorrectConfig)
@@ -188,6 +197,24 @@ func testCorrectConfig() {
188197
Expect(lh.SupportedIPFamilies).To(ContainElements(k8snet.IPv4, k8snet.IPv6))
189198
})
190199
})
200+
201+
When("the LH CoreDNS component ConfigMap exists", func() {
202+
BeforeEach(func() {
203+
newK8sClient = func(_ *rest.Config) (kubernetes.Interface, error) {
204+
return k8sfake.NewClientset(&corev1.ConfigMap{
205+
ObjectMeta: metav1.ObjectMeta{
206+
Name: names.LighthouseCoreDNSComponent,
207+
Namespace: os.Getenv("SUBMARINER_NAMESPACE"),
208+
},
209+
Data: map[string]string{"foo": "bar"},
210+
}), nil
211+
}
212+
})
213+
214+
It("should initialize the global config", func() {
215+
Expect(global.Get("foo", "")).To(Equal("bar"))
216+
})
217+
})
191218
}
192219

193220
func testIncorrectConfig() {

0 commit comments

Comments
 (0)