Skip to content

Commit

Permalink
service-cache: add support for service mutators
Browse files Browse the repository at this point in the history
Extend the service cache to specify additional mutators to be executed
when a service is received from k8s and converted to the internal
representation.

Signed-off-by: Marco Iorio <marco.iorio@isovalent.com>
  • Loading branch information
giorio94 authored and christarazi committed Apr 3, 2023
1 parent de00caa commit a17f0e6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/k8s/service_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ type ServiceCache struct {
nodeAddressing types.NodeAddressing

selfNodeZoneLabel string

ServiceMutators []func(svc *slim_corev1.Service, svcInfo *Service)
}

// NewServiceCache returns a new ServiceCache
Expand Down Expand Up @@ -192,6 +194,10 @@ func (s *ServiceCache) UpdateService(k8sSvc *slim_corev1.Service, swg *lock.Stop
return svcID
}

for _, mutator := range s.ServiceMutators {
mutator(k8sSvc, newService)
}

s.mutex.Lock()
defer s.mutex.Unlock()

Expand Down
23 changes: 23 additions & 0 deletions pkg/k8s/service_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,29 @@ func (s *K8sSuite) TestCacheActionString(c *check.C) {
c.Assert(DeleteService.String(), check.Equals, "service-deleted")
}

func (s *K8sSuite) TestServiceMutators(c *check.C) {
var m1, m2 int

svcCache := NewServiceCache(fakeDatapath.NewNodeAddressing())
svcCache.ServiceMutators = append(svcCache.ServiceMutators,
func(svc *slim_corev1.Service, svcInfo *Service) { m1++ },
func(svc *slim_corev1.Service, svcInfo *Service) { m2++ },
)
swg := lock.NewStoppableWaitGroup()
svcCache.UpdateService(&slim_corev1.Service{
ObjectMeta: slim_metav1.ObjectMeta{Name: "foo", Namespace: "bar"},
Spec: slim_corev1.ServiceSpec{
ClusterIP: "127.0.0.1",
Selector: map[string]string{"foo": "bar"},
Type: slim_corev1.ServiceTypeClusterIP,
},
}, swg)

// Assert that the service mutators configured have been executed.
c.Assert(m1, check.Equals, 1)
c.Assert(m2, check.Equals, 1)
}

func (s *K8sSuite) TestExternalServiceMerging(c *check.C) {
svcCache := NewServiceCache(fakeDatapath.NewNodeAddressing())

Expand Down

0 comments on commit a17f0e6

Please sign in to comment.