-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from openinfradev/fix_snr
feature. change apply rule logic to reload
- Loading branch information
Showing
13 changed files
with
203 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strconv" | ||
|
||
"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 = 2 | ||
|
||
func processReloadThanosRules() error { | ||
organizationIds, err := systemNotificationRuleAccessor.GetRecentlyUpdatedOrganizations(LAST_UPDATED_MIN) | ||
if err != nil { | ||
return err | ||
} | ||
if len(organizationIds) == 0 { | ||
return nil | ||
} | ||
log.Info(context.TODO(), "[processReloadThanosRules] new updated organizationIds : ", organizationIds) | ||
|
||
for _, organizationId := range organizationIds { | ||
organization, err := organizationAccessor.Get(organizationId) | ||
if err != nil { | ||
log.Error(context.TODO(), err) | ||
continue | ||
} | ||
|
||
url, err := GetThanosRulerUrl(organization.PrimaryClusterId) | ||
if err != nil { | ||
log.Error(context.TODO(), err) | ||
continue | ||
} | ||
|
||
if err = Reload(url); err != nil { | ||
log.Error(context.TODO(), err) | ||
continue | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
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") | ||
} | ||
|
||
secrets, err := clientset_admin.CoreV1().Secrets(primaryClusterId).Get(context.TODO(), "tks-endpoint-secret", metav1.GetOptions{}) | ||
if err != nil { | ||
log.Info(context.TODO(), "cannot found tks-endpoint-secret. so use LoadBalancer...") | ||
|
||
clientset_user, err := kubernetes.GetClientFromClusterId(context.TODO(), primaryClusterId) | ||
if err != nil { | ||
return url, errors.Wrap(err, "Failed to get client set for user cluster") | ||
} | ||
|
||
service, err := clientset_user.CoreV1().Services("lma").Get(context.TODO(), "thanos-ruler", metav1.GetOptions{}) | ||
if err != nil { | ||
return url, errors.Wrap(err, "Failed to get services.") | ||
} | ||
|
||
// LoadBalaner 일경우, aws address 형태의 경우만 가정한다. | ||
if service.Spec.Type != "LoadBalancer" { | ||
return url, fmt.Errorf("Service type is not LoadBalancer. [%s] ", service.Spec.Type) | ||
} | ||
|
||
lbs := service.Status.LoadBalancer.Ingress | ||
ports := service.Spec.Ports | ||
if len(lbs) > 0 && len(ports) > 0 { | ||
url = ports[0].TargetPort.StrVal + "://" + lbs[0].Hostname + ":" + strconv.Itoa(int(ports[0].Port)) | ||
} | ||
} else { | ||
url = "http://" + string(secrets.Data["thanos-ruler"]) | ||
} | ||
|
||
cache.Set(prefix+primaryClusterId, url, gcache.DefaultExpiration) | ||
return url, nil | ||
} | ||
|
||
func Reload(thanosRulerUrl string) (err error) { | ||
reqUrl := thanosRulerUrl + "/-/reload" | ||
|
||
log.Info(context.TODO(), "url : ", reqUrl) | ||
resp, err := http.Post(reqUrl, "text/plain", nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
_, err = io.ReadAll(resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.