Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 8a147fc

Browse files
committed
Fix test flakiness by only reacting to MeshConfigs and MRCs in target namespaces
Signed-off-by: Keith Mattix II <[email protected]>
1 parent 3e86f96 commit 8a147fc

File tree

9 files changed

+33
-15
lines changed

9 files changed

+33
-15
lines changed

pkg/certificate/fake_manager.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (c *fakeMRCClient) List() ([]*v1alpha2.MeshRootCertificate, error) {
3434
}}, nil
3535
}
3636

37-
func (c *fakeMRCClient) Watch(ctx context.Context) (<-chan MRCEvent, error) {
37+
func (c *fakeMRCClient) Watch(ctx context.Context, namespace string) (<-chan MRCEvent, error) {
3838
ch := make(chan MRCEvent)
3939
go func() {
4040
ch <- MRCEvent{
@@ -100,6 +100,7 @@ func FakeCertManager() (*Manager, error) {
100100
getCertValidityDuration,
101101
nil,
102102
1*time.Hour,
103+
"",
103104
)
104105
if err != nil {
105106
return nil, fmt.Errorf("error creating fakeCertManager, err: %w", err)

pkg/certificate/manager.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import (
1717
)
1818

1919
// NewManager creates a new CertificateManager with the passed MRCClient and options
20-
func NewManager(ctx context.Context, mrcClient MRCClient, getServiceCertValidityPeriod func() time.Duration, getIngressCertValidityDuration func() time.Duration, msgBroker *messaging.Broker, checkInterval time.Duration) (*Manager, error) {
20+
func NewManager(ctx context.Context, mrcClient MRCClient, getServiceCertValidityPeriod func() time.Duration, getIngressCertValidityDuration func() time.Duration, msgBroker *messaging.Broker, checkInterval time.Duration, ns string) (*Manager, error) {
2121
m := &Manager{
2222
serviceCertValidityDuration: getServiceCertValidityPeriod,
2323
ingressCertValidityDuration: getIngressCertValidityDuration,
2424
msgBroker: msgBroker,
2525
}
2626

27-
err := m.start(ctx, mrcClient)
27+
err := m.start(ctx, mrcClient, ns)
2828
if err != nil {
2929
return nil, err
3030
}
@@ -49,12 +49,12 @@ func (m *Manager) startRotationTicker(ctx context.Context, checkInterval time.Du
4949
}()
5050
}
5151

52-
func (m *Manager) start(ctx context.Context, mrcClient MRCClient) error {
52+
func (m *Manager) start(ctx context.Context, mrcClient MRCClient, ns string) error {
5353
// start a watch and we wait until the manager is initialized so that
5454
// the caller gets a manager that's ready to be used
5555
var once sync.Once
5656
var wg sync.WaitGroup
57-
mrcEvents, err := mrcClient.Watch(ctx)
57+
mrcEvents, err := mrcClient.Watch(ctx, ns)
5858
if err != nil {
5959
return err
6060
}

pkg/certificate/manager_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func TestRotor(t *testing.T) {
8383
stop := make(chan struct{})
8484
defer close(stop)
8585
msgBroker := messaging.NewBroker(stop)
86-
certManager, err := NewManager(context.Background(), &fakeMRCClient{}, getServiceCertValidityPeriod, getIngressGatewayCertValidityPeriod, msgBroker, 5*time.Second)
86+
certManager, err := NewManager(context.Background(), &fakeMRCClient{}, getServiceCertValidityPeriod, getIngressGatewayCertValidityPeriod, msgBroker, 5*time.Second, "")
8787
require.NoError(err)
8888

8989
certA, err := certManager.IssueCertificate(cnPrefix, Service)

pkg/certificate/providers/compat.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func (c *MRCCompatClient) List() ([]*v1alpha2.MeshRootCertificate, error) {
1515
}
1616

1717
// Watch is a basic Watch implementation for the MRC attached to the compat client
18-
func (c *MRCCompatClient) Watch(ctx context.Context) (<-chan certificate.MRCEvent, error) {
18+
func (c *MRCCompatClient) Watch(ctx context.Context, namespace string) (<-chan certificate.MRCEvent, error) {
1919
ch := make(chan certificate.MRCEvent)
2020
go func() {
2121
ch <- certificate.MRCEvent{

pkg/certificate/providers/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func NewCertificateManager(ctx context.Context, kubeClient kubernetes.Interface,
100100
mrcClient = c
101101
}
102102

103-
return certificate.NewManager(ctx, mrcClient, cfg.GetServiceCertValidityPeriod, cfg.GetIngressGatewayCertValidityPeriod, msgBroker, checkInterval)
103+
return certificate.NewManager(ctx, mrcClient, cfg.GetServiceCertValidityPeriod, cfg.GetIngressGatewayCertValidityPeriod, msgBroker, checkInterval, providerNamespace)
104104
}
105105

106106
// GetCertIssuerForMRC returns a certificate.Issuer generated from the provided MRC.

pkg/certificate/providers/mrc.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/openservicemesh/osm/pkg/apis/config/v1alpha2"
99
"github.com/openservicemesh/osm/pkg/certificate"
1010
"github.com/openservicemesh/osm/pkg/k8s/informers"
11+
"github.com/rs/zerolog/log"
1112
)
1213

1314
// MRCComposer is a composer object that allows consumers
@@ -41,11 +42,16 @@ func (m *MRCComposer) List() ([]*v1alpha2.MeshRootCertificate, error) {
4142
// from the informerCollection's MRC store. Channels returned from multiple invocations of
4243
// Watch() are unique and have no coordination with each other. Events are guaranteed
4344
// to be ordered for any particular resources, but NOT across different resources.
44-
func (m *MRCComposer) Watch(ctx context.Context) (<-chan certificate.MRCEvent, error) {
45+
func (m *MRCComposer) Watch(ctx context.Context, namespace string) (<-chan certificate.MRCEvent, error) {
4546
eventChan := make(chan certificate.MRCEvent)
4647
m.informerCollection.AddEventHandler(informers.InformerKeyMeshRootCertificate, cache.ResourceEventHandlerFuncs{
4748
AddFunc: func(obj interface{}) {
49+
log.Debug().Msg("received MRC add event")
4850
mrc := obj.(*v1alpha2.MeshRootCertificate)
51+
// If there's a specific namespace passed in don't send the MRCEvent unless the MRC's namespace matches
52+
if namespace != "" && mrc.GetNamespace() != namespace {
53+
return
54+
}
4955
eventChan <- certificate.MRCEvent{
5056
Type: certificate.MRCEventAdded,
5157
MRC: mrc,
@@ -54,7 +60,11 @@ func (m *MRCComposer) Watch(ctx context.Context) (<-chan certificate.MRCEvent, e
5460
// We don't really care about the previous version
5561
// since the "state machine" of the MRC is well defined
5662
UpdateFunc: func(_, newObj interface{}) {
63+
log.Debug().Msg("received MRC update event")
5764
mrc := newObj.(*v1alpha2.MeshRootCertificate)
65+
if namespace != "" && mrc.GetNamespace() != namespace {
66+
return
67+
}
5868
eventChan <- certificate.MRCEvent{
5969
Type: certificate.MRCEventUpdated,
6070
MRC: mrc,

pkg/certificate/providers/tresor/fake/fake.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (c *fakeMRCClient) List() ([]*v1alpha2.MeshRootCertificate, error) {
3939
return []*v1alpha2.MeshRootCertificate{{Spec: v1alpha2.MeshRootCertificateSpec{TrustDomain: "fake.example.com"}}}, nil
4040
}
4141

42-
func (c *fakeMRCClient) Watch(ctx context.Context) (<-chan certificate.MRCEvent, error) {
42+
func (c *fakeMRCClient) Watch(ctx context.Context, namespace string) (<-chan certificate.MRCEvent, error) {
4343
ch := make(chan certificate.MRCEvent)
4444
go func() {
4545
ch <- certificate.MRCEvent{
@@ -83,7 +83,7 @@ func NewFake(msgBroker *messaging.Broker, checkInterval time.Duration) *certific
8383

8484
// NewFakeWithValidityDuration constructs a fake certificate manager with specified cert validity duration
8585
func NewFakeWithValidityDuration(getCertValidityDuration func() time.Duration, msgBroker *messaging.Broker, checkInterval time.Duration) *certificate.Manager {
86-
tresorCertManager, err := certificate.NewManager(context.Background(), &fakeMRCClient{}, getCertValidityDuration, getCertValidityDuration, msgBroker, checkInterval)
86+
tresorCertManager, err := certificate.NewManager(context.Background(), &fakeMRCClient{}, getCertValidityDuration, getCertValidityDuration, msgBroker, checkInterval, "")
8787
if err != nil {
8888
log.Error().Err(err).Msg("error encountered creating fake cert manager")
8989
return nil

pkg/certificate/types.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ var (
147147
// MRCEventBroker describes any type that allows the caller to Watch() MRCEvents
148148
type MRCEventBroker interface {
149149
// Watch allows the caller to subscribe to events surrounding
150-
// MRCs that belong to this particular mesh. Watch returns a channel
150+
// MRCs within a specific namespace. Watch returns a channel
151151
// that emits events, and an error if the subscription goes awry.
152-
Watch(context.Context) (<-chan MRCEvent, error)
152+
Watch(context.Context, string) (<-chan MRCEvent, error)
153153
}

pkg/configurator/client.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"k8s.io/client-go/tools/cache"
88

9+
"github.com/openservicemesh/osm/pkg/apis/config/v1alpha2"
910
configv1alpha2 "github.com/openservicemesh/osm/pkg/apis/config/v1alpha2"
1011
"github.com/openservicemesh/osm/pkg/k8s/informers"
1112

@@ -31,15 +32,21 @@ func NewConfigurator(informerCollection *informers.InformerCollection, osmNamesp
3132
Delete: announcements.MeshConfigDeleted,
3233
}
3334

34-
informerCollection.AddEventHandler(informers.InformerKeyMeshConfig, k8s.GetEventHandlerFuncs(nil, meshConfigEventTypes, msgBroker))
35+
informerCollection.AddEventHandler(informers.InformerKeyMeshConfig, k8s.GetEventHandlerFuncs(func(obj interface{}) bool {
36+
mc := obj.(*v1alpha2.MeshConfig)
37+
return mc.GetName() == meshConfigName
38+
}, meshConfigEventTypes, msgBroker))
3539
informerCollection.AddEventHandler(informers.InformerKeyMeshConfig, c.metricsHandler())
3640

3741
meshRootCertificateEventTypes := k8s.EventTypes{
3842
Add: announcements.MeshRootCertificateAdded,
3943
Update: announcements.MeshRootCertificateUpdated,
4044
Delete: announcements.MeshRootCertificateDeleted,
4145
}
42-
informerCollection.AddEventHandler(informers.InformerKeyMeshRootCertificate, k8s.GetEventHandlerFuncs(nil, meshRootCertificateEventTypes, msgBroker))
46+
informerCollection.AddEventHandler(informers.InformerKeyMeshRootCertificate, k8s.GetEventHandlerFuncs(func(obj interface{}) bool {
47+
mrc := obj.(*v1alpha2.MeshRootCertificate)
48+
return mrc.GetNamespace() == osmNamespace
49+
}, meshRootCertificateEventTypes, msgBroker))
4350

4451
return c
4552
}

0 commit comments

Comments
 (0)