Skip to content

Commit

Permalink
Merge pull request #11 from run-x/jd/updating
Browse files Browse the repository at this point in the history
More updates
  • Loading branch information
juandiegopalomino authored Apr 14, 2022
2 parents 4818da7 + e79576a commit 8c6f175
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 11 deletions.
2 changes: 1 addition & 1 deletion charts/opta/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
name: opta-agent
description: The Opta Agent to monitor your environment via the opta ui.
home: https://github.com/run-x/opta-agent
version: 0.1.2
version: 0.1.3
appVersion: 0.1.0
type: application
kubeVersion: ">= 1.18.0-0"
Expand Down
8 changes: 5 additions & 3 deletions charts/opta/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Opta Agent

[![Artifact Hub](https://img.shields.io/endpoint?url=https://artifacthub.io/badge/repository/opta-agent)](https://artifacthub.io/packages/search?repo=opta-agent) ![Version: 0.1.1](https://img.shields.io/badge/Version-0.1.1-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 0.1.0](https://img.shields.io/badge/AppVersion-0.1.0-informational?style=flat-square)
[![Artifact Hub](https://img.shields.io/endpoint?url=https://artifacthub.io/badge/repository/opta-agent)](https://artifacthub.io/packages/search?repo=opta-agent) ![Version: 0.1.3](https://img.shields.io/badge/Version-0.1.3-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 0.1.0](https://img.shields.io/badge/AppVersion-0.1.0-informational?style=flat-square)

## Requirements

Kubernetes: `>= 1.18.0`
Kubernetes: `>= 1.18.0-0`

## Maintainers

Expand All @@ -20,6 +20,8 @@ Kubernetes: `>= 1.18.0`
| optaAgent.containerResourceLimits.memory | string | `"256Mi"` | |
| optaAgent.containerResourceRequests.cpu | string | `"100m"` | |
| optaAgent.containerResourceRequests.memory | string | `"128Mi"` | |
| optaAgent.debug | bool | `false` | |
| optaAgent.image | string | `"runx1/opta-agent:latest"` | |
| optaAgent.namespace | string | `"opta-agent"` | |
| optaAgent.token | string | `"nil"` | |
| optaAgent.token | string | `"nil"` | |
| optaAgent.verbose | bool | `false` | |
8 changes: 7 additions & 1 deletion charts/opta/templates/cluster_role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ rules:
- list
- get
- watch
- patch
- patch
- apiGroups:
- ""
resources:
- events
verbs:
- create
8 changes: 8 additions & 0 deletions charts/opta/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ spec:
protocol: TCP
containerPort: 8080
env:
{{- if .Values.optaAgent.verbose }}
- name: VERBOSE
value: "true"
{{- end }}
{{- if .Values.optaAgent.debug }}
- name: DEBUG
value: "true"
{{- end }}
- name: OPTA_TOKEN
valueFrom:
secretKeyRef:
Expand Down
2 changes: 2 additions & 0 deletions charts/opta/values.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
optaAgent:
namespace: "opta-agent"
verbose: false
debug: false
token: nil
image: "runx1/opta-agent:latest"
containerResourceRequests:
Expand Down
30 changes: 25 additions & 5 deletions srv/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ async def get_service(environment_name, service_name):
headers={"opta": jwt},
) as resp:
json = await resp.json()
if len(json) == 0:
return None
service = json[0]
return service

Expand Down Expand Up @@ -131,10 +133,16 @@ async def update_opta_ui_pod_status(uid, status, labels, logger, **_):
if not is_valid_opta_pod(labels):
return
try:
logger.info(f"Routine check on pod {uid}")
environment_name = labels.get("opta.dev/environment-name")
service_name = labels.get("opta.dev/layer-name")

service = await get_service(environment_name, service_name)
if service is None:
raise Exception(
f"No service with name {service_name} found in this environment-- has opta apply been run on it after "
"adding the runx module?"
)
service_id = service["id"]
created_at = get_pod_created_time_from_status(status)

Expand All @@ -147,18 +155,24 @@ async def update_opta_ui_pod_status(uid, status, labels, logger, **_):
updated_at=datetime.now().isoformat(),
)
except Exception:
logger.info(f"Failed to update pod {uid}")
logger.error(f"Failed to update pod {uid}:", exc_info=True)


@kopf.on.delete("pods", retries=5)
async def delete_opta_ui_pod(uid, logger, labels, status, **_):
if not is_valid_opta_pod(labels):
return
try:
logger.info(f"Deletion detected on pod {uid}")
environment_name = labels.get("opta.dev/environment-name")
service_name = labels.get("opta.dev/layer-name")

service = await get_service(environment_name, service_name)
if service is None:
raise Exception(
f"No service with name {service_name} found in this environment-- has opta apply been run on it after "
"adding the runx module?"
)
service_id = service["id"]
created_at = get_pod_created_time_from_status(status)

Expand All @@ -172,15 +186,21 @@ async def delete_opta_ui_pod(uid, logger, labels, status, **_):
deleted_at=datetime.now().isoformat(),
)
except Exception:
logger.info(f"Failed to delete pod {uid}")
logger.error(f"Failed to delete pod {uid}:", exc_info=True)


@kopf.on.update("deployment", field="spec.replicas")
async def update_deployment_info(old, new, labels, logger, **_):
async def update_deployment_info(uid, old, new, labels, logger, **_):
try:
logger.info(f"Replica changed detected on deployment {uid}")
environment_name = labels.get("opta.dev/environment-name")
service_name = labels.get("opta.dev/layer-name")
service = await get_service(environment_name, service_name)
if service is None:
raise Exception(
f"No service with name {service_name} found in this environment-- has opta apply been run on it after "
"adding the runx module?"
)
service_id = service["id"]

await post_event(
Expand All @@ -190,5 +210,5 @@ async def update_deployment_info(old, new, labels, logger, **_):
timestamp=datetime.now().isoformat(),
message=f"scaled from {old} pods to {new} pods",
)
except Exception as e:
logger.error(e)
except Exception:
logger.error(f"Failed to send replica change event for deployment {uid}:", exc_info=True)
12 changes: 11 additions & 1 deletion startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@ source $(pipenv --venv)/bin/activate
# TODO should be separated into prestart and start scripts
cd srv

kopf run handlers.py --liveness=http://0.0.0.0:8080/healthz --verbose
if [[ -n ${VERBOSE} ]]; then
echo "Running verbose mode"
kopf run handlers.py --liveness=http://0.0.0.0:8080/healthz --verbose --log-format=full --namespace=!kube-system,!linkerd
fi

if [[ -n ${DEBUG} ]]; then
echo "Running debug mode"
kopf run handlers.py --liveness=http://0.0.0.0:8080/healthz --debug --log-format=full --namespace=!kube-system,!linkerd
fi

kopf run handlers.py --liveness=http://0.0.0.0:8080/healthz --log-format=full --namespace=!kube-system,!linkerd

0 comments on commit 8c6f175

Please sign in to comment.