Skip to content

Commit

Permalink
refactor: rework internal management of activations (#551)
Browse files Browse the repository at this point in the history
Co-authored-by: Madhu Kanoor <[email protected]>
Co-authored-by: hsong-rh <[email protected]>
Co-authored-by: Bill Wei <[email protected]>
  • Loading branch information
4 people authored Dec 12, 2023
1 parent fa20e53 commit ba512ae
Show file tree
Hide file tree
Showing 45 changed files with 5,403 additions and 760 deletions.
21 changes: 21 additions & 0 deletions Taskfile.dist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ tasks:
vars:
CLI_ARGS: build {{.CLI_ARGS}}

minikube:build:api:
desc: "Build EDA api image and push to minikube"
cmds:
- task: minikube
vars:
CLI_ARGS: build-api {{.CLI_ARGS}}

minikube:deploy:
desc: "Build deployment and deploy to minikube"
cmds:
Expand All @@ -248,6 +255,13 @@ tasks:
vars:
CLI_ARGS: clean {{.CLI_ARGS}}

minikube:clean:api:
desc: "Clean API related minikube resources"
cmds:
- task: minikube
vars:
CLI_ARGS: clean-api {{.CLI_ARGS}}

minikube:fp:ui:
desc: "Forward local port to ui instance port in minikube"
cmds:
Expand Down Expand Up @@ -283,6 +297,13 @@ tasks:
- task: minikube:build
- task: minikube:deploy

minikube:api:
desc: "build, deploy api pods"
cmds:
- task: minikube:clean:api
- task: minikube:build:api
- task: minikube:deploy

macpodman:
desc: "Run eda_podman.sh with specified CLI arguments."
summary: |
Expand Down
102 changes: 101 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 16 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ version = "0.1.0"
description = ""
authors = ["Red Hat, Inc. <[email protected]>"]
readme = "README.md"
packages = [
{include = "aap_eda", from = "src"}
]
packages = [{ include = "aap_eda", from = "src" }]

[tool.poetry.scripts]
aap-eda-manage = 'aap_eda.manage:main'
Expand Down Expand Up @@ -44,23 +42,25 @@ kubernetes = "*"
podman = ">=4.5"
rq-scheduler = "^0.10"
# Experimental LDAP Integration https://issues.redhat.com/browse/AAP-16938
# We are temporarily updating it to use latest from devel branch to keep consistency accross
# We are temporarily updating it to use latest from devel branch to keep consistency accross
# other AAP components
django-ansible-base = {git = "https://github.com/ansible/django-ansible-base.git"}
django-ansible-base = { git = "https://github.com/ansible/django-ansible-base.git" }
jinja2 = "*"

[tool.poetry.group.test.dependencies]
pytest = "*"
pytest-django = "*"
pytest-asyncio = "*"
requests = { version="*", python="<4.0" }
requests = { version = "*", python = "<4.0" }
pytest-cov = "^4.1.0"
pytest-redis = "^3.0.2"
pytest-lazy-fixture = "^0.6.3"

[tool.poetry.group.lint.dependencies]
flake8 = "*"
isort = "*"
black = "*"
flake8-broken-line = { version="*", python="<4.0" }
flake8-broken-line = { version = "*", python = "<4.0" }
flake8-string-format = "*"
# This is an experimental linter.
ruff = "*"
Expand All @@ -71,7 +71,7 @@ flake8-bugbear = "*"
flake8-comprehensions = "*"
flake8-debugger = "*"
flake8-docstrings = "*"
flake8-eradicate = { version="*", python="<4.0" }
flake8-eradicate = { version = "*", python = "<4.0" }
flake8-print = "*"

[tool.poetry.group.dev.dependencies]
Expand All @@ -95,16 +95,21 @@ line-length = 79
select = [
"E",
"F",
"D", # flake8-docstrings
"TID", # flake8-tidy-imports
"D", # flake8-docstrings
"TID", # flake8-tidy-imports
]
extend-ignore = [
"D1", # Missing docstrings errors
"D1", # Missing docstrings errors
]

[tool.ruff.per-file-ignores]
"__init__.py" = ["F401"]
"src/aap_eda/core/migrations/*" = ["E501"]
"tests/**/*.py" = [
"S101", # Asserts allowed in tests
"ARG", # Fixtures are not always used explicitly
"SLF001", # Call private methods in tests
]

[tool.ruff.flake8-tidy-imports]
ban-relative-imports = "parents"
Expand Down
30 changes: 30 additions & 0 deletions scripts/eda_kube.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ build-deployment() {
kustomize build "${DEPLOY_DIR}" -o "${DEPLOY_DIR}/temp"
}

build-deployment-api() {
local _api_image="aap-eda:${1}"
local _temp_dir="${DEPLOY_DIR}"/temp

log-info "Using Deployment Directory: ${DEPLOY_DIR}/temp"

cd "${DEPLOY_DIR}"/eda-api
log-debug "kustomize edit set image aap-eda=${_api_image}"
kustomize edit set image aap-eda="${_api_image}"

cd "${PROJECT_DIR}"
log-debug "kustomize build ${DEPLOY_DIR} -o ${DEPLOY_DIR}/temp"
kustomize build "${DEPLOY_DIR}" -o "${DEPLOY_DIR}/temp"
}

build-eda-api-image() {
local _image="aap-eda:${1}"

Expand Down Expand Up @@ -109,6 +124,11 @@ build-all() {
build-deployment "${1}"
}

build-api() {
build-eda-api-image "${1}"
build-deployment-api "${1}"
}

remove-image() {
local _image_name="${1}"

Expand Down Expand Up @@ -179,6 +199,14 @@ clean-deployment() {
remove-deployment-tempdir
}

clean-api-deployment() {
log-info "cleaning minikube api deployment..."
if kubectl get ns -o jsonpath='{..name}'| grep "${NAMESPACE}" &> /dev/null; then
log-debug "kubectl delete pods -l 'comp in (worker, api, scheduler)' -n ${NAMESPACE}"
kubectl delete pods -l 'comp in (worker, api, scheduler)' -n "${NAMESPACE}"
fi
}

port-forward() {
local _svc_name=${1}
local _local_port=${2}
Expand Down Expand Up @@ -226,7 +254,9 @@ get-eda-logs() {
#
case ${CMD} in
"build") build-all "${VERSION}" ;;
"build-api") build-api "${VERSION}" ;;
"clean") clean-deployment "${VERSION}";;
"clean-api") clean-api-deployment "${VERSION}";;
"deploy") deploy "${VERSION}" ;;
"port-forward-api") port-forward-api ${PORT} ;;
"port-forward-ui") port-forward-ui ${PORT} ;;
Expand Down
36 changes: 11 additions & 25 deletions src/aap_eda/api/views/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@
from aap_eda.api.serializers.activation import is_activation_valid
from aap_eda.core import models
from aap_eda.core.enums import Action, ActivationStatus, ResourceType
from aap_eda.tasks.ruleset import activate, deactivate, restart
from aap_eda.tasks.orchestrator import (
delete_activation,
restart_activation,
start_activation,
stop_activation,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -76,9 +81,7 @@ def create(self, request):
response = serializer.create(serializer.validated_data)

if response.is_enabled:
job = activate(activation_id=response.id)
response.current_job_id = job.id
response.save(update_fields=["current_job_id"])
start_activation(activation_id=response.id)

return Response(
serializers.ActivationReadSerializer(response).data,
Expand Down Expand Up @@ -117,12 +120,7 @@ def perform_destroy(self, activation):
activation.status = ActivationStatus.DELETING
activation.save(update_fields=["status"])
logger.info(f"Now deleting {activation.name} ...")

deactivate.delay(
activation_id=activation.id,
requester=activation.user.username,
delete=True,
)
delete_activation(activation_id=activation.id)

@extend_schema(
description="List all instances for the Activation",
Expand Down Expand Up @@ -226,11 +224,7 @@ def enable(self, request, pk):
"modified_at",
]
)

job = activate(activation_id=pk)

activation.current_job_id = job.id
activation.save(update_fields=["current_job_id"])
start_activation(activation_id=pk)

return Response(status=status.HTTP_204_NO_CONTENT)

Expand All @@ -256,10 +250,7 @@ def disable(self, request, pk):
activation.save(
update_fields=["is_enabled", "status", "modified_at"]
)

deactivate.delay(
activation_id=activation.id, requester=activation.user.username
)
stop_activation(activation_id=activation.id)

return Response(status=status.HTTP_204_NO_CONTENT)

Expand Down Expand Up @@ -299,12 +290,7 @@ def restart(self, request, pk):
{"errors": error}, status=status.HTTP_400_BAD_REQUEST
)

restart.delay(
activation_id=activation.id, requester=activation.user.username
)

activation.restart_count += 1
activation.save(update_fields=["restart_count", "modified_at"])
restart_activation(activation_id=activation.id)

return Response(status=status.HTTP_204_NO_CONTENT)

Expand Down
Loading

0 comments on commit ba512ae

Please sign in to comment.