Skip to content

Commit

Permalink
Replace users cm with secret; remove unused pvc chart; config model r…
Browse files Browse the repository at this point in the history
…efactoring

Signed-off-by: Francesco Torchia <francesco.torchia@suse.com>
  • Loading branch information
torchiaf committed Dec 28, 2023
1 parent a1e854d commit ec78583
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 60 deletions.
9 changes: 0 additions & 9 deletions helm-charts/code-editor/templates/configmaps.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: code-editor-users
data:
users.yaml: |
users:
{{- $.Values.users | toYaml | nindent 4 }}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: code-editor-templates
data:
Expand Down
4 changes: 2 additions & 2 deletions helm-charts/code-editor/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ spec:
readOnly: true
volumes:
- name: users
configMap:
name: code-editor-users
secret:
secretName: code-editor-users
items:
- key: users.yaml
path: users.yaml
Expand Down
29 changes: 0 additions & 29 deletions helm-charts/code-editor/templates/pvc.yaml

This file was deleted.

10 changes: 10 additions & 0 deletions helm-charts/code-editor/templates/secrets.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
apiVersion: v1
kind: Secret
metadata:
name: code-editor-users
type: Opaque
stringData:
users.yaml: |
users:
{{- .Values.users | toYaml | nindent 4 }}
---
apiVersion: v1
kind: Secret
metadata:
name: secret-ssh-auth
type: Opaque
Expand Down
22 changes: 14 additions & 8 deletions src/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import (
models "server/models"
)

type config struct {
IsDev bool
App string
type app struct {
Name string
Namespace string
Users map[string]models.User
}

type config struct {
IsDev bool
App app
Users map[string]models.User
}

func isDevEnv() bool {
Expand Down Expand Up @@ -39,10 +43,12 @@ func getUsers() map[string]models.User {
func initConfig() config {

c := config{
IsDev: isDevEnv(),
App: utils.IfNull(os.Getenv("APP_NAME"), "code-editor"),
Namespace: utils.IfNull(os.Getenv("APP_NAMESPACE"), "code-editor"),
Users: getUsers(),
IsDev: isDevEnv(),
Users: getUsers(),
App: app{
Name: utils.IfNull(os.Getenv("APP_NAME"), "code-editor"),
Namespace: utils.IfNull(os.Getenv("APP_NAMESPACE"), "code-editor"),
},
}

return c
Expand Down
22 changes: 10 additions & 12 deletions src/server/editor/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ func execCmdOnPod(label string, command string, stdin io.Reader, stdout io.Write
command,
}

pods, err := clientset.CoreV1().Pods(c.Namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: label})
pods, err := clientset.CoreV1().Pods(c.App.Namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: label})
if err != nil || len(pods.Items) == 0 {
e.FailOnError(err, "Pod not found")
return err
}

req := clientset.CoreV1().RESTClient().Post().Resource("pods").Name(pods.Items[0].Name).Namespace(c.Namespace).SubResource("exec")
req := clientset.CoreV1().RESTClient().Post().Resource("pods").Name(pods.Items[0].Name).Namespace(c.App.Namespace).SubResource("exec")

scheme := runtime.NewScheme()
if err := v1.AddToScheme(scheme); err != nil {
Expand All @@ -73,7 +73,7 @@ func execCmdOnPod(label string, command string, stdin io.Reader, stdout io.Write
Stdout: true,
Stderr: false,
TTY: true,
Container: c.App,
Container: c.App.Name,
Command: cmd,
}, parameterCodec)
url := req.URL()
Expand All @@ -99,7 +99,7 @@ func execCmdOnPod(label string, command string, stdin io.Reader, stdout io.Write
}

func waitPodRunning(ctx context.Context, label string) error {
watcher, err := clientset.CoreV1().Pods(c.Namespace).Watch(context.TODO(), metav1.ListOptions{
watcher, err := clientset.CoreV1().Pods(c.App.Namespace).Watch(context.TODO(), metav1.ListOptions{
LabelSelector: label,
})

Expand Down Expand Up @@ -203,7 +203,7 @@ func (editor Editor) Login(port int32, password string) (models.CodeServerSessio
}

func deleteDeployment(user models.User, name string) error {
clientset.AppsV1().Deployments(c.Namespace).Delete(context.TODO(), name, metav1.DeleteOptions{})
clientset.AppsV1().Deployments(c.App.Namespace).Delete(context.TODO(), name, metav1.DeleteOptions{})

return nil
}
Expand Down Expand Up @@ -236,7 +236,7 @@ func (editor Editor) credentialsCreate() *v1.Secret {
"PASSWORD": utils.RandomString(20, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"),
}

clientset.CoreV1().Secrets(c.Namespace).Create(context.TODO(), secret, metav1.CreateOptions{})
clientset.CoreV1().Secrets(editor.namespace).Create(context.TODO(), secret, metav1.CreateOptions{})

return secret
}
Expand Down Expand Up @@ -324,15 +324,15 @@ func (editor Editor) deploymentCreate(service *v1.Service) *corev1.Deployment {
},
}

clientset.AppsV1().Deployments(c.Namespace).Create(context.TODO(), deployment, metav1.CreateOptions{})
clientset.AppsV1().Deployments(c.App.Namespace).Create(context.TODO(), deployment, metav1.CreateOptions{})

return deployment
}

func New(user models.User) Editor {
return Editor{
id: fmt.Sprintf("%s-%s", c.App, user.Name),
namespace: c.Namespace,
id: fmt.Sprintf("%s-%s", c.App.Name, user.Name),
namespace: c.App.Namespace,
label: fmt.Sprintf("app.code-editor/path=%s", user.Path),
path: user.Path,
}
Expand Down Expand Up @@ -363,7 +363,5 @@ func (editor Editor) Config(gitCmd string) error {
}

func (editor Editor) Destroy(user models.User) error {
name := fmt.Sprintf("%s-%s", c.App, user.Name)

return deleteDeployment(user, name)
return deleteDeployment(user, editor.id)
}

0 comments on commit ec78583

Please sign in to comment.