From e2e5a9b932760653ae257f3e11a20085748b76e5 Mon Sep 17 00:00:00 2001 From: bewing Date: Thu, 19 Dec 2024 00:05:10 -0600 Subject: [PATCH] Add flag to support ClusterIP exposed CoreDNS (#1788) Some environments (Calico bare-metal, etc) may allow direct client reachability to the Service CIDR, bypassing the need to assign and use LoadBalancerIPs. This PR adds logic to determine if the coredns service is of type ClusterIP or LoadBalancer and returns the respective IP addresses. --------- Signed-off-by: Brandon Ewing --- controllers/mocks/assistant_mock.go | 16 +++++++++++++ controllers/providers/assistant/assistant.go | 3 +++ controllers/providers/assistant/gslb.go | 24 ++++++++++++++++++-- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/controllers/mocks/assistant_mock.go b/controllers/mocks/assistant_mock.go index ffa837c62c..f84c1e6076 100644 --- a/controllers/mocks/assistant_mock.go +++ b/controllers/mocks/assistant_mock.go @@ -33,6 +33,7 @@ import ( assistant "github.com/k8gb-io/k8gb/controllers/providers/assistant" gomock "go.uber.org/mock/gomock" + v1 "k8s.io/api/core/v1" endpoint "sigs.k8s.io/external-dns/endpoint" ) @@ -74,6 +75,21 @@ func (mr *MockAssistantMockRecorder) CoreDNSExposedIPs() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CoreDNSExposedIPs", reflect.TypeOf((*MockAssistant)(nil).CoreDNSExposedIPs)) } +// GetCoreDNSService mocks base method. +func (m *MockAssistant) GetCoreDNSService() (*v1.Service, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetCoreDNSService") + ret0, _ := ret[0].(*v1.Service) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetCoreDNSService indicates an expected call of GetCoreDNSService. +func (mr *MockAssistantMockRecorder) GetCoreDNSService() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCoreDNSService", reflect.TypeOf((*MockAssistant)(nil).GetCoreDNSService)) +} + // GetExternalTargets mocks base method. func (m *MockAssistant) GetExternalTargets(host string, extClusterNsNames map[string]string) assistant.Targets { m.ctrl.T.Helper() diff --git a/controllers/providers/assistant/assistant.go b/controllers/providers/assistant/assistant.go index 196555c050..faef7a2aba 100644 --- a/controllers/providers/assistant/assistant.go +++ b/controllers/providers/assistant/assistant.go @@ -21,10 +21,13 @@ Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic import ( "time" + corev1 "k8s.io/api/core/v1" externaldns "sigs.k8s.io/external-dns/endpoint" ) type Assistant interface { + // GetCoreDNSService returns the CoreDNS Service + GetCoreDNSService() (*corev1.Service, error) // CoreDNSExposedIPs retrieves list of exposed IP by CoreDNS CoreDNSExposedIPs() ([]string, error) // GetExternalTargets retrieves slice of targets from external clusters diff --git a/controllers/providers/assistant/gslb.go b/controllers/providers/assistant/gslb.go index de00eb0c50..c8a6dc63b4 100644 --- a/controllers/providers/assistant/gslb.go +++ b/controllers/providers/assistant/gslb.go @@ -58,8 +58,8 @@ func NewGslbAssistant(client client.Client, k8gbNamespace string, edgeDNSServers } } -// CoreDNSExposedIPs retrieves list of IP's exposed by CoreDNS -func (r *Gslb) CoreDNSExposedIPs() ([]string, error) { +// GetCoreDNSService returns the CoreDNS Service +func (r *Gslb) GetCoreDNSService() (*corev1.Service, error) { serviceList := &corev1.ServiceList{} sel, err := labels.Parse(coreDNSServiceLabel) if err != nil { @@ -88,7 +88,27 @@ func (r *Gslb) CoreDNSExposedIPs() ([]string, error) { return nil, err } coreDNSService := &serviceList.Items[0] + return coreDNSService, nil +} +// CoreDNSExposedIPs retrieves list of IP's exposed by CoreDNS +func (r *Gslb) CoreDNSExposedIPs() ([]string, error) { + coreDNSService, err := r.GetCoreDNSService() + if err != nil { + return nil, err + } + if coreDNSService.Spec.Type == "ClusterIP" { + if len(coreDNSService.Spec.ClusterIPs) == 0 { + errMessage := "no ClusterIPs found" + log.Warn(). + Str("serviceName", coreDNSService.Name). + Msg(errMessage) + err := coreerrors.New(errMessage) + return nil, err + } + return coreDNSService.Spec.ClusterIPs, nil + } + // LoadBalancer / ExternalName / NodePort service var lb corev1.LoadBalancerIngress if len(coreDNSService.Status.LoadBalancer.Ingress) == 0 { errMessage := "no LoadBalancer ExternalIPs are found"