Skip to content

Commit

Permalink
inject script instead of baking into image
Browse files Browse the repository at this point in the history
  • Loading branch information
clbx committed Jul 12, 2024
1 parent 150d35b commit eb8411a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func main() {
},
}

rootCmd.Flags().StringVarP(&image, "image", "i", "clbx/kubectl-browse-pvc", "Image to mount job to")
rootCmd.Flags().StringVarP(&image, "image", "i", "alpine", "Image to mount job to")
kubeConfigFlags.AddFlags(rootCmd.Flags())

if err := rootCmd.Execute(); err != nil {
Expand Down Expand Up @@ -134,7 +134,7 @@ func browseCommand(kubeConfigFlags *genericclioptions.ConfigFlags, pvcName strin
image: image,
namespace: *kubeConfigFlags.Namespace,
pvc: *targetPvc,
cmd: []string{"/bin/bash", "-c", "--"},
cmd: []string{"/bin/sh", "-c", "--"},
}

// Build the Job
Expand Down Expand Up @@ -208,7 +208,7 @@ func browseCommand(kubeConfigFlags *genericclioptions.ConfigFlags, pvcName strin
Namespace(options.namespace).
SubResource("exec").
VersionedParams(&corev1.PodExecOptions{
Command: []string{"bash", "-c", "cd /mnt && /bin/bash"},
Command: []string{"sh", "-c", "cd /mnt && (ash || bash || sh)"},
Stdin: true,
Stdout: true,
Stderr: true,
Expand Down
27 changes: 23 additions & 4 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ type PodOptions struct {
cmd []string
}

var script = `
base_processes=$(ps aux | grep -E "ash|bash|sh" | grep -v grep | wc -l)
echo "Processes: $base_processes"
sleep 2
while :; do
shell_processes=$(ps aux | grep -E "ash|bash|sh" | grep -v grep | wc -l)
if [ "$shell_processes" -gt "$base_processes" ]; then
echo "Found an additional process"
while [ "$shell_processes" -gt "$base_processes" ]; do
sleep 2
shell_processes=$(ps aux | grep -E "ash|bash|sh" | grep -v grep | wc -l)
done
exit 0
fi
done
`

// Finds if a pod that attached to a PVC
func findPodByPVC(podList corev1.PodList, pvc corev1.PersistentVolumeClaim) *corev1.Pod {
for _, pod := range podList.Items {
Expand All @@ -33,24 +51,25 @@ func buildPvcbGetJob(options PodOptions) *batchv1.Job {

job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "pvcb-edit-" + options.pvc.Name,
Name: "browse-pvc" + options.pvc.Name,
Namespace: options.namespace,
},
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: TTLSecondsAfterFinished,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: "pvcb-edit",
Name: "browse-pvc",
},
Spec: corev1.PodSpec{
RestartPolicy: "Never",
Containers: []corev1.Container{
{
Name: "pvcb-edit",
Name: "browser",
Image: image,
//Command: []string{"/bin/bash", "-c", "--"},
Command: options.cmd,
Args: []string{"/entrypoint.sh"},
//Args: []string{"/entrypoint.sh"},
Args: []string{script},
VolumeMounts: []corev1.VolumeMount{
{
Name: "target-pvc",
Expand Down

0 comments on commit eb8411a

Please sign in to comment.