Skip to content

Commit da28e7d

Browse files
authored
Delete previous pod and creates new ond when modifying devfile on podman (#6802)
* Delete previous pod and creates new ond when modifying devfile on podman * Add integration tests
1 parent 5459326 commit da28e7d

File tree

5 files changed

+56
-87
lines changed

5 files changed

+56
-87
lines changed

pkg/dev/podmandev/pod.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ func createPodFromComponent(
4343
customForwardedPorts []api.ForwardedPort,
4444
usedPorts []int,
4545
customAddress string,
46+
devfileObj parser.DevfileObj,
4647
) (*corev1.Pod, []api.ForwardedPort, error) {
4748
var (
4849
appName = odocontext.GetApplication(ctx)
4950
componentName = odocontext.GetComponentName(ctx)
50-
devfileObj = odocontext.GetDevfileObj(ctx)
5151
workingDir = odocontext.GetWorkingDirectory(ctx)
5252
)
5353

54-
podTemplate, err := generator.GetPodTemplateSpec(*devfileObj, generator.PodTemplateParams{})
54+
podTemplate, err := generator.GetPodTemplateSpec(devfileObj, generator.PodTemplateParams{})
5555
if err != nil {
5656
return nil, nil, err
5757
}
@@ -61,7 +61,7 @@ func createPodFromComponent(
6161
}
6262

6363
var fwPorts []api.ForwardedPort
64-
fwPorts, err = getPortMapping(*devfileObj, debug, randomPorts, usedPorts, customForwardedPorts, customAddress)
64+
fwPorts, err = getPortMapping(devfileObj, debug, randomPorts, usedPorts, customForwardedPorts, customAddress)
6565
if err != nil {
6666
return nil, nil, err
6767
}
@@ -88,7 +88,7 @@ func createPodFromComponent(
8888
},
8989
}
9090

91-
devfileVolumes, err := storage.ListStorage(*devfileObj)
91+
devfileVolumes, err := storage.ListStorage(devfileObj)
9292
if err != nil {
9393
return nil, nil, err
9494
}
@@ -108,7 +108,7 @@ func createPodFromComponent(
108108
}
109109
}
110110

111-
containers, err = utils.UpdateContainersEntrypointsIfNeeded(*devfileObj, containers, buildCommand, runCommand, debugCommand)
111+
containers, err = utils.UpdateContainersEntrypointsIfNeeded(devfileObj, containers, buildCommand, runCommand, debugCommand)
112112
if err != nil {
113113
return nil, nil, err
114114
}

pkg/dev/podmandev/pod_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,7 @@ func Test_createPodFromComponent(t *testing.T) {
14231423
tt.args.customForwardedPorts,
14241424
[]int{20001, 20002, 20003, 20004, 20005},
14251425
tt.args.customAddress,
1426+
devfileObj,
14261427
)
14271428
if (err != nil) != tt.wantErr {
14281429
t.Errorf("createPodFromComponent() error = %v, wantErr %v", err, tt.wantErr)

pkg/dev/podmandev/podmandev.go

+5-36
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package podmandev
22

33
import (
4-
"bytes"
54
"context"
6-
"encoding/json"
75
"fmt"
86
"strings"
97

108
devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
11-
"github.com/devfile/library/v2/pkg/devfile/parser"
129
"k8s.io/klog"
1310

1411
"github.com/redhat-developer/odo/pkg/dev"
@@ -17,7 +14,6 @@ import (
1714
"github.com/redhat-developer/odo/pkg/devfile/location"
1815
"github.com/redhat-developer/odo/pkg/exec"
1916
"github.com/redhat-developer/odo/pkg/libdevfile"
20-
"github.com/redhat-developer/odo/pkg/log"
2117
odocontext "github.com/redhat-developer/odo/pkg/odo/context"
2218
"github.com/redhat-developer/odo/pkg/podman"
2319
"github.com/redhat-developer/odo/pkg/portForward"
@@ -164,39 +160,12 @@ func (o *DevClient) checkVolumesFree(pod *corev1.Pod) error {
164160
}
165161

166162
func (o *DevClient) watchHandler(ctx context.Context, pushParams common.PushParameters, componentStatus *watch.ComponentStatus) error {
167-
pushParams.Devfile = *odocontext.GetDevfileObj(ctx) // TOO reload devfile from disk
168-
printWarningsOnDevfileChanges(ctx, pushParams.StartOptions)
169-
return o.reconcile(ctx, pushParams, componentStatus)
170-
}
171163

172-
func printWarningsOnDevfileChanges(ctx context.Context, options dev.StartOptions) {
173-
var warning string
174-
currentDevfile := odocontext.GetDevfileObj(ctx)
175-
newDevfile, err := devfile.ParseAndValidateFromFileWithVariables(location.DevfileLocation(""), options.Variables)
164+
devObj, err := devfile.ParseAndValidateFromFileWithVariables(location.DevfileLocation(""), pushParams.StartOptions.Variables)
176165
if err != nil {
177-
warning = fmt.Sprintf("error while reading the Devfile. Please restart 'odo dev' if you made any changes to the Devfile. Error message is: %v", err)
178-
} else {
179-
devfileEquals := func(d1, d2 parser.DevfileObj) (bool, error) {
180-
// Compare two Devfile objects by comparing the result of their JSON encoding,
181-
// because reflect.DeepEqual does not work properly with the parser.DevfileObj structure.
182-
d1Json, jsonErr := json.Marshal(d1.Data)
183-
if jsonErr != nil {
184-
return false, jsonErr
185-
}
186-
d2Json, jsonErr := json.Marshal(d2.Data)
187-
if jsonErr != nil {
188-
return false, jsonErr
189-
}
190-
return bytes.Equal(d1Json, d2Json), nil
191-
}
192-
equal, eqErr := devfileEquals(*currentDevfile, newDevfile)
193-
if eqErr != nil {
194-
klog.V(5).Infof("error while checking if Devfile has changed: %v", eqErr)
195-
} else if !equal {
196-
warning = "Detected changes in the Devfile, but this is not supported yet on Podman. Please restart 'odo dev' for such changes to be applied."
197-
}
198-
}
199-
if warning != "" {
200-
log.Fwarning(options.Out, warning+"\n")
166+
return fmt.Errorf("unable to read devfile: %w", err)
201167
}
168+
pushParams.Devfile = devObj
169+
170+
return o.reconcile(ctx, pushParams, componentStatus)
202171
}

pkg/dev/podmandev/reconcile.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (o *DevClient) reconcile(
5050
return err
5151
}
5252

53-
pod, fwPorts, err := o.deployPod(ctx, options)
53+
pod, fwPorts, err := o.deployPod(ctx, options, devfileObj)
5454
if err != nil {
5555
return err
5656
}
@@ -197,7 +197,7 @@ func (o *DevClient) buildPushAutoImageComponents(ctx context.Context, devfileObj
197197
}
198198

199199
// deployPod deploys the component as a Pod in podman
200-
func (o *DevClient) deployPod(ctx context.Context, options dev.StartOptions) (*corev1.Pod, []api.ForwardedPort, error) {
200+
func (o *DevClient) deployPod(ctx context.Context, options dev.StartOptions, devfileObj parser.DevfileObj) (*corev1.Pod, []api.ForwardedPort, error) {
201201

202202
spinner := log.Spinner("Deploying pod")
203203
defer spinner.End(false)
@@ -213,6 +213,7 @@ func (o *DevClient) deployPod(ctx context.Context, options dev.StartOptions) (*c
213213
options.CustomForwardedPorts,
214214
o.usedPorts,
215215
options.CustomAddress,
216+
devfileObj,
216217
)
217218
if err != nil {
218219
return nil, nil, err
@@ -225,6 +226,14 @@ func (o *DevClient) deployPod(ctx context.Context, options dev.StartOptions) (*c
225226
return o.deployedPod, fwPorts, nil
226227
}
227228

229+
// Delete previous volumes and pod, if running
230+
if o.deployedPod != nil {
231+
err = o.podmanClient.CleanupPodResources(o.deployedPod)
232+
if err != nil {
233+
return nil, nil, err
234+
}
235+
}
236+
228237
err = o.checkVolumesFree(pod)
229238
if err != nil {
230239
return nil, nil, err

tests/integration/cmd_dev_test.go

+34-44
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,7 @@ ComponentSettings:
11481148
Expect(err).ToNot(HaveOccurred())
11491149
})
11501150

1151-
When("modifying memoryLimit for container in Devfile", func() {
1151+
When("modifying name for container in Devfile", func() {
11521152
var stdout string
11531153
var stderr string
11541154
BeforeEach(func() {
@@ -1171,8 +1171,8 @@ ComponentSettings:
11711171
stdout = string(stdoutBytes)
11721172
stderr = string(stderrBytes)
11731173
}()
1174-
src := "memoryLimit: 1024Mi"
1175-
dst := "memoryLimit: 1023Mi"
1174+
src := "runtime"
1175+
dst := "other"
11761176
helper.ReplaceString("devfile.yaml", src, dst)
11771177
if manual {
11781178
devSession.PressKey('p')
@@ -1181,45 +1181,38 @@ ComponentSettings:
11811181
})
11821182

11831183
It(fmt.Sprintf("should react on the Devfile modification (podman=%v, manual=%v, customPortForwarding=%v, customAddress=%v)", podman, manual, customPortForwarding, customAddress), func() {
1184-
if podman {
1185-
By("warning users that odo dev needs to be restarted", func() {
1186-
Expect(stdout).To(ContainSubstring(
1187-
"Detected changes in the Devfile, but this is not supported yet on Podman. Please restart 'odo dev' for such changes to be applied."))
1188-
})
1189-
} else {
1190-
By("not warning users that odo dev needs to be restarted", func() {
1191-
warning := "Please restart 'odo dev'"
1192-
Expect(stdout).ShouldNot(ContainSubstring(warning))
1193-
Expect(stderr).ShouldNot(ContainSubstring(warning))
1194-
})
1195-
By("updating the pod", func() {
1196-
podName := commonVar.CliRunner.GetRunningPodNameByComponent(cmpName, commonVar.Project)
1197-
bufferOutput := commonVar.CliRunner.Run("get", "pods", podName, "-o", "jsonpath='{.spec.containers[0].resources.requests.memory}'").Out.Contents()
1198-
output := string(bufferOutput)
1199-
Expect(output).To(ContainSubstring("1023Mi"))
1200-
})
1184+
By("not warning users that odo dev needs to be restarted", func() {
1185+
warning := "Please restart 'odo dev'"
1186+
Expect(stdout).ShouldNot(ContainSubstring(warning))
1187+
Expect(stderr).ShouldNot(ContainSubstring(warning))
1188+
})
1189+
By("updating the pod", func() {
1190+
component := helper.NewComponent(cmpName, "app", labels.ComponentDevMode, commonVar.Project, commonVar.CliRunner)
1191+
podDef := component.GetPodDef()
1192+
containerName := podDef.Spec.Containers[0].Name
1193+
Expect(containerName).To(ContainSubstring("other"))
1194+
})
12011195

1202-
By("exposing the endpoint", func() {
1203-
Eventually(func(g Gomega) {
1204-
url := fmt.Sprintf("http://%s", ports[containerPort])
1205-
if customPortForwarding {
1206-
Expect(url).To(ContainSubstring(strconv.Itoa(localPort)))
1207-
}
1208-
if customAddress {
1209-
Expect(url).To(ContainSubstring(localAddress))
1210-
}
1211-
resp, err := http.Get(url)
1212-
g.Expect(err).ToNot(HaveOccurred())
1213-
defer resp.Body.Close()
1214-
1215-
body, _ := io.ReadAll(resp.Body)
1216-
for _, i := range []string{"Hello from Node.js Starter Application!"} {
1217-
g.Expect(string(body)).To(ContainSubstring(i))
1218-
}
1219-
g.Expect(err).ToNot(HaveOccurred())
1220-
}).WithPolling(1 * time.Second).WithTimeout(20 * time.Second).Should(Succeed())
1221-
})
1222-
}
1196+
By("exposing the endpoint", func() {
1197+
Eventually(func(g Gomega) {
1198+
url := fmt.Sprintf("http://%s", ports[containerPort])
1199+
if customPortForwarding {
1200+
Expect(url).To(ContainSubstring(strconv.Itoa(localPort)))
1201+
}
1202+
if customAddress {
1203+
Expect(url).To(ContainSubstring(localAddress))
1204+
}
1205+
resp, err := http.Get(url)
1206+
g.Expect(err).ToNot(HaveOccurred())
1207+
defer resp.Body.Close()
1208+
1209+
body, _ := io.ReadAll(resp.Body)
1210+
for _, i := range []string{"Hello from Node.js Starter Application!"} {
1211+
g.Expect(string(body)).To(ContainSubstring(i))
1212+
}
1213+
g.Expect(err).ToNot(HaveOccurred())
1214+
}).WithPolling(1 * time.Second).WithTimeout(20 * time.Second).Should(Succeed())
1215+
})
12231216
})
12241217
})
12251218
})
@@ -1327,9 +1320,6 @@ ComponentSettings:
13271320

13281321
By("not warning users that odo dev needs to be restarted because the Devfile has not changed", func() {
13291322
warning := "Please restart 'odo dev'"
1330-
if podman {
1331-
warning = "Detected changes in the Devfile, but this is not supported yet on Podman. Please restart 'odo dev' for such changes to be applied."
1332-
}
13331323
Expect(stdout).ShouldNot(ContainSubstring(warning))
13341324
Expect(stderr).ShouldNot(ContainSubstring(warning))
13351325
})

0 commit comments

Comments
 (0)