@@ -28,11 +28,11 @@ import (
28
28
29
29
type K8sWorkspaceService struct {
30
30
services.WorkspaceService
31
- clientset * kubernetes.Clientset
32
- workspaceServiceConfig * workspaceconfig.WorkspaceServiceConfig
31
+ clientset * kubernetes.Clientset
32
+ workspaceServiceConfig * workspaceconfig.WorkspaceServiceConfig
33
33
frontendWorkspaceConfig * workspaceconfig.FrontendWorkspaceConfig
34
- k8sControllerClient client.Client
35
- logger * zap.Logger
34
+ k8sControllerClient client.Client
35
+ logger * zap.Logger
36
36
}
37
37
38
38
func (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
115
115
return nil , err
116
116
}
117
117
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
+
118
125
return response , nil
119
126
}
120
127
@@ -146,6 +153,59 @@ func (ws K8sWorkspaceService) CreateFrontendWorkspace(storyHashId, workspaceId s
146
153
return response , nil
147
154
}
148
155
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
+
149
209
func (ws K8sWorkspaceService ) checkAndCreateWorkspacePVC (workspaceId string ) (err error ) {
150
210
ws .logger .Info ("Checking if PVC exists" , zap .String ("workspaceId" , workspaceId ))
151
211
pvc , err := ws .clientset .CoreV1 ().PersistentVolumeClaims (ws .workspaceServiceConfig .WorkspaceNamespace ()).Get (context .Background (), workspaceId , v12.GetOptions {})
0 commit comments