-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for more kubernetes functionality
- Loading branch information
Showing
9 changed files
with
315 additions
and
5 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
78 changes: 78 additions & 0 deletions
78
libs/scenario_execution_kubernetes/scenario_execution_kubernetes/kubernetes_base_action.py
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,78 @@ | ||
# Copyright (C) 2024 Intel Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions | ||
# and limitations under the License. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from kubernetes import client, config | ||
from enum import Enum | ||
import py_trees | ||
import json | ||
from scenario_execution.actions.base_action import BaseAction | ||
|
||
|
||
class KubernetesBaseActionState(Enum): | ||
IDLE = 1 | ||
REQUEST_SENT = 2 | ||
FAILURE = 3 | ||
|
||
|
||
class KubernetesBaseAction(BaseAction): | ||
|
||
def __init__(self, namespace: str, within_cluster: bool): | ||
super().__init__() | ||
self.namespace = namespace | ||
self.within_cluster = within_cluster | ||
self.client = None | ||
self.current_state = KubernetesBaseActionState.IDLE | ||
self.current_request = None | ||
|
||
def setup(self, **kwargs): | ||
if self.within_cluster: | ||
config.load_incluster_config() | ||
else: | ||
config.load_kube_config() | ||
self.client = client.CoreV1Api() | ||
|
||
def execute(self, namespace: str, within_cluster: bool): | ||
self.namespace = namespace | ||
if within_cluster != self.within_cluster: | ||
raise ValueError("parameter 'within_cluster' is not allowed to change since initialization.") | ||
|
||
def update(self) -> py_trees.common.Status: # pylint: disable=too-many-return-statements | ||
if self.current_state == KubernetesBaseActionState.IDLE: | ||
self.current_request = self.kubernetes_call() | ||
self.current_state = KubernetesBaseActionState.REQUEST_SENT | ||
return py_trees.common.Status.RUNNING | ||
elif self.current_state == KubernetesBaseActionState.REQUEST_SENT: | ||
success = True | ||
if self.current_request.ready(): | ||
if not self.current_request.successful(): | ||
try: | ||
self.current_request.get() | ||
except client.exceptions.ApiException as e: | ||
message = "" | ||
body = json.loads(e.body) | ||
if "message" in body: | ||
message = f", message: '{body['message']}'" | ||
self.feedback_message = f"Failure! Reason: {e.reason} {message}" # pylint: disable= attribute-defined-outside-init | ||
success = False | ||
if success: | ||
return py_trees.common.Status.SUCCESS | ||
else: | ||
return py_trees.common.Status.FAILURE | ||
return py_trees.common.Status.FAILURE | ||
|
||
def kubernetes_call(self): | ||
# Use async_req = True, namespace=self.namespace | ||
raise NotImplementedError("Implement in derived action") |
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
39 changes: 39 additions & 0 deletions
39
libs/scenario_execution_kubernetes/scenario_execution_kubernetes/kubernetes_patch_pod.py
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,39 @@ | ||
# Copyright (C) 2024 Intel Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions | ||
# and limitations under the License. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from ast import literal_eval | ||
from .kubernetes_base_action import KubernetesBaseAction | ||
|
||
|
||
class KubernetesPatchPod(KubernetesBaseAction): | ||
|
||
def __init__(self, namespace: str, target: str, body: str, within_cluster: bool): | ||
super().__init__(namespace, within_cluster) | ||
self.target = target | ||
self.body = None | ||
|
||
def execute(self, namespace: str, target: str, body: str, within_cluster: bool): # pylint: disable=arguments-differ | ||
super().execute(namespace, within_cluster) | ||
self.target = target | ||
trimmed_data = body.encode('utf-8').decode('unicode_escape') | ||
try: | ||
self.body = literal_eval(trimmed_data) | ||
except ValueError as e: | ||
raise ValueError(f"Could not parse body '{trimmed_data}': {e}") from e | ||
|
||
def kubernetes_call(self): | ||
self.feedback_message = f"Requested patching '{self.target}' in namespace '{self.namespace}'" # pylint: disable= attribute-defined-outside-init | ||
return self.client.patch_namespaced_pod(self.target, body=self.body, namespace=self.namespace, async_req=True) |
99 changes: 99 additions & 0 deletions
99
libs/scenario_execution_kubernetes/scenario_execution_kubernetes/kubernetes_pod_exec.py
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,99 @@ | ||
# Copyright (C) 2024 Intel Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions | ||
# and limitations under the License. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import py_trees | ||
from scenario_execution.actions.base_action import BaseAction | ||
import queue | ||
import threading | ||
from kubernetes import client, config, stream | ||
from enum import Enum | ||
|
||
|
||
class KubernetesPodExecState(Enum): | ||
IDLE = 1 | ||
RUNNING = 2 | ||
FAILURE = 3 | ||
|
||
|
||
class KubernetesPodExec(BaseAction): | ||
|
||
def __init__(self, target: str, command: list, namespace: str, within_cluster: bool): | ||
super().__init__() | ||
self.target = target | ||
self.namespace = namespace | ||
self.command = command | ||
self.within_cluster = within_cluster | ||
self.client = None | ||
self.reponse_queue = queue.Queue() | ||
self.current_state = KubernetesPodExecState.IDLE | ||
self.output_queue = queue.Queue() | ||
|
||
def setup(self, **kwargs): | ||
if self.within_cluster: | ||
config.load_incluster_config() | ||
else: | ||
config.load_kube_config() | ||
self.client = client.CoreV1Api() | ||
|
||
self.exec_thread = threading.Thread(target=self.pod_exec, daemon=True) | ||
|
||
def execute(self, target: str, command: list, namespace: str, within_cluster: bool): | ||
if within_cluster != self.within_cluster: | ||
raise ValueError("parameter 'within_cluster' is not allowed to change since initialization.") | ||
self.target = target | ||
self.namespace = namespace | ||
self.command = command | ||
self.current_state = KubernetesPodExecState.IDLE | ||
|
||
def update(self) -> py_trees.common.Status: | ||
if self.current_state == KubernetesPodExecState.IDLE: | ||
self.current_state = KubernetesPodExecState.RUNNING | ||
self.feedback_message = f"Executing on pod '{self.target}': {self.command}..." # pylint: disable= attribute-defined-outside-init | ||
self.exec_thread.start() | ||
return py_trees.common.Status.RUNNING | ||
elif self.current_state == KubernetesPodExecState.RUNNING: | ||
while not self.output_queue.empty(): | ||
self.logger.debug(self.output_queue.get()) | ||
try: | ||
response = self.reponse_queue.get_nowait() | ||
try: | ||
if response.returncode == 0: | ||
self.feedback_message = f"Execution successful." # pylint: disable= attribute-defined-outside-init | ||
return py_trees.common.Status.SUCCESS | ||
except ValueError: | ||
self.feedback_message = f"Error while executing." # pylint: disable= attribute-defined-outside-init | ||
except queue.Empty: | ||
return py_trees.common.Status.RUNNING | ||
|
||
return py_trees.common.Status.FAILURE | ||
|
||
def pod_exec(self): | ||
resp = stream.stream(self.client.connect_get_namespaced_pod_exec, | ||
self.target, | ||
self.namespace, | ||
command=self.command, | ||
stderr=True, stdin=False, | ||
stdout=True, tty=False, | ||
_preload_content=False) | ||
|
||
while resp.is_open(): | ||
resp.update(timeout=0.1) | ||
if resp.peek_stdout(): | ||
self.output_queue.put(resp.read_stdout()) | ||
if resp.peek_stderr(): | ||
self.output_queue.put(resp.read_stderr()) | ||
|
||
self.reponse_queue.put(resp) |
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
14 changes: 14 additions & 0 deletions
14
libs/scenario_execution_kubernetes/scenarios/test_kubernetes_pod_exec.osc
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,14 @@ | ||
import osc.standard.base | ||
import osc.kubernetes | ||
import osc.helpers | ||
|
||
scenario test_kubernetes_create_from_yaml: | ||
timeout(60s) | ||
do serial: | ||
kubernetes_create_from_yaml(yaml_file: "test.yaml") | ||
kubernetes_wait_for_pod_status(target: "test", status: kubernetes_pod_status!running) | ||
kubernetes_patch_pod(target: "test", body: '{\"spec\":{\"containers\":[{\"name\":\"main\", \"resources\":{\"requests\":{\"cpu\":\"200m\"}, \"limits\":{\"cpu\":\"200m\"}}}]}}') | ||
kubernetes_pod_exec(target: "test", command: ['sysbench', 'cpu', 'run']) | ||
kubernetes_patch_pod(target: "test", body: '{\"spec\":{\"containers\":[{\"name\":\"main\", \"resources\":{\"requests\":{\"cpu\":\"800m\"}, \"limits\":{\"cpu\":\"800m\"}}}]}}') | ||
kubernetes_pod_exec(target: "test", command: ['sysbench', 'cpu', 'run']) | ||
kubernetes_delete(target: "test", element_type: kubernetes_element_type!pod) |
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