Skip to content

Commit

Permalink
Merge pull request #217 from averdagu/fix/delete-dbcluster-svc
Browse files Browse the repository at this point in the history
Add type label into headless/normal services
  • Loading branch information
openshift-merge-bot[bot] committed Mar 5, 2024
2 parents 67c05d8 + 3c21989 commit c8756a9
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 6 deletions.
5 changes: 5 additions & 0 deletions api/v1beta1/ovndbcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ const (
// SBDBType - Southbound database type
SBDBType = "SB"

// ServiceHeadlessType - Constant to identify Headless services
ServiceHeadlessType = "headless"
// ServiceClusterType - Constant to identify Cluster services
ServiceClusterType = "cluster"

// DNSSuffix : hardcoded value on how DNSCore domain is configured
DNSSuffix = "cluster.local"
// TODO: retrieve it from environment
Expand Down
11 changes: 7 additions & 4 deletions controllers/ovndbcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,10 @@ func (r *OVNDBClusterReconciler) reconcileServices(
//
// Ensure the ovndbcluster headless service Exists
//
headlessServiceLabels := util.MergeMaps(serviceLabels, map[string]string{"type": v1beta1.ServiceHeadlessType})

headlesssvc, err := service.NewService(
ovndbcluster.HeadlessService(serviceName, instance, serviceLabels),
ovndbcluster.HeadlessService(serviceName, instance, headlessServiceLabels, serviceLabels),
time.Duration(5)*time.Second,
nil,
)
Expand All @@ -559,12 +560,13 @@ func (r *OVNDBClusterReconciler) reconcileServices(
//
// Create the ovndbcluster pod service if none exists
//
ovndbServiceLabels := map[string]string{
ovndbSelectorLabels := map[string]string{
common.AppSelector: serviceName,
"statefulset.kubernetes.io/pod-name": ovnPod.Name,
}
ovndbServiceLabels := util.MergeMaps(ovndbSelectorLabels, map[string]string{"type": v1beta1.ServiceClusterType})
svc, err := service.NewService(
ovndbcluster.Service(ovnPod.Name, instance, ovndbServiceLabels),
ovndbcluster.Service(ovnPod.Name, instance, ovndbServiceLabels, ovndbSelectorLabels),
time.Duration(5)*time.Second,
nil,
)
Expand All @@ -581,11 +583,12 @@ func (r *OVNDBClusterReconciler) reconcileServices(
}

// Delete any extra services left after scale down
clusterServiceLabels := util.MergeMaps(serviceLabels, map[string]string{"type": v1beta1.ServiceClusterType})
svcList, err := service.GetServicesListWithLabel(
ctx,
helper,
helper.GetBeforeObject().GetNamespace(),
serviceLabels,
clusterServiceLabels,
)
if err == nil && len(svcList.Items) > int(*(instance.Spec.Replicas)) {
for i := len(svcList.Items) - 1; i >= int(*(instance.Spec.Replicas)); i-- {
Expand Down
6 changes: 4 additions & 2 deletions pkg/ovndbcluster/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func Service(
serviceName string,
instance *ovnv1.OVNDBCluster,
serviceLabels map[string]string,
selectorLabels map[string]string,
) *corev1.Service {
dbPortName := "north"
raftPortName := "north-raft"
Expand All @@ -30,7 +31,7 @@ func Service(
Labels: serviceLabels,
},
Spec: corev1.ServiceSpec{
Selector: serviceLabels,
Selector: selectorLabels,
Ports: []corev1.ServicePort{
{
Name: dbPortName,
Expand All @@ -52,6 +53,7 @@ func HeadlessService(
serviceName string,
instance *ovnv1.OVNDBCluster,
serviceLabels map[string]string,
selectorLabels map[string]string,
) *corev1.Service {
raftPortName := "north-raft"
var raftPort int32 = 6643
Expand All @@ -66,7 +68,7 @@ func HeadlessService(
Labels: serviceLabels,
},
Spec: corev1.ServiceSpec{
Selector: serviceLabels,
Selector: selectorLabels,
Ports: []corev1.ServicePort{
{
Name: raftPortName,
Expand Down
22 changes: 22 additions & 0 deletions tests/functional/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,25 @@ func GetPod(name types.NamespacedName) *corev1.Pod {
func UpdatePod(pod *corev1.Pod) {
k8sClient.Update(ctx, pod)
}

func GetServicesListWithLabel(namespace string, labelSelectorMap ...map[string]string) *corev1.ServiceList {
serviceList := &corev1.ServiceList{}
serviceListOpts := client.ListOptions{
Namespace: namespace,
}
if len(labelSelectorMap) > 0 {
for i := 0; i < len(labelSelectorMap); i++ {
for key, value := range labelSelectorMap[i] {
ml := client.MatchingLabels{
key: value,
}
ml.ApplyToList(&serviceListOpts)
}
}
}
Eventually(func(g Gomega) {
g.Expect(k8sClient.List(ctx, serviceList, &serviceListOpts)).Should(Succeed())
}).Should(Succeed())

return serviceList
}
34 changes: 34 additions & 0 deletions tests/functional/ovndbcluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,40 @@ var _ = Describe("OVNDBCluster controller", func() {
DeferCleanup(th.DeleteInstance, instance)
})

It("should create services", func() {
internalAPINADName := types.NamespacedName{Namespace: namespace, Name: "internalapi"}
nad := th.CreateNetworkAttachmentDefinition(internalAPINADName)
DeferCleanup(th.DeleteInstance, nad)

statefulSetName := types.NamespacedName{
Namespace: namespace,
Name: "ovsdbserver-sb",
}
th.SimulateStatefulSetReplicaReadyWithPods(
statefulSetName,
map[string][]string{namespace + "/internalapi": {"10.0.0.1"}},
)
// Cluster is created with 1 replica, serviceListWithoutTypeLabel should be 2:
// - ovsdbserver-sb (headless type)
// - ovsdbserver-sb-0 (cluster type)
Eventually(func(g Gomega) {
serviceListWithoutTypeLabel := GetServicesListWithLabel(namespace)
g.Expect(len(serviceListWithoutTypeLabel.Items)).To(Equal(2))
}).Should(Succeed())

Eventually(func(g Gomega) {
serviceListWithClusterType := GetServicesListWithLabel(namespace, map[string]string{"type": "cluster"})
g.Expect(len(serviceListWithClusterType.Items)).To(Equal(1))
g.Expect(serviceListWithClusterType.Items[0].Name).To(Equal("ovsdbserver-sb-0"))
}).Should(Succeed())

Eventually(func(g Gomega) {
serviceListWithHeadlessType := GetServicesListWithLabel(namespace, map[string]string{"type": "headless"})
g.Expect(len(serviceListWithHeadlessType.Items)).To(Equal(1))
g.Expect(serviceListWithHeadlessType.Items[0].Name).To(Equal("ovsdbserver-sb"))
}).Should(Succeed())
})

It("reports that the definition is missing", func() {
th.ExpectConditionWithDetails(
OVNDBClusterName,
Expand Down

0 comments on commit c8756a9

Please sign in to comment.