Skip to content

Commit 0a78b49

Browse files
committed
fix : execute preStart devfile events after project-clone initContainer
Change ordering of initContainers to ensure that `project-clone` initContainer is executed before preStart initContainers Signed-off-by: Rohan Kumar <[email protected]>
1 parent f45fd01 commit 0a78b49

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

controllers/workspace/devworkspace_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func (r *DevWorkspaceReconciler) Reconcile(ctx context.Context, req ctrl.Request
355355
if projectClone, err := projects.GetProjectCloneInitContainer(&workspace.Spec.Template, projectCloneOptions, workspace.Config.Routing.ProxyConfig); err != nil {
356356
return r.failWorkspace(workspace, fmt.Sprintf("Failed to set up project-clone init container: %s", err), metrics.ReasonInfrastructureFailure, reqLogger, &reconcileStatus), nil
357357
} else if projectClone != nil {
358-
devfilePodAdditions.InitContainers = append(devfilePodAdditions.InitContainers, *projectClone)
358+
devfilePodAdditions.InitContainers = append([]corev1.Container{*projectClone}, devfilePodAdditions.InitContainers...)
359359
}
360360

361361
// Add ServiceAccount tokens into devfile containers

controllers/workspace/devworkspace_controller_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,39 @@ var _ = Describe("DevWorkspace Controller", func() {
10521052
workspacecontroller.SetupHttpClientsForTesting(getBasicTestHttpClient())
10531053
})
10541054

1055+
It("Ensures preStart initContainers are run after project-clone", func() {
1056+
createDevWorkspace(devWorkspaceName, "test-devworkspace-prestart.yaml")
1057+
defer deleteDevWorkspace(devWorkspaceName)
1058+
devWorkspace := getExistingDevWorkspace(devWorkspaceName)
1059+
workspaceID := devWorkspace.Status.DevWorkspaceId
1060+
1061+
By("Manually making Routing ready to continue")
1062+
markRoutingReady("test-url", common.DevWorkspaceRoutingName(workspaceID))
1063+
1064+
By("Setting the deployment to have 1 ready replica")
1065+
markDeploymentReady(common.DeploymentName(workspaceID))
1066+
1067+
deployment := &appsv1.Deployment{}
1068+
err := k8sClient.Get(ctx, namespacedName(workspaceID, testNamespace), deployment)
1069+
Expect(err).ToNot(HaveOccurred(), "Failed to get DevWorkspace deployment")
1070+
1071+
By("Checking initContainer order in the created Deployment")
1072+
initContainers := deployment.Spec.Template.Spec.InitContainers
1073+
Expect(len(initContainers)).To(BeNumerically(">", 0), "No initContainers found")
1074+
1075+
expectedOrder := []string{
1076+
"project-clone",
1077+
"go-mod-builder",
1078+
"go-builder",
1079+
}
1080+
1081+
for i, name := range expectedOrder {
1082+
if i >= len(initContainers) {
1083+
Fail(fmt.Sprintf("Expected init container %s at position %d, but only %d containers found", name, i, len(initContainers)))
1084+
}
1085+
Expect(initContainers[i].Name).To(Equal(name), fmt.Sprintf("Init container at position %d is not %s", i, name))
1086+
}
1087+
})
10551088
})
10561089

10571090
})
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
apiVersion: workspace.devfile.io/v1alpha2
2+
kind: DevWorkspace
3+
metadata:
4+
labels:
5+
controller.devfile.io/creator: ""
6+
spec:
7+
started: true
8+
template:
9+
projects:
10+
- name: my-app
11+
clonePath: my-app
12+
git:
13+
remotes:
14+
origin: https://github.com/che-samples/web-nodejs-sample.git
15+
checkoutFrom:
16+
revision: main
17+
components:
18+
- name: tools
19+
container:
20+
image: quay.io/devfile/universal-developer-image:ubi8-latest
21+
memoryLimit: 256Mi
22+
mountSources: true
23+
sourceMapping: /projects
24+
- name: go-mod-builder
25+
container:
26+
image: quay.io/devfile/universal-developer-image:ubi8-latest
27+
command:
28+
- "go"
29+
- "mod"
30+
- "download"
31+
- name: go-builder
32+
container:
33+
image: quay.io/devfile/universal-developer-image:ubi8-latest
34+
command:
35+
- "go"
36+
- "build"
37+
- "-buildvcs=false"
38+
- "-o"
39+
- "./main"
40+
commands:
41+
- id: go-mod
42+
apply:
43+
component: go-mod-builder
44+
- id: go-build
45+
apply:
46+
component: go-builder
47+
events:
48+
preStart:
49+
- go-mod
50+
- go-build

0 commit comments

Comments
 (0)