From 5784570b560c0d4f11b22668ba3cc7dbbb5eb630 Mon Sep 17 00:00:00 2001 From: Arnau Verdaguer Date: Tue, 2 Apr 2024 16:52:17 +0200 Subject: [PATCH] Retrieve DNS domain name from cluster OVNDBCluster uses openshift dns name for Status.InternalDbAddress, which the dnsrecord is created using the following pattern: [pod_name].[namespace].[type(svc)].[ClusterDomain] The default for ClusterDomain is 'cluster.local' but this could change in the future as mentioned in [0]. With this patch the ClusterDomain is gathered from the environment and not hardcoded. [0] https://github.com/openshift/cluster-dns-operator/blob/208d50c1a5e0aaeb991366daa749abdffa803224/pkg/operator/controller/controller.go#L498 Jira: OSPRH-3627 --- config/rbac/role.yaml | 8 ++++++++ controllers/ovndbcluster_controller.go | 23 ++++++++++++++++++++++- go.mod | 2 +- main.go | 2 ++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/config/rbac/role.yaml b/config/rbac/role.yaml index 7947cc3d..af606599 100644 --- a/config/rbac/role.yaml +++ b/config/rbac/role.yaml @@ -143,6 +143,14 @@ rules: - patch - update - watch +- apiGroups: + - operator.openshift.io + resources: + - dnses + verbs: + - get + - list + - watch - apiGroups: - ovn.openstack.org resources: diff --git a/controllers/ovndbcluster_controller.go b/controllers/ovndbcluster_controller.go index 4a21566a..d093c23e 100644 --- a/controllers/ovndbcluster_controller.go +++ b/controllers/ovndbcluster_controller.go @@ -37,6 +37,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" + operatorv1 "github.com/openshift/api/operator/v1" infranetworkv1 "github.com/openstack-k8s-operators/infra-operator/apis/network/v1beta1" "github.com/openstack-k8s-operators/lib-common/modules/common" "github.com/openstack-k8s-operators/lib-common/modules/common/condition" @@ -97,6 +98,7 @@ func (r *OVNDBClusterReconciler) GetLogger(ctx context.Context) logr.Logger { //+kubebuilder:rbac:groups=core,resources=pods,verbs=get;list; //+kubebuilder:rbac:groups=k8s.cni.cncf.io,resources=network-attachment-definitions,verbs=get;list;watch //+kubebuilder:rbac:groups=network.openstack.org,resources=dnsdata,verbs=get;list;watch;create;update;patch;delete +//+kubebuilder:rbac:groups="operator.openshift.io",resources=dnses,verbs=get;list;watch // service account, role, rolebinding // +kubebuilder:rbac:groups="",resources=serviceaccounts,verbs=get;list;watch;create;update;patch @@ -607,6 +609,8 @@ func (r *OVNDBClusterReconciler) reconcileNormal(ctx context.Context, instance * internalDbAddress := []string{} var svcPort int32 scheme := "tcp" + // Get DNS Cluster suffix + DNSSuffix := getDNSDomain(ctx, r.GetClient(), r.GetLogger(ctx)) if instance.Spec.TLS.Enabled() { scheme = "ssl" } @@ -615,7 +619,7 @@ func (r *OVNDBClusterReconciler) reconcileNormal(ctx context.Context, instance * // Filter out headless services if svc.Spec.ClusterIP != "None" { - internalDbAddress = append(internalDbAddress, fmt.Sprintf("%s:%s.%s.svc.%s:%d", scheme, svc.Name, svc.Namespace, ovnv1.DNSSuffix, svcPort)) + internalDbAddress = append(internalDbAddress, fmt.Sprintf("%s:%s.%s.svc.%s:%d", scheme, svc.Name, svc.Namespace, DNSSuffix, svcPort)) } } @@ -641,6 +645,23 @@ func (r *OVNDBClusterReconciler) reconcileNormal(ctx context.Context, instance * return ctrl.Result{}, nil } +func getDNSDomain(ctx context.Context, c client.Client, Log logr.Logger) string { + DNSDomain := ovnv1.DNSSuffix + DNSClusterInfoList := &operatorv1.DNSList{} + err := c.List(ctx, DNSClusterInfoList) + if err != nil { + Log.Info(fmt.Sprintf("Warning: Couldn't retrieve DNS cluster info, using default DNS Suffix: %s", DNSDomain)) + return DNSDomain + } + // Using this approach in case CP have multiple domains + // also it does not depend on the DNS CR name. + // ATM in case of multiple domains will return last one. + for _, dns := range DNSClusterInfoList.Items { + DNSDomain = dns.Status.ClusterDomain + } + return DNSDomain +} + func getPodIPInNetwork(ovnPod corev1.Pod, namespace string, networkAttachment string) (string, error) { netStat, err := nad.GetNetworkStatusFromAnnotation(ovnPod.Annotations) if err != nil { diff --git a/go.mod b/go.mod index cf72b4a3..3778001f 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.7.5 github.com/onsi/ginkgo/v2 v2.20.1 github.com/onsi/gomega v1.34.1 + github.com/openshift/api v3.9.0+incompatible github.com/openstack-k8s-operators/infra-operator/apis v0.5.1-0.20241024081600-3e23dc62002c github.com/openstack-k8s-operators/lib-common/modules/common v0.5.1-0.20241025164019-30baa23bf6f1 github.com/openstack-k8s-operators/lib-common/modules/test v0.5.1-0.20241025164019-30baa23bf6f1 @@ -47,7 +48,6 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect - github.com/openshift/api v3.9.0+incompatible // indirect github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.18.0 // indirect github.com/prometheus/client_model v0.5.0 // indirect diff --git a/main.go b/main.go index 52591aac..c5e10ab8 100644 --- a/main.go +++ b/main.go @@ -40,6 +40,7 @@ import ( metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" networkv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1" + operatorv1 "github.com/openshift/api/operator/v1" infranetworkv1 "github.com/openstack-k8s-operators/infra-operator/apis/network/v1beta1" ovnv1 "github.com/openstack-k8s-operators/ovn-operator/api/v1beta1" @@ -57,6 +58,7 @@ func init() { utilruntime.Must(ovnv1.AddToScheme(scheme)) utilruntime.Must(networkv1.AddToScheme(scheme)) utilruntime.Must(infranetworkv1.AddToScheme(scheme)) + utilruntime.Must(operatorv1.AddToScheme(scheme)) //+kubebuilder:scaffold:scheme }