Skip to content

Commit

Permalink
Utilize env variables and helm chart values for configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
iyannsch committed Dec 2, 2024
1 parent 12a500a commit 9951a58
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ This operator is responsible for cleaning up the workspace of theia-ide pods.
It is a Kubernetes operator that cleans unused workspaces periodically.

## Configuration
The operator's timing can be configured in the upper parts of `main.go`.
The following values can be configured via environment variables:
- `K8S_NAMESPACE`: The namespace in which theia-ide pods are running. Default is `theia-prod`.
- `WORKSPACE_TTL`: The time to live of a workspace. Default is 2 weeks (1209600 000 000 000).
- `CHECK_INTERVAL`: The interval at which the operator checks for unused workspaces. Default is 1 hour (1800 000 000 000).

## Deployment
`helm upgrade --install theia-workspace-garbage-collector ./helm -f ./helm/values.yaml`
7 changes: 7 additions & 0 deletions helm/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ spec:
- name: garbage-collector
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
env:
- name: K8S_NAMESPACE
value: {{ .Values.env.K8S_NAMESPACE | quote }}
- name: WORKSPACE_TTL
value: {{ .Values.env.WORKSPACE_TTL | quote}}
- name: CHECK_INTERVAL
value: {{ .Values.env.CHECK_INTERVAL | quote }}
7 changes: 6 additions & 1 deletion helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ image:
pullPolicy: Always

serviceAccount:
name: garbage-collector-sa
name: garbage-collector-sa

env:
K8S_NAMESPACE: "theia-prod"
WORKSPACE_TTL: "1209600000000000"
CHECK_INTERVAL: "1800000000000"
27 changes: 20 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"os"
"os/signal"
"strconv"
"syscall"
"theia-workspace-garbage-collector/api/types/v1beta5"
clientV1Beta5 "theia-workspace-garbage-collector/clientset/v1beta5"
Expand All @@ -18,17 +20,23 @@ Adapted from https://www.martin-helmich.de/en/blog/kubernetes-crd-client.html
@ https://github.com/martin-helmich/kubernetes-crd-example/tree/master
*/

const (
namespace = "theia-prod"
maxDuration = 15 * time.Minute
checkInterval = 1 * time.Minute
)
func getEnv(key, defaultValue string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return defaultValue
}

func main() {

var config *rest.Config
var err error

namespace := getEnv("K8S_NAMESPACE", "theia-prod")
checkInterval, err := time.ParseDuration(getEnv("CHECK_INTERVAL", strconv.FormatInt(int64(30*time.Minute), 10)))
if err != nil {
panic(err.Error())
}

config, err = rest.InClusterConfig()
if err != nil {
panic(err.Error())
Expand Down Expand Up @@ -59,13 +67,18 @@ func main() {
} else {
fmt.Println("Garbage collection completed successfully")
}
fmt.Printf("Will check again in %s\n\n", time.Now().Add(checkInterval).Format(time.TimeOnly))
fmt.Printf("Will check again at %s\n\n", time.Now().Add(checkInterval).Format(time.DateTime))
time.Sleep(checkInterval)
}
}
}

func garbageCollectWorkspaces(clientSet *clientV1Beta5.V1Beta5Client, namespace string) error {
maxDuration, err := time.ParseDuration(getEnv("WORKSPACE_TTL", strconv.FormatInt(int64(14*24*time.Hour), 10)))
if err != nil {
return err
}

workspaces, err := clientSet.Workspaces(namespace).List(metav1.ListOptions{})
if err != nil {
return err
Expand Down

0 comments on commit 9951a58

Please sign in to comment.