From 2217a2f9428732304f973303c6e03cad2d97afd0 Mon Sep 17 00:00:00 2001
From: Simon Ehrenstein <simon.ehrenstein@gmail.com>
Date: Fri, 4 Dec 2020 18:48:06 +0100
Subject: [PATCH] Restructure and possibility to configure namespace

---
 .../random-scheduler/Dockerfile                 |  2 ++
 .../random-scheduler/deployment.yaml            |  3 +++
 .../random-scheduler/rbac.yaml                  |  0
 .../infrastructure/random-scheduler/schedule.sh | 17 +++++++++++++++++
 .../random-scheduler/test.yaml                  |  0
 execution/random-scheduler/schedule.sh          | 15 ---------------
 6 files changed, 22 insertions(+), 15 deletions(-)
 rename execution/{ => infrastructure}/random-scheduler/Dockerfile (93%)
 rename execution/{ => infrastructure}/random-scheduler/deployment.yaml (85%)
 rename execution/{ => infrastructure}/random-scheduler/rbac.yaml (100%)
 create mode 100755 execution/infrastructure/random-scheduler/schedule.sh
 rename execution/{ => infrastructure}/random-scheduler/test.yaml (100%)
 delete mode 100755 execution/random-scheduler/schedule.sh

diff --git a/execution/random-scheduler/Dockerfile b/execution/infrastructure/random-scheduler/Dockerfile
similarity index 93%
rename from execution/random-scheduler/Dockerfile
rename to execution/infrastructure/random-scheduler/Dockerfile
index 4c78f6a20..fadb231bd 100644
--- a/execution/random-scheduler/Dockerfile
+++ b/execution/infrastructure/random-scheduler/Dockerfile
@@ -1,6 +1,8 @@
 FROM ubuntu:bionic
 
 RUN apt-get update
+RUN apt-get dist-upgrade
+
 RUN apt-get install -y curl jq
 
 RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
diff --git a/execution/random-scheduler/deployment.yaml b/execution/infrastructure/random-scheduler/deployment.yaml
similarity index 85%
rename from execution/random-scheduler/deployment.yaml
rename to execution/infrastructure/random-scheduler/deployment.yaml
index 1bbc60a1c..8e79fcdab 100644
--- a/execution/random-scheduler/deployment.yaml
+++ b/execution/infrastructure/random-scheduler/deployment.yaml
@@ -20,3 +20,6 @@ spec:
         - name: random-scheduler
           image: sehrenstein/random-scheduler:latest
           imagePullPolicy: Always
+          env:
+            - name: TARGET_NAMESPACE
+              value: default
diff --git a/execution/random-scheduler/rbac.yaml b/execution/infrastructure/random-scheduler/rbac.yaml
similarity index 100%
rename from execution/random-scheduler/rbac.yaml
rename to execution/infrastructure/random-scheduler/rbac.yaml
diff --git a/execution/infrastructure/random-scheduler/schedule.sh b/execution/infrastructure/random-scheduler/schedule.sh
new file mode 100755
index 000000000..2eec0f18c
--- /dev/null
+++ b/execution/infrastructure/random-scheduler/schedule.sh
@@ -0,0 +1,17 @@
+#!/bin/bash
+
+KUBE_API_SERVER=($(kubectl cluster-info | grep 'Kubernetes master' | awk '/http/ {print $NF}' | awk -F : '{print $1$2}' | awk '{ gsub(/\/\//, "://"); print }'))
+echo "K8's API server: $KUBE_API_SERVER"
+echo "Target Namespace: $TARGET_NAMESPACE"
+while true;
+do
+    for PODNAME in $(kubectl get pods -n $TARGET_NAMESPACE -o json | jq '.items[] | select(.spec.schedulerName == "random-scheduler") | select(.spec.nodeName == null) | .metadata.name' | tr -d '"');
+    do
+        NODES=($(kubectl get nodes -o json | jq '.items[].metadata.name' | tr -d '"'))
+        NUMNODES=${#NODES[@]}
+        CHOSEN=${NODES[$[$RANDOM % $NUMNODES]]}
+        curl --insecure --header "Content-Type:application/json" --request POST --data '{"apiVersion":"v1", "kind": "Binding", "metadata": {"name": "'$PODNAME'"}, "target": {"apiVersion": "v1", "kind": "Node", "name": "'$CHOSEN'"}}' https://10.96.0.1/api/v1/namespaces/$TARGET_NAMESPACE/pods/$PODNAME/binding/
+        echo "Assigned $PODNAME to $CHOSEN"
+    done
+    sleep 1
+done
\ No newline at end of file
diff --git a/execution/random-scheduler/test.yaml b/execution/infrastructure/random-scheduler/test.yaml
similarity index 100%
rename from execution/random-scheduler/test.yaml
rename to execution/infrastructure/random-scheduler/test.yaml
diff --git a/execution/random-scheduler/schedule.sh b/execution/random-scheduler/schedule.sh
deleted file mode 100755
index 51be5c839..000000000
--- a/execution/random-scheduler/schedule.sh
+++ /dev/null
@@ -1,15 +0,0 @@
-#!/bin/bash
-
-KUBE_API_KUBE_API_SERVER=($(kubectl config view -o json | jq '.clusters[0].cluster.server'))
-while true;
-do
-    for PODNAME in $(kubectl get pods -n default -o json | jq '.items[] | select(.spec.schedulerName == "random-scheduler") | select(.spec.nodeName == null) | .metadata.name' | tr -d '"');
-    do
-        NODES=($(kubectl get nodes -o json | jq '.items[].metadata.name' | tr -d '"'))
-        NUMNODES=${#NODES[@]}
-        CHOSEN=${NODES[$[$RANDOM % $NUMNODES]]}
-        curl --header "Content-Type:application/json" --request POST --data '{"apiVersion":"v1", "kind": "Binding", "metadata": {"name": "'$PODNAME'"}, "target": {"apiVersion": "v1", "kind": "Node", "name": "'$CHOSEN'"}}' $KUBE_API_SERVER/api/v1/namespaces/default/pods/$PODNAME/binding/
-        echo "Assigned $PODNAME to $CHOSEN"
-    done
-    sleep 1
-done
\ No newline at end of file
-- 
GitLab