def _spin_up_node(self): # spin up dummy ec2 node self.asg_client.set_desired_capacity(AutoScalingGroupName='dummy-asg', DesiredCapacity=1) response = self.asg_client.describe_auto_scaling_groups() instance_id = response['AutoScalingGroups'][0]['Instances'][0]['InstanceId'] self.dummy_node['metadata']['labels']['aws/id'] = instance_id node = KubeNode(pykube.Node(self.api, self.dummy_node)) node.cordon = mock.Mock(return_value="mocked stuff") node.drain = mock.Mock(return_value="mocked stuff") node.uncordon = mock.Mock(return_value="mocked stuff") node.delete = mock.Mock(return_value="mocked stuff") return node
def create_node(self, pool_name, index): dummy_node = deepcopy(self.dummy_node_ref) node_name = 'k8-{}-16334397-{}'.format(pool_name, index) dummy_node['metadata']['name'] = node_name dummy_node['metadata']['labels']['kubernetes.io/hostname'] = node_name node = KubeNode(pykube.Node(self.api, dummy_node)) return node
def test_get_pending_pods(self): dummy_node = copy.deepcopy(self.dummy_node) dummy_node['metadata']['name'] = 'k8s-agentpool1-16334397-0' node = KubeNode(pykube.Node(self.api, dummy_node)) pod = KubePod(pykube.Pod(self.api, self.dummy_pod)) act = self.cluster.get_pending_pods([pod], [node]) self.assertEqual(len(act), 0) node = KubeNode(pykube.Node(self.api, dummy_node)) pod2 = KubePod(pykube.Pod(self.api, self.dummy_pod)) pod3 = KubePod(pykube.Pod(self.api, self.dummy_pod)) act = self.cluster.get_pending_pods([pod, pod2, pod3], [node]) #only one should fit self.assertEqual(len(act), 2)
def _spin_up_node(self): # spin up dummy ec2 node self.asg_client.set_desired_capacity(AutoScalingGroupName='dummy-asg', DesiredCapacity=1) response = self.asg_client.describe_auto_scaling_groups() instance_id = response['AutoScalingGroups'][0]['Instances'][0]['InstanceId'] self.dummy_node['metadata']['labels']['aws/id'] = instance_id node = KubeNode(pykube.Node(self.api, self.dummy_node)) return node
def test_max_scale_in(self): node1 = copy.deepcopy(self.dummy_node) node2 = copy.deepcopy(self.dummy_node) TestInstance = collections.namedtuple('TestInstance', ['launch_time']) instance1 = TestInstance(datetime.now(pytz.utc)) instance2 = TestInstance(datetime.now(pytz.utc)) for node in [node1, node2]: for condition in node['status']['conditions']: if condition['type'] == 'Ready': condition['status'] = 'Unknown' condition['lastHeartbeatTime'] = datetime.isoformat( datetime.now(pytz.utc) - timedelta(hours=2)) break kube_node1 = KubeNode(pykube.Node(self.api, node1)) kube_node1.delete = mock.Mock(return_value="mocked stuff") kube_node2 = KubeNode(pykube.Node(self.api, node2)) kube_node2.delete = mock.Mock(return_value="mocked stuff") self.cluster.maintain([kube_node1, kube_node2], { kube_node1.instance_id: instance1, kube_node2.instance_id: instance2 }, {}, [], []) kube_node1.delete.assert_not_called() kube_node2.delete.assert_not_called()
def test_reap_dead_node(self): node = copy.deepcopy(self.dummy_node) TestInstance = collections.namedtuple('TestInstance', ['launch_time']) instance = TestInstance(datetime.now(pytz.utc)) ready_condition = None for condition in node['status']['conditions']: if condition['type'] == 'Ready': ready_condition = condition break ready_condition['status'] = 'Unknown' ready_condition['lastHeartbeatTime'] = datetime.isoformat( datetime.now(pytz.utc) - timedelta(minutes=30)) kube_node = KubeNode(pykube.Node(self.api, node)) kube_node.delete = mock.Mock(return_value="mocked stuff") self.cluster.maintain([kube_node], {kube_node.instance_id: instance}, {}, [], []) kube_node.delete.assert_not_called() ready_condition['lastHeartbeatTime'] = datetime.isoformat( datetime.now(pytz.utc) - timedelta(hours=2)) kube_node = KubeNode(pykube.Node(self.api, node)) kube_node.delete = mock.Mock(return_value="mocked stuff") self.cluster.maintain([kube_node], {kube_node.instance_id: instance}, {}, [], []) kube_node.delete.assert_called_once_with()
def create_nodes(self, nb_pool, nb_nodes_per_pool): nodes = [] for pool_idx in range(nb_pool): for node_idx in range(nb_nodes_per_pool): dummy_node = copy.deepcopy(self.dummy_node_ref) node_name = 'k8-agentpool{}-16334397-{}'.format( pool_idx, node_idx) dummy_node['metadata']['name'] = node_name dummy_node['metadata']['labels'][ 'kubernetes.io/hostname'] = node_name node = KubeNode(pykube.Node(self.api, dummy_node)) nodes.append(node) return nodes
def _spin_up_node(self, launch_time=None): # spin up dummy ec2 node self.asg_client.set_desired_capacity(AutoScalingGroupName='dummy-asg', DesiredCapacity=1) response = self.asg_client.describe_auto_scaling_groups() instance_id = response['AutoScalingGroups'][0]['Instances'][0][ 'InstanceId'] self.dummy_node['metadata']['labels']['aws/id'] = instance_id node = KubeNode(pykube.Node(self.api, self.dummy_node)) node.cordon = mock.Mock(return_value="mocked stuff") node.drain = mock.Mock(return_value="mocked stuff") node.uncordon = mock.Mock(return_value="mocked stuff") node.delete = mock.Mock(return_value="mocked stuff") return node
def _spin_up_nodes(self, count, launch_time=None): assert count <= 256 # spin up dummy ec2 node self.asg_client.set_desired_capacity(AutoScalingGroupName='dummy-asg', DesiredCapacity=count) response = self.asg_client.describe_auto_scaling_groups() nodes = [] for i, instance in enumerate(response['AutoScalingGroups'][0]['Instances']): instance_id = instance['InstanceId'] dummy_node = copy.deepcopy(self.dummy_node) dummy_node['metadata']['labels']['aws/id'] = instance_id dummy_node['metadata']['name'] = '10.0.' + str(i) + '.228' node = KubeNode(pykube.Node(self.api, dummy_node)) node.cordon = mock.Mock(return_value="mocked stuff") node.drain = mock.Mock(return_value="mocked stuff") node.uncordon = mock.Mock(return_value="mocked stuff") node.delete = mock.Mock(return_value="mocked stuff") nodes.append(node) return nodes
def test_can_fit(self): pod = KubePod(pykube.Pod(self.api, self.dummy_pod)) node = KubeNode(pykube.Node(self.api, self.dummy_node)) assert node.can_fit(pod.resources)
def create_kube_node(self, node): kube_node = KubeNode(node) kube_node.capacity = capacity.get_capacity_for_instance_type(kube_node.instance_type) return kube_node