Skip to content

Commit

Permalink
add commit action
Browse files Browse the repository at this point in the history
  • Loading branch information
abikouo committed Mar 28, 2023
1 parent afb99da commit f10462e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,9 @@ jobs:
git status
shell: bash
if: steps.black_check.outcome != 'success'

- name: commit and push changes
id: commit
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: "automated changes for changelog and/or black formatting"
51 changes: 43 additions & 8 deletions plugins/module_utils/k8s/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,51 @@ def _prepend_resource_info(resource, msg):
return [_prepend_resource_info(resource, msg) for msg in warnings + errors]


def get_definitions(svc, params):
try:
definitions = create_definitions(params)
except Exception as e:
msg = "Failed to load resource definition: {0}".format(e)
raise CoreException(msg) from e

delete_all = params.get("delete_all")
src = params.get("src")
resource_definition = params.get("resource_definition")
name = params.get("name")
state = params.get("state")

if (
delete_all
and state == "absent"
and name is None
and resource_definition is None
and src is None
):
# Delete all resources in the namespace for the specified resource type
if params.get("kind") is None:
raise CoreException(
"'kind' option is required to specify the resource type."
)

resource = svc.find_resource(
params.get("kind"), params.get("api_version"), fail=True
)
definitions = svc.retrieve_all(
resource,
params.get("namespace"),
params.get("label_selectors"),
)

return definitions


def run_module(module) -> None:
results = []
changed = False
client = get_api_client(module)
svc = K8sService(client, module)
try:
definitions = create_definitions(module.params)
except Exception as e:
msg = "Failed to load resource definition: {0}".format(e)
raise CoreException(msg) from e

definitions = get_definitions(svc, module.params)

for definition in definitions:
result = {"changed": False, "result": {}}
Expand Down Expand Up @@ -130,9 +165,9 @@ def perform_action(svc, definition: Dict, params: Dict) -> Dict:
result["msg"] = (
"resource 'kind={kind},name={name},namespace={namespace}' "
"filtered by label_selectors.".format(
kind=kind,
name=origin_name,
namespace=namespace,
kind=kind,
name=origin_name,
namespace=namespace,
)
)
return result
Expand Down
27 changes: 25 additions & 2 deletions plugins/module_utils/k8s/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,30 @@ def retrieve(self, resource: Resource, definition: Dict) -> ResourceInstance:

return existing

def retrieve_all(
self, resource: Resource, namespace: str, label_selectors: List[str] = None
) -> List[Dict]:
definitions: List[ResourceInstance] = []

try:
params = dict(namespace=namespace)
if label_selectors:
params["label_selector"] = ",".join(label_selectors)
resource_list = self.client.get(resource, **params)
for item in resource_list.items:
existing = self.client.get(
resource, name=item.metadata.name, namespace=namespace
)
definitions.append(existing.to_dict())
except (NotFoundError, MethodNotAllowedError):
pass
except Exception as e:
reason = e.body if hasattr(e, "body") else e
msg = "Failed to retrieve requested object: {0}".format(reason)
raise CoreException(msg) from e

return definitions

def find(
self,
kind: str,
Expand All @@ -231,8 +255,7 @@ def find(
return dict(
resources=[],
msg='Failed to find API for resource with apiVersion "{0}" and kind "{1}"'.format(
api_version,
kind,
api_version, kind
),
api_found=False,
)
Expand Down

0 comments on commit f10462e

Please sign in to comment.