diff --git a/api/v1beta1/ovndbcluster_types.go b/api/v1beta1/ovndbcluster_types.go index d8c8fc53..98066ebd 100644 --- a/api/v1beta1/ovndbcluster_types.go +++ b/api/v1beta1/ovndbcluster_types.go @@ -200,5 +200,8 @@ func (instance OVNDBCluster) GetExternalEndpoint() (string, error) { if instance.Status.DBAddress == "" { return "", fmt.Errorf("external DBEndpoint not ready yet for %s", instance.Spec.DBType) } - return instance.Status.DBAddress, nil + //return instance.Status.DBAddress, nil + //return "tcp:ovsdbserver-sb.openstack.svc:6642", nil + dns_hostname := "tcp:ovsdbserver-sb." + instance.Namespace + ".svc:6642" + return dns_hostname, nil } diff --git a/controllers/ovncontroller_controller.go b/controllers/ovncontroller_controller.go index d5b3004a..b0d19a8d 100644 --- a/controllers/ovncontroller_controller.go +++ b/controllers/ovncontroller_controller.go @@ -580,6 +580,7 @@ func (r *OVNControllerReconciler) generateExternalConfigMaps( } externalTemplateParameters := make(map[string]interface{}) + // TODO change externalEndpoint to DNS externalTemplateParameters["OvnRemote"] = externalEndpoint externalTemplateParameters["OvnEncapType"] = instance.Spec.ExternalIDS.OvnEncapType diff --git a/controllers/ovndbcluster_controller.go b/controllers/ovndbcluster_controller.go index 9383a6b7..61f2b224 100644 --- a/controllers/ovndbcluster_controller.go +++ b/controllers/ovndbcluster_controller.go @@ -22,6 +22,8 @@ import ( "strings" "time" + "github.com/go-logr/logr" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes" ctrl "sigs.k8s.io/controller-runtime" @@ -29,7 +31,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/log" - "github.com/go-logr/logr" + 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" "github.com/openstack-k8s-operators/lib-common/modules/common/configmap" @@ -519,6 +521,10 @@ func (r *OVNDBClusterReconciler) reconcileServices( if err != nil { return ctrl.Result{}, err } + // averdagu I don't think it's needed, maybe will delete later + headlesssvc.AddAnnotation(map[string]string{ + service.AnnotationHostnameKey: headlesssvc.GetServiceHostname(), + }) ctrlResult, err := headlesssvc.CreateOrPatch(ctx, helper) if err != nil { @@ -548,6 +554,9 @@ func (r *OVNDBClusterReconciler) reconcileServices( if err != nil { return ctrl.Result{}, err } + svc.AddAnnotation(map[string]string{ + service.AnnotationHostnameKey: svc.GetServiceHostname(), + }) ctrlResult, err := svc.CreateOrPatch(ctx, helper) if err != nil { return ctrl.Result{}, err @@ -557,6 +566,7 @@ func (r *OVNDBClusterReconciler) reconcileServices( // create service - end } + // TODO: Delete also DNS info if exists // Delete any extra services left after scale down svcList, err := service.GetServicesListWithLabel( ctx, @@ -581,6 +591,79 @@ func (r *OVNDBClusterReconciler) reconcileServices( } } } + + Log.Info("DNS Starts here") + if instance.Spec.DBType == v1beta1.SBDBType { + Log.Info("Only doing it for SB pods") + serviceName = ovndbcluster.ServiceNameSB + serviceLabels = map[string]string{ + common.AppSelector: serviceName, + } + podList, err = ovndbcluster.OVNDBPods(ctx, instance, helper, serviceLabels) + if err != nil { + return ctrl.Result{}, err + } + for _, ovnPod := range podList.Items { + Log.Info(fmt.Sprintf("Using Pod: %v", ovnPod.Name)) + var dnsName string + var dnsIP string + var hostnames []string + dnsName = "dns-" + ovnPod.Name + // Get Hostname + svc, _ := service.GetServiceWithName( + ctx, + helper, + ovnPod.Name, + ovnPod.Namespace, + ) + hostname := svc.ObjectMeta.Annotations[infranetworkv1.AnnotationHostnameKey] + hostnames = append(hostnames, hostname) + + // Get IP + net_stat, _ := nad.GetNetworkStatusFromAnnotation(ovnPod.Annotations) + for _, v := range net_stat { + if v.Interface == instance.Spec.NetworkAttachment { + dnsIP = v.IPs[0] + } + } + + // Create DNSRecord + var DNSRecords []infranetworkv1.DNSHost + // ovsdbserver-sb-x entry + DNSRecord := infranetworkv1.DNSHost{} + DNSRecord.IP = dnsIP + DNSRecord.Hostnames = hostnames + // ovsdbserver-sb entry + headless_dns_hostname := ovndbcluster.ServiceNameSB + "." + instance.Namespace + ".svc" + DNSRecordCname := infranetworkv1.DNSHost{} + DNSRecordCname.IP = dnsIP + DNSRecordCname.Hostnames = append(DNSRecordCname.Hostnames, headless_dns_hostname) + DNSRecords = append(DNSRecords, DNSRecord) + DNSRecords = append(DNSRecords, DNSRecordCname) + + // Create DNSData + DNSData := &infranetworkv1.DNSData{ + ObjectMeta: metav1.ObjectMeta{ + Name: dnsName, + Namespace: ovnPod.Namespace, + }, + } + _, err := controllerutil.CreateOrPatch(ctx, helper.GetClient(), DNSData, func() error { + DNSData.Spec.Hosts = DNSRecords + DNSData.Spec.DNSDataLabelSelectorValue = "dnsdata" + err := controllerutil.SetControllerReference(helper.GetBeforeObject(), DNSData, helper.GetScheme()) + if err != nil { + Log.Info(fmt.Sprintf("Inside CreateOrPatch error: %v", err)) + } + return err + }) + if err != nil { + Log.Info(fmt.Sprintf("Outside CreateOrPatch error: %v", err)) + } + + } + } + Log.Info("Reconciled OVN DB Cluster Service successfully") return ctrl.Result{}, nil } diff --git a/go.mod b/go.mod index 87d82ab3..1d26a181 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0 github.com/onsi/ginkgo/v2 v2.13.0 github.com/onsi/gomega v1.29.0 + github.com/openstack-k8s-operators/infra-operator/apis v0.3.0 github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20231102083359-58e12a1299b2 github.com/openstack-k8s-operators/lib-common/modules/test v0.3.1-0.20231102083359-58e12a1299b2 github.com/openstack-k8s-operators/ovn-operator/api v0.0.0-20230418071801-b5843d9e05fb diff --git a/go.sum b/go.sum index b6ddfcb1..0dcc7410 100644 --- a/go.sum +++ b/go.sum @@ -132,8 +132,14 @@ github.com/onsi/gomega v1.29.0 h1:KIA/t2t5UBzoirT4H9tsML45GEbo3ouUnBHsCfD2tVg= github.com/onsi/gomega v1.29.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ= github.com/openshift/api v0.0.0-20230414143018-3367bc7e6ac7 h1:rncLxJBpFGqBztyxCMwNRnMjhhIDOWHJowi6q8G6koI= github.com/openshift/api v0.0.0-20230414143018-3367bc7e6ac7/go.mod h1:ctXNyWanKEjGj8sss1KjjHQ3ENKFm33FFnS5BKaIPh4= +github.com/openstack-k8s-operators/infra-operator/apis v0.3.0 h1:omqNm2mG5YOXdNLuUs4fNCvi/2B13njLXfbS2Z4GNUE= +github.com/openstack-k8s-operators/infra-operator/apis v0.3.0/go.mod h1:zqFs5MrBKeaE4HQroUgMWwIkBwmmcygg6sghcidSdCA= +github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20231011150636-e8a0540a3c32 h1:r24jE5tdacLivcZczb3t6RvbvHp6kXQrW2ECuekzgH8= +github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20231011150636-e8a0540a3c32/go.mod h1:xXAuy7HtWN4p7LF5Q+NHLkwAsKVh0KrzpnuPYIG3XaA= github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20231102083359-58e12a1299b2 h1:U4hwPDoo9k2wo6PldPyJbgfvo9Mdyuhum7vLWTHez1c= github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20231102083359-58e12a1299b2/go.mod h1:NvjAETczXby5m3IvylR3YaOiEBWkmWbHBx/UrnUVtfA= +github.com/openstack-k8s-operators/lib-common/modules/test v0.3.1-0.20231011150636-e8a0540a3c32 h1:JCMXaDSjy46ZaHLHb1j2uzGIy2RUmYRCsbtSPkuEUV8= +github.com/openstack-k8s-operators/lib-common/modules/test v0.3.1-0.20231011150636-e8a0540a3c32/go.mod h1:Apy5OTK60yj9cQgVZ0HcGq+CDsLDaaEydJtj8ca0IBk= github.com/openstack-k8s-operators/lib-common/modules/test v0.3.1-0.20231102083359-58e12a1299b2 h1:9RHkP3OSPkZsGbCCbX5YetA4sNxRkFsWmNiDfokB1KQ= github.com/openstack-k8s-operators/lib-common/modules/test v0.3.1-0.20231102083359-58e12a1299b2/go.mod h1:St2fdpMneNUq5XTamRLSgI5iKPm096JFGSFit5FjJ/4= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/main.go b/main.go index 28a56508..d3b5ea6d 100644 --- a/main.go +++ b/main.go @@ -38,6 +38,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log/zap" networkv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1" + infranetworkv1 "github.com/openstack-k8s-operators/infra-operator/apis/network/v1beta1" ovnv1 "github.com/openstack-k8s-operators/ovn-operator/api/v1beta1" "github.com/openstack-k8s-operators/ovn-operator/controllers" @@ -53,6 +54,7 @@ func init() { utilruntime.Must(clientgoscheme.AddToScheme(scheme)) utilruntime.Must(ovnv1.AddToScheme(scheme)) utilruntime.Must(networkv1.AddToScheme(scheme)) + utilruntime.Must(infranetworkv1.AddToScheme(scheme)) //+kubebuilder:scaffold:scheme }