Skip to content

Commit

Permalink
feature. add cache feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ktkfree committed Apr 26, 2024
1 parent 4dfcd23 commit ae1571f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 33 deletions.
50 changes: 25 additions & 25 deletions .github/workflows/golangcic-lint.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
name: Lint
on:
push:
tags:
- v*
branches:
- main
- develop
- release
pull_request:

branches:
- main
- develop
- release
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: latest
args: --timeout=5m

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true then the action will use pre-installed Go.
# skip-go-installation: true

# Optional: if set to true then the action don't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true
go-version: "1.21"
cache: false
- name: Install golangci-lint
# Install golangci-lint from source instead of using
# golangci-lint-action to ensure the golangci-lint binary is built with
# the same Go version we're targeting.
# Avoids incompatibility issues such as:
# - https://github.com/golangci/golangci-lint/issues/2922
# - https://github.com/golangci/golangci-lint/issues/2673
# - https://github.com/golangci/golangci-lint-action/issues/442
run: go install github.com/golangci/golangci-lint/cmd/[email protected]
- name: Run golangci-lint
run: golangci-lint run --verbose --out-format=github-actions
9 changes: 6 additions & 3 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
_apiClient "github.com/openinfradev/tks-api/pkg/api-client"
argo "github.com/openinfradev/tks-api/pkg/argo-client"
"github.com/openinfradev/tks-api/pkg/log"
"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/openinfradev/tks-batch/internal/application"
cloudAccount "github.com/openinfradev/tks-batch/internal/cloud-account"
"github.com/openinfradev/tks-batch/internal/cluster"
"github.com/openinfradev/tks-batch/internal/database"
"github.com/openinfradev/tks-batch/internal/organization"
systemNotificationRule "github.com/openinfradev/tks-batch/internal/system-notification-rule"
gcache "github.com/patrickmn/go-cache"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

const INTERVAL_SEC = 5
Expand All @@ -30,6 +30,7 @@ var (
organizationAccessor *organization.OrganizationAccessor
systemNotificationRuleAccessor *systemNotificationRule.SystemNotificationAccessor
apiClient _apiClient.ApiClient
cache *gcache.Cache
)

func init() {
Expand Down Expand Up @@ -84,6 +85,8 @@ func main() {
log.Fatal(context.TODO(), "failed to create tks-api client : ", err)
}

cache = gcache.New(5*time.Minute, 10*time.Minute)

for {
err = processClusterStatus()
if err != nil {
Expand Down
12 changes: 11 additions & 1 deletion cmd/server/thanos_ruler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (

"github.com/openinfradev/tks-api/pkg/kubernetes"
"github.com/openinfradev/tks-api/pkg/log"
gcache "github.com/patrickmn/go-cache"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const LAST_UPDATED_MIN = 1
const LAST_UPDATED_MIN = 2

func processReloadThanosRules() error {
organizationIds, err := systemNotificationRuleAccessor.GetRecentlyUpdatedOrganizations(LAST_UPDATED_MIN)
Expand Down Expand Up @@ -48,6 +49,13 @@ func processReloadThanosRules() error {
}

func GetThanosRulerUrl(primaryClusterId string) (url string, err error) {
const prefix = "CACHE_KEY_THANOS_RULER_URL"
value, found := cache.Get(prefix + primaryClusterId)
if found {
log.Info(context.TODO(), "Cache HIT [CACHE_KEY_THANOS_RULER_URL] ", value)
return value.(string), nil
}

clientset_admin, err := kubernetes.GetClientAdminCluster(context.TODO())
if err != nil {
return url, errors.Wrap(err, "Failed to get client set for user cluster")
Expand Down Expand Up @@ -80,6 +88,8 @@ func GetThanosRulerUrl(primaryClusterId string) (url string, err error) {
} else {
url = "http://" + string(secrets.Data["thanos-ruler"])
}

cache.Set(prefix+primaryClusterId, url, gcache.DefaultExpiration)
return url, nil
}

Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ toolchain go1.21.7

require (
github.com/gofrs/uuid v4.0.0+incompatible
github.com/openinfradev/tks-api v0.0.0-20240411053710-5b8a434e8797
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.18.2
gopkg.in/yaml.v2 v2.4.0
Expand Down Expand Up @@ -47,9 +50,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/openinfradev/tks-api v0.0.0-20240411053710-5b8a434e8797 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ github.com/onsi/ginkgo/v2 v2.4.0 h1:+Ig9nvqgS5OBSACXNk15PLdp0U9XPYROt9CFzVdFGIs=
github.com/onsi/ginkgo/v2 v2.4.0/go.mod h1:iHkDK1fKGcBoEHT5W7YBq4RFWaQulw+caOMkAt4OrFo=
github.com/onsi/gomega v1.23.0 h1:/oxKu9c2HVap+F3PfKort2Hw5DEU+HGlW8n+tguWsys=
github.com/onsi/gomega v1.23.0/go.mod h1:Z/NWtiqwBrwUt4/2loMmHL63EDLnYHmVbuBpDr2vQAg=
github.com/openinfradev/tks-api v0.0.0-20240409091158-eff7241c1731 h1:gmVBHSDzJGdf9p4wm28bDFcA3yFU6QjZl4prCd2fvIg=
github.com/openinfradev/tks-api v0.0.0-20240409091158-eff7241c1731/go.mod h1:OGfXiL0YRby+OzOm+OI0d+wtPkOj3SMCiAv3lvpmaiU=
github.com/openinfradev/tks-api v0.0.0-20240411053710-5b8a434e8797 h1:DQ5naso3RdA0XxQ2Fj70xZ4O3vBhtWYR9Kpdy7LQqRE=
github.com/openinfradev/tks-api v0.0.0-20240411053710-5b8a434e8797/go.mod h1:Ph4lPgdWg06R1GUNCtmXfzHNlNCW/XjUAvei+m5DD2o=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down

0 comments on commit ae1571f

Please sign in to comment.