Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init macro pipeline #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions .github/workflows/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
CONFIG_PATH = "config.json"
UPDATE_PATH = "/v2/leveetate/org/{tenant}/v1/mgmt/tenant/{tenant}/clusters/{cluster_id}/events/sap"
VALIDATE_PATH = "/v2/leveetate/org/{tenant}/v1/mgmt/tenant/{tenant}/clusters/{cluster_id}/events/validate_sap"
VALIDATE_MACROS_PATH = "/v2/leveetate/org/{tenant}/v1/mgmt/macros/tenant/{tenant}validate"
MACROS_PATH= "/v2/leveetate/org/{tenant}/v1/mgmt/tenant/{tenant}/clusters/{cluster_id}/macros"
TOKEN_HEADER = "X-LAST9-API-TOKEN"
PIPELINES_PATH = os.environ.get("PIPELINES_PATH", ".")

Expand Down Expand Up @@ -90,6 +92,85 @@ def get_sap(config):
print(f"SAP for {cluster}:")
print(r.text)

def get_macros(config):
tenant = config["tenant"]
if not tenant:
raise Exception("Tenant not found in config")

for cluster, config in config["clusters"].items():
path = MACROS_PATH.format(
tenant=tenant, cluster_id=config["cluster_id"]
)
url = APP_URL + path
r = requests.get(
url,
headers={
"region": config["region"],
"tenant": tenant,
TOKEN_HEADER: "Bearer {}".format(get_access_token("read")),
},
)
if r.status_code != 200:
print(f"Failed to get macros for {cluster}")
else:
print(f"mscros for {cluster}:")
print(r.text)

def do_macros(action, config):
tenant = config["tenant"]
if not tenant:
raise Exception("Tenant not found in config")

ret = []
for cluster, config in config["clusters"].items():
if "macros" not in config:
continue

if config["macros"] == "":
continue

macro_file = os.path.join(PIPELINES_PATH, config["macros"])
with open(macro_file) as f:
macroBody = f.read()
if action == "update":
path = MACROS_PATH.format(
tenant=tenant, cluster_id=config["cluster_id"]
)
r = requests.post(
APP_URL + path,
data=macroBody,
headers={
"region": config["region"],
"tenant": tenant,
TOKEN_HEADER: "Bearer {}".format(get_access_token("write")),
},
)
elif action == "validate":
path = VALIDATE_MACROS_PATH.format(
tenant=tenant,
)
r = requests.post(
APP_URL + path,
data=macroBody,
headers={
"region": config["region"],
"tenant": tenant,
TOKEN_HEADER: "Bearer {}".format(get_access_token("write")),
},
)

if r.status_code > 201:
ret.append((cluster, r.text))
if ret:
print(f"{action} failed for the following clusters:")
for cluster, error in ret:
print(cluster, error)
exit(1)
else:
print(f"{action} successful")



def main():
parser = argparse.ArgumentParser()
parser.add_argument("action", choices=["update", "validate", "get"])
Expand All @@ -99,8 +180,10 @@ def main():
config = json.load(f)
if args.action == "get":
get_sap(config)
get_macros(config)
return
doit(args.action, config)
do_macros(args.action, config)

if __name__ == "__main__":
main()
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"clusters": {
"cluster-foo": {
"region": "eu-west-1",
"cluster_id": "72d858c1-e310-42a1-a2a4-8ad7a0230087"
"cluster_id": "72d858c1-e310-42a1-a2a4-8ad7a0230087",
"macros": "macros/defaults.macro"
}
}
}
9 changes: 9 additions & 0 deletions macros/defaults.macro
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function percentile(p, metric) {
let clusterName = "alpha-tsdb.last9.io"
let filter = {cluster=clusterName,status="200"}
return histogram_quantile(p, sum(rate(metric{filter}[5m])) by (le))
}

function job_rate5m(metric) {
return sum without (instance, method, controller, status_code) \ (rate (metric [5m]))
}