Skip to content

Commit

Permalink
Retrieve DNS domain name from cluster
Browse files Browse the repository at this point in the history
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
  • Loading branch information
averdagu committed Oct 29, 2024
1 parent 8e6c045 commit 5784570
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
8 changes: 8 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ rules:
- patch
- update
- watch
- apiGroups:
- operator.openshift.io
resources:
- dnses
verbs:
- get
- list
- watch
- apiGroups:
- ovn.openstack.org
resources:
Expand Down
23 changes: 22 additions & 1 deletion controllers/ovndbcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
}
Expand All @@ -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))
}
}

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

Expand Down

0 comments on commit 5784570

Please sign in to comment.