Skip to content

Commit

Permalink
fix: backend clientset
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Lauber <jan.lauber@protonmail.ch>
  • Loading branch information
janlauber committed Jun 18, 2023
1 parent e59e45b commit bb9ed8b
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 45 deletions.
8 changes: 8 additions & 0 deletions deploy/clusterrole.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: cluster-admin
rules:
- apiGroups: ["*"]
resources: ["*"]
verbs: ["*"]
12 changes: 12 additions & 0 deletions deploy/clusterrolebinding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kubelab-admin
subjects:
- kind: ServiceAccount
name: kubelab-admin
namespace: kubelab
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io
20 changes: 20 additions & 0 deletions deploy/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: kubelab-ingress
namespace: kubelab
annotations:
ingress.kubernetes.io/force-ssl-redirect: "true"
spec:
ingressClassName: nginx
rules:
- host: kubelab.swisscom.k8s.natron.cloud
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: kubelab-service
port:
number: 8090
11 changes: 11 additions & 0 deletions deploy/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: kubelab
resources:
- ns.yaml
- service.yaml
- statefulset.yaml
- ingress.yaml
- serviceaccount.yaml
- clusterrole.yaml
- clusterrolebinding.yaml
4 changes: 4 additions & 0 deletions deploy/ns.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: kubelab
11 changes: 11 additions & 0 deletions deploy/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: kubelab-service
namespace: kubelab
spec:
selector:
app: kubelab
ports:
- protocol: TCP
port: 8090
5 changes: 5 additions & 0 deletions deploy/serviceaccount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: kubelab-admin
namespace: kubelab
35 changes: 35 additions & 0 deletions deploy/statefulset.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: kubelab-statefulset
namespace: kubelab
spec:
serviceName: "kubelab-service"
replicas: 1
selector:
matchLabels:
app: kubelab
template:
metadata:
labels:
app: kubelab
spec:
serviceAccountName: kubelab-admin
containers:
- name: kubelab
image: ghcr.io/natrontech/kubelab:latest
# image: kubelab:local
ports:
- containerPort: 8090
volumeMounts:
- name: pb-data
mountPath: /app/kubelab/pb_data
volumeClaimTemplates:
- metadata:
name: pb-data
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 10Gi
storageClassName: hcloud-volumes
43 changes: 21 additions & 22 deletions kubelab-backend/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"io"
"log"
"net/http"
Expand Down Expand Up @@ -83,13 +82,13 @@ func main() {
// deploy a new vcluster
helmclient, err := helm.CreateHelmClient(e.Record.GetString("lab"), e.Record.GetString("user"))
if err != nil {
fmt.Println(err)
log.Println(err)
return err
}

err = helm.AddHelmRepositoryToClient(helmclient, "loft-sh", "https://charts.loft.sh")
if err != nil {
fmt.Println(err)
log.Println(err)
return err
}

Expand Down Expand Up @@ -128,7 +127,7 @@ func main() {
// convert to yaml
yamlValuesBytes, err := yaml.Marshal(yamlValues)
if err != nil {
fmt.Println(err)
log.Println(err)
return err
}

Expand All @@ -141,13 +140,13 @@ func main() {
string(string(yamlValuesBytes)),
)
if err != nil {
fmt.Println(err)
log.Println(err)
return err
}

err = k8s.CreateResourceQuota(helm.GetNamespaceName(e.Record.GetString("lab"), e.Record.GetString("user")), env.Config.ResourceName, env.Config.PodsLimit, env.Config.StorageLimit)
if err != nil {
fmt.Println(err)
log.Println(err)
return err
}

Expand All @@ -157,7 +156,7 @@ func main() {
// delete the namespace
err := k8s.DeleteNamespace(helm.GetNamespaceName(e.Record.GetString("lab"), e.Record.GetString("user")))
if err != nil {
fmt.Println(err)
log.Println(err)
return err
}

Expand All @@ -172,37 +171,37 @@ func main() {
// retrieve the exercise
exercise, err = app.Dao().FindRecordById("exercises", e.Record.GetString("exercise"))
if err != nil {
fmt.Println(err)
log.Println(err)
return err
}

// check if the namespace exists
err = k8s.CreateNamespace(helm.GetNamespaceName(exercise.GetString("lab"), e.Record.GetString("user")))
if err != nil {
fmt.Println(err)
log.Println(err)
} else {
return err
}

// check if vcluster pod exists 'vcluster-0'
_, err = k8s.GetPodByName(helm.GetNamespaceName(exercise.GetString("lab"), e.Record.GetString("user")), "vcluster-0")
if err != nil {
fmt.Println(err)
log.Println(err)
return err
}

// get kubeconfig secret called 'vc-vcluster'
var secret *v1.Secret
secret, err = k8s.GetSecretByName(helm.GetNamespaceName(exercise.GetString("lab"), e.Record.GetString("user")), "vc-vcluster")
if err != nil {
fmt.Println(err)
log.Println(err)
return err
}

// get exercise.GetString("bootstrap") this is a url to a bootstrap script over https github raw
bootstrap, err := http.Get(exercise.GetString("bootstrap"))
if err != nil {
fmt.Println(err)
log.Println(err)
return err
}

Expand All @@ -211,13 +210,13 @@ func main() {
// read the body
bootstrapBody, err := io.ReadAll(bootstrap.Body)
if err != nil {
fmt.Println(err)
log.Println(err)
return err
}

check, err := http.Get(exercise.GetString("check"))
if err != nil {
fmt.Println(err)
log.Println(err)
return err
}

Expand All @@ -226,7 +225,7 @@ func main() {
// read the body
checkBody, err := io.ReadAll(check.Body)
if err != nil {
fmt.Println(err)
log.Println(err)
return err
}

Expand All @@ -242,7 +241,7 @@ func main() {
env.Config.AllowedHosts,
)
if err != nil {
fmt.Println(err)
log.Println(err)
}

// create a new service
Expand All @@ -252,7 +251,7 @@ func main() {
8376,
)
if err != nil {
fmt.Println(err)
log.Println(err)
}

// create a new ingress
Expand All @@ -267,7 +266,7 @@ func main() {
// check if deployment is ready
err = k8s.WaitForDeployment(helm.GetNamespaceName(exercise.GetString("lab"), e.Record.GetString("user")), "kubelab-agent-"+exercise.Id)
if err != nil {
fmt.Println(err)
log.Println(err)
}

// sleep for 5 seconds
Expand All @@ -279,27 +278,27 @@ func main() {
// retrieve the exercise
exercise, err = app.Dao().FindRecordById("exercises", e.Record.GetString("exercise"))
if err != nil {
fmt.Println(err)
log.Println(err)
return err
}
// delete the deployment
err = k8s.DeleteDeployment(helm.GetNamespaceName(exercise.GetString("lab"), e.Record.GetString("user")), "kubelab-agent-"+exercise.Id)
if err != nil {
fmt.Println(err)
log.Println(err)
// return err
}

// delete the service
err = k8s.DeleteService(helm.GetNamespaceName(exercise.GetString("lab"), e.Record.GetString("user")), "kubelab-agent-"+exercise.Id)
if err != nil {
fmt.Println(err)
log.Println(err)
// return err
}

// delete the ingress
err = k8s.DeleteIngress(helm.GetNamespaceName(exercise.GetString("lab"), e.Record.GetString("user")), "kubelab-"+exercise.GetString("lab")+"-"+exercise.Id+"-"+e.Record.GetString("user"))
if err != nil {
fmt.Println(err)
log.Println(err)
// return err
}
}
Expand Down
6 changes: 3 additions & 3 deletions kubelab-backend/pkg/env/env.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package env

import (
"fmt"
"log"

"github.com/caarlos0/env/v8"
)
Expand All @@ -19,11 +19,11 @@ var Config config

func Init() {
if err := env.Parse(&Config); err != nil {
fmt.Printf("%+v\n", err)
log.Printf("%+v\n", err)
}

if Config.Local {
fmt.Println("Running in local mode")
log.Println("Running in local mode")
}

if Config.KubelabImage == "" {
Expand Down
12 changes: 12 additions & 0 deletions kubelab-backend/pkg/k8s/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ func Init() {
if err != nil {
panic(err.Error())
}

DiscoveryClient, err = discovery.NewDiscoveryClientForConfig(Kubeconfig)
if err != nil {
panic(err)
}

Clientset, err = kubernetes.NewForConfig(Kubeconfig)
if err != nil {
panic(err)
}

Ctx = context.Background()
}
}

Expand Down
12 changes: 6 additions & 6 deletions kubelab-backend/pkg/k8s/deployment.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package k8s

import (
"fmt"
"log"
"strings"
"time"

Expand All @@ -24,7 +24,7 @@ func CreateDeployment(name string, namespace string, image string, replicas int3
},
}
if _, err := Clientset.CoreV1().ConfigMaps(namespace).Create(Ctx, configMap, metav1.CreateOptions{}); err != nil {
fmt.Println(err)
log.Println(err)
}

// create config maps for scripts
Expand All @@ -39,7 +39,7 @@ func CreateDeployment(name string, namespace string, image string, replicas int3
}

if _, err := Clientset.CoreV1().ConfigMaps(namespace).Create(Ctx, scriptsConfigMap, metav1.CreateOptions{}); err != nil {
fmt.Println(err)
log.Println(err)
}

deployment := &appsv1.Deployment{
Expand Down Expand Up @@ -153,7 +153,7 @@ func CreateDeployment(name string, namespace string, image string, replicas int3

deployment, err := Clientset.AppsV1().Deployments(namespace).Create(Ctx, deployment, metav1.CreateOptions{})
if err != nil {
fmt.Println(err)
log.Println(err)
}

return deployment, nil
Expand All @@ -162,11 +162,11 @@ func CreateDeployment(name string, namespace string, image string, replicas int3
func DeleteDeployment(namespace string, name string) error {
// Delete the configmap first
if err := Clientset.CoreV1().ConfigMaps(namespace).Delete(Ctx, "kubeconfig", metav1.DeleteOptions{}); err != nil {
fmt.Println(err)
log.Println(err)
}

if err := Clientset.CoreV1().ConfigMaps(namespace).Delete(Ctx, "scripts-"+name, metav1.DeleteOptions{}); err != nil {
fmt.Println(err)
log.Println(err)
}

return Clientset.AppsV1().Deployments(namespace).Delete(Ctx, name, metav1.DeleteOptions{})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import Prism from "prismjs";
import toast from "svelte-french-toast";
export let text: string;
export let lang: string;
let language = "javascript";
Expand Down
Loading

0 comments on commit bb9ed8b

Please sign in to comment.