Skip to content

Commit

Permalink
add flag to set period of uploading node status
Browse files Browse the repository at this point in the history
  • Loading branch information
randmonkey committed Feb 13, 2023
1 parent 3b52070 commit f9234dd
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ Adding a new version? You'll need three changes:
[#3521](https://github.com/Kong/kubernetes-ingress-controller/pull/3521)
- Leader election is enabled by default then kong admin service discovery is enabled.
[#3529](https://github.com/Kong/kubernetes-ingress-controller/pull/3529)
- Added flag `--konnect-refresh-node-period` to set the period of uploading
status of KIC instance to Konnect runtime group.
[#3533](https://github.com/Kong/kubernetes-ingress-controller/pull/3533)

### Fixed

Expand Down
1 change: 1 addition & 0 deletions internal/adminapi/konnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type KonnectConfig struct {
ConfigSynchronizationEnabled bool
RuntimeGroupID string
Address string
RefreshNodePeriod time.Duration
TLSClient TLSClientConfig
}

Expand Down
28 changes: 17 additions & 11 deletions internal/konnect/node_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (
"github.com/kong/kubernetes-ingress-controller/v2/internal/util"
)

const defaultRefreshNodeInterval = 30 * time.Second
const (
MinRefreshNodePeriod = 30 * time.Second
DefaultRefreshNodePeriod = 60 * time.Second
)

type NodeAgent struct {
NodeID string
Expand All @@ -18,8 +21,8 @@ type NodeAgent struct {

Logger logr.Logger

konnectClient *Client
refreshInterval time.Duration
konnectClient *Client
refreshPeriod time.Duration

hasTranslationFailureChan chan bool
hasTranslationFailure bool
Expand All @@ -31,20 +34,22 @@ type NodeAgent struct {
func NewNodeAgent(
hostname string,
version string,
refreshPeriod time.Duration,
hasTranslationFailureChan chan bool,
sendConfigErrorChan chan error,
logger logr.Logger,
client *Client,
) *NodeAgent {
if refreshPeriod < MinRefreshNodePeriod {
refreshPeriod = MinRefreshNodePeriod
}
return &NodeAgent{
Hostname: hostname,
Version: version,
Logger: logger.
WithName("konnect-node").WithValues("runtime_group_id", client.RuntimeGroupID),
konnectClient: client,
// TODO: set refresh interval by some flag
// https://github.com/Kong/kubernetes-ingress-controller/issues/3515
refreshInterval: defaultRefreshNodeInterval,
refreshPeriod: refreshPeriod,
}
}

Expand All @@ -58,7 +63,7 @@ func (a *NodeAgent) createNode() error {
}
resp, err := a.konnectClient.CreateNode(createNodeReq)
if err != nil {
return fmt.Errorf("failed to update node, hostname %s: %w", a.Hostname, err)
return fmt.Errorf("failed to create node, hostname %s: %w", a.Hostname, err)
}

a.NodeID = resp.Item.ID
Expand All @@ -73,6 +78,10 @@ func (a *NodeAgent) clearOutdatedNodes() error {
}

for _, node := range nodes.Items {
// REVIEW: what should the condition be to delete the node in Konnect RG?
// (1) Do we check the "last update" of the node, and only delete it when the last update is too old(say, 5 mins ago)?
// (2) What if there is a node with the same name but not the same node exists?
// for example, When KIC runs in minikube/kind env and whole cluster is stopped then started again.
if node.Type == NodeTypeIngressController && node.Hostname != a.Hostname {
a.Logger.V(util.DebugLevel).Info("remove outdated KIC node", "node_id", node.ID, "hostname", node.Hostname)
err := a.konnectClient.DeleteNode(node.ID)
Expand All @@ -98,7 +107,6 @@ func (a *NodeAgent) updateNode() error {
err := a.clearOutdatedNodes()
if err != nil {
a.Logger.Error(err, "failed to clear outdated nodes")
return err
}

ingressControllerStatus := a.calculateStatus()
Expand All @@ -121,10 +129,8 @@ func (a *NodeAgent) updateNode() error {
}

func (a *NodeAgent) updateNodeLoop() {
ticker := time.NewTicker(a.refreshInterval)
ticker := time.NewTicker(a.refreshPeriod)
defer ticker.Stop()
// TODO: add some mechanism to break the loop
// https://github.com/Kong/kubernetes-ingress-controller/issues/3515
for range ticker.C {
err := a.updateNode()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/manager/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/kong/kubernetes-ingress-controller/v2/internal/annotations"
"github.com/kong/kubernetes-ingress-controller/v2/internal/controllers/gateway"
"github.com/kong/kubernetes-ingress-controller/v2/internal/dataplane"
"github.com/kong/kubernetes-ingress-controller/v2/internal/konnect"
)

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -237,6 +238,7 @@ func (c *Config) FlagSet() *pflag.FlagSet {
flagSet.StringVar(&c.Konnect.TLSClient.CertFile, "konnect-tls-client-cert-file", "", "Konnect TLS client certificate file path.")
flagSet.StringVar(&c.Konnect.TLSClient.Key, "konnect-tls-client-key", "", "Konnect TLS client key.")
flagSet.StringVar(&c.Konnect.TLSClient.KeyFile, "konnect-tls-client-key-file", "", "Konnect TLS client key file path.")
flagSet.DurationVar(&c.Konnect.RefreshNodePeriod, "konnect-refresh-node-period", konnect.DefaultRefreshNodePeriod, "Period of uploading status of KIC and controlled kong gateway instances")

// Deprecated flags
_ = flagSet.Float32("sync-rate-limit", dataplane.DefaultSyncSeconds, "Use --proxy-sync-seconds instead")
Expand Down
1 change: 1 addition & 0 deletions internal/manager/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func Run(ctx context.Context, c *Config, diagnostic util.ConfigDumpDiagnostic, d
version := metadata.Release
agent := konnect.NewNodeAgent(
hostname, version,
c.Konnect.RefreshNodePeriod,
hasTranslationFailureChan,
sendConfigErrorChan,
setupLog, konnectClient,
Expand Down

0 comments on commit f9234dd

Please sign in to comment.