Skip to content

Commit da9a9ef

Browse files
authored
Display a warning that "odo dev" on Podman needs to be restarted if the Devfile is changed (redhat-developer#6477)
* Display a warning that "odo dev" on Podman might need to be restarted when local changes are detected The initial idea was to display such a warning only when changes are detected in the Devfile, which are not yet handled completely at this time. But to take into account the manual synchronization case (where there is no file watcher (and therefore no way to determine which files have actually been modified)), a generic warning is being displayed whenever the Podman-specific watch handler is called. * Add integration test case * fixup! Add integration test case * amend! Display a warning that "odo dev" on Podman might need to be restarted when local changes are detected Display a warning that "odo dev" on Podman might need to be restarted when the Devfile is modified * fixup! Add integration test case * Assert that no warning is displayed if the Devfile has not changed
1 parent 06b95fc commit da9a9ef

File tree

2 files changed

+97
-28
lines changed

2 files changed

+97
-28
lines changed

pkg/dev/podmandev/podmandev.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
package podmandev
22

33
import (
4+
"bytes"
45
"context"
6+
"encoding/json"
57
"fmt"
68
"io"
79
"path/filepath"
810
"strings"
911

12+
"github.com/devfile/library/pkg/devfile/parser"
13+
"k8s.io/klog"
14+
1015
"github.com/redhat-developer/odo/pkg/dev"
1116
"github.com/redhat-developer/odo/pkg/dev/common"
17+
"github.com/redhat-developer/odo/pkg/devfile"
1218
"github.com/redhat-developer/odo/pkg/devfile/adapters"
19+
"github.com/redhat-developer/odo/pkg/devfile/location"
1320
"github.com/redhat-developer/odo/pkg/exec"
21+
"github.com/redhat-developer/odo/pkg/log"
1422
odocontext "github.com/redhat-developer/odo/pkg/odo/context"
1523
"github.com/redhat-developer/odo/pkg/podman"
1624
"github.com/redhat-developer/odo/pkg/state"
@@ -158,6 +166,8 @@ func (o *DevClient) checkVolumesFree(pod *corev1.Pod) error {
158166
}
159167

160168
func (o *DevClient) watchHandler(ctx context.Context, pushParams adapters.PushParameters, watchParams watch.WatchParameters, componentStatus *watch.ComponentStatus) error {
169+
printWarningsOnDevfileChanges(ctx, watchParams)
170+
161171
startOptions := dev.StartOptions{
162172
IgnorePaths: watchParams.FileIgnores,
163173
Debug: watchParams.Debug,
@@ -169,3 +179,35 @@ func (o *DevClient) watchHandler(ctx context.Context, pushParams adapters.PushPa
169179
}
170180
return o.reconcile(ctx, watchParams.Out, watchParams.ErrOut, startOptions, componentStatus)
171181
}
182+
183+
func printWarningsOnDevfileChanges(ctx context.Context, parameters watch.WatchParameters) {
184+
var warning string
185+
currentDevfile := odocontext.GetDevfileObj(ctx)
186+
newDevfile, err := devfile.ParseAndValidateFromFileWithVariables(location.DevfileLocation(""), parameters.Variables)
187+
if err != nil {
188+
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)
189+
} else {
190+
devfileEquals := func(d1, d2 parser.DevfileObj) (bool, error) {
191+
// Compare two Devfile objects by comparing the result of their JSON encoding,
192+
// because reflect.DeepEqual does not work properly with the parser.DevfileObj structure.
193+
d1Json, jsonErr := json.Marshal(d1.Data)
194+
if jsonErr != nil {
195+
return false, jsonErr
196+
}
197+
d2Json, jsonErr := json.Marshal(d2.Data)
198+
if jsonErr != nil {
199+
return false, jsonErr
200+
}
201+
return bytes.Equal(d1Json, d2Json), nil
202+
}
203+
equal, eqErr := devfileEquals(*currentDevfile, newDevfile)
204+
if eqErr != nil {
205+
klog.V(5).Infof("error while checking if Devfile has changed: %v", eqErr)
206+
} else if !equal {
207+
warning = "Detected changes in the Devfile, but this is not supported yet on Podman. Please restart 'odo dev' for such changes to be applied."
208+
}
209+
}
210+
if warning != "" {
211+
log.Fwarning(parameters.Out, warning+"\n")
212+
}
213+
}

tests/integration/cmd_dev_test.go

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -692,45 +692,61 @@ ComponentSettings:
692692
Expect(err).ToNot(HaveOccurred())
693693
})
694694

695-
if !podman {
696-
When("modifying memoryLimit for container in Devfile", func() {
697-
BeforeEach(func() {
698-
src := "memoryLimit: 1024Mi"
699-
dst := "memoryLimit: 1023Mi"
700-
helper.ReplaceString("devfile.yaml", src, dst)
701-
if manual {
702-
if os.Getenv("SKIP_KEY_PRESS") == "true" {
703-
Skip("This is a unix-terminal specific scenario, skipping")
704-
}
705-
706-
devSession.PressKey('p')
695+
When("modifying memoryLimit for container in Devfile", func() {
696+
var stdout string
697+
var stderr string
698+
BeforeEach(func() {
699+
src := "memoryLimit: 1024Mi"
700+
dst := "memoryLimit: 1023Mi"
701+
helper.ReplaceString("devfile.yaml", src, dst)
702+
if manual {
703+
if os.Getenv("SKIP_KEY_PRESS") == "true" {
704+
Skip("This is a unix-terminal specific scenario, skipping")
707705
}
708-
var err error
709-
_, _, ports, err = devSession.WaitSync()
710-
Expect(err).Should(Succeed())
711-
})
712706

713-
It("should expose the endpoint on localhost", func() {
707+
devSession.PressKey('p')
708+
}
709+
var err error
710+
var stdoutBytes []byte
711+
var stderrBytes []byte
712+
stdoutBytes, stderrBytes, ports, err = devSession.WaitSync()
713+
Expect(err).Should(Succeed())
714+
stdout = string(stdoutBytes)
715+
stderr = string(stderrBytes)
716+
})
717+
718+
It("should react on the Devfile modification", func() {
719+
if podman {
720+
By("warning users that odo dev needs to be restarted", func() {
721+
Expect(stdout).To(ContainSubstring(
722+
"Detected changes in the Devfile, but this is not supported yet on Podman. Please restart 'odo dev' for such changes to be applied."))
723+
})
724+
} else {
725+
By("not warning users that odo dev needs to be restarted", func() {
726+
warning := "Please restart 'odo dev'"
727+
Expect(stdout).ShouldNot(ContainSubstring(warning))
728+
Expect(stderr).ShouldNot(ContainSubstring(warning))
729+
})
714730
By("updating the pod", func() {
715731
podName := commonVar.CliRunner.GetRunningPodNameByComponent(cmpName, commonVar.Project)
716732
bufferOutput := commonVar.CliRunner.Run("get", "pods", podName, "-o", "jsonpath='{.spec.containers[0].resources.requests.memory}'").Out.Contents()
717733
output := string(bufferOutput)
718734
Expect(output).To(ContainSubstring("1023Mi"))
719735
})
736+
}
720737

721-
By("exposing the endpoint", func() {
722-
url := fmt.Sprintf("http://%s", ports["3000"])
723-
resp, err := http.Get(url)
724-
Expect(err).ToNot(HaveOccurred())
725-
defer resp.Body.Close()
738+
By("exposing the endpoint", func() {
739+
url := fmt.Sprintf("http://%s", ports["3000"])
740+
resp, err := http.Get(url)
741+
Expect(err).ToNot(HaveOccurred())
742+
defer resp.Body.Close()
726743

727-
body, _ := io.ReadAll(resp.Body)
728-
helper.MatchAllInOutput(string(body), []string{"Hello from Node.js Starter Application!"})
729-
Expect(err).ToNot(HaveOccurred())
730-
})
744+
body, _ := io.ReadAll(resp.Body)
745+
helper.MatchAllInOutput(string(body), []string{"Hello from Node.js Starter Application!"})
746+
Expect(err).ToNot(HaveOccurred())
731747
})
732748
})
733-
}
749+
})
734750
})
735751
})
736752

@@ -792,9 +808,20 @@ ComponentSettings:
792808
devSession.PressKey('p')
793809
}
794810

795-
_, _, _, err := devSession.WaitSync()
811+
var stdout, stderr []byte
812+
var err error
813+
stdout, stderr, _, err = devSession.WaitSync()
796814
Expect(err).Should(Succeed())
797815

816+
By("not warning users that odo dev needs to be restarted because the Devfile has not changed", func() {
817+
warning := "Please restart 'odo dev'"
818+
if podman {
819+
warning = "Detected changes in the Devfile, but this is not supported yet on Podman. Please restart 'odo dev' for such changes to be applied."
820+
}
821+
Expect(stdout).ShouldNot(ContainSubstring(warning))
822+
Expect(stderr).ShouldNot(ContainSubstring(warning))
823+
})
824+
798825
for _, p := range containerPorts {
799826
By(fmt.Sprintf("returning the right response when querying port forwarded for container port %d", p),
800827
func() {

0 commit comments

Comments
 (0)