consul/proto-public/pbcatalog/v2beta1/service_addon_test.go

121 lines
2.1 KiB
Go
Raw Normal View History

package catalogv2beta1
import (
"testing"
"github.com/stretchr/testify/require"
)
mesh: sidecar proxy controller improvements (#19083) This change builds on #19043 and #19067 and updates the sidecar controller to use those computed resources. This achieves several benefits: * The cache is now simplified which helps us solve for previous bugs (such as multiple Upstreams/Destinations targeting the same service would overwrite each other) * We no longer need proxy config cache * We no longer need to do merging of proxy configs as part of the controller logic * Controller watches are simplified because we no longer need to have complex mapping using cache and can instead use the simple ReplaceType mapper. It also makes several other improvements/refactors: * Unifies all caches into one. This is because originally the caches were more independent, however, now that they need to interact with each other it made sense to unify them where sidecar proxy controller uses one cache with 3 bimappers * Unifies cache and mappers. Mapper already needed all caches anyway and so it made sense to make the cache do the mapping also now that the cache is unified. * Gets rid of service endpoints watches. This was needed to get updates in a case when service's identities have changed and we need to update proxy state template's spiffe IDs for those destinations. This will however generate a lot of reconcile requests for this controller as service endpoints objects can change a lot because they contain workload's health status. This is solved by adding a status to the service object tracking "bound identities" and have service endpoints controller update it. Having service's status updated allows us to get updates in the sidecar proxy controller because it's already watching service objects * Add a watch for workloads. We need it so that we get updates if workload's ports change. This also ensures that we update cached identities in case workload's identity changes.
2023-10-12 19:20:13 +00:00
func TestServiceIsMeshEnabled(t *testing.T) {
cases := map[string]struct {
service *Service
exp bool
}{
"nil": {service: nil, exp: false},
"no ports": {
service: &Service{},
exp: false,
},
"no mesh ports": {
service: &Service{
Ports: []*ServicePort{
{
TargetPort: "foo",
Protocol: Protocol_PROTOCOL_HTTP,
},
{
TargetPort: "bar",
Protocol: Protocol_PROTOCOL_TCP,
},
},
},
exp: false,
},
"with mesh ports": {
service: &Service{
Ports: []*ServicePort{
{
TargetPort: "foo",
Protocol: Protocol_PROTOCOL_HTTP,
},
{
TargetPort: "bar",
Protocol: Protocol_PROTOCOL_TCP,
},
{
TargetPort: "baz",
Protocol: Protocol_PROTOCOL_MESH,
},
},
},
exp: true,
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
require.Equal(t, c.exp, c.service.IsMeshEnabled())
})
}
}
func TestFindServicePort(t *testing.T) {
cases := map[string]struct {
service *Service
port string
exp *ServicePort
}{
"nil": {service: nil, port: "foo", exp: nil},
"no ports": {
service: &Service{},
port: "foo",
exp: nil,
},
"non-existing port": {
service: &Service{
Ports: []*ServicePort{
{
TargetPort: "foo",
Protocol: Protocol_PROTOCOL_HTTP,
},
{
TargetPort: "bar",
Protocol: Protocol_PROTOCOL_TCP,
},
},
},
port: "not-found",
exp: nil,
},
"existing port": {
service: &Service{
Ports: []*ServicePort{
{
TargetPort: "foo",
Protocol: Protocol_PROTOCOL_HTTP,
},
{
TargetPort: "bar",
Protocol: Protocol_PROTOCOL_TCP,
},
{
TargetPort: "baz",
Protocol: Protocol_PROTOCOL_MESH,
},
},
},
port: "bar",
exp: &ServicePort{
TargetPort: "bar",
Protocol: Protocol_PROTOCOL_TCP,
},
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
require.Equal(t, c.exp, c.service.FindServicePort(c.port))
})
}
}