Added mirror support to avoid hitting remote constantly
This commit is contained in:
101
devtools/dep-pkg-mirror.yaml
Normal file
101
devtools/dep-pkg-mirror.yaml
Normal file
@@ -0,0 +1,101 @@
|
||||
# Hosts a mirror for dep pkg
|
||||
# kubectl apply -f dep-pkg-mirror.yaml -n [namespace]
|
||||
# kubectl -n [namespace] cp ./packages \ deploy/monok8s-mirror:/usr/share/nginx/html/monok8s/
|
||||
# Fetch helper contract:
|
||||
# DEP_PKG_MIRROR=https://mirror.example.com/monok8s
|
||||
# mirror URL = ${DEP_PKG_MIRROR}/packages/${mirror_path}
|
||||
apiVersion: v1
|
||||
kind: PersistentVolumeClaim
|
||||
metadata:
|
||||
name: dep-pkg-mirror-data
|
||||
spec:
|
||||
accessModes:
|
||||
- ReadWriteOnce
|
||||
resources:
|
||||
requests:
|
||||
storage: 20Gi
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: dep-pkg-mirror-nginx
|
||||
data:
|
||||
default.conf: |
|
||||
server {
|
||||
listen 8080;
|
||||
server_name _;
|
||||
|
||||
root /usr/share/nginx/html;
|
||||
|
||||
autoindex on;
|
||||
autoindex_exact_size off;
|
||||
autoindex_localtime on;
|
||||
|
||||
location /monok8s/packages/ {
|
||||
try_files $uri =404;
|
||||
}
|
||||
|
||||
location = /healthz {
|
||||
access_log off;
|
||||
return 200 "ok\n";
|
||||
}
|
||||
}
|
||||
---
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: dep-pkg-mirror
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: dep-pkg-mirror
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: dep-pkg-mirror
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx:1.27-alpine
|
||||
imagePullPolicy: IfNotPresent
|
||||
ports:
|
||||
- name: http
|
||||
containerPort: 8080
|
||||
readinessProbe:
|
||||
httpGet:
|
||||
path: /healthz
|
||||
port: http
|
||||
livenessProbe:
|
||||
httpGet:
|
||||
path: /healthz
|
||||
port: http
|
||||
volumeMounts:
|
||||
- name: data
|
||||
mountPath: /usr/share/nginx/html/monok8s/packages
|
||||
subPath: packages
|
||||
readOnly: false
|
||||
- name: nginx-conf
|
||||
mountPath: /etc/nginx/conf.d/default.conf
|
||||
subPath: default.conf
|
||||
readOnly: true
|
||||
volumes:
|
||||
- name: data
|
||||
persistentVolumeClaim:
|
||||
claimName: dep-pkg-mirror-data
|
||||
- name: nginx-conf
|
||||
configMap:
|
||||
name: dep-pkg-mirror-nginx
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: dep-pkg-mirror
|
||||
spec:
|
||||
type: ClusterIP
|
||||
selector:
|
||||
app: dep-pkg-mirror
|
||||
ports:
|
||||
- name: http
|
||||
port: 80
|
||||
targetPort: http
|
||||
80
devtools/push-dep-pkg-mirror.sh
Executable file
80
devtools/push-dep-pkg-mirror.sh
Executable file
@@ -0,0 +1,80 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
NAMESPACE="${NAMESPACE:-default}"
|
||||
PACKAGES_DIR="$(realpath "$SCRIPT_DIR/../packages/")"
|
||||
APP_LABEL="${APP_LABEL:-app=dep-pkg-mirror}"
|
||||
CONTAINER="${CONTAINER:-nginx}"
|
||||
REMOTE_DIR="${REMOTE_DIR:-/usr/share/nginx/html/monok8s/packages}"
|
||||
|
||||
if [ ! -d "$PACKAGES_DIR" ]; then
|
||||
echo "error: package dir not found: $PACKAGES_DIR" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
need_cmd() {
|
||||
command -v "$1" >/dev/null 2>&1 || {
|
||||
echo "error: missing command: $1" >&2
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
need_cmd kubectl
|
||||
need_cmd mktemp
|
||||
need_cmd tar
|
||||
need_cmd cp
|
||||
need_cmd find
|
||||
|
||||
pod="$(
|
||||
kubectl -n "$NAMESPACE" get pod \
|
||||
-l "$APP_LABEL" \
|
||||
--field-selector=status.phase=Running \
|
||||
-o jsonpath='{.items[0].metadata.name}'
|
||||
)"
|
||||
|
||||
if [ -z "$pod" ]; then
|
||||
echo "error: no running pod found with label: $APP_LABEL in namespace: $NAMESPACE" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "using pod: $pod"
|
||||
echo "remote dir: $REMOTE_DIR"
|
||||
|
||||
echo "checking remote dir is writable"
|
||||
kubectl -n "$NAMESPACE" exec "$pod" -c "$CONTAINER" -- sh -c "
|
||||
mkdir -p '$REMOTE_DIR' &&
|
||||
touch '$REMOTE_DIR/.write-test' &&
|
||||
rm -f '$REMOTE_DIR/.write-test'
|
||||
"
|
||||
|
||||
stage="$(mktemp -d)"
|
||||
cleanup() {
|
||||
rm -rf "$stage"
|
||||
}
|
||||
trap cleanup EXIT INT TERM
|
||||
|
||||
mkdir -p "$stage/packages"
|
||||
|
||||
echo "staging $PACKAGES_DIR/ as packages/"
|
||||
tar \
|
||||
--exclude='.DS_Store' \
|
||||
--exclude='.stamp-*' \
|
||||
-C "$PACKAGES_DIR" \
|
||||
-cf - . | tar -C "$stage/packages" -xf -
|
||||
|
||||
echo "copying staged packages into mirror pod"
|
||||
kubectl -n "$NAMESPACE" cp \
|
||||
"$stage/packages/." \
|
||||
"$pod:$REMOTE_DIR" \
|
||||
-c "$CONTAINER"
|
||||
|
||||
echo "done"
|
||||
echo
|
||||
echo "Mirror base URL:"
|
||||
echo " DEP_PKG_MIRROR=http://dep-pkg-mirror.${NAMESPACE}.svc.cluster.local/monok8s"
|
||||
echo
|
||||
echo "Example:"
|
||||
echo " packages/kubernetes/kubelet-v1.35.3"
|
||||
echo " -> http://dep-pkg-mirror.${NAMESPACE}.svc.cluster.local/monok8s/packages/kubernetes/kubelet-v1.35.3"
|
||||
Reference in New Issue
Block a user