diff --git a/.github/workflows/golangcic-lint.yml b/.github/workflows/golangcic-lint.yml index cdd16fb..f889b7a 100644 --- a/.github/workflows/golangcic-lint.yml +++ b/.github/workflows/golangcic-lint.yml @@ -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/golangci-lint@v1.56.2 + - name: Run golangci-lint + run: golangci-lint run --verbose --out-format=github-actions diff --git a/cmd/server/main.go b/cmd/server/main.go index 65d2d53..da7a59f 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -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 @@ -30,6 +30,7 @@ var ( organizationAccessor *organization.OrganizationAccessor systemNotificationRuleAccessor *systemNotificationRule.SystemNotificationAccessor apiClient _apiClient.ApiClient + cache *gcache.Cache ) func init() { @@ -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 { diff --git a/cmd/server/thanos_ruler.go b/cmd/server/thanos_ruler.go index fb2ab43..72ccfce 100644 --- a/cmd/server/thanos_ruler.go +++ b/cmd/server/thanos_ruler.go @@ -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) @@ -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") @@ -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 } diff --git a/go.mod b/go.mod index f6b78f4..33f8f35 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 1235b18..dc73524 100644 --- a/go.sum +++ b/go.sum @@ -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=