Skip to content

Commit

Permalink
Move OVN db discovery code from submariner-operator to route-agent
Browse files Browse the repository at this point in the history
Signed-off-by: Aswin Suryanarayanan <asuryana@redhat.com>
  • Loading branch information
aswinsuryan committed Sep 9, 2023
1 parent 4f7b237 commit 783a580
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 184 deletions.
1 change: 0 additions & 1 deletion config/rbac/submariner-operator/cluster_role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ rules:
- pods
- services
- nodes
- endpoints
verbs:
- get
- list
Expand Down
4 changes: 2 additions & 2 deletions controllers/submariner/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (r *Reconciler) runComponentCleanup(ctx context.Context, instance *operator
}

// This has the side effect of setting the CIDRs in the Submariner instance.
clusterNetwork, err := r.discoverNetwork(ctx, instance, log)
_, err := r.discoverNetwork(ctx, instance, log)
if err != nil {
return reconcile.Result{}, err
}
Expand All @@ -59,7 +59,7 @@ func (r *Reconciler) runComponentCleanup(ctx context.Context, instance *operator
},
{
Resource: newDaemonSet(names.RouteAgentComponent, instance.Namespace),
UninstallResource: newRouteAgentDaemonSet(instance, clusterNetwork, opnames.AppendUninstall(names.RouteAgentComponent)),
UninstallResource: newRouteAgentDaemonSet(instance, opnames.AppendUninstall(names.RouteAgentComponent)),
},
{
Resource: newDaemonSet(names.GlobalnetComponent, instance.Namespace),
Expand Down
21 changes: 3 additions & 18 deletions controllers/submariner/route_agent_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/submariner-io/admiral/pkg/names"
"github.com/submariner-io/submariner-operator/api/v1alpha1"
"github.com/submariner-io/submariner-operator/controllers/apply"
"github.com/submariner-io/submariner-operator/pkg/discovery/network"
"github.com/submariner-io/submariner-operator/pkg/images"
opnames "github.com/submariner-io/submariner-operator/pkg/names"
appsv1 "k8s.io/api/apps/v1"
Expand All @@ -38,13 +37,13 @@ import (

//nolint:wrapcheck // No need to wrap errors here.
func (r *Reconciler) reconcileRouteagentDaemonSet(ctx context.Context, instance *v1alpha1.Submariner,
clusterNetwork *network.ClusterNetwork, reqLogger logr.Logger,
reqLogger logr.Logger,
) (*appsv1.DaemonSet, error) {
return apply.DaemonSet(ctx, instance, newRouteAgentDaemonSet(instance, clusterNetwork, names.RouteAgentComponent),
return apply.DaemonSet(ctx, instance, newRouteAgentDaemonSet(instance, names.RouteAgentComponent),
reqLogger, r.config.ScopedClient, r.config.Scheme)
}

func newRouteAgentDaemonSet(cr *v1alpha1.Submariner, clusterNetwork *network.ClusterNetwork, name string) *appsv1.DaemonSet {
func newRouteAgentDaemonSet(cr *v1alpha1.Submariner, name string) *appsv1.DaemonSet {
labels := map[string]string{
"app": name,
"component": "routeagent",
Expand Down Expand Up @@ -141,19 +140,5 @@ func newRouteAgentDaemonSet(cr *v1alpha1.Submariner, clusterNetwork *network.Clu
},
}

if ovndb, ok := clusterNetwork.PluginSettings[network.OvnNBDB]; ok {
ds.Spec.Template.Spec.Containers[0].Env = append(
ds.Spec.Template.Spec.Containers[0].Env, corev1.EnvVar{
Name: network.OvnNBDB, Value: ovndb,
})
}

if ovnsb, ok := clusterNetwork.PluginSettings[network.OvnSBDB]; ok {
ds.Spec.Template.Spec.Containers[0].Env = append(
ds.Spec.Template.Spec.Containers[0].Env, corev1.EnvVar{
Name: network.OvnSBDB, Value: ovnsb,
})
}

return ds
}
5 changes: 3 additions & 2 deletions controllers/submariner/submariner_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (

initialStatus := instance.Status.DeepCopy()

clusterNetwork, err := r.discoverNetwork(ctx, instance, reqLogger)
// This has the side effect of setting the CIDRs in the Submariner instance.
_, err = r.discoverNetwork(ctx, instance, reqLogger)
if err != nil {
return reconcile.Result{}, err
}
Expand All @@ -174,7 +175,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (
}
}

routeagentDaemonSet, err := r.reconcileRouteagentDaemonSet(ctx, instance, clusterNetwork, reqLogger)
routeagentDaemonSet, err := r.reconcileRouteagentDaemonSet(ctx, instance, reqLogger)
if err != nil {
return reconcile.Result{}, err
}
Expand Down
22 changes: 1 addition & 21 deletions pkg/discovery/network/openshift4.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,7 @@ func discoverOpenShift4Network(ctx context.Context, client controllerClient.Clie
return nil, errors.WithMessage(err, "error obtaining the default 'cluster' OpenShift4 Network config resource")
}

clusterNetwork, err := parseOS4Network(network)
if err != nil {
return nil, err
}

if clusterNetwork.NetworkPlugin == cni.OVNKubernetes {
ovnDBPod, err := FindPod(ctx, client, "name=ovnkube-db")
if err != nil {
return nil, err
}

if ovnDBPod == nil {
// IC is enabled and Openshift uses zone per node and to connect to OVN DB using
// socket connection we need to use this path.
clusterNetwork.PluginSettings = map[string]string{
OvnNBDB: "unix:/var/run/ovn-ic/ovnnb_db.sock",
}
}
}

return clusterNetwork, err
return parseOS4Network(network)
}

func parseOS4Network(cr *unstructured.Unstructured) (*ClusterNetwork, error) {
Expand Down
125 changes: 2 additions & 123 deletions pkg/discovery/network/ovnkubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,120 +20,26 @@ package network

import (
"context"
"fmt"
"strings"

"github.com/pkg/errors"
"github.com/submariner-io/submariner/pkg/cni"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
controllerClient "sigs.k8s.io/controller-runtime/pkg/client"
)

const (
ovnKubeService = "ovnkube-db"
OvnNBDB = "OVN_NBDB"
OvnSBDB = "OVN_SBDB"
DefaultOvnNBDB = "/var/run/openvswitch/ovnnb_db.sock"
OvnNBDBDefaultPort = 6641
OvnSBDBDefaultPort = 6642
)

func discoverOvnKubernetesNetwork(ctx context.Context, client controllerClient.Client) (*ClusterNetwork, error) {
ovnDBPod, err := FindPod(ctx, client, "name=ovnkube-db")
if err != nil {
return nil, err
}

var clusterNetwork *ClusterNetwork

if ovnDBPod != nil {
clusterNetwork, err = discoverOvnDBClusterNetwork(ctx, client, ovnDBPod)
} else {
clusterNetwork, err = discoverOvnNodeClusterNetwork(ctx, client)
}

if err != nil || clusterNetwork == nil {
return nil, err
}

clusterNetwork.NetworkPlugin = cni.OVNKubernetes

return clusterNetwork, nil
}

func discoverOvnDBClusterNetwork(ctx context.Context, client controllerClient.Client, ovnDBPod *corev1.Pod) (*ClusterNetwork, error) {
err := client.Get(ctx, types.NamespacedName{Namespace: ovnDBPod.Namespace, Name: ovnKubeService}, &corev1.Service{})
if err != nil {
return nil, fmt.Errorf("error finding %q service in %q namespace", ovnKubeService, ovnDBPod.Namespace)
}

dbConnectionProtocol := findProtocol(ovnDBPod)

clusterNetwork := &ClusterNetwork{
PluginSettings: map[string]string{
OvnNBDB: fmt.Sprintf("%s:%s.%s:%d", dbConnectionProtocol, ovnKubeService, ovnDBPod.Namespace, OvnNBDBDefaultPort),
OvnSBDB: fmt.Sprintf("%s:%s.%s:%d", dbConnectionProtocol, ovnKubeService, ovnDBPod.Namespace, OvnSBDBDefaultPort),
},
}

updateClusterNetworkFromConfigMap(ctx, client, ovnDBPod.Namespace, clusterNetwork)

return clusterNetwork, nil
}

func discoverOvnNodeClusterNetwork(ctx context.Context, client controllerClient.Client) (*ClusterNetwork, error) {
// In OVN IC deployments, the ovn DB will be a part of ovnkube-node
ovnPod, err := FindPod(ctx, client, "name=ovnkube-node")
ovnPod, err := FindPod(ctx, client, "app=ovnkube-node")
if err != nil || ovnPod == nil {
return nil, err
}

endpointList, err := findEndpoint(ctx, client, ovnPod.Namespace)
if err != nil {
return nil, errors.Wrapf(err, "error retrieving the endpoints from namespace %q", ovnPod.Namespace)
}

var clusterNetwork *ClusterNetwork

if endpointList == nil || len(endpointList.Items) == 0 {
clusterNetwork = createLocalClusterNetwork()
} else {
clusterNetwork = createClusterNetworkWithEndpoints(endpointList.Items)
}
clusterNetwork := &ClusterNetwork{NetworkPlugin: cni.OVNKubernetes}

updateClusterNetworkFromConfigMap(ctx, client, ovnPod.Namespace, clusterNetwork)

return clusterNetwork, nil
}

func createLocalClusterNetwork() *ClusterNetwork {
return &ClusterNetwork{
PluginSettings: map[string]string{
OvnNBDB: "unix:" + DefaultOvnNBDB,
},
}
}

func createClusterNetworkWithEndpoints(endPoints []corev1.Endpoints) *ClusterNetwork {
var northboundDBIPs []string

for index := range endPoints {
for _, subset := range endPoints[index].Subsets {
for _, port := range subset.Ports {
if strings.Contains(port.Name, "north") {
northboundDBIPs = append(northboundDBIPs, fmt.Sprintf("IC:%s:%s:%s:%d",
endPoints[index].Name, port.Protocol, subset.Addresses[0].IP, OvnNBDBDefaultPort))
}
}
}
}

return &ClusterNetwork{
PluginSettings: map[string]string{OvnNBDB: strings.Join(northboundDBIPs, ",")},
}
}

func updateClusterNetworkFromConfigMap(ctx context.Context, client controllerClient.Client, ovnPodNamespace string,
clusterNetwork *ClusterNetwork,
) {
Expand All @@ -150,30 +56,3 @@ func updateClusterNetworkFromConfigMap(ctx context.Context, client controllerCli
}
}
}

func findEndpoint(ctx context.Context, client controllerClient.Client, endpointNameSpace string) (*corev1.EndpointsList, error) {
endpointsList := &corev1.EndpointsList{}
listOptions := &controllerClient.ListOptions{
Namespace: endpointNameSpace,
}

err := client.List(ctx, endpointsList, listOptions)

return endpointsList, errors.WithMessagef(err, "error listing endpoints in namespace %q", endpointNameSpace)
}

func findProtocol(pod *corev1.Pod) string {
dbConnectionProtocol := "tcp"

for i := range pod.Spec.Containers {
for _, envVar := range pod.Spec.Containers[i].Env {
if envVar.Name == "OVN_SSL_ENABLE" {
if !strings.EqualFold(envVar.Value, "NO") {
dbConnectionProtocol = "ssl"
}
}
}
}

return dbConnectionProtocol
}
17 changes: 1 addition & 16 deletions pkg/discovery/network/ovnkubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package network_test

import (
"context"
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -34,18 +33,7 @@ import (
const ovnKubeNamespace = "ovn-kubernetes"

var _ = Describe("OvnKubernetes Network", func() {
const ovnKubeSvcTest = "ovnkube-db"

When("ovn-kubernetes database is found, but no database service", func() {
It("Should return error", func() {
clusterNet, err := testOvnKubernetesDiscoveryWith(
fakePodWithNamespace(ovnKubeNamespace, ovnKubeSvcTest, ovnKubeSvcTest, []string{}, []v1.EnvVar{}),
)

Expect(err).To(HaveOccurred())
Expect(clusterNet).To(BeNil())
})
})
const ovnKubeSvcTest = "ovnkube-node"

When("ovn-kubernetes database and service found, no configmap", func() {
It("Should return cluster network with default CIDRs", func() {
Expand All @@ -57,9 +45,6 @@ var _ = Describe("OvnKubernetes Network", func() {
Expect(err).NotTo(HaveOccurred())
Expect(clusterNet).NotTo(BeNil())
Expect(clusterNet.NetworkPlugin).To(Equal(cni.OVNKubernetes))
connectionStr := fmt.Sprintf("tcp:%s.%s", ovnKubeSvcTest, ovnKubeNamespace)
Expect(clusterNet.PluginSettings["OVN_NBDB"]).To(Equal(connectionStr + ":6641"))
Expect(clusterNet.PluginSettings["OVN_SBDB"]).To(Equal(connectionStr + ":6642"))
Expect(clusterNet.PodCIDRs).To(BeEmpty())
Expect(clusterNet.ServiceCIDRs).To(HaveLen(1))
})
Expand Down
1 change: 0 additions & 1 deletion pkg/embeddedyamls/yamls.go
Original file line number Diff line number Diff line change
Expand Up @@ -2611,7 +2611,6 @@ rules:
- pods
- services
- nodes
- endpoints
verbs:
- get
- list
Expand Down

0 comments on commit 783a580

Please sign in to comment.