Skip to content

Commit

Permalink
fix: add kong update sha checks
Browse files Browse the repository at this point in the history
  • Loading branch information
shaneutt committed May 5, 2021
1 parent bbe221f commit 8db2228
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
1 change: 1 addition & 0 deletions pkg/sendconfig/sendconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func PerformUpdate(ctx context.Context,
}
// disable optimization if reverse sync is enabled
if !reverseSync {
// use the previous SHA to determine whether or not to perform an update
if equalSHA(oldSHA, newSHA) {
log.Info("no configuration change, skipping sync to kong")
return oldSHA, nil
Expand Down
15 changes: 5 additions & 10 deletions railgun/internal/proxy/clientgo_cached_proxy_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ type clientgoCachedProxyResolver struct {
k8s client.Client
cache *store.CacheStores

// lastConfigSHA indicates the last SHA sum for the last configuration
// updated in the Kong Proxy and is used to avoid making unnecessary updates.
lastConfigSHA []byte

// kong configuration
kongConfig sendconfig.Kong

Expand Down Expand Up @@ -149,31 +153,22 @@ func (p *clientgoCachedProxyResolver) startCacheServer() {
syncTicker := time.NewTicker(p.stagger)

// updates tracks whether any updates/deletes were tracked this cycle
updates := false
for {
select {
case cobj := <-p.update:
if err := p.cacheUpdate(cobj); err != nil {
p.logger.Error(err, "object could not be updated in the cache and will be discarded")
break
}
updates = true
case cobj := <-p.del:
if err := p.cacheDelete(cobj); err != nil {
p.logger.Error(err, "object could not be deleted from the cache and will be discarded")
break
}
updates = true
case <-syncTicker.C:
// if there are no relevant object updates for this cycle, then there's no reason to
// bother the Kong Proxy with updates from the cache.
if !updates {
break
}
if err := p.updateKongAdmin(); err != nil {
p.logger.Error(err, "could not update kong admin")
}
updates = false
case <-p.ctx.Done():
p.logger.Info("the proxy cache server's context is done, shutting down")
return
Expand Down Expand Up @@ -218,7 +213,7 @@ func (p *clientgoCachedProxyResolver) updateKongAdmin() error {
// apply the configuration update in Kong
timedCtx, cancel := context.WithTimeout(p.ctx, 10*time.Second)
defer cancel()
_, err = sendconfig.PerformUpdate(timedCtx, logrus.StandardLogger(), &p.kongConfig, true, false, targetConfig, nil, nil, nil)
p.lastConfigSHA, err = sendconfig.PerformUpdate(timedCtx, logrus.StandardLogger(), &p.kongConfig, true, false, targetConfig, nil, nil, p.lastConfigSHA)
if err != nil {
return err
}
Expand Down

0 comments on commit 8db2228

Please sign in to comment.