Exemplo n.º 1
0
    def monitor(self) -> None:
        api_nodes_response = call_k8s_api(self.core_api.list_node, watch=False)
        # Filter out names of untainted nodes:
        node_names = [item for item in api_nodes_response.items
                      if not item.spec.unschedulable and (not item.spec.taints or len(item.spec.taints) == 0)]
        api_pods_response = call_k8s_api(self.core_api.list_pod_for_all_namespaces)
        nodes: Dict[str, Node] = {}
        for item in node_names:
            node_name = item.metadata.labels.pop(HOSTNAME_LABEL)
            data_center = item.metadata.labels.pop(DATACENTER_LABEL, "")
            hardware_id = item.metadata.labels.pop(HARDWARE_ID_LABEL, DEFAULT_HARDWARE_ID)
            pod_list: List[str] = []
            node_ip: str = ""
            for pod in api_pods_response.items:
                if pod.spec.node_name == node_name:
                    pod_list.append(pod.metadata.name)
            for address in item.status.addresses:
                if address.type == "InternalIP":
                    node_ip: str = address.address
            assert node_ip != ""
            node = Node(node_name, hardware_id, node_ip, pod_list, data_center)
            node.labels = item.metadata.labels
            nodes[node_name] = node

        namespaces: Dict[str, Namespace] = {}
        api_ns_response = call_k8s_api(self.core_api.list_namespace)
        for item in api_ns_response.items:
            namespace_name = item.metadata.name
            api_deployments_response = call_k8s_api(self.extensions_api.list_namespaced_deployment, namespace=namespace_name)
            deployments: List[str] = []
            for deployment in api_deployments_response.items:
                deployments.append(deployment.metadata.name)
            namespaces[namespace_name] = Namespace(namespace_name, deployments, item.status.phase)

        self._knowledge.update_cluster_state(nodes, namespaces)
Exemplo n.º 2
0
 def execute(self, context: KubernetesExecutionContext) -> bool:
     secret = client.V1Secret(
         data={".dockerconfigjson": self._dockersecret},
         metadata=client.V1ObjectMeta(name=DEFAULT_SECRET_NAME),
         type="kubernetes.io/dockerconfigjson")
     call_k8s_api(context.basic_api.create_namespaced_secret,
                  namespace=self._namespace,
                  body=secret)
     logging.info(f"Docker secret for namespace {self._namespace} created.")
     return True
Exemplo n.º 3
0
    def execute(self, context: KubernetesExecutionContext) -> bool:
        call_k8s_api(context.extensions_api.create_namespaced_deployment,
                     body=self._deployment,
                     namespace=self._namespace)
        logging.info(
            f"Deployment {self._deployment['metadata']['name']} created")

        api_response = call_k8s_api(
            context.basic_api.create_namespaced_service,
            namespace=self._namespace,
            body=self._service)
        self._ip = api_response.spec.cluster_ip
        logging.info(f"Service {self._service['metadata']['name']} created")
        return True
Exemplo n.º 4
0
 def execute(self, context: KubernetesExecutionContext) -> bool:
     api_response = call_k8s_api(
         context.basic_api.create_namespaced_service,
         namespace=self._namespace,
         body=self._service)
     self._ip = api_response.spec.cluster_ip
     logging.info(f"Service created. Status={api_response.status}")
     return True
Exemplo n.º 5
0
 def execute(self, context: KubernetesExecutionContext) -> bool:
     api_response = call_k8s_api(
         context.extensions_api.create_namespaced_deployment,
         body=self._deployment,
         namespace=self._namespace)
     logging.info(
         f"Deployment {self._deployment_name} created. Status={api_response.status}"
     )
     return True
Exemplo n.º 6
0
 def execute(self, context: KubernetesExecutionContext) -> bool:
     namespace = client.V1Namespace()
     namespace.metadata = client.V1ObjectMeta()
     namespace.metadata.name = self._namespace
     api_response = call_k8s_api(context.basic_api.create_namespace,
                                 body=namespace)
     logging.info(
         f'Namespace {self._namespace} created.  Status={api_response.status}'
     )
     return True
Exemplo n.º 7
0
 def execute(self, context: KubernetesExecutionContext) -> bool:
     options = client.V1DeleteOptions()
     options.propagation_policy = 'Background'
     api_response = call_k8s_api(context.basic_api.delete_namespace,
                                 name=self._namespace,
                                 body=options,
                                 propagation_policy='Background',
                                 grace_period_seconds=0)
     logging.info(
         f'Namespace {self._namespace} deleted. status={api_response.status}'
     )
     return True
Exemplo n.º 8
0
 def execute(self, context: KubernetesExecutionContext) -> bool:
     # Delete the corresponding deployment:
     options = client.V1DeleteOptions()
     options.propagation_policy = 'Background'
     api_response = call_k8s_api(
         context.extensions_api.delete_namespaced_deployment,
         name=self._instance.deployment_name(),
         namespace=self._namespace,
         body=options,
         propagation_policy='Background',
         grace_period_seconds=0)
     logging.info(f"Deployment {self._instance.deployment_name()} deleted")
     # Delete the corresponding service:
     api_response = call_k8s_api(
         context.basic_api.delete_namespaced_service,
         name=self._instance.service_name(),
         namespace=self._namespace,
         body=options,
         propagation_policy='Background',
         grace_period_seconds=0)
     logging.info(f"Service {self._instance.service_name()} deleted")
     return True
Exemplo n.º 9
0
 def execute(self, context: KubernetesExecutionContext) -> bool:
     options = client.V1DeleteOptions()
     options.propagation_policy = 'Background'
     api_response = call_k8s_api(
         context.extensions_api.delete_namespaced_deployment,
         name=self._deployment_name,
         namespace=self._namespace,
         body=options,
         propagation_policy='Background',
         grace_period_seconds=0)
     logging.info(
         f"Deployment {self._deployment_name} deleted. Status={api_response.status}"
     )
     return True
Exemplo n.º 10
0
 def execute(self, context: KubernetesExecutionContext) -> bool:
     call_k8s_api(context.basic_api.delete_namespaced_secret,
                  namespace=self._namespace,
                  name=DEFAULT_SECRET_NAME)
     logging.info(f"Docker secret for namespace {self._namespace} deleted.")
     return True