@@ -28,11 +28,11 @@ import (
2828
2929type K8sWorkspaceService struct {
3030 services.WorkspaceService
31- clientset * kubernetes.Clientset
32- workspaceServiceConfig * workspaceconfig.WorkspaceServiceConfig
31+ clientset * kubernetes.Clientset
32+ workspaceServiceConfig * workspaceconfig.WorkspaceServiceConfig
3333 frontendWorkspaceConfig * workspaceconfig.FrontendWorkspaceConfig
34- k8sControllerClient client.Client
35- logger * zap.Logger
34+ k8sControllerClient client.Client
35+ logger * zap.Logger
3636}
3737
3838func (ws K8sWorkspaceService ) CreateWorkspace (workspaceId string , backendTemplate string , frontendTemplate * string , remoteURL string , gitnessUser string , gitnessToken string ) (* dto.WorkspaceDetails , error ) {
@@ -115,6 +115,13 @@ func (ws K8sWorkspaceService) CreateWorkspace(workspaceId string, backendTemplat
115115 return nil , err
116116 }
117117
118+ // create terminal for workspace
119+ err = ws .CreateWorkspaceTerminal (workspaceId )
120+ if err != nil {
121+ ws .logger .Error ("Failed to create workspace terminal" , zap .Error (err ))
122+ return nil , err
123+ }
124+
118125 return response , nil
119126}
120127
@@ -146,6 +153,59 @@ func (ws K8sWorkspaceService) CreateFrontendWorkspace(storyHashId, workspaceId s
146153 return response , nil
147154}
148155
156+ func (ws K8sWorkspaceService ) CreateWorkspaceTerminal (workspaceId string ) error {
157+
158+ terminalId := workspaceId + "-terminal"
159+
160+ u := & unstructured.Unstructured {}
161+ u .Object = map [string ]interface {}{
162+ "metadata" : map [string ]interface {}{
163+ "name" : terminalId ,
164+ "namespace" : "argocd" ,
165+ "finalizers" : []string {
166+ "resources-finalizer.argocd.argoproj.io" ,
167+ },
168+ },
169+ "spec" : map [string ]interface {}{
170+ "project" : ws .workspaceServiceConfig .WorkspaceProject (),
171+ "source" : map [string ]interface {}{
172+ "repoURL" : ws .workspaceServiceConfig .ArgoRepoUrl (),
173+ "targetRevision" : "HEAD" ,
174+ "path" : "supercoder/terminal" ,
175+ "helm" : map [string ]interface {}{
176+ "valueFiles" : []string {
177+ ws .workspaceServiceConfig .WorkspaceValuesFileName (),
178+ },
179+ },
180+ },
181+ "destination" : map [string ]interface {}{
182+ "server" : "https://kubernetes.default.svc" ,
183+ "namespace" : ws .workspaceServiceConfig .WorkspaceNamespace (),
184+ },
185+ "syncPolicy" : map [string ]interface {}{
186+ "syncOptions" : []string {"PruneLast=true" },
187+ "automated" : map [string ]interface {}{
188+ "prune" : true ,
189+ "selfHeal" : true ,
190+ "allowEmpty" : true ,
191+ },
192+ },
193+ },
194+ }
195+ u .SetGroupVersionKind (schema.GroupVersionKind {
196+ Group : "argoproj.io" ,
197+ Kind : "Application" ,
198+ Version : "v1alpha1" ,
199+ })
200+ err := ws .k8sControllerClient .Create (context .Background (), u )
201+ if err != nil {
202+ _ = ws .k8sControllerClient .Delete (context .Background (), u , & client.DeleteOptions {})
203+ ws .logger .Error ("Failed to create workspace" , zap .Error (err ))
204+ return err
205+ }
206+ return nil
207+ }
208+
149209func (ws K8sWorkspaceService ) checkAndCreateWorkspacePVC (workspaceId string ) (err error ) {
150210 ws .logger .Info ("Checking if PVC exists" , zap .String ("workspaceId" , workspaceId ))
151211 pvc , err := ws .clientset .CoreV1 ().PersistentVolumeClaims (ws .workspaceServiceConfig .WorkspaceNamespace ()).Get (context .Background (), workspaceId , v12.GetOptions {})
0 commit comments