From b81c895162375b9f593b38a6bb7205291b7cc397 Mon Sep 17 00:00:00 2001 From: Mingchen Dai Date: Mon, 14 Oct 2024 05:49:23 +0000 Subject: [PATCH] [feature] git clone repo upon initialization --- api/v1/devcontainerapp_types.go | 2 ++ ...devcontainer.devstar.cn_devcontainerapps.yaml | 3 +++ .../samples/devcontainer_v1_devcontainerapp.yaml | 5 +++-- .../controller/devcontainerapp_controller.go | 10 +++++----- internal/controller/templates/statefulset.yaml | 16 +++++++++++++--- 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/api/v1/devcontainerapp_types.go b/api/v1/devcontainerapp_types.go index 35c9cd2..651a6fb 100644 --- a/api/v1/devcontainerapp_types.go +++ b/api/v1/devcontainerapp_types.go @@ -65,6 +65,8 @@ type StatefulSetSpec struct { Image string `json:"image"` Command []string `json:"command"` + GitRepositoryURL string `json:"gitRepositoryURL"` + // +kubebuilder:validation:MinItems=1 // 至少包含一个 SSH Public Key 才能通过校验规则 SSHPublicKeyList []string `json:"sshPublicKeyList"` diff --git a/config/crd/bases/devcontainer.devstar.cn_devcontainerapps.yaml b/config/crd/bases/devcontainer.devstar.cn_devcontainerapps.yaml index 3478273..a293554 100644 --- a/config/crd/bases/devcontainer.devstar.cn_devcontainerapps.yaml +++ b/config/crd/bases/devcontainer.devstar.cn_devcontainerapps.yaml @@ -74,6 +74,8 @@ spec: containerPort: minimum: 1 type: integer + gitRepositoryURL: + type: string image: type: string sshPublicKeyList: @@ -84,6 +86,7 @@ spec: type: array required: - command + - gitRepositoryURL - image - sshPublicKeyList type: object diff --git a/config/samples/devcontainer_v1_devcontainerapp.yaml b/config/samples/devcontainer_v1_devcontainerapp.yaml index 288ac0f..801a882 100644 --- a/config/samples/devcontainer_v1_devcontainerapp.yaml +++ b/config/samples/devcontainer_v1_devcontainerapp.yaml @@ -8,11 +8,12 @@ metadata: app.kubernetes.io/managed-by: kustomize spec: statefulset: - image: mcr.microsoft.com/devcontainers/base:dev-ubuntu-20.04 + image: devstar.cn/public/base-ssh-devcontainer:ubuntu-20.04-20241014 + gitRepositoryURL: https://gitee.com/daimingchen_gitee/mock-repo command: - /bin/bash - -c - - apt-get update && apt-get install -y openssh-server && service ssh start && apt-get clean && tail -f /dev/null + - tail -f /dev/null containerPort: 22 sshPublicKeyList: - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC7TXcmW9GNAUou+s1PgBcJlZkw0xZ1ZxLktfsVKvsllYhpjYL7CTgUkl+t5GCvUdBvjQkoOGjenHj7VO3fkF43cFWsHPgBqIFMNDCXHfafbw7OCaKSyPb1Bn+y4mypoYhSy6YirGKSnC/Vw32VK9rtGpBuQse6KvJCMq34wOmdt7Em1vWYWxZaAkDNYUjrmY6/7bjSgUYWNlHx/kM8YQ4gluuackotAHaDo1owCK9dc9FZX2XUZcCJZVVRxCr0LRiyQqfO4s/YIUAetDmE0PP/FKc6lotRZGoNS9t1XSrMRVApF8V2+IgWNDMIzqtap/waMSNhSAOZtiyzx/VjLk92Bm8LPYpv3M1gsoQBZlp1lKstxDjhApq/H5pHOFVNGWPGW4rW5XB8F+Yrwg1PUvV/rBolG73BUAQF+V9UpV4SGKRZtzcOyTPwVZYJx9M5zF6NKAi5Dw8HXqDEvm/r6xc2Ak47NCfikdE3Szd4FZHUJrMY24bv1W4eiON7hHul8e8= TempSessionSSHPublicKey diff --git a/internal/controller/devcontainerapp_controller.go b/internal/controller/devcontainerapp_controller.go index 51eba49..a3d5354 100644 --- a/internal/controller/devcontainerapp_controller.go +++ b/internal/controller/devcontainerapp_controller.go @@ -97,11 +97,11 @@ func (r *DevcontainerAppReconciler) Reconcile(ctx context.Context, req ctrl.Requ } logger.Info("DevContainer is READY", "ReadyReplicas", statefulSetInNamespace.Status.ReadyReplicas) } else { - app.Status.Ready = false - if err := r.Status().Update(ctx, app); err != nil { - logger.Error(err, "Failed to un-mark DevcontainerApp.Status.Ready", "DevcontainerApp.Status.Ready", app.Status.Ready) - return ctrl.Result{}, err - } + // app.Status.Ready = false + // if err := r.Status().Update(ctx, app); err != nil { + // logger.Error(err, "Failed to un-mark DevcontainerApp.Status.Ready", "DevcontainerApp.Status.Ready", app.Status.Ready) + // return ctrl.Result{}, err + // } logger.Info("DevContainer is NOT ready", "ReadyReplicas", statefulSetInNamespace.Status.ReadyReplicas) } diff --git a/internal/controller/templates/statefulset.yaml b/internal/controller/templates/statefulset.yaml index 6319e21..38a2f56 100644 --- a/internal/controller/templates/statefulset.yaml +++ b/internal/controller/templates/statefulset.yaml @@ -26,7 +26,7 @@ spec: emptyDir: {} initContainers: - name: init-root-ssh-dir - image: {{.Spec.StatefulSet.Image}} + image: devstar.cn/public/busybox:27a71e19c956 imagePullPolicy: IfNotPresent command: - /bin/sh @@ -36,6 +36,16 @@ spec: volumeMounts: - name: root-ssh-dir mountPath: /root/.ssh + - name: init-git-repo-dir + image: {{.Spec.StatefulSet.Image}} + imagePullPolicy: IfNotPresent + command: + - /bin/sh + - -c + - if [ ! -d '/data/workspace' ]; then git clone {{.Spec.StatefulSet.GitRepositoryURL}} /data/workspace && echo "Git Repository cloned."; else echo "Folder already exists."; fi + volumeMounts: + - name: pvc-devcontainer + mountPath: /data containers: - name: {{.ObjectMeta.Name}} image: {{.Spec.StatefulSet.Image}} @@ -61,7 +71,7 @@ spec: - -c - exec ls ~ failureThreshold: 6 - initialDelaySeconds: 10 + initialDelaySeconds: 5 periodSeconds: 10 successThreshold: 1 timeoutSeconds: 5 @@ -72,7 +82,7 @@ spec: - -c - exec cat /etc/ssh/ssh_host*.pub failureThreshold: 6 - initialDelaySeconds: 10 + initialDelaySeconds: 5 periodSeconds: 10 successThreshold: 1 timeoutSeconds: 5