-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MANOPD-70719 Added DaemonSets and Deployments expect
- Loading branch information
Showing
6 changed files
with
310 additions
and
7 deletions.
There are no files selected for viewing
File renamed without changes.
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,46 @@ | ||
# Copyright 2021-2022 NetCracker Technology 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. | ||
|
||
from core.cluster import KubernetesCluster | ||
from kubemarine.kubernetes.object import KubernetesObject | ||
|
||
|
||
class DaemonSet(KubernetesObject): | ||
|
||
def __init__(self, cluster: KubernetesCluster, name=None, namespace=None, obj=None): | ||
super().__init__(cluster, kind='DaemonSet', name=name, namespace=namespace, obj=obj) | ||
|
||
def is_actual_and_ready(self) -> bool: | ||
return self.is_ready() and self.is_up_to_date() | ||
|
||
def is_up_to_date(self) -> bool: | ||
desired_number_scheduled = self._obj.get('status', {}).get('desiredNumberScheduled') | ||
updated_number_scheduled = self._obj.get('status', {}).get('updatedNumberScheduled') | ||
return desired_number_scheduled is not None \ | ||
and updated_number_scheduled is not None \ | ||
and desired_number_scheduled == updated_number_scheduled | ||
|
||
def is_ready(self) -> bool: | ||
desired_number_scheduled = self._obj.get('status', {}).get('desiredNumberScheduled') | ||
number_ready = self._obj.get('status', {}).get('numberReady') | ||
return desired_number_scheduled is not None \ | ||
and number_ready is not None \ | ||
and desired_number_scheduled == number_ready | ||
|
||
def is_scheduled(self) -> bool: | ||
desired_number_scheduled = self._obj.get('status', {}).get('desiredNumberScheduled') | ||
current_number_scheduled = self._obj.get('status', {}).get('currentNumberScheduled') | ||
return desired_number_scheduled is not None \ | ||
and current_number_scheduled is not None \ | ||
and desired_number_scheduled == current_number_scheduled |
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 2021-2022 NetCracker Technology 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. | ||
|
||
from core.cluster import KubernetesCluster | ||
from kubemarine.kubernetes.object import KubernetesObject | ||
|
||
|
||
class Deployment(KubernetesObject): | ||
|
||
def __init__(self, cluster: KubernetesCluster, name=None, namespace=None, obj=None): | ||
super().__init__(cluster, kind='Deployment', name=name, namespace=namespace, obj=obj) | ||
|
||
def is_actual_and_ready(self) -> bool: | ||
return self.is_ready() and self.is_up_to_date() | ||
|
||
def is_up_to_date(self) -> bool: | ||
desired_number_scheduled = self._obj.get('spec', {}).get('replicas') | ||
updated_number_scheduled = self._obj.get('status', {}).get('updatedReplicas') | ||
return desired_number_scheduled is not None \ | ||
and updated_number_scheduled is not None \ | ||
and desired_number_scheduled == updated_number_scheduled | ||
|
||
def is_ready(self) -> bool: | ||
desired_number_scheduled = self._obj.get('spec', {}).get('replicas') | ||
number_ready = self._obj.get('status', {}).get('readyReplicas') | ||
return desired_number_scheduled is not None \ | ||
and number_ready is not None \ | ||
and desired_number_scheduled == number_ready |
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,101 @@ | ||
# Copyright 2021-2022 NetCracker Technology 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. | ||
|
||
from __future__ import annotations | ||
|
||
import io | ||
import json | ||
import uuid | ||
import time | ||
|
||
import yaml | ||
|
||
from core.cluster import KubernetesCluster | ||
|
||
|
||
class KubernetesObject: | ||
def __init__(self, cluster: KubernetesCluster, kind=None, name=None, namespace=None, obj=None): | ||
|
||
self._cluster = cluster | ||
self._reload_t = -1 | ||
|
||
if not kind and not name and not namespace and not obj: | ||
raise RuntimeError('Not enough parameter values to construct the object') | ||
if obj: | ||
self._obj = obj | ||
else: | ||
if not kind or not name or not namespace: | ||
raise RuntimeError('An unsynchronized object has not enough parameters ' | ||
'to be reloaded') | ||
self._obj = { | ||
"kind": kind, | ||
"metadata": { | ||
"name": name, | ||
"namespace": namespace, | ||
} | ||
} | ||
|
||
def __str__(self): | ||
return self.to_yaml() | ||
|
||
@property | ||
def uid(self): | ||
uid = self._obj.get('metadata', {}).get('uid') | ||
if uid: | ||
return uid | ||
|
||
return str(uuid.uuid4()) | ||
|
||
@property | ||
def kind(self): | ||
return self._obj.get('kind').lower() | ||
|
||
@property | ||
def namespace(self): | ||
return self._obj.get('metadata', {}).get('namespace').lower() | ||
|
||
@property | ||
def name(self): | ||
return self._obj.get('metadata', {}).get('name').lower() | ||
|
||
def to_json(self): | ||
return json.dumps(self._obj) | ||
|
||
def to_yaml(self): | ||
return yaml.dump(self._obj) | ||
|
||
def is_reloaded(self): | ||
return self._reload_t > -1 | ||
|
||
def reload(self, master=None, suppress_exceptions=False) -> KubernetesObject: | ||
if not master: | ||
master = self._cluster.nodes['master'].get_any_member() | ||
cmd = f'kubectl get {self.kind} -n {self.namespace} {self.name} -o json' | ||
result = master.sudo(cmd, warn=suppress_exceptions) | ||
self._cluster.log.verbose(result) | ||
if not result.is_any_failed(): | ||
self._obj = json.loads(result.get_simple_out()) | ||
self._reload_t = time.time() | ||
return self | ||
|
||
def apply(self, master=None): | ||
if not master: | ||
master = self._cluster.nodes['master'].get_any_member() | ||
|
||
json_str = self.to_json() | ||
obj_filename = "_".join([self.kind, self.namespace, self.name, self.uid]) + '.json' | ||
obj_path = f'/tmp/{obj_filename}' | ||
|
||
master.put(io.StringIO(json_str), obj_path, sudo=True) | ||
master.sudo(f'kubectl apply -f {obj_path} && sudo rm -f {obj_path}') |
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