Skip to content

Commit

Permalink
Expose bind9 endpoints
Browse files Browse the repository at this point in the history
This patch creates series of services and map them to the statefulset
members using the podname.

Each service has an IP from a metallb pool.
  • Loading branch information
omersch381 committed Nov 22, 2024
1 parent c06a12f commit bcd079d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 26 deletions.
31 changes: 31 additions & 0 deletions controllers/designatebackendbind9_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/openstack-k8s-operators/lib-common/modules/common/labels"
nad "github.com/openstack-k8s-operators/lib-common/modules/common/networkattachment"
"github.com/openstack-k8s-operators/lib-common/modules/common/secret"
"github.com/openstack-k8s-operators/lib-common/modules/common/service"
"github.com/openstack-k8s-operators/lib-common/modules/common/statefulset"
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
mariadbv1 "github.com/openstack-k8s-operators/mariadb-operator/api/v1beta1"
Expand Down Expand Up @@ -524,6 +525,36 @@ func (r *DesignateBackendbind9Reconciler) reconcileNormal(ctx context.Context, i
}
// create StatefulSet - end

// create Bind Services - start
services := designatebackendbind9.CreateBindServices(instance, *instance.Spec.Replicas)
for _, svc := range services {
svc, err := service.NewService(svc, 5, nil)

if err != nil {
return ctrl.Result{}, err
}

ctrlResult, err = svc.CreateOrPatch(ctx, helper)
if err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.CreateServiceReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.CreateServiceReadyErrorMessage,
err.Error()))

return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.CreateServiceReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
condition.CreateServiceReadyRunningMessage))
return ctrlResult, nil
}
}
// create Bind Services - end

// We reached the end of the Reconcile, update the Ready condition based on
// the sub conditions
if instance.Status.Conditions.AllSubConditionIsTrue() {
Expand Down
78 changes: 52 additions & 26 deletions pkg/designatebackendbind9/service.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,66 @@
package designatebackendbind9

import (
"fmt"
designatev1beta1 "github.com/openstack-k8s-operators/designate-operator/api/v1beta1"
labels "github.com/openstack-k8s-operators/lib-common/modules/common/labels"
service "github.com/openstack-k8s-operators/lib-common/modules/common/service"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Service(m *designatev1beta1.DesignateBackendbind9) *corev1.Service {
labels := labels.GetLabels(m, "designatebackendbind9", map[string]string{
const (
Bind9AppName = "designatebackendbind9"
)

// CreateBindServices creates a service for each bind9 pod
func CreateBindServices(m *designatev1beta1.DesignateBackendbind9, replicas int32) []*corev1.Service {
services := make([]*corev1.Service, replicas)

for i := int32(0); i < replicas; i++ {
podName := fmt.Sprintf("%s-%d", m.Name, i)
services[i] = createSingleBindService(m, podName)
}

return services
}

func createSingleBindService(m *designatev1beta1.DesignateBackendbind9, podName string) *corev1.Service {
labels := labels.GetLabels(m, Bind9AppName, map[string]string{
"owner": "designate",
"cr": m.GetName(),
"app": "designatebackendbind9",
"app": Bind9AppName,
})
servicePorts := make([]corev1.ServicePort, 2)
servicePorts[0] = corev1.ServicePort{
Name: "named",
Port: 53,
Protocol: corev1.ProtocolUDP,
}
servicePorts[1] = corev1.ServicePort{
Name: "named",
Port: 53,
Protocol: corev1.ProtocolTCP,
}
details := &service.GenericServiceDetails{
Name: m.GetName(),
Namespace: m.GetNamespace(),
Labels: labels,
Selector: map[string]string{
"app": "bind",

return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Namespace: m.Namespace,
Labels: labels,
Annotations: map[string]string{
"metallb.universe.tf/address-pool": "internalapi",
},
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(m, designatev1beta1.GroupVersion.WithKind(Bind9AppName)),
},
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeLoadBalancer,
Selector: map[string]string{
"app": "bind",
"statefulset.kubernetes.io/pod-name": podName,
},
Ports: []corev1.ServicePort{
{
Name: "named-udp",
Port: 53,
Protocol: corev1.ProtocolUDP,
},
{
Name: "named-tcp",
Port: 53,
Protocol: corev1.ProtocolTCP,
},
},
},
Ports: servicePorts,
ClusterIP: "None",
}

svc := service.GenericService(details)
return svc
}

0 comments on commit bcd079d

Please sign in to comment.