Skip to content

Commit

Permalink
Merge pull request #18 from observatorium/ttl
Browse files Browse the repository at this point in the history
fix: use durations for TTL
  • Loading branch information
squat committed Jun 12, 2024
2 parents f75c0ab + 25c825a commit 57e2544
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
namespace: namespace-provisioner
data:
server: "$SERVER"
ttl: 1h
EOF
kubectl apply -f manifests/example-grants/pods.yaml
kubectl -n namespace-provisioner set image deployment namespace-provisioner namespace-provisioner=quay.io/observatorium/namespace-provisioner:test
Expand All @@ -62,7 +63,7 @@ jobs:
run: |
kubectl -n namespace-provisioner port-forward service/namespace-provisioner 8080 &
until lsof -nP -iTCP:8080 -sTCP:LISTEN >/dev/null; do sleep 1; done
curl localhost:8080/api/v1/namespace?ttl=1 -X POST -H "Authorization: bearer PASSWORD" > kubeconfig
curl localhost:8080/api/v1/namespace?ttl=1s -X POST -H "Authorization: bearer PASSWORD" > kubeconfig
kubectl --kubeconfig kubeconfig get pods
sleep 5
[ $(kubectl get ns | grep np- | wc -l) -eq 0 ]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ The Namespace Provisioner runs an API server over HTTP that exposes two API endp
#### Namespace Creation - POST /api/v1/namespace

The Namespace creation endpoint accepts the following optional query parameters:
1. `ttl`: the time in seconds that the Namespace should exist in the Kubernetes cluster; if 0 is given, then the Namespace Provisioner’s default lifetime is applied.
1. `ttl`: the duration, e.g. `30s`, `5m`, `1h`, that the Namespace should exist in the Kubernetes cluster; if 0 is given, then the Namespace Provisioner’s default lifetime is applied.
All provisioned Namespaces will be labeled with a Unix timestamp equal to the current time plus this duration; and
1. `url`; the URL of the Kubernetes API that the generated Kubeconfig should use.

Expand Down
7 changes: 7 additions & 0 deletions manifests/namespace-provisioner.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ metadata:
app.kubernetes.io/part-of: namespace-provisioner
data:
server: https://kubernetes
ttl: 1h
---
apiVersion: v1
kind: ServiceAccount
Expand Down Expand Up @@ -163,6 +164,7 @@ spec:
- --listen-internal=:9090
- --cluster-role=namespace-provisioner-grant
- --server=$(SERVER)
- --ttl=$(TTL)
- --token=$(TOKEN)
env:
- name: TOKEN
Expand All @@ -175,6 +177,11 @@ spec:
configMapKeyRef:
name: namespace-provisioner
key: server
- name: TTL
valueFrom:
configMapKeyRef:
name: namespace-provisioner
key: ttl
ports:
- containerPort: 8080
name: http
Expand Down
9 changes: 3 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -98,13 +97,12 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
}(start)

ttl := h.ttl
var err error
if r.URL.Query().Has("ttl") {
s, err := strconv.Atoi(r.URL.Query().Get("ttl"))
if err != nil {
if ttl, err = time.ParseDuration(r.URL.Query().Get("ttl")); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
ttl = time.Duration(s) * time.Second
if ttl == 0 || (h.ttl > 0 && ttl > h.ttl) {
ttl = h.ttl
}
Expand Down Expand Up @@ -143,8 +141,7 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
Labels: h.labels,
},
}
sa, err := h.c.CoreV1().ServiceAccounts(namespace).Create(r.Context(), sa, metav1.CreateOptions{})
if err != nil {
if sa, err = h.c.CoreV1().ServiceAccounts(namespace).Create(r.Context(), sa, metav1.CreateOptions{}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
Expand Down

0 comments on commit 57e2544

Please sign in to comment.