Skip to content

Commit

Permalink
Dev command environments handling fix (#576)
Browse files Browse the repository at this point in the history
* Fixes `dev` command so that all environments are rendered by default. When in skaffold mode, then it locks to a specified environment, or `dev` if none provided.

* Fixes skaffold#UpdateBuildArtifacts function to ensure that nil and empty slices are handled correctly before comparison

Co-authored-by: marcinc <[email protected]>
  • Loading branch information
marcinc and marcinc authored Jun 18, 2021
1 parent aa41e59 commit 43e6677
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
3 changes: 2 additions & 1 deletion cmd/kev/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ func runDevCmd(cmd *cobra.Command, _ []string) error {
eventHandler := func(e kev.RunnerEvent, r kev.Runner) error { return nil }

var envs []string
if len(kevenv) > 0 {
if len(kevenv) > 0 && skaffold {
// when in --skaffold mode - only watch, render and deploy a specified environment
envs = append(envs, kevenv)
}

Expand Down
23 changes: 18 additions & 5 deletions pkg/kev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io"
"os"
"os/signal"
"regexp"
"syscall"

"github.com/appvia/kev/pkg/kev/log"
Expand Down Expand Up @@ -65,16 +66,23 @@ func (r *DevRunner) Run() error {
r.UI.Output("[development mode] ... watching for changes - press Ctrl+C to stop", kmd.WithStyle(kmd.LogStyle))
r.DisplaySkaffoldOptionsIfAvailable()

runPreCommands := func() error {
runPreCommands := func(envs []string) error {
sg := r.UI.StepGroup()
defer sg.Done()

step := sg.Add(fmt.Sprintf("Running render for environment: %s", r.config.Envs[0]))
var msg string
if len(envs) == 0 {
msg = "Running render for all environments"
} else {
msg = fmt.Sprintf("Running render for environment: %s", envs[0])
}

step := sg.Add(msg)

renderRunner = NewRenderRunner(
r.WorkingDir,
WithEventHandler(r.eventHandler),
WithEnvs(r.config.Envs),
WithEnvs(envs),
WithUI(kmd.NoOpUI()),
)
if _, err := renderRunner.Run(); err != nil {
Expand All @@ -90,7 +98,7 @@ func (r *DevRunner) Run() error {
defer close(change)

// initial manifests generation for specified environments only
if err := runPreCommands(); err != nil {
if err := runPreCommands(r.config.Envs); err != nil {
return err
}

Expand Down Expand Up @@ -133,6 +141,8 @@ func (r *DevRunner) Run() error {

go r.Watch(change)

envRe := regexp.MustCompile(`^.*\.(.*)\.ya?ml$`)

for {
ch := <-change
if len(ch) > 0 {
Expand All @@ -143,11 +153,14 @@ func (r *DevRunner) Run() error {
kmd.WithStyle(kmd.LogStyle),
)

match := envRe.FindStringSubmatch(ch)
env := match[1]

if err := r.eventHandler(DevLoopIterated, r); err != nil {
return newEventError(err, DevLoopIterated)
}

_ = runPreCommands()
_ = runPreCommands([]string{env})

// empty the buffer as we only ever do one re-render cycle per a batch of changes
if len(change) > 0 {
Expand Down
7 changes: 6 additions & 1 deletion pkg/kev/skaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,19 @@ func UpdateSkaffoldBuildArtifacts(path string, project *ComposeProject) error {
// true - when list of artefacts was updated, false - otherwise
func (s *SkaffoldManifest) UpdateBuildArtifacts(analysis *Analysis, project *ComposeProject) bool {
prevArts := s.Build.Artifacts

if prevArts == nil {
prevArts = []*latest.Artifact{}
}
sort.SliceStable(prevArts, func(i, j int) bool {
return prevArts[i].ImageName < prevArts[j].ImageName
})

s.SetBuildArtifacts(analysis, project)

currArts := s.Build.Artifacts
if currArts == nil {
currArts = []*latest.Artifact{}
}
sort.SliceStable(currArts, func(i, j int) bool {
return currArts[i].ImageName < currArts[j].ImageName
})
Expand Down
17 changes: 17 additions & 0 deletions pkg/kev/skaffold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,23 @@ var _ = Describe("Skaffold", func() {
Expect(images).ToNot(ContainElement("quay.io/myorg/svc2"))
})
})

When("previous build artefacts were not defined (nil)", func() {
BeforeEach(func() {
skaffoldManifest.Build.Artifacts = nil
})

When("and current build artefacts were not detected (empty slice)", func() {
BeforeEach(func() {
analysis = nil
project.Services[0].Build = nil // reminder: images with no build context are excluded
})

It("returns false", func() {
Expect(changed).To(BeFalse())
})
})
})
})

Describe("InjectProfiles", func() {
Expand Down

0 comments on commit 43e6677

Please sign in to comment.