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

Improve CertAuthorityWatcher #10403

Merged
merged 4 commits into from
May 17, 2022
Merged

Improve CertAuthorityWatcher #10403

merged 4 commits into from
May 17, 2022

Conversation

rosstimothy
Copy link
Contributor

@rosstimothy rosstimothy commented Feb 16, 2022

CertAuthorityWatcher and its usage are refactored to allow for
all the following:

  • eliminate retransmission of the same CAs
  • reduce memory usage by having one local watcher per proxy
  • adds the ability to filter only the CAs that are desired
  • reduce the time required to send the first CAs

watchCertAuthorities now compares all CAs it receives from the
watcher with the previous CA of the same type and only sends to
the remote site if they are not identical. This is to reduce
unnecessary network traffic which can be problematic for a
root cluster with a larger number of leafs.

The CertAuthorityWatcher is refactored to leverage a fanout
to emit events to any number of watchers, each subscription
can be for a subset of the configured CA types. The proxy
now has only one CertAuthorityWatcher that is passed around
similarly to the LockWatcher. This reduces the memory usage
for proxies, which prior to this has one local CAWatcher per
remote site.

updateCertAuthorities no longer waits on the utils.Retry it
is provided with before starting to watch CAs. By doing this
the proxy no longer has to wait ~8 minutes before it even
starts to watch CAs.

@espadolini espadolini self-requested a review February 16, 2022 17:57
@rosstimothy rosstimothy force-pushed the tross/fix_ca_watcher branch 3 times, most recently from ab57537 to 5b12c90 Compare May 11, 2022 17:48
@rosstimothy rosstimothy requested a review from jakule May 11, 2022 19:23
@rosstimothy rosstimothy marked this pull request as ready for review May 11, 2022 19:23
lib/reversetunnel/srv.go Show resolved Hide resolved
lib/reversetunnel/srv.go Outdated Show resolved Hide resolved
return trace.Wrap(ctx.Err())
case c.CertAuthorityC <- updatedCerts.ToSlice():
for _, ca := range cas {
c.cas[ca.GetType()][ca.GetName()] = ca
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if GetCertAuthorities is guaranteed to doublecheck the type of the CAs? If not we should probably doublecheck it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Updated to only add them if configured to watch for that ca type now

CertAuthorityWatcher and its usage are refactored to allow for
all the following:
 - eliminate retransmission of the same CAs
 - reduce memory usage by having one local watcher per proxy
 - adds the ability to filter only the CAs that are desired
 - reduce the time required to send the first CAs

watchCertAuthorities now compares all CAs it receives from the
watcher with the previous CA of the same type and only sends to
the remote site if they are not identical. This is to reduce
unnecessary network traffic which can be problematic for a
root cluster with a larger number of leafs.

The CertAuthorityWatcher is refactored to leverage a fanout
to emit events to any number of watchers, each subscription
can be for a subset of the configured CA types. The proxy
now has only one CertAuthorityWatcher that is passed around
similarly to the LockWatcher. This reduces the memory usage
for proxies, which prior to this has one local CAWatcher per
remote site.

updateCertAuthorities no longer waits on the utils.Retry it
is provided with before starting to watch CAs. By doing this
the proxy no longer has to wait ~8 minutes before it even
starts to watch CAs.
@rosstimothy rosstimothy force-pushed the tross/fix_ca_watcher branch from a420eba to 58b5d5c Compare May 17, 2022 13:29
@@ -961,13 +956,20 @@ func NewCertAuthorityWatcher(ctx context.Context, cfg CertAuthorityWatcherConfig

collector := &caCollector{
CertAuthorityWatcherConfig: cfg,
fanout: NewFanout(),
cas: make(map[types.CertAuthType]map[string]types.CertAuthority, len(cfg.Types)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
cas: make(map[types.CertAuthType]map[string]types.CertAuthority, len(cfg.Types)),
cas: make(types.CertAuthorityTypeMap, len(cfg.Types)),

I also don't mind renaming the CertAuthorityTypeMap if you have any better idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I didn't know these were added, but looking at them, they seem really specific to the CAWatcher internals and I'm not entirely convinced that they should even be exported.

// CertAuthorityMap maps clusterName -> types.CertAuthority
type CertAuthorityMap map[string]types.CertAuthority

// CertAuthorityTypeMap maps types.CertAuthType -> map(clusterName -> types.CertAuthority)
type CertAuthorityTypeMap map[types.CertAuthType]CertAuthorityMap

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not even entirely sure they are needed any more after migrating to use the fanout. Thoughts on just removing them?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind removing them, just map[types.CertAuthType]map[string]types.CertAuthority looks a bit "scary" in my opinion.

}

for _, t := range cfg.Types {
collector.cas[t] = make(map[string]types.CertAuthority)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
collector.cas[t] = make(map[string]types.CertAuthority)
collector.cas[t] = make(CertAuthorityMap)

@@ -980,9 +982,63 @@ type CertAuthorityWatcher struct {
// caCollector accompanies resourceWatcher when monitoring cert authority resources.
type caCollector struct {
CertAuthorityWatcherConfig
lock sync.RWMutex
cas map[types.CertAuthType]map[string]types.CertAuthority
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
cas map[types.CertAuthType]map[string]types.CertAuthority
cas CertAuthorityTypeMap

lib/services/watcher.go Outdated Show resolved Hide resolved
lib/services/watcher.go Show resolved Hide resolved
lib/services/watcher.go Show resolved Hide resolved
integration/db_integration_test.go Outdated Show resolved Hide resolved

cas := make(map[types.CertAuthType]types.CertAuthority)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
cas := make(map[types.CertAuthType]types.CertAuthority)
cas := make(CertAuthorityMap)

}
}

func (s *remoteSite) watchCertAuthorities(remoteClusterVersion string) error {
localWatchedTypes, err := s.getLocalWatchedCerts(remoteClusterVersion)
func (s *remoteSite) watchCertAuthorities(remoteWatcher *services.CertAuthorityWatcher, remoteVersion string, cas map[types.CertAuthType]types.CertAuthority) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
func (s *remoteSite) watchCertAuthorities(remoteWatcher *services.CertAuthorityWatcher, remoteVersion string, cas map[types.CertAuthType]types.CertAuthority) error {
func (s *remoteSite) watchCertAuthorities(remoteWatcher *services.CertAuthorityWatcher, remoteVersion string, cas CertAuthorityMap) error {

@jakule
Copy link
Contributor

jakule commented May 17, 2022

Unit test failure looks like something related:

    sshserver_test.go:1888: 
        	Error Trace:	sshserver_test.go:1888
        	Error:      	Received unexpected error:
        	            	missing parameter CertAuthorityWatcher
        	Test:       	TestIgnorePuTTYSimpleChannel

@rosstimothy rosstimothy enabled auto-merge (squash) May 17, 2022 17:02
@rosstimothy rosstimothy merged commit 1ac0957 into master May 17, 2022
@github-actions
Copy link

@rosstimothy See the table below for backport results.

Branch Result
branch/v7 Failed
branch/v8 Failed
branch/v9 Failed

@rosstimothy
Copy link
Contributor Author

💚 All backports created successfully

Status Branch Result
branch/v7
branch/v8
branch/v9

Questions ?

Please refer to the Backport tool documentation

rosstimothy added a commit that referenced this pull request May 18, 2022
* Improve CertAuthorityWatcher

CertAuthorityWatcher and its usage are refactored to allow for
all the following:
 - eliminate retransmission of the same CAs
 - reduce memory usage by having one local watcher per proxy
 - adds the ability to filter only the CAs that are desired
 - reduce the time required to send the first CAs

watchCertAuthorities now compares all CAs it receives from the
watcher with the previous CA of the same type and only sends to
the remote site if they are not identical. This is to reduce
unnecessary network traffic which can be problematic for a
root cluster with a larger number of leafs.

The CertAuthorityWatcher is refactored to leverage a fanout
to emit events to any number of watchers, each subscription
can be for a subset of the configured CA types. The proxy
now has only one CertAuthorityWatcher that is passed around
similarly to the LockWatcher. This reduces the memory usage
for proxies, which prior to this has one local CAWatcher per
remote site.

updateCertAuthorities no longer waits on the utils.Retry it
is provided with before starting to watch CAs. By doing this
the proxy no longer has to wait ~8 minutes before it even
starts to watch CAs.

(cherry picked from commit 1ac0957)
rosstimothy added a commit that referenced this pull request May 18, 2022
* Improve CertAuthorityWatcher

CertAuthorityWatcher and its usage are refactored to allow for
all the following:
 - eliminate retransmission of the same CAs
 - reduce memory usage by having one local watcher per proxy
 - adds the ability to filter only the CAs that are desired
 - reduce the time required to send the first CAs

watchCertAuthorities now compares all CAs it receives from the
watcher with the previous CA of the same type and only sends to
the remote site if they are not identical. This is to reduce
unnecessary network traffic which can be problematic for a
root cluster with a larger number of leafs.

The CertAuthorityWatcher is refactored to leverage a fanout
to emit events to any number of watchers, each subscription
can be for a subset of the configured CA types. The proxy
now has only one CertAuthorityWatcher that is passed around
similarly to the LockWatcher. This reduces the memory usage
for proxies, which prior to this has one local CAWatcher per
remote site.

updateCertAuthorities no longer waits on the utils.Retry it
is provided with before starting to watch CAs. By doing this
the proxy no longer has to wait ~8 minutes before it even
starts to watch CAs.

(cherry picked from commit 1ac0957)
rosstimothy added a commit that referenced this pull request May 18, 2022
* Improve CertAuthorityWatcher

CertAuthorityWatcher and its usage are refactored to allow for
all the following:
 - eliminate retransmission of the same CAs
 - reduce memory usage by having one local watcher per proxy
 - adds the ability to filter only the CAs that are desired
 - reduce the time required to send the first CAs

watchCertAuthorities now compares all CAs it receives from the
watcher with the previous CA of the same type and only sends to
the remote site if they are not identical. This is to reduce
unnecessary network traffic which can be problematic for a
root cluster with a larger number of leafs.

The CertAuthorityWatcher is refactored to leverage a fanout
to emit events to any number of watchers, each subscription
can be for a subset of the configured CA types. The proxy
now has only one CertAuthorityWatcher that is passed around
similarly to the LockWatcher. This reduces the memory usage
for proxies, which prior to this has one local CAWatcher per
remote site.

updateCertAuthorities no longer waits on the utils.Retry it
is provided with before starting to watch CAs. By doing this
the proxy no longer has to wait ~8 minutes before it even
starts to watch CAs.

(cherry picked from commit 1ac0957)
rosstimothy added a commit that referenced this pull request May 24, 2022
* Improve CertAuthorityWatcher

CertAuthorityWatcher and its usage are refactored to allow for
all the following:
 - eliminate retransmission of the same CAs
 - reduce memory usage by having one local watcher per proxy
 - adds the ability to filter only the CAs that are desired
 - reduce the time required to send the first CAs

watchCertAuthorities now compares all CAs it receives from the
watcher with the previous CA of the same type and only sends to
the remote site if they are not identical. This is to reduce
unnecessary network traffic which can be problematic for a
root cluster with a larger number of leafs.

The CertAuthorityWatcher is refactored to leverage a fanout
to emit events to any number of watchers, each subscription
can be for a subset of the configured CA types. The proxy
now has only one CertAuthorityWatcher that is passed around
similarly to the LockWatcher. This reduces the memory usage
for proxies, which prior to this has one local CAWatcher per
remote site.

updateCertAuthorities no longer waits on the utils.Retry it
is provided with before starting to watch CAs. By doing this
the proxy no longer has to wait ~8 minutes before it even
starts to watch CAs.

(cherry picked from commit 1ac0957)
rosstimothy added a commit that referenced this pull request May 24, 2022
* Improve CertAuthorityWatcher

CertAuthorityWatcher and its usage are refactored to allow for
all the following:
 - eliminate retransmission of the same CAs
 - reduce memory usage by having one local watcher per proxy
 - adds the ability to filter only the CAs that are desired
 - reduce the time required to send the first CAs

watchCertAuthorities now compares all CAs it receives from the
watcher with the previous CA of the same type and only sends to
the remote site if they are not identical. This is to reduce
unnecessary network traffic which can be problematic for a
root cluster with a larger number of leafs.

The CertAuthorityWatcher is refactored to leverage a fanout
to emit events to any number of watchers, each subscription
can be for a subset of the configured CA types. The proxy
now has only one CertAuthorityWatcher that is passed around
similarly to the LockWatcher. This reduces the memory usage
for proxies, which prior to this has one local CAWatcher per
remote site.

updateCertAuthorities no longer waits on the utils.Retry it
is provided with before starting to watch CAs. By doing this
the proxy no longer has to wait ~8 minutes before it even
starts to watch CAs.

(cherry picked from commit 1ac0957)
rosstimothy added a commit that referenced this pull request May 24, 2022
* Improve CertAuthorityWatcher

CertAuthorityWatcher and its usage are refactored to allow for
all the following:
 - eliminate retransmission of the same CAs
 - reduce memory usage by having one local watcher per proxy
 - adds the ability to filter only the CAs that are desired
 - reduce the time required to send the first CAs

watchCertAuthorities now compares all CAs it receives from the
watcher with the previous CA of the same type and only sends to
the remote site if they are not identical. This is to reduce
unnecessary network traffic which can be problematic for a
root cluster with a larger number of leafs.

The CertAuthorityWatcher is refactored to leverage a fanout
to emit events to any number of watchers, each subscription
can be for a subset of the configured CA types. The proxy
now has only one CertAuthorityWatcher that is passed around
similarly to the LockWatcher. This reduces the memory usage
for proxies, which prior to this has one local CAWatcher per
remote site.

updateCertAuthorities no longer waits on the utils.Retry it
is provided with before starting to watch CAs. By doing this
the proxy no longer has to wait ~8 minutes before it even
starts to watch CAs.

(cherry picked from commit 1ac0957)
rosstimothy added a commit that referenced this pull request May 24, 2022
* Improve CertAuthorityWatcher

CertAuthorityWatcher and its usage are refactored to allow for
all the following:
 - eliminate retransmission of the same CAs
 - reduce memory usage by having one local watcher per proxy
 - adds the ability to filter only the CAs that are desired
 - reduce the time required to send the first CAs

watchCertAuthorities now compares all CAs it receives from the
watcher with the previous CA of the same type and only sends to
the remote site if they are not identical. This is to reduce
unnecessary network traffic which can be problematic for a
root cluster with a larger number of leafs.

The CertAuthorityWatcher is refactored to leverage a fanout
to emit events to any number of watchers, each subscription
can be for a subset of the configured CA types. The proxy
now has only one CertAuthorityWatcher that is passed around
similarly to the LockWatcher. This reduces the memory usage
for proxies, which prior to this has one local CAWatcher per
remote site.

updateCertAuthorities no longer waits on the utils.Retry it
is provided with before starting to watch CAs. By doing this
the proxy no longer has to wait ~8 minutes before it even
starts to watch CAs.

(cherry picked from commit 1ac0957)
@webvictim webvictim mentioned this pull request Jun 8, 2022
@rosstimothy rosstimothy deleted the tross/fix_ca_watcher branch January 5, 2023 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants