Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/local kube connection anpl 857 #1017

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ RUN python3 -m venv --system-site-packages dev-packages \
&& dev-packages/bin/pip3 install -U --no-cache-dir pip \
&& dev-packages/bin/pip3 install -r requirements.dev.txt

RUN apt-get update
RUN apt-get install -y curl ca-certificates apt-transport-https gnupg
RUN curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
RUN touch /etc/apt/sources.list.d/kubernetes.list
RUN echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" | tee -a /etc/apt/sources.list.d/kubernetes.list
RUN apt-get update
RUN apt-get install -y kubectl
RUN apt-get install -y awscli
RUN apt-get install -y iputils-ping
RUN apt install iproute2 -y

# RUN echo "kind-control-plane host.docker.internal" > /etc/host.aliases
# RUN echo "export HOSTALIASES=/etc/host.aliases" >> /etc/profile

USER controlpanel
COPY controlpanel controlpanel
COPY docker docker
Expand Down
38 changes: 35 additions & 3 deletions Makefile.local.mk
Original file line number Diff line number Diff line change
@@ -1,33 +1,65 @@
all: help

## docker-login: Authenticate docker with ECR
docker-login:
aws-vault exec admin-data -- aws ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin $(REGISTRY)

## dev-prepare-up: Run migration before doing up
dev-prepare-up:
docker-compose -f docker-compose.yaml -f docker-compose.dev.yaml run migration

## local-prepare-up: Run migration before doing up
local-prepare-up:
docker-compose -f docker-compose.yaml -f docker-compose.dev.yaml run migration

## local-daemon: Startup with docker process in background (to stop afterwards use make clean)
local-daemon: local-prepare-up
docker-compose -f docker-compose.yaml -f docker-compose.dev.yaml up -d frontend_eks

## dev-daemon: Startup with docker process in background (to stop afterwards use make clean)
dev-daemon: dev-prepare-up
docker-compose -f docker-compose.yaml -f docker-compose.dev.yaml up -d frontend

## local-fg: Startup with docker process in foreground
local-fg: local-prepare-up
docker-compose -f docker-compose.yaml -f docker-compose.dev.yaml up frontend_eks

## dev-fg: Startup with docker process in foreground
dev-fg: dev-prepare-up
docker-compose -f docker-compose.yaml -f docker-compose.dev.yaml up frontend

## dev-debug: Startup clean docker process in background, and docker attach to foreground for debugging
dev-debug: clean dev-daemon
docker attach $(shell sh -c "docker-compose ps -q frontend")
# ## dev-debug: Startup clean docker process in background, and docker attach to foreground for debugging
# dev-debug: clean dev-daemon
# docker attach $(shell sh -c "docker-compose ps -q frontend")

## local-attach: Attach to existing running background docker process for purposes of debugging
local-attach:
docker attach $(shell sh -c "docker-compose ps -q frontend_eks")

## dev-attach: Attach to existing running background docker process for purposes of debugging
dev-attach:
docker attach $(shell sh -c "docker-compose ps -q frontend")

## local-py: Start django shell (in the dev-packages context) in new container
local-py:
docker-compose -f docker-compose.yaml -f docker-compose.dev.yaml run frontend_eks sh -c "dev-packages/bin/python manage.py shell"

## dev-py: Start django shell (in the dev-packages context) in new container
dev-py:
docker-compose -f docker-compose.yaml -f docker-compose.dev.yaml run frontend sh -c "dev-packages/bin/python manage.py shell"

## local-run: Start shell in new copy of container
local-run:
docker-compose -f docker-compose.yaml -f docker-compose.dev.yaml run --rm frontend_eks bash

## dev-run: Start shell in new copy of container
dev-run:
docker-compose -f docker-compose.yaml -f docker-compose.dev.yaml run --rm frontend bash

## local-exec: Exec into shell of existing container
local-exec:
docker-compose -f docker-compose.yaml -f docker-compose.dev.yaml exec frontend_eks bash

## dev-exec: Exec into shell of existing container
dev-exec:
docker-compose -f docker-compose.yaml -f docker-compose.dev.yaml exec frontend bash
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# KIND SPIKE BRANCH

For details check [here](./current_status_of_spike.md)


[![Docker Repository on Quay](https://quay.io/repository/mojanalytics/control-panel/status "Docker Repository on Quay")](https://quay.io/repository/mojanalytics/control-panel)

# Analytical Platform Control Panel
Expand Down
27 changes: 14 additions & 13 deletions controlpanel/api/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ def _init_user(self):
f"Username={self.user.slug}"
),
)
helm.upgrade_release(
f"provision-user-{self.user.slug}", # release
f"{settings.HELM_REPO}/provision-user", # chart
f"--namespace={self.k8s_namespace}",
f"--set="
+ (
f"Username={self.user.slug},"
f"Efsvolume={settings.EFS_VOLUME},"
f"OidcDomain={settings.OIDC_DOMAIN},"
f"Email={self.user.email},"
f"Fullname={self.user.name},"
),
)
# helm.upgrade_release(
# f"provision-user-{self.user.slug}", # release
# f"{settings.HELM_REPO}/provision-user", # chart
# f"--namespace={self.k8s_namespace}",
# f"--set="
# + (
# f"Username={self.user.slug},"
# f"Efsvolume={settings.EFS_VOLUME},"
# f"OidcDomain={settings.OIDC_DOMAIN},"
# f"Email={self.user.email},"
# f"Fullname={self.user.name},"
# ),
# )
else:
helm.upgrade_release(
f"init-user-{self.user.slug}", # release
Expand Down Expand Up @@ -485,6 +485,7 @@ def get_deployments(
k8s = KubernetesClient(id_token=id_token)
results = k8s.AppsV1Api.list_namespaced_deployment(user.k8s_namespace)
for deployment in results.items:
# breakpoint()
app_name = deployment.metadata.labels["app"]
_, version = deployment.metadata.labels["chart"].rsplit("-", 1)
if search_name and search_name not in app_name:
Expand Down
4 changes: 2 additions & 2 deletions controlpanel/develop/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from django.urls import path

from .views import develop_index
from .views import is_kube_connected_view


urlpatterns = [
path("", develop_index, name="develop_index"),
path("kube_connected/", is_kube_connected_view, name="is_kube_connected"),
]
66 changes: 42 additions & 24 deletions controlpanel/develop/views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
import json
import subprocess
from os import environ
from typing import List

from kubernetes import client, config

from controlpanel.api.models import User
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import render

from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny
from rest_framework.response import Response

def run_command(command, *args):
env = environ.copy()
bits = command.split()
command = bits[0]
args = bits[1:]
output = subprocess.Popen(
[command, *args],
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
encoding="utf8",
env=env,
)
out, err = output.communicate()
return out, err


def installed_tools(username: str) -> List[str]:
# TODO: Get a list of this user's installed tools and return
# a list of string ["like", "this"]

user = User.objects.get(username=username)
raw_cmd = f"kubectl get tools -n user-{username} -o json"
raw_bits = raw_cmd.split()
command = raw_bits[0]
args = raw_bits[1:]
out, err = run_command(command, *args)
breakpoint()
return []


Expand All @@ -18,27 +50,13 @@ def user_selected_tool(username: str, toolname: str) -> str:
return f"Install {toolname} for {username}"


@login_required()
def develop_index(request):
status = None
tool = None

if request.method == "POST":
data = request.POST

tool = data.get("tool", "")
if not tool:
status = "No tool selected"
else:
status = user_selected_tool(request.user, tool)

return render(
request,
"develop/index.html",
{
"username": request.user,
"status": status,
"tool": tool,
"installed_tools": installed_tools(request.user),
},
)
# @login_required()
@api_view()
@permission_classes([AllowAny])
def is_kube_connected_view(request):
config.load_kube_config()

v1 = client.CoreV1Api()
services = v1.list_namespaced_service("default")
# breakpoint()
return Response(services.to_dict())
2 changes: 1 addition & 1 deletion controlpanel/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
path("metrics", exports.ExportToDjangoView, name="prometheus-django-metrics"),
]

if "controlpanel.develop" in settings.INSTALLED_APPS:
if "controlpanel.develop" in settings.INSTALLED_APPS and settings.DEBUG:
urlpatterns += [
path("develop/", include('controlpanel.develop.urls')),
]
Expand Down
Loading