Skip to content

Commit

Permalink
feat: implement --enable-reverse-sync flag
Browse files Browse the repository at this point in the history
Disable the optimization of reducing the number of syncs when this flag
is enabled.
This should be used only if there is any human who has write access to
Kong's Admin API or Kong Manager in case of Kong Enterprise.

Fix #559
  • Loading branch information
hbagdi committed Apr 13, 2020
1 parent 327a495 commit 4a42467
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
9 changes: 6 additions & 3 deletions cli/ingress-controller/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ type cliConfig struct {
UpdateStatus bool
UpdateStatusOnShutdown bool

// Rutnime behavior
SyncPeriod time.Duration
SyncRateLimit float32
// Runtime behavior
SyncPeriod time.Duration
SyncRateLimit float32
EnableReverseSync bool

// k8s connection details
APIServerHost string
Expand Down Expand Up @@ -182,6 +183,7 @@ IP/hostname when the controller is being stopped.`)
`Relist and confirm cloud resources this often.`)
flags.Float32("sync-rate-limit", 0.3,
`Define the sync frequency upper limit`)
flag.Bool("enable-reverse-sync", false, `Enable reverse checks from Kong to Kubernetes`)

// k8s connection details
flags.String("apiserver-host", "",
Expand Down Expand Up @@ -293,6 +295,7 @@ func parseFlags() (cliConfig, error) {
// Rutnime behavior
config.SyncPeriod = viper.GetDuration("sync-period")
config.SyncRateLimit = (float32)(viper.GetFloat64("sync-rate-limit"))
config.EnableReverseSync = viper.GetBool("enable-reverse-sync")

// k8s connection details
config.APIServerHost = viper.GetString("apiserver-host")
Expand Down
5 changes: 3 additions & 2 deletions cli/ingress-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ func controllerConfigFromCLIConfig(cliConfig cliConfig) controller.Configuration
Concurrency: cliConfig.KongAdminConcurrency,
},

ResyncPeriod: cliConfig.SyncPeriod,
SyncRateLimit: cliConfig.SyncRateLimit,
ResyncPeriod: cliConfig.SyncPeriod,
SyncRateLimit: cliConfig.SyncRateLimit,
EnableReverseSync: cliConfig.EnableReverseSync,

Namespace: cliConfig.WatchNamespace,

Expand Down
7 changes: 4 additions & 3 deletions internal/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ type Configuration struct {
KongConfigClient configurationClientSet.Interface
KnativeClient knativeClientSet.Interface

ResyncPeriod time.Duration
SyncRateLimit float32
ResyncPeriod time.Duration
SyncRateLimit float32
EnableReverseSync bool

Namespace string

Expand Down Expand Up @@ -219,7 +220,7 @@ type KongController struct {
// backgroundGroup tracks running background goroutines, on which we'll wait to stop in Stop.
backgroundGroup errgroup.Group

runningConfigHash [32]byte
runningConfigHash []byte

isShuttingDown uint32

Expand Down
39 changes: 26 additions & 13 deletions internal/ingress/controller/kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,39 @@ func (n *KongController) OnUpdate(state *parser.KongState) error {
return err
}

jsonConfig, err := json.Marshal(targetContent)
if err != nil {
return errors.Wrap(err,
"marshaling Kong declarative configuration to JSON")
}
shaSum := sha256.Sum256(jsonConfig)
if reflect.DeepEqual(n.runningConfigHash, shaSum) {
glog.Info("no configuration change, skipping sync to Kong")
return nil
var shaSum []byte
// disable optimization if reverse sync is enabled
if !n.cfg.EnableReverseSync {
shaSum, err = generateSHA(targetContent)
if err != nil {
return err
}
if reflect.DeepEqual(n.runningConfigHash, shaSum) {
glog.Info("no configuration change, skipping sync to Kong")
return nil
}
}
if n.cfg.InMemory {
err = n.onUpdateInMemoryMode(targetContent)
} else {
err = n.onUpdateDBMode(targetContent)
}
if err == nil {
glog.Info("successfully synced configuration to Kong")
n.runningConfigHash = shaSum
if err != nil {
return err
}
n.runningConfigHash = shaSum
glog.Info("successfully synced configuration to Kong")
return nil
}

func generateSHA(targetContent *file.Content) ([]byte, error) {
jsonConfig, err := json.Marshal(targetContent)
if err != nil {
return nil, errors.Wrap(err,
"marshaling Kong declarative configuration to JSON")
}
return err
shaSum := sha256.Sum256(jsonConfig)
return shaSum[:], nil
}

func cleanUpNullsInPluginConfigs(state *file.Content) {
Expand Down

0 comments on commit 4a42467

Please sign in to comment.