Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

annotate a service with the pool used to provide its IP #1637

Merged
merged 1 commit into from Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions controller/main.go
Expand Up @@ -86,16 +86,24 @@ func (c *controller) SetBalancer(l log.Logger, name string, svcRo *v1.Service, _
return successRes
}

toWrite := svcRo.DeepCopy()
if !reflect.DeepEqual(svcRo.Status, svc.Status) {
var st v1.ServiceStatus
st, svc = svc.Status, svcRo.DeepCopy()
svc.Status = st
toWrite.Status = svc.Status
}

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

if !reflect.DeepEqual(toWrite, svcRo) {
if err := c.client.UpdateStatus(svc); err != nil {
level.Error(l).Log("op", "updateServiceStatus", "error", err, "msg", "failed to update service status")
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")
}
level.Info(l).Log("event", "serviceUpdated", "msg", "updated service object")

level.Info(l).Log("event", "serviceUpdated", "msg", "service is not updated")
return successRes
}

Expand Down
10 changes: 8 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
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