Skip to content

Commit 122d1c8

Browse files
committed
preparation for ephemeral mode
1 parent 1b4690e commit 122d1c8

File tree

5 files changed

+45
-20
lines changed

5 files changed

+45
-20
lines changed

docker/Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
FROM debian:stable-slim
22

33
RUN apt-get update && \
4-
apt-get install -y openssh-server && \
4+
apt-get install -y openssh-server openssh-client && \
55
mkdir /var/run/sshd && \
66
mkdir /volume
77

88
COPY entrypoint.sh /entrypoint.sh
99
COPY sshd_config /etc/ssh/sshd_config
1010
RUN chmod +x /entrypoint.sh
1111

12-
EXPOSE 22
12+
EXPOSE 2137
1313

1414
ENTRYPOINT ["/entrypoint.sh"]

docker/entrypoint.sh

+19-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,23 @@ if [ -n "$SSH_KEY" ]; then
66
echo "Public key added to /root/.ssh/authorized_keys"
77
fi
88

9-
# Redirect sshd logs to stdout
10-
mkdir -p /var/log/sshd
11-
touch /var/log/sshd/sshd.log
12-
ln -sf /dev/stdout /var/log/sshd/sshd.log
9+
# Check the ROLE environment variable
10+
case "$ROLE" in
11+
standalone)
12+
echo "Running as standalone"
13+
/usr/sbin/sshd -D -e
14+
;;
15+
proxy)
16+
echo "Running as proxy"
17+
/usr/sbin/sshd -D -e
18+
;;
19+
ephemeral)
20+
echo "Running as ephemeral"
21+
/usr/sbin/sshd -D-e
22+
;;
23+
*)
24+
echo "Running default..."
25+
/usr/sbin/sshd -D -e
26+
;;
27+
esac
1328

14-
/usr/sbin/sshd -D -e

docker/sshd_config

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# /etc/ssh/sshd_config
2+
Port 2137
23
PermitRootLogin prohibit-password
34
PasswordAuthentication no
45
ChallengeResponseAuthentication no

pkg/plugin/clean.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func Clean(namespace, pvcName, localMountPoint string) error {
4949
port := podList.Items[0].Labels["portNumber"]
5050

5151
// Kill the port-forward process
52-
pkillCmd := exec.Command("pkill", "-f", fmt.Sprintf("kubectl port-forward pod/%s %s:22", podName, port))
52+
pkillCmd := exec.Command("pkill", "-f", fmt.Sprintf("kubectl port-forward pod/%s %s:2137", podName, port))
5353
pkillCmd.Stdout = os.Stdout
5454
pkillCmd.Stderr = os.Stderr
5555
if err := pkillCmd.Run(); err != nil {

pkg/plugin/mount.go

+22-12
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func Mount(namespace, pvcName, localMountPoint string) error {
4646
return err
4747
}
4848

49-
podName, port, err := setupPod(clientset, namespace, pvcName, sshKey)
49+
podName, port, err := setupPod(clientset, namespace, pvcName, sshKey, "standalone")
5050
if err != nil {
5151
return err
5252
}
@@ -115,9 +115,9 @@ func checkPVCUsage(clientset *kubernetes.Clientset, namespace, pvcName string) (
115115
return pvc, nil
116116
}
117117

118-
func setupPod(clientset *kubernetes.Clientset, namespace, pvcName, sshKey string) (string, int, error) {
118+
func setupPod(clientset *kubernetes.Clientset, namespace, pvcName, sshKey, role string) (string, int, error) {
119119
podName, port := generatePodNameAndPort(pvcName)
120-
pod := createPodSpec(podName, port, pvcName, sshKey)
120+
pod := createPodSpec(podName, port, pvcName, sshKey, role)
121121
if _, err := clientset.CoreV1().Pods(namespace).Create(context.TODO(), pod, metav1.CreateOptions{}); err != nil {
122122
return "", 0, fmt.Errorf("failed to create pod: %v", err)
123123
}
@@ -141,7 +141,7 @@ func waitForPodReady(clientset *kubernetes.Clientset, namespace, podName string)
141141
}
142142

143143
func setupPortForwarding(namespace, podName string, port int) error {
144-
cmd := exec.Command("kubectl", "port-forward", fmt.Sprintf("pod/%s", podName), fmt.Sprintf("%d:22", port), "-n", namespace)
144+
cmd := exec.Command("kubectl", "port-forward", fmt.Sprintf("pod/%s", podName), fmt.Sprintf("%d:2137", port), "-n", namespace)
145145
cmd.Stdout = os.Stdout
146146
cmd.Stderr = os.Stderr
147147
if err := cmd.Start(); err != nil {
@@ -170,7 +170,22 @@ func generatePodNameAndPort(pvcName string) (string, int) {
170170
return podName, port
171171
}
172172

173-
func createPodSpec(podName string, port int, pvcName, sshKey string) *corev1.Pod {
173+
func createPodSpec(podName string, port int, pvcName, sshKey, role string) *corev1.Pod {
174+
envVars := []corev1.EnvVar{
175+
{
176+
Name: "SSH_KEY",
177+
Value: sshKey,
178+
},
179+
}
180+
181+
// Add the ROLE environment variable if the role is "standalone"
182+
if role == "standalone" {
183+
envVars = append(envVars, corev1.EnvVar{
184+
Name: "ROLE",
185+
Value: "standalone",
186+
})
187+
}
188+
174189
return &corev1.Pod{
175190
ObjectMeta: metav1.ObjectMeta{
176191
Name: podName,
@@ -187,7 +202,7 @@ func createPodSpec(podName string, port int, pvcName, sshKey string) *corev1.Pod
187202
Image: "bfenski/volume-exposer:latest",
188203
Ports: []corev1.ContainerPort{
189204
{
190-
ContainerPort: 22,
205+
ContainerPort: 2137,
191206
},
192207
},
193208
VolumeMounts: []corev1.VolumeMount{
@@ -196,12 +211,7 @@ func createPodSpec(podName string, port int, pvcName, sshKey string) *corev1.Pod
196211
Name: "my-pvc",
197212
},
198213
},
199-
Env: []corev1.EnvVar{
200-
{
201-
Name: "SSH_KEY",
202-
Value: sshKey,
203-
},
204-
},
214+
Env: envVars,
205215
},
206216
},
207217
Volumes: []corev1.Volume{

0 commit comments

Comments
 (0)