Beispiel #1
0
    def logs_pod(self, request, *args, **kwargs):
        """
        Return the tail n lines logs of pod.
        """
        user = request.user

        assert 'pid' in self.kwargs
        pid = self.kwargs['pid']
        project = Project.objects.get(id=pid)

        if not check_member_in_project(project, user):
            raise PermissionDenied(
                detail="User {} is not in project {}.".format(
                    user.username, project.name))

        assert 'pod' in self.kwargs
        pod = self.kwargs['pod']
        tailLine = int(request.query_params['tail'])

        kubeclient = KubeClient("http://{}:{}{}".format(
            settings.MASTER_IP, settings.K8S_PORT, settings.K8S_API_PATH))
        logs = kubeclient.get_logs_of_pod(project.name, pod, tailLine)
        # print(logs)

        return Response(data=logs, status=status.HTTP_200_OK)
Beispiel #2
0
    def create(self, request, *args, **kwargs):
        """
        Create project by only admin users.
        """
        self.check_admin_permission()

        project = Project.objects.filter(name=request.data.get('name', None))
        if project:
            raise ValidationError(detail="Already has an project called {}.".
                                  format(request.data['name']))

        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        response = Response(serializer.data,
                            status=status.HTTP_201_CREATED,
                            headers=headers)

        # Create the namespace
        try:
            kubeclient = KubeClient("http://{}:{}{}".format(
                settings.MASTER_IP, settings.K8S_PORT, settings.K8S_API_PATH))
            kubeclient.create_namespace(serializer.data['name'])
        except Exception as e:
            logger.error(e)

        logger.info("User {} create a new project {}.".format(
            request.user.username, serializer.data['name']))

        return response
Beispiel #3
0
    def __init__(self, application):
        self.application = application
        self.application_name = get_application_instance_name(self.application)
        self.namespace = self.application.image.project.name

        self.kubeclient = KubeClient("http://{}:{}{}".format(
            settings.MASTER_IP, settings.K8S_PORT,
            settings.K8S_V1BETA1_API_PATH))
Beispiel #4
0
    def __init__(self, volume):
        self.kubeclient = KubeClient("http://{}:{}{}".format(settings.MASTER_IP,
            settings.K8S_PORT, settings.K8S_API_PATH))

        self.volume = volume
        self.namespace = self.volume.project.name
        self.project_name = self.volume.project.name
        self.volume_name = self.volume.name
        self.capacity = str(self.volume.capacity) + self.volume.capacity_unit
        self.nfs_path = get_volume_nfs_dir(settings.NFS_BASE_DIR,
            self.namespace, self.volume.name)
        self.nfs_server = settings.NFS_IP
Beispiel #5
0
def get_optimal_external_ip(namespace, app_name):
    """
    Return the optimal external ip from all host ips of the application.
    """
    kubeclient = KubeClient("http://{}:{}{}".format(settings.MASTER_IP,
                                                    settings.K8S_PORT,
                                                    settings.K8S_API_PATH))
    label = "app=" + app_name
    hosts = kubeclient.list_host_ips(namespace=namespace, label=label)
    # logger.debug(hosts)
    index = random.randint(0, len(hosts) - 1)
    return hosts[index]
Beispiel #6
0
    def __init__(self,
                 application,
                 min_replicas=-1,
                 max_replicas=-1,
                 cpu_target=-1):
        self.application = application
        self.application_name = get_application_instance_name(self.application)
        self.namespace = self.application.image.project.name
        self.min_replicas = min_replicas
        self.max_replicas = max_replicas
        self.cpu_target = cpu_target

        self.kubeclient = KubeClient("http://{}:{}{}".format(
            settings.MASTER_IP, settings.K8S_PORT,
            settings.K8S_V1BETA1_API_PATH))
Beispiel #7
0
    def pod_lists(self, request, *args, **kwargs):
        """
        Return the pods list of the application with json.
        """
        application = self.get_object()

        if not application:
            raise ValidationError(detail="The application doesn't exist.")

        kubeclient = KubeClient("http://{}:{}{}".format(settings.MASTER_IP,
            settings.K8S_PORT, settings.K8S_API_PATH))
        pods = kubeclient.list_pods(namespace=application.image.project.name,
            label="app={}".format(get_application_instance_name(application)))
        logger.debug(pods)

        return Response(pods, status=status.HTTP_200_OK)
Beispiel #8
0
    def destroy(self, request, *args, **kwargs):
        """
        Destroy an Project instance.
        """
        self.check_admin_permission()

        project = self.get_object()

        # Delete the namespace
        try:
            kubeclient = KubeClient("http://{}:{}{}".format(
                settings.MASTER_IP, settings.K8S_PORT, settings.K8S_API_PATH))
            kubeclient.delete_namespace(project.name)
        except Exception as e:
            logger.error(e)

        logger.info("User {} delete the project {}.".format(
            request.user.username, project.name))

        return super(ProjectViewSet, self).destroy(request, *args, **kwargs)
Beispiel #9
0
    def __init__(self,
                 application,
                 image_name,
                 tcp_ports=None,
                 udp_ports=None,
                 commands=None,
                 args=None,
                 envs=None,
                 is_public=False,
                 volumes=None,
                 min_replicas=None,
                 max_replicas=None,
                 cpu_target=None):
        self.kubeclient = KubeClient("http://{}:{}{}".format(
            settings.MASTER_IP, settings.K8S_PORT, settings.K8S_API_PATH))

        self.application = application
        self.namespace = self.application.image.project.name
        self.application_name = get_application_instance_name(self.application)
        self.image_name = image_name
        self.tcp_ports = tcp_ports
        self.udp_ports = udp_ports
        self.commands = commands
        self.args = args
        self.envs = envs
        self.is_public = is_public
        self.volumes = volumes

        # Compute the resource limit of the application
        resource_limit = self.application.resource_limit
        self.cpu = str(resource_limit.cpu) + resource_limit.cpu_unit
        self.memory = str(resource_limit.memory) + resource_limit.memory_unit

        # autoscaler
        if self.application.is_autoscaler:
            self.min_replicas = int(min_replicas)
            self.max_replicas = int(max_replicas)
            self.cpu_target = int(cpu_target)
            logger.debug(
                str(self.min_replicas) + " " + str(self.max_replicas) + " " +
                str(self.cpu_target))
Beispiel #10
0
    def list_pods(self, request, *args, **kwargs):
        """
        AdminUser search pods for application id.
        """
        user = self.request.user
        if not user.is_staff:
            raise PermissionDenied()

        app_id = request.GET.get('app', 0)
        try:
            application = Application.objects.get(id=app_id)
        except Exception:
            return Response(status=status.HTTP_200_OK)

        kubeclient = KubeClient("http://{}:{}{}".format(
            settings.MASTER_IP, settings.K8S_PORT, settings.K8S_API_PATH))
        pods = kubeclient.list_pods(
            namespace=application.image.project.name,
            label="app={}".format(get_application_instance_name(application)))
        logger.debug(pods)

        return Response(pods, status=status.HTTP_200_OK)
Beispiel #11
0
 def setUp(self):
     self.client = KubeClient("http://192.168.0.10:8080/api/v1/")
Beispiel #12
0
 def test_create_instance(self):
     url = "http://192.168.0.10:8080"
     client = KubeClient(url)
     self.assertEqual(client.base_url, "http://192.168.0.10:8080/")
Beispiel #13
0
 def test_delete_autoscaler(self):
     beta_client = KubeClient(
         "http://192.168.0.10:8080/apis/extensions/v1beta1/")
     res = beta_client.delete_autoscaler('user', 'project0-nginx-test')
     print(res)