Skip to content

Commit

Permalink
feat(scalability): adding first scalability checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Kurz committed Feb 10, 2023
1 parent 04f533c commit 2336ba1
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 9 deletions.
Empty file.
33 changes: 33 additions & 0 deletions hardeneks/cluster_wide/scalability/control_plane.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from ...resources import Resources
from rich.console import Console
from rich.panel import Panel
from rich import print
import kubernetes

console = Console()

def _get_version() -> str:
client = kubernetes.client.VersionApi()
version = client.get_code()
return version

def check_EKS_version(resources: Resources):
version = _get_version()
major = int(version.major)
minor = version.minor
last_char = version.minor[-1]
if last_char == "+":
minor = int(version.minor[:-1])
else:
minor = int(minor)

good = False

if major >= 1 and minor >= 24:
good = True

if good == False:
print(Panel("[red] Current Version == " + version.major + "." + version.minor + "", title="EKS Version Should be greater or equal too 1.24"))
console.print()

return good
21 changes: 12 additions & 9 deletions hardeneks/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,9 @@ rules:
applications:
- check_metrics_server_is_running
- check_vertical_pod_autoscaler_exists
cluster_autoscaling:
cluster_autoscaler:
- check_any_cluster_autoscaler_exists
- ensure_cluster_autoscaler_and_cluster_versions_match
- ensure_cluster_autoscaler_has_autodiscovery_mode
- use_separate_iam_role_for_cluster_autoscaler
- employ_least_privileged_access_cluster_autoscaler_role
- use_managed_nodegroups
scalability:
control_plane:
- check_EKS_version
namespace_based:
security:
iam:
Expand Down Expand Up @@ -79,4 +74,12 @@ rules:
- schedule_replicas_across_nodes
- run_multiple_replicas
- avoid_running_singleton_pods

# data_plane:
# networking:
# vpc_subnets:
# vpc_cni:
# prefix_mode:
# ipv6:
# security_groups:
# custom_networking:
# load_balancing:
35 changes: 35 additions & 0 deletions hardeneks/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,38 @@ def set_resources(self):
.list_namespaced_horizontal_pod_autoscaler(self.namespace)
.items
)

# Needed to convert dict to Map for testing purposes mainly.
# IF there is a better way for this, i'm all for it.
class Map(dict):
"""
Example:
m = Map({'first_name': 'Eduardo'}, last_name='Pool', age=24, sports=['Soccer'])
"""
def __init__(self, *args, **kwargs):
super(Map, self).__init__(*args, **kwargs)
for arg in args:
if isinstance(arg, dict):
for k, v in arg.items():
self[k] = v

if kwargs:
for k, v in kwargs.items():
self[k] = v

def __getattr__(self, attr):
return self.get(attr)

def __setattr__(self, key, value):
self.__setitem__(key, value)

def __setitem__(self, key, value):
super(Map, self).__setitem__(key, value)
self.__dict__.update({key: value})

def __delattr__(self, item):
self.__delitem__(item)

def __delitem__(self, key):
super(Map, self).__delitem__(key)
del self.__dict__[key]
23 changes: 23 additions & 0 deletions tests/test_scalability_control_plane.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest
from hardeneks.resources import Resources
from hardeneks.resources import Map
from unittest.mock import patch

from hardeneks.cluster_wide.scalability.control_plane import (
check_EKS_version
)

@patch("hardeneks.cluster_wide.scalability.control_plane._get_version")
def test_check_EKS_version(mocked_client):
namespaced_resources = Resources(
"some_region", "some_context", "some_cluster", []
)
mocked_client.return_value = Map({'major': '1', 'minor': "23+"})
good = check_EKS_version(namespaced_resources)
assert good == False, "Value should be False"
mocked_client.return_value = Map({'major': '1', 'minor': "24+"})
good = check_EKS_version(namespaced_resources)
assert good == True, "Value should be True"
mocked_client.return_value = Map({'major': '1', 'minor': "24"})
good = check_EKS_version(namespaced_resources)
assert good == True, "Value should be True and Handle without +"

0 comments on commit 2336ba1

Please sign in to comment.