Skip to content

Commit

Permalink
perf: Cache list of nodes instead of retrieving the full list every time
Browse files Browse the repository at this point in the history
  • Loading branch information
TwiN committed Feb 14, 2022
1 parent 929f958 commit 14c56ec
Show file tree
Hide file tree
Showing 16 changed files with 1,289 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/TwiN/aws-eks-asg-rolling-update-handler
go 1.16

require (
github.com/TwiN/gocache/v2 v2.0.0
github.com/aws/aws-sdk-go v1.36.5
github.com/google/gofuzz v1.2.0 // indirect
github.com/googleapis/gnostic v0.5.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbt
github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/TwiN/gocache/v2 v2.0.0 h1:CPbDNKdSJpmBkh7aWcO7D3KK1yWaMlwX+3dsBPE8/so=
github.com/TwiN/gocache/v2 v2.0.0/go.mod h1:j4MABVaia2Tp53ERWc/3l4YxkswtPjB2hQzmL/kD/VQ=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand Down
18 changes: 18 additions & 0 deletions k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"time"

"github.com/TwiN/gocache/v2"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"k8s.io/api/core/v1"
Expand All @@ -18,6 +19,12 @@ const (
RollingUpdateStartedTimestampAnnotationKey = "aws-eks-asg-rolling-update-handler/started-at"
RollingUpdateDrainedTimestampAnnotationKey = "aws-eks-asg-rolling-update-handler/drained-at"
RollingUpdateTerminatedTimestampAnnotationKey = "aws-eks-asg-rolling-update-handler/terminated-at"

nodesCacheKey = "nodes"
)

var (
cache = gocache.NewCache().WithMaxSize(1000).WithEvictionPolicy(gocache.LeastRecentlyUsed)
)

type KubernetesClientApi interface {
Expand All @@ -42,10 +49,21 @@ func NewKubernetesClient(client *kubernetes.Clientset) *KubernetesClient {

// GetNodes retrieves all nodes from the cluster
func (k *KubernetesClient) GetNodes() ([]v1.Node, error) {
nodes, exists := cache.Get(nodesCacheKey)
if exists {
if v1Nodes, ok := nodes.([]v1.Node); ok {
// Return cached nodes
return v1Nodes, nil
} else {
log.Println("[k8s.GetNodes] Failed to cast cached nodes to []v1.Node; retrieving nodes from API instead")
cache.Delete(nodesCacheKey)
}
}
nodeList, err := k.client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
cache.SetWithTTL(nodesCacheKey, nodeList.Items, 10*time.Second)
return nodeList.Items, nil
}

Expand Down
1 change: 1 addition & 0 deletions vendor/github.com/TwiN/gocache/v2/.gitattributes

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/github.com/TwiN/gocache/v2/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions vendor/github.com/TwiN/gocache/v2/LICENSE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

370 changes: 370 additions & 0 deletions vendor/github.com/TwiN/gocache/v2/README.md

Large diffs are not rendered by default.

108 changes: 108 additions & 0 deletions vendor/github.com/TwiN/gocache/v2/entry.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/github.com/TwiN/gocache/v2/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
Loading

0 comments on commit 14c56ec

Please sign in to comment.