Skip to content

Commit

Permalink
Annotate a service with the pool used to provide its IP
Browse files Browse the repository at this point in the history
when we have multiple configurations tied to the ipaddresspool, it'd be useful to see which pool was used to assign the ip to a given service. You can see it by looking service's annotation "metallb.universe.tf/ip-allocated-from-pool".

Signed-off-by: cyclinder <qifeng.guo@daocloud.io>
  • Loading branch information
cyclinder committed Nov 14, 2022
1 parent 626fce8 commit 1ea2ebc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
18 changes: 11 additions & 7 deletions controller/main.go
Expand Up @@ -87,14 +87,18 @@ func (c *controller) SetBalancer(l log.Logger, name string, svcRo *v1.Service, _
}

if !reflect.DeepEqual(svcRo.Status, svc.Status) {
var st v1.ServiceStatus
st, svc = svc.Status, svcRo.DeepCopy()
svc.Status = st
if err := c.client.UpdateStatus(svc); err != nil {
level.Error(l).Log("op", "updateServiceStatus", "error", err, "msg", "failed to update service status")
return controllers.SyncStateError
}
updateSvcStatus(svc, svcRo)
}

if !reflect.DeepEqual(svcRo.Annotations, svc.Annotations) {
updateSvcAnnotations(svc, svcRo)
}

if err := c.client.UpdateStatus(svc); err != nil {
level.Error(l).Log("op", "updateServiceStatus", "error", err, "msg", "failed to update service")
return controllers.SyncStateError
}

level.Info(l).Log("event", "serviceUpdated", "msg", "updated service object")
return successRes
}
Expand Down
25 changes: 23 additions & 2 deletions controller/service.go
Expand Up @@ -30,8 +30,9 @@ import (
)

const (
annotationAddressPool = "metallb.universe.tf/address-pool"
annotationLoadBalancerIPs = "metallb.universe.tf/loadBalancerIPs"
annotationAddressPool = "metallb.universe.tf/address-pool"
annotationLoadBalancerIPs = "metallb.universe.tf/loadBalancerIPs"
annotationIPAllocateFromPool = "metallb.universe.tf/ip-allocated-from-pool"
)

func (c *controller) convergeBalancer(l log.Logger, key string, svc *v1.Service) bool {
Expand Down Expand Up @@ -161,13 +162,18 @@ func (c *controller) convergeBalancer(l log.Logger, key string, svc *v1.Service)
lbIngressIPs = append(lbIngressIPs, v1.LoadBalancerIngress{IP: lbIP.String()})
}
svc.Status.LoadBalancer.Ingress = lbIngressIPs
if svc.Annotations == nil {
svc.Annotations = make(map[string]string)
}
svc.Annotations[annotationIPAllocateFromPool] = pool
return true
}

// clearServiceState clears all fields that are actively managed by
// this controller.
func (c *controller) clearServiceState(key string, svc *v1.Service) {
c.ips.Unassign(key)
delete(svc.Annotations, annotationIPAllocateFromPool)
svc.Status.LoadBalancer = v1.LoadBalancerStatus{}
}

Expand Down Expand Up @@ -260,3 +266,18 @@ func isEqualIPs(ipsA, ipsB []net.IP) bool {
})
return reflect.DeepEqual(ipsA, ipsB)
}

// updateSvcStatus updating svc's status and svcRo's status
func updateSvcStatus(svc, svcRo *v1.Service) {
var st v1.ServiceStatus
st, svc = svc.Status, svcRo.DeepCopy()
// we need be update svcRo's status
svc.Status, svcRo.Status = st, st
}

// updateSvcAnnotations updating svc's annotations
func updateSvcAnnotations(svc, svcRo *v1.Service) {
var annotations map[string]string
annotations, svc = svc.Annotations, svcRo.DeepCopy()
svc.Annotations = annotations
}
4 changes: 4 additions & 0 deletions e2etest/l2tests/l2.go
Expand Up @@ -676,10 +676,14 @@ var _ = ginkgo.Describe("L2", func() {
err = config.ValidateIPInRange([]metallbv1beta1.IPAddressPool{pool}, ingressIP)
framework.ExpectNoError(err)

ginkgo.By("validate annotating a service with the pool used to provide its IP")
framework.ExpectEqual(svc.Annotations["metallb.universe.tf/ip-allocated-from-pool"], pool.Name)

services = append(services, svc)
servicesIngressIP = append(servicesIngressIP, ingressIP)

for j := 0; j <= i; j++ {

ginkgo.By(fmt.Sprintf("validate service %d IP didn't change", j+1))
ip := e2eservice.GetIngressPoint(&services[j].Status.LoadBalancer.Ingress[0])
framework.ExpectEqual(ip, servicesIngressIP[j])
Expand Down

0 comments on commit 1ea2ebc

Please sign in to comment.